2

So I would like to add a custom imageview to a ProgressDialog:

LinearLayout root = (LinearLayout)findViewById(R.id.root);
ProgressDialog pdialog = new ProgressDialog(context);
pdialog.setTitle("Wait");
pdialog.setMessage("Loading...");
pdialog.addContentView((ImageView)findViewById(R.id.imageV3iew1), root.getLayoutParams());
pdialog.setCancelable(false);
pdialog.show();

It says that the specified child already has a parent and I have to call removeView on the parent first. I know how to create custom dialogs but I would like to append my imageview right to the ProgressDialog. What kind of feature do I need to request?

Iiro Krankka
  • 4,959
  • 5
  • 38
  • 42

3 Answers3

8

I had the same problem. Check out this solution: https://stackoverflow.com/a/10070995/1487456

Basically, you need to call .show() before you call .setContentView() and therefore probably .addContentView() also.

You can then inflate a layout and customise it however you wish.

Community
  • 1
  • 1
infl3x
  • 1,492
  • 19
  • 26
3

You can't do this with a progress dialog. Checkout this post to see why. For more details, checkout this StackoverFlow post.

Community
  • 1
  • 1
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • So I'll have to just use Dialog? How to make it look like ProgressDialog (any way finding it from android.R.layout or something)? I can make a custom dialog with my image but it looks ugly. – Iiro Krankka Oct 20 '11 at 21:38
  • Yea, you'll just have to implement your own dialog because the progress dialog layout is fixed apparently. – Kurtis Nusbaum Oct 20 '11 at 21:40
  • That sucks. The Android SDK is just teasing me for having that addView method available for ProgressDialog. – Iiro Krankka Oct 20 '11 at 21:49
  • Well in all fairness, the addContentView is up in the Dialog class. That said, one could definitely make the argument that it's bad API design for methods valid parent classes to not be valid in children classes. – Kurtis Nusbaum Oct 20 '11 at 21:58
  • I was so hoping there was a way for doing this. Creating custom dialogs sucks. – Iiro Krankka Oct 20 '11 at 22:04
  • I know I am late* But to find the progress dialog layout look in your sdks path.. For me it was android-sdks/platforms/android-19/data/res/layout. But you can choose any api above 11 to get progress_dialog_holo – Aaron Smentkowski Nov 15 '13 at 17:59
0

It looks as if you are trying to load a view that is currently displayed in your activity into the dialog. If this is not the case that is why you are getting the null pointer. When you use findViewById the view you are looking for must be in the current activity. I think that you might want to use layout inflater to load the new view from a layout file.

I might be a bit confused on what you are trying to accomplish though.

Bobbake4
  • 24,509
  • 9
  • 59
  • 94