How to show progress dialog in Android?
ProgressDialog pd = new ProgressDialog(yourActivity.this); pd.setMessage(“loading”); pd.show(); And that’s all you need.
ProgressDialog pd = new ProgressDialog(yourActivity.this); pd.setMessage(“loading”); pd.show(); And that’s all you need.
I did some testing and I feel that the best way to achieve this is doing a custom Dialog. Here is an example of what I did. This will answer question number 2 but will give you an idea of how to fix question number 1. public class MyProgressDialog extends Dialog { public static MyProgressDialog … Read more
Yes, get() waits if necessary for the computation to complete, and then retrieves its result. This means, that you are blocking your UI thread, waiting for the result. Solution: Don’t call get Usually, you will call a function (callback) in the postExecute.
Place your ProgressDialog in onPreExecute, sample code below: private ProgressDialog pdia; @Override protected void onPreExecute(){ super.onPreExecute(); pdia = new ProgressDialog(yourContext); pdia.setMessage(“Loading…”); pdia.show(); } @Override protected void onPostExecute(String result){ super.onPostExecute(result); pdia.dismiss(); } and in your onClickListener, just put this line inside: new EfetuaLogin().execute(null, null , null);
Yes, in API level 26 it’s deprecated. Instead, you can use progressBar. To create it programmatically: First get a reference to the root layout RelativeLayout layout = findViewById(R.id.display); //specify here Root layout Id or RelativeLayout layout = findViewById(this); Then add the progress bar progressBar = new ProgressBar(youractivity.this, null, android.R.attr.progressBarStyleLarge); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100); … Read more
You should load data in an AsyncTask and update your interface when the data finishes loading. You could even start a new activity in your AsyncTask’s onPostExecute() method. More specifically, you will need a new class that extends AsyncTask: public class MyTask extends AsyncTask<Void, Void, Void> { public MyTask(ProgressDialog progress) { this.progress = progress; } … Read more
The method show() must be called from the User-Interface (UI) thread, while doInBackground() runs on different thread which is the main reason why AsyncTask was designed. You have to call show() either in onProgressUpdate() or in onPostExecute(). For example: class ExampleTask extends AsyncTask<String, String, String> { // Your onPreExecute method. @Override protected String doInBackground(String… params) … Read more
AsyncTask has method onProgressUpdate(Integer…) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress(). Refer to the docs for more details
Declare your progress dialog: ProgressDialog progress; When you’re ready to start the progress dialog: progress = ProgressDialog.show(this, “dialog title”, “dialog message”, true); and to make it go away when you’re done: progress.dismiss(); Here’s a little thread example for you: // Note: declare ProgressDialog progress as a field in your class. progress = ProgressDialog.show(this, “dialog title”, … Read more
Fragments can actually make this a lot easier. Just use the method Fragment.setRetainInstance(boolean) to have your fragment instance retained across configuration changes. Note that this is the recommended replacement for Activity.onRetainnonConfigurationInstance() in the docs. If for some reason you really don’t want to use a retained fragment, there are other approaches you can take. Note … Read more