Beginner
    AJAX
    Android
    ASP.Net
    C#
    Cloud Computing
    Crystal Report
    Database
    Drupal
    HTML5
    JavaScript
    Joomla
    JQuery
    MVC
    php
    Reporting
    SharePoint
    SQL Server
    VB.Net
    Windows Phone
    WordPress
    WPF
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> Using Handler in Android Application




Using Handler in Android Application - MindStick

Using Handler in Android Application

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



 Author Information
Name: Rohit Kesharwani
Member Since: 8/8/2011
Article Submitted Date: 10/31/2011
Location:     India
Report Abuse Form
Reason:    
 
Total Online Users: 6582
Advertisement
MindStick SurveyManager
Advertise with Us
  
Dudelabs
Copyright © 2013MindStick. All Rights Reserved.