advanced

Checking users installed flash version

SDK Version: 
M3

Here is a little tutorial on checking users flash version. It comes in handy, if you want to use flash content in your app. Flash lite can cause some problems, so this snippet is also about that...

Interrupting/cancelling threads

SDK Version: 
M3

threadWhen you run a background operation using a Thread, or AsyncTask in most of the times it is needed to be able to interrupt it.
In lot of cases, when the user starts a sceen in our application, a thread is started in the background, to load its content. Hoever if the user leaves the screen before its loading is complete, the loading process should be interrupted. Even if the loading prosess can not be interrupted immediately, or it would be too mutch troube, it shold detect that the display of the result is no longer needed.

So how do you stop a thread exactly? You can see in the documentation that Thread class has a stop() method. This method offered an "easy" way to interrupt threads in older android versions, by killing the thread. It is deprecated now, do not use it, it may left things in inconstent state, among other problems, so this was an unelegant way.

Remove autofocus from an EditText in Android

SDK Version: 
M3

When we create a layout with an EditText or an AutoCompleteTextView, for some reason, it always gains the focus on starting.

focused

SQLite subqueries

SDK Version: 
M3

sqlite
Recently we worked with big databases under android, and during that I met query optimization problems when using subqueries.

From my previous experiences with MySQL I thought, the database engine optimization is always the best, and there is no way, that multiple queries from program code is better, but I ended running multiple selects in a loop in this case for the best result.

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

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>

Database transactions

SDK Version: 
M3

In android it is highly important to use transactions when working with databases.

First, in android database operations - especially writing - are very slow. Batching them into transactions will make them much faster.

Second, the database remains consistent under any circumstances. The database system makes sure to all the operations in a transaction take effect, or on error, rollback all of them.

If you are used to other platforms like PHP+MySQL where the code usually runs on a powerful server, witch is not likely to stop execution "unexpectedly", you can be surprised how much it affects the performance in android.
The android system can kill apps/threads/activities and so interrupt database usage, the battery can discharge or can be removed etc.

The implementation is very simple, using 3 methods in the SQLiteDatabase class:

beginTransaction();
setTransactionSuccessful();
endTransaction();

Encrypting your data

SDK Version: 
M3

Sometimes you don't want to store any data on the sd card, but you have to, because your apps resources would take up too much space int the internal memory.
Here is a method on how to store your data securely.

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.

Share on - popup snippet, and facebook problems

SDK Version: 
M3

Android always had very good social network, and communcation tool integration. All of the preinstalled communication tpye apps, and a lot of 3rd party apps have support for communcation between each other, via the ACTION_SEND intent.

Syndicate content