Saturday, September 17, 2011

Threads and Handlers

Newbies in Android face difficulty with communicating between different threads.

It is better to create a new background thread instead of delaying the UI thread when there is lengthy task.
Let us say you want to read an html file. If you read this file in UI thread, the program appears to be unresponsive. Hence it is better to read the file in a background thread.
But when this reading is completed, how do you show the html in a webview in this example? The background threads can not access the ui widgets. Hence there is a need to communicate between ui thread and background thread.
This can be done with Handlers. Handlers are used to send and process messages. A handler created in  a thread, can process all the messages sent by this thread.



You should create a handler class for processing messages


private class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {  
 
if(msg.getData().getString("message").equals("your message here")){
Toast.makeText(ShowPage.this, "your toast here...", Toast.LENGTH_LONG).show();
}
}
    
    }

Next  initialize an object of this MyHandler class in onCreate

class MyActivity extends Activity
{
    MyHandler mHandler;
//more variables here
//


public void onCreate(Bundle b){
            //some code here
            mHandler = new MyHandler();
            //
}


Let us say I create a thread to write to a file

Thread th = new Thread(){
        public void run(){
            //here goes my lengthy
            //processing statements
            ------


           //processing completed
           Message msg = new Message();
           Bundle b = new Bundle();
                           b.putString("message", "your message here");//key and value
           msg.setData(b); 
                           mHandler.sendMessage(msg);//inform that thread is completed
}
}//my thread ends here


At the end, I should create a message object and write the string to it. Then send this message to the handler.
Handler has handleMessage method which checks at the string and if it is matching, takes appropriate action.

That's all.  Our thread with handler is ready to go.




Thursday, September 15, 2011

Kannada/ Hindi/Marathi keyboard for Android

Many of you might be wondering how to type kannada in your android phone.  I have developed and published an app in android market which is kannda Input Method Editor.  Here is the link.  You install the apk and then you enable the keyboard. During typing, make sure you select kannada input method.
In fact my app supports Devanagari font also. So those of you who want to type in Hindi/marathi can use the app.
Please leave your comments and help me in improving it.



Saturday, September 10, 2011

Orientation change in android

Many times you want to have a different layout for landscape view and another one for portrait view.

The simplest way to achieve this is

1> create folder    /res/layout-land
2> in this folder store your layout xml file for landscape view
3> The xml file name must be same for both portrait and landscape orientations

That's it. Now if the user rotates his screen, automatically the landscape layout will be used


And when user changes orientation, the activity restarts. If you do not want that to happen

Add this line to manifest file

<activity
android:configChanges="orientation|keyboardHidden"
--
>

Now the activity will not restart if the orientation is changed.