How to create a progress dialog in an Android application?

I am developing an application to get some data from the Internet, getting the data that I want to show "Progress Dialog." I used "AsyncTask" in my application.

Question: how to use it and how to show the percentage as 100%?

Please suggest me and give me an example. Thanks and sorry for my english.

+4
source share
2 answers

To display the prgress dialog, you can use the code below

ProgressDialog dialog = new ProgressDialog(MainActivity.this); dialog.setMessage("Your message.."); dialog.show(); 

before calling the async task, i.e. before new YourTask.execute().

and in the onPostExecute function for asynthesis. you can use

  dialog.dismiss(); 

to close the dialog box.

+22
source

You can use the parowing method:

 public void launchBarDialog(View view) { barProgressDialog = new ProgressDialog(MainActivity.this); barProgressDialog.setTitle("Downloading Image ..."); barProgressDialog.setMessage("Download in progress ..."); barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL); barProgressDialog.setProgress(0); barProgressDialog.setMax(20);//In this part you can set the MAX value of data barProgressDialog.show(); new Thread(new Runnable() { @Override public void run() { try { // Here you should write your time consuming task... while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) { Thread.sleep(2000); updateBarHandler.post(new Runnable() { public void run() { barProgressDialog.incrementProgressBy(1);//At this, you can put how many data is downloading by a time //And with the porcentage it is in progress } }); if (barProgressDialog.getProgress() == barProgressDialog.getMax()) { barProgressDialog.dismiss(); } } } catch (Exception e) { } } }).start(); } 

Hope this works for all of you.

+2
source

Source: https://habr.com/ru/post/1490203/


All Articles