Wednesday, August 11, 2010

show ProgressBar while processing in background in ANDROID

AIM: To show progress bar while doing work in background thread like downloading image/vedio/response etc..

Solution:
You can use AsyncTask   class which will do the background work in background thread while showing another UI(like progressbar,textview with loading or any other layout what ever you like) in UI thread.

You can do this by using the following code snippet.

Suppose you are downloading any thing from internet .here i am sending request to server and reading the response from the server.

The AsyncTask class which will does the background task.

       // ASync Task Begin to perform Billing information
private class performBackgroundTask extends Async Task < Void, Void, Void >  {

                private ProgressDialog Dialog = new ProgressDialog( this);

protected void onPreExecute() {
                       // here you have place code which you want to show in UI thread like progressbar or dialog or any layout . here  i am displaying a progressDialog with test please wait while loading......

                        Dialog.setMessage(" please wait while loading............");
Dialog.show();

              
}

protected Void doInBackground(Void... params) {
 // write here the code to download  or any background task.
                         DownloadVideo();  // example call method which will download vedio .
return null;
}

protected void onPostExecute(Void unused) {
                       Dialog.dismiss();    
// the background task is completed .You can do the code here for next things

}

}

Call this method with the following line whereever you wants .for example like oncreate() or in anybutton handler as follows:
 new performBackgroundTask().execute();

Thats it.by this way we can solve the problem.


