Top 10 Tips for Developing Android Apps

Over the course of developing Android apps you may encounter various speed bumps.  I certainly ran into my fair share of them.  I painstakingly worked through each and every one of the issues.  I wish someone would have told me about all these ups and downs before I started my Android app development.  I’m going to bequeath the things that I learned while tackling Android app development on to you. If you’re an Android app beginner, here are my top 10 tips for Android app development!

1. Use a Global Theme

Using a theme can save you from having to change styles for every Activity in your app.  Depending on the look and feel you want for your app, you should at least use the built-in Light or Dark themes. Tweak those styles in sub-activities if needed:

<application android:icon=”@drawable/app_icon” android:theme=”@android:style/Theme.Light”  android:label=”WordPress” android:name=”WordPress”>

2. Design Flexible Layouts

Android can run on multiple screen sizes. Make sure to use layout designs that stretch their content to fit the screen. Use “fill_parent” and “wrap_content” in the layout XML where appropriate to adapt the content of your application to the screen size automatically. Here’s an example from WordPress of different screen sizes:

Check out row.xml to see how the content is set up to expand in the comment view row.

3. runOnUiThread() is Your Friend

If you’re running threads in your application and need to display something to the user, you’ll discover that the application won’t actually display anything unless it is called from the main thread. You can use runOnUiThread() to ensure that your code is run in the UI.

4. Use Selectors as View Backgrounds, Not Images

A common mistake, when coming from the web world, is to set the background for buttons and lists with an image file. Since Android has multiple states for views, you should create a custom selector in XML that details what should display when the view is in different states:

xmlns:android=”http://schemas.android.com/apk/res/android”>     <item android:state_selected=”true”         android:drawable=”@drawable/menu_button_bg” />     <item android:state_pressed=”true” android:state_selected=”false”         android:drawable=”@drawable/menu_button_bg” />     <item android:state_selected=”false”         android:drawable=”@android:color/transparent” /> </selector>

5. Handling Device Rotation

If you do nothing in your activity to handle device rotation, Android will default to restarting your activity. This is especially annoying if you are running a ProgressDialog while the application is working on something. You can override onConfigurationChanged() to have your activity stay alive when the device is rotated:

@Override public void onConfigurationChanged(Configuration newConfig) {   //ignore orientation change   super.onConfigurationChanged(newConfig); }

and in your manifest XML:

<activity android:name=”selectCategories” android:configChanges=”orientation|keyboardHidden”></activity>

6. Test on all Compatible Android Versions

Have 5 emulators up at once to test each version the app can run on (currently 1.5, 1.6, 2.0, 2.1 and 2.2).

7. Optimize Your ListViews for Smooth Scrolling

Wrap your ListView rows in a container class for faster loading and scrolling.

StackOverflow has a good example.

8. Use DIP instead of PX

To ensure that your margins and padding appears the same on different screen sizes and resolutions, make sure to use ‘dip’ instead of ‘px’ so that Android adjusts accordingly for the device:

<button id=”@+id/example_button”              android:layout_width=”wrap_content”              android:layout_height=”wrap_content”              android:textSize=”20dip”              android:text=”Use Dips!”/>

9. Android is Already Fragmented

Because Android is so open, device manufacturers have had to change a lot of the Android core. This can cause oddities to occur in your application. For example, take a look at how the app appears on the Motoblur platform:

Agh, those black buttons! Motorola replaces the default Android buttons with black ones to fit their ‘dark’ theme for Motoblur. In order to correct this we have to create our own buttons for the app.

10. Use the Dev Tools Application

Google has created an excellent application that is installed by default in the emulators, but you can also install it on your device. My favorite tool in the app is the ‘Monitor CPU Usage’ option, which will put a few bars at the top of your device that show how much CPU your app is using in real-time! Learn more about the Dev Tools here.

There you go! Android is a great platform to develop for, and I hope this article will help you get started!

 

2 Comments to “Top 10 Tips for Developing Android Apps”

  1. cialis 26 April 2011 at 7:31 pm #

    Hey mate! I completely agree with your thoughts. I really value what you’re doing here.

    • cindy 27 April 2011 at 8:59 pm #

      Thank you cialis!


Leave a Reply