The concept here is android handlers. Feel free to add more handler examples.
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Oclemy
Android Handler – Update ProgressBar
Android Handler and ProgressBar Update Tutorial and Examples
In this tutorial we want to see how to post updates from a background thread to the user interface thread using a Handler.
We click a button and simulate doing heavy work in the background. Meanwhile we are able to update our progressbar as the work continues.
Video Tutorial(ProgrammingWizards TV Channel)
Well we have a video tutorial as an alternative to this. If you prefer tutorials like this one then it would be good you subscribe to our YouTube channel. Basically we have a TV for programming where do daily tutorials especially android.
(a). MainActivity.java
This is the main activity. We derive from the AppCompatActivity. One of the imports we add is the Handler from the
android.os
package.We will maintain three instance fields:
android.os
package through which we can send and processMessage
andRunnable
objects associated with a thread’sMessageQueue
.Here’s the full code:
(b). activity_main.xml
This is the main activity layout. At the root we have a LinearLayout. This element allows us to arrange it’s children linearly either horizontally or vertically. We also have TextView to display the header text of our app. Also we have a progressbar to display progress. We have also a button that when clicked will start the thread to perform our work in the background thread.