Press ESC to close

Android Handler – Update ProgressBar

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.

...
import android.os.Handler;
...

We will maintain three instance fields:

Handler – a class defined in the android.os package through which we can send and process Message and Runnable objects associated with a thread’s MessageQueue.

(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.

Share Article:

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com .
Follow me below;
Github
, and on my channel:
ProgrammingWizards TV