17

I would like to set theme of progressDialog. To create it, I use this code:

progressDialog = ProgressDialog.show(this, "Please Wait", "Loading dictionary file....", true, false);

I can't just write

progressDialog = new ProgressDialog(...);
progressDialog.(do_sth_with_dialog);
progressDialog.show(...)

because the show() method is static and I get compiler warning. Is there any way to use available constants like

progressDialog.THEME_HOLO_DARK 

to set the dialog theme?

I would also like to change the Dialog background and make the corners round (I don't want to change anything with the progressBar that is inside progressDialog. There is many tutorials here, but they usually describe how to create new class that extends progressDialog class.

Is there easier way to set THEME and BACKGROUND color of progressDialog?
Why I can access constants like progressDialog.THEME_HOLO_DARK if I cant use them?

Marek
  • 3,935
  • 10
  • 46
  • 70
  • You cannot inflate progress Dialog UI, what you can do is while doing Async Task, you can show custom dialog – Adil Jun 04 '13 at 04:53
  • Adil Mughal could you please write in the answer how can I make customProgressDialog with just THEME, BACKGROUND and CORNERS modified? I don't want to touch the ProgressBar... – Marek Jun 04 '13 at 04:55
  • @Marek: you can not inflate Progress Dialog's layout (that is mentioned by Adil Mughal above) you need to use custom dialog (not custom progree dialog). you can create a simple dialog and customize it according to your requirement. – Mahaveer Muttha Jun 04 '13 at 05:08

4 Answers4

30

ProgressDialog.show() are static methods, so you don't get a class instance of ProgressDialog that you can set properties on.

To get a ProgressDialog instance:

// create a ProgressDialog instance, with a specified theme:    
ProgressDialog dialog = new ProgressDialog(mContext, ProgressDialog.THEME_HOLO_DARK);
// set indeterminate style
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// set title and message
dialog.setTitle("Please wait");
dialog.setMessage("Loading dictionary file...");
// and show it
dialog.show();

EDIT 8/2016: Regarding the comments about deprecated themes, you may also use styles.xml and inherit from a base theme, e.g.:

<style name="MyProgressDialog" parent="Theme.AppCompat.Dialog">
</style>

the details on how to do this are already covered extensively elsewhere, start with https://developer.android.com/guide/topics/ui/themes.html.

Using themes and styles.xml is (in my opinion) a much cleaner and easier to maintain solution than hard-coding a theme when instantiating the ProgressDialog, i.e. set it once and forget it.

Then you can just do

new ProgressDialog(mContext);

and let your global theme/style provide the styling.

CSmith
  • 13,318
  • 3
  • 39
  • 42
14

Sorry.. I'm working right now. Can't give full details. But here is the answer.

ProgressDialog progressDialog;

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
   progressDialog = new ProgressDialog(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Light_Dialog));
}else{
   progressDialog = new ProgressDialog(context);
}

progressDialog.setMessage("Loading....");
progressDialog.show();
JeffMeJones
  • 354
  • 4
  • 5
0

You cannot inflate ProgressDialog.

What you can do is while doing async task, you can show custom dialog which you can create by inheriting from Dialog class.

Also see how to set background image for progress dialog?

Community
  • 1
  • 1
Adil
  • 3,248
  • 1
  • 22
  • 27
0
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.item_dialog);
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
hm7
  • 1
  • 1