Saturday, June 26, 2010

Sample Handler Implementation

Handler is a verry useful and powerful component in Android. Some special features of Handler are,

     If handler is created without any parameter, then it will be created in the same thread itself.
     We can pass data to handler using message

I have created the below sample application, which is using handler efficiently. Increment the count for one second.

So if we need to create a seperate thread for handler, first we need to create a thread and create the handler inside it.

public class MainActivity extends Activity {
    TextView tv;
    public void onCreate(Bundle savedInstanceState) {
        tv = (TextView) findViewById(;
        new Thread() {
            public void run() {
                int i = 0;
                while (i <= 100) {
                    try {
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                            .obtain(messageHandler, i));
    private Handler messageHandler = new Handler() {
        public void handleMessage(Message msg) {
            tv.setText(msg.what + "");

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" android:layout_width="fill_parent"
<TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textSize="25dip" android:layout_gravity="center"

Tuesday, June 8, 2010

Sample Rating Bar Implementation

A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars.

The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar.

The smaller RatingBar style and
the larger indicator-only style do not support user interaction and should only be used as indicators.

larger indicator-only    -    style="?android:attr/ratingBarStyleIndicator"
smaller RatingBar     -    style="?android:attr/ratingBarStyleSmall"
Default            -    style="?android:attr/ratingBarStyle"