Android Thread with Handler example tutorial

How to Use of thread including handler.

Thread and handler are most useful for android application development purpose because they gives us the facility to do background tasks like image loading, like clock, data loading, json parsing from url, contacts loading etc. So here is the complete step by step tutorial for Android Thread with Handler example tutorial.
android-project-download-code-button

Definition of Thread : Thread is used in android application to do background tasks. For past time thread is applied in all programming languages to perform certain background tasks like calculating in background, automatically perform developer defined tasks.

Definition of Handler : Handler is same as thread but there are a major difference in both of them that is thread cannot change or modify UI( Android screen ) but with the combination of handler application developer can change screen widgets automatically.

Android Thread with Handler example tutorial.

Code for MainActivity.java file.

package com.android_examples.com.handlerthread;
import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends Activity {

int status = 0;
Handler handlerCalled;
Thread threadCalled;
TextView Txt;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 handlerCalled = new Handler();
 Txt = (TextView)findViewById(R.id.textView1);
 threadCalled = new Thread(new Runnable()
 {
 @Override
 public void run() 
 {
 while(status < 10)
 {
 try {
 Thread.sleep(1000);
 } catch (InterruptedException e) {

 e.printStackTrace();
 }

 status++;
 }

 handlerCalled.post(new Runnable()
 {
 @Override
 public void run()
 {
 // Define here UI element change code.
 Txt.setText("Text After changed Successfully");
 }
 });
 }

 });
 threadCalled.start();

 }

}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.handlerthread.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="161dp"
 android:text="Before Thread + Handler Called Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Android Thread with Handler example tutorial

text-after-handler-thread-called

Click Here to Download Android Thread with Handler example tutorial project.

Leave a Reply

Your email address will not be published. Required fields are marked *