[TinyTut] - Displaying a simple ProgressDialog

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby Mrs Harris » Fri Jan 16, 2009 4:07 pm

Hey thanks for the quick reply... you've got some quick fingers!

I think perhaps I was not too clear... what I was trying to do was have the backgroundProc do some work which would come up with some values and the UI would then be updated to show these values.

As an example I would have an app that would retrieve some data from the web and then update the GUI... but this retrieve operation would not occur on the UI thread.

Hope this makes sense.

What I have now (with thanks to your code) is the following. This seems nicer I think.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private Runnable doBackgroundProc = new Runnable(){
  2.                 private String strTest = null;
  3.                 public void run(){
  4.                         try{
  5.                     // Do some Fake-Work
  6.                     Thread.sleep(2000);
  7.                } catch (Exception e) {  }
  8.                 strTest = "foobar";
  9.                
  10.                 runOnUiThread(new Runnable() {
  11.                                 @Override
  12.                                 public void run() {
  13.                                         TextView tvBal = (TextView)findViewById(R.id.txtTest);
  14.                                         tvBal.setText(strTest);
  15.                                         myProgressDialog.dismiss();
  16.                                 }      
  17.                 });
  18.                 }
  19.         };
  20.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Mrs Harris
Freshman
Freshman
 
Posts: 8
Joined: Fri Jan 16, 2009 2:22 pm

Top

Postby MrSnowflake » Fri Jan 16, 2009 4:42 pm

So this is what I understand you need: Say you want to download 10 images in the background from the web. And when each image is downloaded you want to display it? This can be done using the runOnUiThread. So one a image is completely downloaded, you post a runnable to Activity.runOnUiThread() which then adds the new image to the layout.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Mrs Harris » Fri Jan 16, 2009 5:18 pm

MrSnowflake wrote:So this is what I understand you need: Say you want to download 10 images in the background from the web. And when each image is downloaded you want to display it? This can be done using the runOnUiThread. So one a image is completely downloaded, you post a runnable to Activity.runOnUiThread() which then adds the new image to the layout.

Yes that would be a good example. Perhaps I should have thought more about my "query" than the example code I posted.
Mrs Harris
Freshman
Freshman
 
Posts: 8
Joined: Fri Jan 16, 2009 2:22 pm

setCancelable(true)

Postby sikus » Tue Apr 07, 2009 7:36 pm

Hi All,
I'm trying to cancel the progress. I'm useing myProgressDialog.setCancelable(true); but when I cancel ProgresDialog thread still run. OnKeyDown don't react for my press. How can I stop the thread?
Thanks for your recomend.
Sikus
sikus
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Dec 15, 2008 3:48 pm

Postby Alen » Wed Apr 08, 2009 8:34 pm

HI PlusMinus,

iam new here.
Just wonna say: Thank you very much for sharing this.
Helped me alot.

Regards Alen
Alen
Freshman
Freshman
 
Posts: 8
Joined: Tue Mar 17, 2009 12:54 pm

Postby rahulp » Thu Apr 09, 2009 10:03 am

hi all... i am using following code...
new Thread() {
public void run() {
try{
// Do some Fake-Work


for (;;)
{
// System.out.println("sdgagasgfgasgasg");
// System.out.println("i am here");
if(msg_count==1)
{
System.out.println("new message");
//int k =show(msg1,reciever);
show(msg1, reciever);
msg_count=0;
}
else{
System.out.println("no new message");
}


sleep(3000);
} }catch (Exception e) { }
// Dismiss the Dialog

}
}.start();




but as soon as i call my show function which sets text in a text field. nothing happens and also the thread stops.
can anyone please help me
thanks
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Top

Postby sikus » Thu Apr 09, 2009 10:18 am

