Download Code

Runnable and Threads Handler

In this tutorial you will learn how to create number Counter application using Threads with Runnable,Handler class.

I have discussed about Thread , Runnable and Handler class in Previous Tutorials.So we will start directly.

Handler

Discussed in previous tutorial.

Now create new Android Application Project.We will use a and a button in activity_main.xml like this.

handler,Threads,runnable,counter app runnable

Now go to your MainActivity.java class.Create variable’s for textview and button.Refer their id’s.Then implements onClickListener class,import click method.

public class MainActivity extends Activity implements OnClickListener{

	TextView timer;     // textview instance
	Button reBtn;       // reset button variable
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();    // method to get id's and applied click listener
       
    }
    public void initialize(){
    	timer = (TextView) findViewById(R.id.textTimer);
    	reBtn = (Button) findViewById(R.id.resetBtn);
    	reBtn.setOnClickListener(this);
    }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.resetBtn:
			
			break;
		}
	}
	
}

Next Step create variable for delay,elapsedTime,Handler.Then create Runnable method,count number method.
Your Complete MainActivity.java should look this.

package androidcodec.counter;

/*
 * 
 * All Rights Reserved By: Farhan Rasheed
 * 
 * */

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener{

	TextView timer;     // textview instance
	Button reBtn;       // reset button variable
	int elapsedTime;    // counting values integer
	Handler myHandler;   // used to delay runnable for a second which delay_RATE
	int delay_RATE = 1000;    //delay
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();    // method to get id's and applied click listener
        elapsedTime = 0; // default value
        myHandler = new Handler();     //initializing handler
        counter();                    //counter method to count values
    }
    
    // method to get all UI element id's and apply click listener on button
    
    public void initialize(){
    	timer = (TextView) findViewById(R.id.textTimer);  // text view id
    	reBtn = (Button) findViewById(R.id.resetBtn);     // button  id
    	reBtn.setOnClickListener(this);                   // apply click listener on button
    }
    
    // click method using OnClickListener class
    
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		// case for reset button
		case R.id.resetBtn:
			elapsedTime = 0;    // resetting elapsed time
			break;
		}
	}
	
	public void counter(){
		elapsedTime++;      // increment
		timer.setText(String.valueOf(elapsedTime));    // setting value of elapsed time in textview
		myHandler.postDelayed(r, delay_RATE);         // handler delay for a second
	}

    private Runnable r = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			counter();   // calling counter method
		}
	};
}

Now Run your android application project.It will start counting number,when application get started.Output should be like this.

handler,Threads,runnable,counter app runnable

that’s it for this tutorial,i hope you get the idea of Runnable and Threads Handler properly.

Thanks.