ProgressDialog with Thread

Put your problem here if it does not fit any of the other categories.

ProgressDialog with Thread

Postby conedm » Thu Feb 16, 2012 10:48 pm

I am attempting to show a progress dialog while I am performing some actions. I am having troubles getting it to perform the way I would like. Here is what I want to do:
click event of a button
pop up a progress dialog showing a busy message.
perform some action
then once the action is complete, inflate a new layout into my main layout. (changing screens)
It works fine except either I inflate the layout while the other actions are still processing (layout change is outside the thread) or if I put the layout change in the thread, it forces a close of the app, at the layout.removeallviews; line.

Code: Select all
private OnClickListener feeAcceptListener = new OnClickListener() { 
       @Override
      public void onClick(View v) {
          progressBar = ProgressDialog.show(v.getContext(), "Please Wait...","Communicating",true);
           new Thread(new Runnable() { 
                 @Override
                 public void run() {
                    GetWts(); //processes some action
                     progressBar.dismiss();
                 }
           }).start();
          
           Handler handle=new Handler(){
                 @Override
                 public void handleMessage(Message msg) {
                       super.handleMessage(msg);
                       
                 }
           };     
           LinearLayout layout = (LinearLayout)findViewById(R.id.main_view);       
           layout.removeAllViews();      
           LayoutInflater inflater = getLayoutInflater();             
           layout.addView(inflater.inflate(R.layout.wts, null));
                 
           Button print = (Button)findViewById(R.id.btnPrintTic);
           Button done= (Button)findViewById(R.id.btnDone);
           print.setOnClickListener(wtsPrintListener);
           done.setOnClickListener(wtsDoneListener);
      }
    };


Thank you in advance!
conedm
Freshman
Freshman
 
Posts: 4
Joined: Thu Feb 16, 2012 10:36 pm

Top

Re: ProgressDialog with Thread

Postby Phyll » Fri Feb 17, 2012 12:29 am

Hi conedm,

Is there some reason that you must use the same activity? A reason you can't just start another activity to display the results of the action that goes on during the progress bar?

That would be a more typical way to handle it I think. For instance, a startup screen that shows a progress dialog while an RSS feed loads then starts the main screen to show the list of articles. Something like that.

Just curious.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: ProgressDialog with Thread

Postby conedm » Fri Feb 17, 2012 5:29 pm

I am new to java and android development. I am a .net developer. So, I am unsure how to do that. This wouldn't be the start of the app, it would be something that takes place on a button clickevent. Does that matter?
What I am basically doing is communicating with a webpage using urlconnection and getting simple post and get routines to retrieve a string to display to the user. At the same time showing the progressdialog until it completes, then finally changing the layout. Should I be creating an activity to do the url connections or creating an activity to show the progressdialog? does the new activity take place of the thread I am currently trying to use? Thank you very much for the input!!!
conedm
Freshman
Freshman
 
Posts: 4
Joined: Thu Feb 16, 2012 10:36 pm

Re: ProgressDialog with Thread

Postby Phyll » Fri Feb 17, 2012 7:48 pm

Hi conedm,

I guess what I was inquiring about was, it sounds like it is all kind of taking place on the same screen and you were having to jump through hoops to lay it out. Just sounded kind of unorthodox.

I think it would make sense to have one activity that allows selection of the URL or whatever you were doing on the first layout. When you get your input, go ahead and send the info to another screen and load the data. While its loading use a progress dialog which just overlays your viewing activity and show its progress until it has finished and is dismissed. Then just show the data. This activity would return to the first if it was finished or the back key was pressed. I don't know exactly how your program works but you could do other things at that point as well.

Does that make sense to you? If you need to know how to do any of that just ask.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: ProgressDialog with Thread

Postby conedm » Fri Feb 17, 2012 8:30 pm

I think I understand the concept. I will dig into it to see if I can get it to work. I haven't used more than one activity yet.
conedm
Freshman
Freshman
 
Posts: 4
Joined: Thu Feb 16, 2012 10:36 pm

Re: ProgressDialog with Thread

Postby conedm » Fri Feb 17, 2012 10:10 pm

Figured it out! That was exactly what I needed to do! Thank you.
To recap, I was attempting to "inflate" xml layouts into my main layout. When I attempted to use a progressdialog
It would fail because you cannot edit a layout from a thread that did not create it.
The fix: create a seperate activity and once I was done with the progressdialog (and any actions that needed to be done with it) I created a new "intent" loading the new activity.
conedm
Freshman
Freshman
 
Posts: 4
Joined: Thu Feb 16, 2012 10:36 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 23 guests