Call a Method Task after a delay in android using Handler

How to activate a function in android application activity after a particular given time delay programmatically.

In this tutorial we are using Handler and Runnable to perform task delay method. This will gives us the facility to perform task inside android applications after a certain given time delay, So app developer can do anything like opening message, start web calls after a delay. So here is the complete step by step tutorial for Call a Method Task after a delay in android using Handler.

android-project-download-code-button

How to Call a Method Task after a delay in android using Handler.

Code for MainActivity.java file.

package com.callmethodafteradelay_android_examples.com;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

 Handler handler;
 Runnable runnable;
 Button button;
 TextView textview;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 handler = new Handler();
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 runnable = new Runnable() {
 
 @Override
 public void run() {
 
 //Perform any task here which you want to do after time finish.
 
 textview.setText(" TextView Text Color Change ");
 
 textview.setTextColor(Color.BLUE);
 }
 };
 handler.postDelayed(runnable, 7000);
 
 }
 });
 }
}

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.callmethodafteradelay_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here to Change TextView Text After 7 seconds" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="43dp"
 android:text="TextView Text Before Change"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshots:

TaskDelay-1

 

Call a Method Task after a delay in android using Handler

Click here to download Call a Method Task after a delay in android using Handler project with source code.

Leave a Reply

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