32 comments:

  1. thanks,

    I wonder if we can change the progressdialog to a progressBar? Can you post an example for this?

    ReplyDelete
  2. ProgressBar myProgressBar;
    int myProgress = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Log.d("SuccessfulScreen","Show progress bar and view button to view the content.");

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);


    //set control made in XML
    setContentView(R.layout.success);
    Log.d("Progress bar", "Start count progress bar to 10");

    myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Library);

    new Thread(myThread).start();

    Button btn = (Button) findViewById(R.id.view_btn);
    btn.setOnClickListener(this);

    }

    private Runnable myThread = new Runnable(){

    @Override
    public void run() {
    // TODO Auto-generated method stub
    while (myProgress<10){
    try{
    Log.d("myProgress","myProgress count is " +myProgress);
    myHandle.sendMessage(myHandle.obtainMessage());
    Thread.sleep(1000);

    }
    catch(Throwable t){
    Log.d("catch message","show message" + t.getMessage());
    }
    }
    //---hides the progress bar---
    myHandle.post(new Runnable()
    {
    public void run() {
    //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
    //myProgressBar.setVisibility(8);

    Log.d("successed message","message will show in textview");
    TextView textmsg = (TextView) findViewById(R.id.successmessage);
    textmsg.setText("The file is now saved to My Library which is located in the More category");
    Log.d("successed message","text nmessage" +textmsg.getText());
    Button btnview = (Button) findViewById(R.id.view_btn);
    //btnview.setEnabled(true); android:enabled="false"
    btnview.setVisibility(0);
    }
    });
    }

    Handler myHandle = new Handler(){

    @Override
    public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    myProgress++;
    myProgressBar.setProgress(myProgress);
    }
    };
    };

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. I am getting the error at this line
    private ProgressDialog Dialog = new ProgressDialog( this);

    The Error is:
    The constructor ProgressDialog(AsyncTaskTest.performBackgroundTask) is undefined

    ReplyDelete
  6. I didn't understand what your problem exactly.
    Probably,this 2 approaches may be useful for you.

    1)add the above declaration line in Oncreate() method.
    2)instead of
    new ProgressDialog( this);
    replace "this " with application context (or) classname.this.

    please check for any error that is happening in in BackgroundProcess like nullpointers,any exceptions.

    ReplyDelete
  7. thanks. exactly what i was looking for :)

    ReplyDelete
  8. Interestingly, a bit of these male enhancement devices and methods own been
    proved to be Flaccid and Raise states. This may look to elementary
    to really do work but sometimes elaboration is possible through the use of male enhancement.


    My weblog; site

    ReplyDelete
  9. I don't know if it's just me or if everyone else experiencing problems with your site.
    It appears as if some of the text in your content are running off
    the screen. Can somebody else please provide feedback and let me know if this is
    happening to them as well? This could be a problem with my internet browser because I've had this happen previously. Many thanks

    Here is my web-site :: bonuses slots

    ReplyDelete
  10. I know this if off topic but I'm looking into starting my own weblog and was curious what all is required to get set up? I'm assuming having a blog like
    yours would cost a pretty penny? I'm not very internet savvy so I'm
    not 100% certain. Any suggestions or advice would be greatly appreciated.
    Thanks

    Feel free to surf to my page: best enhancement reviews

    ReplyDelete
  11. If some one needs to be updated with most up-to-date technologies
    after that he must be visit this website and
    be up to date every day.

    Also visit my page ... Genf20 plus

    ReplyDelete
  12. I love what you guys are up too. This type of clever work and exposure!
    Keep up the wonderful works guys I've incorporated you guys to my own blogroll.

    my blog - super charge labrada

    ReplyDelete
  13. I'm amazed, I have to admit. Seldom do I come across a blog that's equally educative and engaging, and without a doubt, you've hit the nail on the head. The problem is an issue that too few people are speaking intelligently about. Now i'm
    very happy that I came across this during my
    hunt for something relating to this.

    My website :: buy Total curve

    ReplyDelete
  14. I am not positive the place you're getting your info, but good topic. I needs to spend a while studying much more or working out more. Thanks for excellent information I used to be in search of this information for my mission.

    Visit my weblog - venapro direction to use

    ReplyDelete
  15. I get pleasure from, cause I discovered exactly what I used to be looking for.
    You've ended my 4 day long hunt! God Bless you man. Have a great day. Bye

    my web page: tinnitus control

    ReplyDelete
  16. Excellent beat ! I wish to apprentice while you amend your web
    site, how could i subscribe for a blog web site? The account helped
    me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright
    clear idea

    Also visit my homepage ... www.nexopia.com

    ReplyDelete
  17. Nice post. I was checking continuously this blog and
    I am inspired! Very useful info specially the final part :) I handle such info much.

    I used to be looking for this certain info for a long time.

    Thank you and best of luck.

    my page; proactol online

    ReplyDelete
  18. I do trust all of the ideas you've offered in your post. They are very convincing and can certainly work. Nonetheless, the posts are too brief for newbies. May just you please lengthen them a bit from subsequent time? Thanks for the post.

    my blog ... capciplex

    ReplyDelete
  19. This is a good tip especially to those new to the blogosphere.
    Brief but very accurate info… Thanks for sharing this one.
    A must read article!

    My blog post: %anchor_text%

    ReplyDelete
  20. Hello There. I discovered your blog the use of msn. This is a very well written article.
    I will be sure to bookmark it and come back to learn extra of your helpful info.
    Thank you for the post. I'll definitely comeback.

    Feel free to surf to my web site: capsiplextruth.net

    ReplyDelete
  21. You can certainly see your enthusiasm in the
    article you write. The arena hopes for more passionate writers like you who are not afraid to mention how they
    believe. Always follow your heart.

    Also visit my web site ... start playing im netz

    ReplyDelete
  22. Good wгite-up. I definitely apprecіatе this ѕite.
    Keep ωrіting!

    Also visit my web blοg - 500fаstcash.
    com (http://500fastcashtipspay.weebly.com/)

    ReplyDelete
  23. Thanks for finally writing about > "show ProgressBar while processing in background in ANDROID" < Liked it!

    Here is my web-site: book of ra free games online

    ReplyDelete
  24. I have been browsing online more than 3 hours today, yet I never found
    any interesting article like yours. It is pretty worth enough for me.
    In my opinion, if all web owners and bloggers made good content as you did, the net will be much more useful than ever before.


    Stop by my web page: achat viagra boutique paris

    ReplyDelete
  25. gucci bags online
    Heya i'm for the first time here. I came across this board and I find It truly useful & it helped me out a
    lot. I hope to give something back and help others like you helped
    me.

    ReplyDelete
  26. De quelle manière acheter des bitcoins anonymement sur le réseau tout de
    suite

    My web blog; acheter bitcoin

    ReplyDelete
  27. I'm pretty pleased to find this page. I want to to thank you for your time due to this wonderful read!!
    I definitely loved every part of it and i also have you saved as a favorite to look at
    new information in your web site.

    Stop by my webpage; voice cabling toronto (http://www.kimjeongim.com/zbxe/?document_srl=621083)

    ReplyDelete
  28. I was very pleased to discover this site. I wanted to thank you for your
    time due to this wonderful read!! I definitely enjoyed every bit of it and I have you
    book-marked to see new stuff in your web site.

    Stop by my web page :: hosting service free

    ReplyDelete
  29. ӏt's an amazing piece of writing for all the web visitors; they will get benefit from it I am
    sure.

    Review myy web site; how can i rebuild my rebuild credit after chapter 7

    ReplyDelete
  30. Fruits like pears and prunes are high in fiber and so can naturally rectify bowel dysfunction.

    Here is my homepage ... senna

    ReplyDelete
  31. Awesome! Its truly remarkable post, I have got much clear
    idea on the topic of from this article.

    Here is my webpage web traffic

    ReplyDelete
  32. Quality articles or reviews is the important to invite
    the viewers to pay a quick visit the web page, that's
    what this website is providing.

    Here is my web site: dui lawyer portland or

    ReplyDelete

Android Developers Blog

Ram's shared items