Android – Multithreading in a UI environment

Why do we need multithreading in Android applications? Let’s say you want to do a very long operation when the user pushes a button. If you are not using another thread, it will look something like this: What will happen? The UI freezes. This is a really bad UI experience. The program may even crash. The problem in using threads in a UI environment So what will happen if we use a Thread for a long running operation. Let’s try a simple example: The result in this case is that the application crashes. 12-07 16:24:29.089: ERROR/AndroidRuntime(315): FATAL EXCEPTION: Thread-8 12-07 16:24:29.089: ERROR/AndroidRuntime(315): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 12-07 16:24:29.089: ERROR/AndroidRuntime(315): at … Clearly the Android OS wont let threads other than the main thread change UI elements. But why? Android UI toolkit, like many other UI environments, is not thread-safe. The solution A queue of messages. Each message is a job to be handled. Threads can add messages. Only a single thread pulls messages one by one from the queue. The same solution was implemented in swing (Event dispatching thread and SwingUtilities.invokeLater() ) Handler The Handler is the middleman between a Continue reading Android – Multithreading in a UI environment

Android – Opening a new screen

  Opening a new screen is fairly easy in android. This post will show you almost all you need to know about switching back and forth between screens in your Android application. Add all activities to manifest Each screen is usually an Activity. Each activity should be mentioned in the manifest file. The simple use Bear in mind that every time you will call the function startActivity(intent) a new instance of MyActivity2 will be created. Passing a parameter In most times you need to pass a parameter to the new screen. You do that by using the function putExtra(). This function Receiving a parameter And of course you will need to receive that parameter on the other side. Going back If you need to go back to the previous screen and terminate the current screen, than all you have to do is to use the function finish() from anywhere in the new Activity. Open for result When you need to open a new screen and get result back from it, than use the function startActivityForResult() instead of startActivity() Returning a result Getting the result from the new activity

Android – Text to speech simple example

  In this article I will show the most simple way to add text to speech abilities to your Android application. Actually that’s the whole code. You just need to create a TextToSpeech object. Notice the constructor takes 2 parameters, the first is the Context Activity extends from. The second is an OnInitListener which you will need to implement. Ofcourese there are a lot more options but this is the gist of it. You can read more in the Android developer website. Download code example

Android – Using SQLite DataBase

Android OS comes with SQLite Database already built-in. This article will explain how to store and draw data. Currently I am working with Android version 2.2. 1. Extend SQLiteOpenHelper SQLiteOpenHelper is the main class responsible for SQLite operations. You will need to extend it. After extending SQLiteOpenHelper, you will need to trigger the super constructor with the proper parameters – the context (which Activity inherit from it) , the DB file name, CursorFactory (which we wont discuss in this article, leave it as null for now) and the DB version. 2. Create a table One of the functions you have inherited from SQLiteOpenHelper is public void onCreate(SQLiteDatabase db), which is triggered when the DB was created. You can use this function to create the table. 3. Create SQL queries From this point it is fairly easy to create SQL queries. Your object is now holding reference to 2 objects one for writing/deleting and one for reading. You will use them to make operations. Further reading – Android developer website Download demo project