---
title: "Using Handler in Android Application"  
description: "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 Prog"  
author: "Chris Anderson"  
published: 2011-10-31  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/798/using-handler-in-android-application  
category: "android"  
tags: ["android"]  
reading_time: 4 minutes  

---

# Using Handler in Android Application

The class Handler can update the UI. A handle provides methods for receiving [messages](https://www.mindstick.com/news/4179/google-faces-backlash-for-claiming-all-texts-in-google-messages-are-end-to-end-encrypted) 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](https://www.mindstick.com/interview/12746/the-last-callback-in-the-lifecycle-of-an-activity-is-ondestroy-the-system-calls-this-method-on-your-activity-as-the-final-signal-that-your-activity-instance-is-being-completely-removed-from-the-sys).\

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](https://www.mindstick.com/articles/1653/creating-libraries-for-android-applications). In this example I will use the class Handler to update a ProgressBar in a [background thread](https://www.mindstick.com/articles/620/foreground-and-background-thread-in-c-sharp-threading).

- Start a new [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) 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](https://www.mindstick.com/mindstickarticle/b1b5ed16-848a-4f34-9dbe-82e881a73fe9/images/bf25032f-d1f6-441f-a220-3a3749ec4bcd.png)

##### When you click on the Start Progress button, progress bar will start and looks

##### like below:

![Using Handler in Android Application](https://www.mindstick.com/mindstickarticle/b1b5ed16-848a-4f34-9dbe-82e881a73fe9/images/68234bab-b98b-4f25-b194-c866e64a9aa9.png)

\

---

Original Source: https://www.mindstick.com/articles/798/using-handler-in-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
