1

I have a method which is list my items from a server. Therefore, this method takes time until getting all the items. So I want to use ProgressDialog for waiting this method. I read some modules but I couldnt find any useful solution.

    public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            // setContentView(R.layout.main;
                    loginScreen();
        }
    public boolean getItems() throws MessagingException 
           {
                    items receiving here
           }
    public void loginScreen() 
           {
                   setContentView(R.layout.main);
                   Button loginBtn = (Button) findViewById(R.id.btnLogin);

                   loginBtn.setOnClickListener(new Button.OnClickListener() 
                     {          
                      public void onClick(View arg0) 
                       {
                          getItems();
                       }
                     }
           }

On button Click I call the method which received all items. When I click the button I want to show a progressbar until all items getting.

halfer
  • 19,824
  • 17
  • 99
  • 186
Merve Gül
  • 1,377
  • 6
  • 23
  • 40

1 Answers1

1

create an AsyncTask which shows an ProgressBar on getItems(). You need an AsyncTask because the main Thread will be busy fetching stuff from server and ProgressBar won't show up untill that is finished.

Mayank
  • 8,777
  • 4
  • 35
  • 60
  • Thank you so much! It works almost like I want. Progressdialog is show when the method is preparing for the result but it is just too fast. So, the progressdialog shown so for short time. Can I add something (like timer) for delay the show of progressdialog for 2 second more? – Merve Gül Mar 28 '12 at 17:32
  • all you gotta do is `Thread.sleep(2000);`, that will cause it to delay 2sec or 2000 milsec. – Mayank Mar 28 '12 at 18:36