Use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. catch (Exception e) {
  2.  
  3.                                         e.printStackTrace();
  4.  
  5. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
because you maybe have some error, but you can't see it. And use Log.w("",""); instead of System.out.println();
sikus
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Dec 15, 2008 3:48 pm

Postby rahulp » Thu Apr 09, 2009 11:05 am

thanks freshman,
can u please tell me how to change a ui in a thread...
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Postby sikus » Fri Apr 10, 2009 8:34 am

I think that code below works.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. new Thread() {
  2.  
  3.                         public void run() {
  4.  
  5. setContentView(R.layout.new_xml_layout_ui);
  6.  
  7. }.start();
  8.  
  9.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
sikus
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Dec 15, 2008 3:48 pm

Postby rahulp » Fri Apr 10, 2009 11:02 am

hi
i have tried that code.. it doesn work
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Postby Untouchab1e » Tue Jun 02, 2009 8:27 am

Thanks for the great guide, however, I am having problems with actually doing some work while the progressbar is running... Here is my code:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onClick(View v) {
  2.  
  3.                                 // TODO Auto-generated method stub
  4.  
  5.                                
  6.  
  7.                                
  8.  
  9.                                 myProgressDialog = ProgressDialog.show(DidYouKnow.this,      
  10.  
  11.                         "Please wait...", "Loading fact...", true);
  12.  
  13.                
  14.  
  15.               new Thread() {
  16.  
  17.                    public void run() {
  18.  
  19.                         try{
  20.  
  21.                              // Fetch the fact
  22.  
  23.                                 fetchFact();
  24.  
  25.                         } catch (Exception e) {  }
  26.  
  27.                         // Dismiss the Dialog
  28.  
  29.                        
  30.  
  31.                         myProgressDialog.dismiss();
  32.  
  33.                    }
  34.  
  35.               }.start();
  36.  
  37.          }
  38.  
  39.    });
  40.  
  41.    
  42.  
  43.          
  44.  
  45.        
  46.  
  47.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Problem is that fetchFact() downloads an xml file and updates a TextView with a text string. However, with the progressbar showing, it wont actually do anything. I think this is what you have been discussing above, but I would like some help with my specific case?

Kind Regards
Untouchab1e
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 02, 2009 8:24 am

Postby Untouchab1e » Tue Jun 02, 2009 8:29 am

Got it working!


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onClick(View v) {
  2.  
  3.                                 // TODO Auto-generated method stub
  4.  
  5.                                
  6.  
  7.                                
  8.  
  9.                                 myProgressDialog = ProgressDialog.show(DidYouKnow.this,      
  10.  
  11.                         "Please wait...", "Loading fact...", true);
  12.  
  13.                
  14.  
  15.               new Thread() {
  16.  
  17.                    public void run() {
  18.  
  19.                         try{
  20.  
  21.                              
  22.  
  23.                                 Thread.sleep(2000);
  24.  
  25.                         } catch (Exception e) {  }
  26.  
  27.                        
  28.  
  29.                         runOnUiThread(new Runnable() {
  30.  
  31.                             @Override
  32.  
  33.                             public void run() {
  34.  
  35.                                 // Fetch the fact
  36.  
  37.                                 fetchFact();
  38.  
  39.                             }
  40.  
  41.                             });
  42.  
  43.                         // Dismiss the Dialog
  44.  
  45.                        
  46.  
  47.                         myProgressDialog.dismiss();
  48.  
  49.                    }
  50.  
  51.               }.start();
  52.  
  53.          }
  54.  
  55.    });
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
Untouchab1e
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 02, 2009 8:24 am

Postby Richa » Fri Jan 22, 2010 12:44 pm

hey thanks...
it really helped me in my application....
Richa
Freshman
Freshman
 
Posts: 3
Joined: Tue Jan 12, 2010 11:44 am

RIGHT WAY

Postby ztrafe » Mon Feb 15, 2010 6:13 pm

This is the right way...

It´t just don´t display the dialog but alls let you perform some action.

http://thedevelopersinfo.wordpress.com/ ... n-android/
http://thedevelopersinfo.com/2009/10/19 ... n-android/
ztrafe
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Feb 15, 2010 6:10 pm

Postby strider2023 » Sat Mar 06, 2010 9:00 am

I got the progress bar working...but i want to change the way it looks...can anybody help???
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests