Best Coding Practices in Android – Part 2

By | April 5, 2016

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

Re-Use Elements and Layouts

Use

<include>

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.

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

This can be rewritten as follows.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android" />

    <TextView
        android:id="@+id/top_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:text="My Text 1" />

    <TextView
        android: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
coderzheaven@gmail.com.

Leave a Reply

Your email address will not be published. Required fields are marked *