Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Handler example in Android

In this video tutorial, we will see about Android Handler with Example for a better understanding of What is Handler and How it works in the Android app.

Step:1 Now to understand the android handler we will start a new project and name it as 'Myhandler'.

Step:2 Then we will design its layout, so for that, we will first open 'activity_main.xml' file from the layout folder.

Step:3 So for layout, we will use relative layout, and for that, we have taken a parent relative layout and set all its basic properties like height, width, background.

Step:4 And after that we will create a child relative layout in which we are using off text view, which will make 'Button will appear after 10 seconds' text appear on the screen.

Step:5 And after that we have taken the second child relative layout, in which we have taken a text view and set all its graphic properties like width, height, text size, background, text color, and now we will display a timer.

Step:6 Now we will use the third relative layout in which we will take a button, whose basic properties we have set, and it will show text 'ClickMe'.

Step:7 We have kept that button invisible which means that it will not appear directly but after performing a certain amount of code it will be visible.

Step:8 Now we will see MainActivity inside MyHandler's code.
Firstly we will create an instance for a handler

Handler hand= new Handler();

After that we will create instance for button and textView,

Button clickme;

TextView timer;

Step:9 After that we will look 'Oncreate' method in which we have done view identification,

protected void onCreate (Bundle savedInstanceState)

{

super.onCreate(saved InstanceState);

setContentView (R.layout.activity_main);

timer=(TextView)findViewById(R.id.timer);

clickme=(Button) findViewById(R.id.clickme);

}

Step:10 Now we will use 'PostDelayed' method with the handler, and we will need to provide an argument, and then we will need to provide time in milliseconds for how many milliseconds we want to delay it,

hand.postDelayed(run,1000);

 

Step:11 Now we will create 'Runnable' for run,

Runnable run=new Runnable();

 

Step:12 Now we will call a method 'updateTime',

updateTime();                 

}

};

public void updateTime()

{

timer.setText("" "" + (Integer.parseInt(timer.getText().toString()) - 1)

};

 

Step:13 As per our required time duration we need to create a condition,

if (Integer.parseInt(timer.getText().toString()) == 0)

{

clickme.setVisibility(View.VISIBLE);

}

else

{

hand.postDelayed(run,1000);

}                             

Step:14 Now we will execute it and see how its output is generated. The output is successfully generated.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere