Sometimes it’s hard to make Android programs without trying to reinvent the wheel: often there are simple tasks that aren’t obvious at the start, and it can be more trouble than it’s worth writing them from scratch. To help make your code writing process easier, here are 5 Android code snippets for some common tasks that may not be obvious!
1. Reading a Text File from A Device SD Card
This one is a snippet that’s often used simply because it’s a common use case- the way to do it, however, isn’t always obvious. Here’s a snippet to help you read a text file from a device’s SD card!
02 | File sdcardfile = new File(Environment.getExternalStorageDirectory() + "/example.txt" ); |
03 | fileIS = new FileInputStream(sdcardfile); |
05 | BufferedReader buffer1 = new BufferedReader( new InputStreamReader(fileIS)); |
06 | String stringRead = new String(); |
08 | while ((stringRead = buffer1.readLine()) != null ) { |
09 | Log.d( "line: " , stringRead); |
11 | } catch (FileNotFoundException e) { |
13 | } catch (IOException e) { |
2. Start A New Activity On-Click In List View
As with the above example, you’ll often need to start a new activity in a list view but it’s not always obvious how to do so. Here’s the quick workaround you need to start a new activity on-click in a list view!
01 | lv1 = (ListView) findViewById(R.id.lv1); |
02 | ArrayAdapter<CharSequence> arrayadapter = ArrayAdapter.createFromResource( this , R.array.items, android.R.layout.simple_list_item_1); |
03 | arrayadapter.setDropDownViewResource(android.R.layout.simple_list_item_1); |
04 | lv1.setAdapter(arrayadapter); |
06 | lv1.setOnItemClickListener( new AdapterView.OnItemClickListener() { |
08 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
10 | Intent intent = new Intent(getApplicationContext(), Activity2. class ); |
11 | startActivity(intent); |
3. Send Emails From Within Your App
Users often like the ability to get in touch with developers from within the app. Use this snippet to start up the email activity with pre-inputted strings!
1 | final Intent email_intent = new Intent(android.content.Intent.ACTION_SEND); |
2 | email_intent.setType( "plain/text" ); |
3 | email_intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "to@example.com" }); |
4 | email_intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample Subject Here" ); |
5 | email_intent.putExtra(android.content.Intent.EXTRA_TEXT, "Put Your Text Here" ); |
7 | context.startActivity(Intent.createChooser(email_intent, "Send An Email" )); |
4. Get The Size And Orientation Of Your Display
It can be extremely useful in any app to know the size / orientation of your display- here’s a code snippet to grab it and figure that out.
1 | Display d1 = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); |
5 | int o = d1.getOrientation(); |
5. Check For Network Connectivity
This one is also a fairly common need in Android devices, and you’d think there would be an easier way to do it. The actual code is a bit long, however – here it is in all its glory!
01 | ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); |
03 | NetworkInfo info = manager.getActiveNetworkInfo(); |
06 | if (info.isConnected()) { |
09 | AlertDialog.Builder alert = new AlertDialog.Builder( this ); |
10 | alert.setTitle( "Connectivity" ); |
11 | alert.setMessage( "No Connectivity" ); |
12 | alert.setNeutralButton( "Ok" , okClickListener); |
Keep these five snippets handy in your Android toolchest and I’m sure you’ll use them from time to time instead of doing redundant Google searches!
No comments:
Post a Comment