10 Tips for Developing Android Apps

Over the course of developing WordPress for Android there have been a lot of speed bumps along the way of various issues. After figuring out how to resolve them I’ve typically thought about how nice it would have been to have known that when I started out developing for Android. If you’re an Android beginner, here’s a list of these things so you don’t have the same problems!

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 for your app, you should at least use the built in Light or Dark themes. For WordPress for Android, we use the Light theme and tweak some of those styles in sub-activities if needed:

2. Design Flexible Layouts

Android can run on multiple screen sizes these days. 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 the comment view of WordPress on 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 at different states:

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:

6. Test on all Compatible Android Versions

We target Android 1.6 with WordPress for Android, but it is compatible with 1.5 or higher. I typically dev and build on my G1 with 1.6 installed on it. One time I was testing on the device as well as a 2.0 emulator, but forgot to check it on a 1.5 emulator. Some of the listview layouts were displaying incorrectly on the 1.5 device even though it looked fine on all of the later versions. Whoops! Now I have 4 emulators up at once, to test each version the app can run on (currently 1.5, 1.6, 2.0 and 2.1).

7. Optimize Your ListViews for Smooth Scrolling

8. Use DIP instead of PX

A wise man once said: “May your dips be dips, and your pixels be pixels.” 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:

9. Android is Already Fragmented

Because Android is so open, device manufacturers have run with it and changed a lot of the Android core. This can cause oddities to occur in your application. For example, take a look at how WordPress appears on the Motoblur platform:

Ahg, those black buttons! Motorola replace the default Android buttons with black ones to fit their ‘dark’ theme for motoblur. In order to correct this we are going to have to create our own buttons for the app.

10. Use the Dev Tools Application

Google 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 we go! Android is a great platform to develop for, and I hope this article helps you get started!

Good tips. #6 (Test on all Compatible Android Versions) and #9 (Android is Already Fragmented) are probably both the most important and the most difficult. Receiving a series of one-star ratings for “force-close” because you did not test on all versions can be very detrimental to the success of your app.

I’m sorry, but I have to disagree with #9 (Android is already fragmented).
You should prefer default themes over custom buttons any time you can. Of course you might want your app to look the same on every device, but you also have to consider, that the user won’t use your app on multiple devices, he will probably just use the one he has. And the reason why manufacturers change the themes, is because they want all applications to fit into the theme of the phone.
If everyone uses default buttons for his app, you’ll have a totally consistent system, where the user doesn’t have to get used to another design in every app. This is what should be preferred, because it is an important point for the user, when he is choosing whether to buy an android powered phone or not.
Just my opinion though 😉 still a good help

My android app shows default false value loaded to text and edit boxes when invoked from a different activity
also i used px and will this be a reason for my screen not getting allignet to my emulator screen.
kindly reply

> 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’s absolutely nothing about “install a CPU monitor on your phone” in that document.