ProgressBar updating using Message Handler


SDK Version: 
M3

You're going to learn how to make a progressbar, which gives you a feedback what's going on in the background.

Let say you have a LoadingScreen activity (loadingscreen.xml layout), and a Main activity (main.xml).

You'd like to do some job in the Main activity, and update the progressbar status at the same time. To do this, we are going to use Messages. Although, you can update you're progressbar by sending the progressbar instance itself to your Main activity, but that's obviously not the prettiest solution.

What we going to do is:

1. Declaring a progressbar instance in LoadingScreen Activity:

So in your LoadingScreen.java, after Oncreate(), you should add something like this line to your codeline: (don't forget to add a progressbar to your layout first!)

  1.   ProgressBar pb = (ProgressBar)findViewById(R.id.ProgressBar01);

2. Setting up a Handler to handle Messages from outside:

These few line go right after declaring the progressbar. We are also declaring "progress" variable, and update it's value from the Message we get.

Now that we know the progress status, simply just update the progressbar with it. (It has to be an int number, value: 0-100.)

  1.           /* Setting up handler for ProgressBar */
  2.          pbHandle = new Handler(){
  3.               @Override</font>
  4.                 public void handleMessage(Message msg) {
  5.                  /* get the value from the Message */
  6.                  int progress = msg.arg1;
  7.                  pb.setProgress(progress);
  8.                 }
  9.                };

Okay, we finished message handling, now let's send some messages from another (in this case Main) activity.

3. Creating and Sending Messages

Please notice that in the 3rd line, we put the progress value (it depends on your project) into the Message's arg1 variable.

Go to your Main(or any other) activity, and paste the following where you need to send the message about the progress.

  1.  /* Creating a message */
  2. Message progressMsg = new Message();
  3.                  progressMsg.arg1 = progress();
  4.                  /* Sending the message */
  5.                  pbHandle.sendMessage(progressMsg);

That's it.

One more thing: you should not use the same message twice, in the same cycle. If you want to send 2 messages in a cycle, you should create a new message with another instance name, Otherwise your app will be crashed.

Comments

I got lucky and found your site from a random Google search. this is a good ressource. Thanks the diet solution program

Good post. Very impressive. Thanks for sharing.
android tablet
wenger backpack

Built one of these in flash once and it came out very well. The thing is to make it flow naturally and not ump about which makes it annoying to the eye. Great tutorial. I will see if i can get it to go on my phone.

DVD Duplication Ireland

BusinessThanks for posting this article. I'm definitely tired of struggling to find relevant and intelligent comments on this topic. Everybody today seems to go to extremes to either drive home their views or suggest that anyone else in the world is wrong. Thank you for your accurate and relevant insights.

I found the perfect place for my needs. Contains wonderful and useful messages. I have read most of them and has a lot of them. To me, he's doing the great work. Masterboard

Thank you - the site very very nice

المجتمع المصري هو مجتمع يحب الترفية حيث ان سكان مصر تصل الى تسعون مليون فرد ولذلك نحن نقدم خدمة دردشة مصرية لكل اهل مصر ام الدنيا , وتعتبر مصر متقدمة فى هذا العالم من حيث النمو فى مجالات التكنولوجيا و الكمبيوتر بنسبة كبيرة جدا لذلك نفضل دخول شات مصري اكبر تجمع بنات و شباب فى شات الحب و اجمل بنات فى دردشة دردشة الحب , اضافة الى ذلك حيث يوجد دردشة قوية تضم جميع محافظات مصر هي شات مصرية الكتابية نتمني لكم قضاء وقت ممكن فى شات الاصدقاء و احلي دردشة التعارف الذي يوجد به بنات رومانسية فى دردشة بنات مصر المصرية


شات الحب - دردشة مصرية - شات - شات مصري - شات حب - دردشة - دردشة كتابية - دردشة الحب - شات حبنا - شات حبي - شات مصرية - دردشة حب - شات بنات - دردشة مصر - دردشة بنات مصر - شات بنات مصر - شات صوتي - chat love - دردشة ياحبي - شات بنات لبنان - الحب - شات كتابي - منتدي ياحبي - شات مصر - دردشة حبنا - العاب تلبيس البنات - دردشة بنات - شات ياحبي - Chat - شات لبناني - شات كلامنجي - دردشة كلامنجي - شبكة - تصميم مواقع - تصميم - شات الاسكندرية - دردشة لبنانية - دردشة بنات لبنان - بنات لبنان - موقع شات - سعودي كول - شات سعودي كول - شات بنت مصر - شات سكس

I think I could disagree with the main ideas. I won't share it with my friends.. You should think of other ways to express your ideas.
Pete, from pregnancy test

Excellent read I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch! Peter Bishop // Klangschale for each Chakra

thank you for information

حجز فنادق مكة
فنادق مكة

Thank you for providing the perspective. I just read a part of the article,so I don't know much what to say. Good luck for the new member!
frigidaire dehumidifier
Get your Ex Back

Youre so cool! I dont think Ive read anything like this before. So good to find somebody with some original thoughts on this subject. Thanks for starting this up. This blog is something that is needed on the web someone with a little originality. Good job for bringing something new to the internet! Jason Hunt // imprimerie montreal

It is very rare these days to find blogs that provide information someone is looking for. I am glad to see that your blog share valued information that can help to many readers. Thanks and keep writing! Simon Walsh // world art

This is such an informative article and very clearly written. Every single thought and idea is direct to the point. Perfectly laid out. Thank you for taking your time sharing this to you readers. Paul Schrute // Hochsteckfrisur Dresden

Thank you! it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity! John Green // Hochsteckfrisur Dresden

Great post and now I know what to do thank you! Actually this Blog post helped me a lot. I hope you continue writing about this kind of entry. Ken Tipple // Dormitorios juveniles

It's so tough to encounter right information on the blog. I really loved reading this post. It has strengthen my faith more. You all do such a great job at such concepts. ..can't tell you how much I for one appreciate all you do. Rupert Jones // cerrajeria barcelona

The difference between the right word and the almost right word is more than just a fine line! it's like the difference between a lightning bug and the lightning! Jason Hughes // Web Design Manchester

Anything designed according to the users needs and comfort works greatly and has wide acceptance. Your is a nice idea in this regard. It is innovative and creative in nature. John Garnick // Buy 5-APB

I dont think Ive seen all the angles of this subject the way youve pointed them out. Youre a true star a rock star man. Youve got so much to say and know so much about the subject that I think you should just teach a class about it Rich Peterson // online mastering

What I wouldnt give to learn how you got your design to be so amazing! I mean it. Besides the blog just being awesome this page is too sweet! Its not too flashy. It doesnt do too much with colours and things and the videos you use are perfect for this topic! Really awesome blog. David Jenkins // autocaravanas

Amazingly insightful article. If only it was as easy to implement some of the solutions as it was to read and nod my head at each of your points :P Robb Green // Mudanzas en Valencia

The both of us really enjoyed your article. It appears like you've put a lot of effort into your article and the human race need much more of these on the Net these days. I do not real have a large deal to say in retort I just wanted to comment to tell well done. marvelous post. i never thought of that. Harry Gojavez // Talleres de coches Barcelona

Certainly an article to be read! This was a great and informative read! Fabulous work by the author and creator! Nice feedback from the readers as well! I must admit the author had some very valid points here. Thank you for taking the time to share this with us!
Best DSLR cameras

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It's the old what goes around comes around routine. Garry Paul // Pisos en las palmas

Thank you for such a fantastic blog. Where else could anyone get that kind of info written in such a perfect way? I have a presentation that I am presently working on and I have been on the look out for such information. Barry Twinkle // Mudanzas internacionales

It's so tough to encounter right information on the blog. I really loved reading this post. It has strengthen my faith more. You all do such a great job at such concepts. ..can't tell you how much I for one appreciate all you do. Gene Thomas // Damen Handtaschen

Nicely put and executed. Regarding websites for sale this article really gets down to the nitty-gritty. When it comes to websites for sale as in your excellent post, then turnkey websites or dropship websites simply take the cake. What enables you to buy a website or an online business is the very down-to-earth information given above.

Thats what i called awesome truly and informative information.. Good work done...Your post sounds great.. Very unique informations you've posted.. good job done.. Peter Sandrick // Muebles

you can update you're progressbar by sending the progressbar instance itself to your Main activity, but that's obviously not the prettiest solution. dog shop

This is a great blog post. Thank you very much for the fantastic insight and we really appreciate the time you took to write this. Thanks again. Jerry Baldwin // Golf Drills

Great!

seo tips

Happy to see your blog as it is just what I’ve looking for and excited to read all the posts. I am looking forward to another great article from you. Dawn Treveli // imoveis em BH

The post is very well written with lot of useful information for me. I am happy to find your great way of writing the post. With your generous help it is easier for me to understand and implement the concept. Thank you for the post. Property Investment

I think I could disagree with the main ideas. I won't share it with my friends.. You should think of other ways to express your ideas. Ted Harris // Druckerpatronen

This is a fantastic website and I can not recommend you guys enough. Full of useful resource and great layout very easy on the eyes. Please do keep up this great work. Simon Chesney // Hair Salons Sacramento

Excellent read I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch! John Barne // Hamburg Steuerberater

Lay out of this site really a nice one and very clear to view
tax accountant

Great post and now I know what to do thank you! Actually this Blog post helped me a lot. I hope you continue writing about this kind of entry. Dan Whitaker // Foro coches

Perhaps this is a bit off topic but in any case I have been surfing about your blog and it looks really neat. impassioned about your writing. I am creating a new blog and hard-pressed to make it appear great and supply excellent articles. I have discovered a lot on your site and I look forward to additional updates and will be back. Harry Jones // Coches

have never thought that surfing online can be so much beneficial and having found your blog I feel really happy and grateful for providing me with such priceless information. Terry Burns // Motores para persianas

This is a really good read for me Must admit that you are one of the best blogger I ever saw.Thanks for posting this informative article. Jon Marshall // Reformas madrid

It's so tough to encounter right information on the blog. I really loved reading this post. It has strengthen my faith more. You all do such a great job at such concepts. ..can't tell you how much I for one appreciate all you do. Simon Todd // Hostel Barcelona

I dont think Ive seen all the angles of this subject the way youve pointed them out. Youre a true star a rock star man. Youve got so much to say and know so much about the subject that I think you should just teach a class about it James Webster // Hostel granada

Substantially the article is really the best on this laudable topic. I concur with your conclusions and will eagerly look forward to your future updates.Just saying thank you will not just be enough for the wonderful lucidity in your writing. Cole Gene // digital developments

I think I could disagree with the main ideas. I won't share it with my friends.. You should think of other ways to express your ideas. John Whitehall // forex trainer

Its very awesome articleall the content is so beneficial and valuable for us.Thanks for sharing nice and pretty post. Guy Wood // Mudanzas en valencia

This is one of the good articles you can find in the net explaining everything in detail regarding the topic. I thank you for taking your time sharing your thoughts and ideas to a lot of readers out there. Colin Kemp // Familienurlaub Tirol