platform specials

Advanced 9Patch drawing in android

SDK Version: 
M3

Drawing 9patch can turn into a nightmare easily. I would like to present the other side. The most common problem, to create draws wich are look well in portrait mode and landscape mode too.

Here are some 9patch technics:

(Note: This article don’t tell the basics of 9patching. If you are new in this technics, examine this page first: http://developer.android.com/guide/developing/tools/draw9patch.html)

(From this link, you can download the files: download the 9patch files)

Example One: Simple button
This is a just simple button, with a glowing effect. The 9patching wich I use here, keeps the button’s edges, so it keeps the rounded rectangle’s radius too. With 9patch, the content area of the button can be set easily.

draw9

Handling old data in new app versions

SDK Version: 
M3

When you release a newer version of your application, on update the android system identifies from the package name and version number, that the user has an older version installed. During updating not all data of the previous version is overwritten, databases, preferences, previously downloaded/created files on sdcard or internal storage remain unchanged. If there are any incompatibility with the old version of these, is must be taken care of.

First of all, files on the external storage can be lost or corrupted due to many other reasons, so version incompatibility is just one more reason to check those files before using them.
However the internal storage is theoretically not modified by external sources, if there are version incompatibilities, you have to check the state of this files too.

Android 2.3 Gingerbread SDK review and bugs

SDK Version: 
M3

gingerbreadIt's been over a week since gingerbread came out, i've been using the updated sdk since day one.

Here is a little review about it from the developers point of view (it's not as sweet as the name suggests...).

How to use ViewFlipper

SDK Version: 
M3

You have probably already heard about ViewFlipper. If not, go to website: http://developer.android.com/reference/android/widget/ViewFlipper.html
and check it.

"Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval."

Sounds good. Let's try it!

Create a ViewFlipper in your main xml layout like this:

  1. <ViewFlipper android:id="@+id/flipper"
  2.                 android:layout_width="fill_parent" android:layout_height="fill_parent"
  3.                 android:layout_below="@+id/CockpitLayout">
  4.                 <include android:id="@+id/firstlayout" layout="@layout/first" />
  5.                 <include android:id="@+id/listlayout" layout="@layout/list" />
  6.                 <include android:id="@+id/thirdlayout" layout="@layout/productlayout&quot; />
  7.                 <include android:id="@+id/fourthlayout" layout="@layout/vendorlayout&quot; />
  8.  
  9.         </ViewFlipper>

Android licensing simple example

SDK Version: 
M3

Android Market Licensing is a network-based service that lets an application on an Android-powered device query a trusted licensing server, to determine whether the application is licensed to the current device user. After receiving the server response, the application can then allow or disallow further use of the application as needed. In the service, the role of the licensing server is to provide the license status for the current user; the application itself is responsible for querying the server and conditionally granting access to the application.

You can find the licensing library and a cool sample application in your android-sdk folder after you download it with Android SDK and AVD manager. This tool located under Windows menu in Eclipse.

Interrupting a call

SDK Version: 
M3

end callAs far as I know there is no public API for interrupting/ending a call. You can detect calls and the states of calls, maybe even prevent an outgoing call, but there is no API for ending an ongoing call.
This must be a security issue, but I also met the need of this feature, and only was able to do some workaround: turning on airplane mode interrupts all wireless connections including phone calls.

Running code on phone boot

SDK Version: 
M3

Some applications, for example a mail client, needs to run in the background all the time. You may want to run code right after the device booted.


The device is booting

In the Android system BroadcastReaceivers are just for such mechanism, they can listen for defined intents with given parameters, and run code when they receive one, even when your application is not running at the moment. You can define own actions and fire the manually to control your receivers, but there are a lot of predefined broadcast in the system that are fired automatically on certain events.
One of the is the BOOT_COMPLETED action. The complete list is available here.

All you need to do is a class that extends Broadcastreceiver:

  1. public class HelloReceiver extends BroadcastReceiver {
  2.   @Override

Maintaining global Application state

SDK Version: 
M3

As a possible solutions mentioned in previous article Leaving an Android application the Application object can come handy. If you want to store data, global variables that needs to be accessed from everywhere in the application, from multiple Activities, in other words is you want to maintain a global "state" of the whole application the Application object can help.

For this we must make a class which extends the Android.app.Application class add our own methods to it, and define this class in the AndroidManifest.xml as below:

  1. An example for the Application class:
  2.  
  3. public class HelloApplication extends Application {
  4.         private int globalVariable=1;
  5.  
  6.         public int getGlobalVariable() {
  7.                 return globalVariable;
  8.         }

Speeding up android applications

SDK Version: 
M3

First of all, define what do we mean under "speed": in one hand its the time that the code needs to execute, on the other hand its the time the user needs to wait for the user interface. The two things can greatly differ, of course you must optimize the code performance, but the most important is what the user sees from it. Don't make the user wait, unless its necessary.

Do not pull back the ui thread
The very basic principle is to never run slow operations on the user interface thread! If you do this the interface will freeze until the operation is executed, which is not a nice user experience. If the execution takes too long the android system will detect it and offer the opportunity to the user to force close the frozen application:

Leaving an Android application

SDK Version: 
M3
One of the strange things with android is that there is no "the user has closed the application" event. Applications activities often just go to the background, and the android system may kill them whenever it wants, even when another activity of the application is still running in the foreground.

You can not be sure that activities you started and sent to the background are sill there storing their state, and if you are just an android user, you can not be sure that applications that you left simply by pressing back button are completely destroyed.

To make it more complex, using intents it is even possible to start an activity of another application.

Syndicate content