The class Handler can update the UI. A handle provides methods for receiving messages and for runnables. To use a handler you have to subclass it and overide handleMessage () to process messages. To process runables you can use the method post (); you only need one instance of a handler in your activity.

Your thread can post messages via the method sendMessage (Message msg) or sendEmptyMessage.

In this article I am going to explain how to create a handler in an Android application. In this example I will use the class Handler to update a ProgressBar in a background thread.

  •      Start a new project named HandlerDemo.
  •      Open res/values/strings.xml and add the following strings element in the resource tag:
    <string name="startProgress">startProgress</string>
  •    Open res/layout/main.xml and insert the following code:
<?xml  version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ProgressBar android:id="@+id/progressBar1"
              style="?android:attr/progressBarStyleHorizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:indeterminate="false"
              android:max="10" android:padding="4dip">
        </ProgressBar>
        <Button android:text="Start Progress"
                     android:onClick="startProgress"
                     android:id="@+id/button1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content">
        </Button>
</LinearLayout>

 

·         Now open the Activity file and insert the following code:

import android.app.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;

public class HandlerActivity extends Activity {

        private Handler handler;
        private ProgressBar progress;

        @Override
        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
               progress = (ProgressBar) findViewById(R.id.progressBar1);
               handler = new Handler();
       }

        public void startProgress(View view) {

              Runnable runnable = new Runnable() {

                      public void run() {
                            for (int i = 0; i <= 10; i++) {
                                   final int value = i;
                                   try {
                                         Thread.sleep(1000);
                                  } catch (InterruptedException e) {
                                         e.printStackTrace();
                                  }
                                   handler.post(new Runnable() {

                                          public void run() {
                                                 progress.setProgress(value);
                                         }
                                  });
                            }
                     }
              };
               new Thread(runnable).start();
       }
}

·         Run the application.

The output looks like below:

Using Handler in Android Application

When you click on the Start Progress button, progress bar will start and looks
like below:

Using Handler in Android Application


  Modified On Mar-23-2018 06:05:30 AM

Leave Comment