April 5, 2016

Best Coding Practices in Android – Part 2

You can read the First Article about Best Coding Practices in Android here.

Re-Use Elements and Layouts



for reusing layouts.

You can read about this from here.

Use Different resources for different density Screens.

Properly organize your images in mdpi, hdpi,xhdpi etc folders for corresponding screens. This helps to prevent blurry images.

Read more from here.

Try to Use shapes instead of Images whenever possible.

For simple color change and rounded Corners you can use shapes like below

<shape android:shape="oval" ><solid android:color="#ff556677" /></shape>

Read more about shapes from here.

Try to Lower the layout levels.

For example Consider the below layout.

<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/android" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="My text 1" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="My Text 2" /></LinearLayout></LinearLayout>

This can be rewritten as follows.

<RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/android" /><TextViewandroid:id="@+id/top_text"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_toRightOf="@id/image"android:text="My Text 1" /><TextViewandroid:id="@+id/bottom_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/top_text"android:layout_toRightOf="@id/image"android:text="My Text 2" /></RelativeLayout>

You can read more about Optimizing Layouts here.

Use Google Recommended Library Volley for HTTP.

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository.

Volley offers the following benefits:

  • Automatic scheduling of network requests.
  • Multiple concurrent network connections.
  • Transparent disk and memory response caching with standard HTTP cache coherence.
  • Support for request prioritization.
  • Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
  • Ease of customization, for example, for retry and backoff.
  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
  • Debugging and tracing tools.

You can read more about Volley from here.

Better to Use Parcealable instead of Serializable

A class the implements the Serializable interface is marked as serializable and Java serializes it using reflection (which makes it slow).

When using the Parcelable interface, the whole object doesn’t get serialized automatically. Rather, we can selectively add data from the object to a Parcel using which the object is later deserialized.

Use Loaders and LoaderMangers for loading large Data.

Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment. Loaders have these characteristics:

  • They are available to every Activity and Fragment.
  • They provide asynchronous loading of data.
  • They monitor the source of their data and deliver new results when the content changes.
  • They automatically reconnect to the last loader’s cursor when being recreated after a configuration change. Thus, they don’t need to re-query their data.

Read more about Loaders.

File Operations

You should do all file operations outside the UI thread. i.e either use AsyncTask or separate thread for File operations.
You can read more about threads from here.

Learn the Activity lifecycle.

You should have clear understanding of the Activity lifecycle when each functions are called.
You can read more about it here.

Practice good MVC.

Activities should just take data from the models and insert them into the views, or take events from a view and tell the data to do something. Don’t run URL requests or other raw data fetches in controllers.

Thanks for reading…
Send your comments to

Leave a Reply Cancel reply

');isvalidName=false;}else{isvalidName=true;$n(element).html('');}}else{var element=$n("#yourname").next().next();$n(element).html('
This field is required.
');emailAdd=false;}if(emailAdd!=""){var element=$n("#youremail").next().next();if(emailAdd.toLowerCase()=='Email'.toLowerCase()){$n(element).html('
This field is required.
');isvalidEmail=false;}else{var JsRegExPatern = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/if(JsRegExPatern.test(emailAdd)){isvalidEmail=true;$n(element).html('');}else{var element=$n("#youremail").next().next();$n(element).html('
Please enter valid email address.
');isvalidEmail=false;}}}else{var element=$n("#yourname").next().next();$n(element).html('
This field is required.
');isvalidEmail=false;}if(isvalidName==true && isvalidEmail==true){$n(".AjaxLoader").show();$n('#mysuccess_msg').html('');$n('#mysuccess_msg').hide();$n('#myerror_msg').html('');$n('#myerror_msg').hide();var nonce ='fa8c4567c5';var url = '';var email=$n("#youremail").val();var name =$n("#yourname").val();var str="action=store_email&email="+email+'&name='+name+'&sec_string='+nonce;$n.ajax({type: "POST",url: '',data:str,async:true,success: function(msg){if(msg!=''){var result=msg.split("|");if(result[0]=='success'){$n(".AjaxLoader").hide();$n('#mysuccess_msg').html(result[1]);$n('#mysuccess_msg').show();setTimeout(function(){$n.fancybox_ns.close();},2000);}else{$n(".AjaxLoader").hide();$n('#myerror_msg').html(result[1]);$n('#myerror_msg').show();}}}});}}