farm ostrich
farm ostrich

Reputation: 5959

Setting the "dim" color for a Dialog

I understand how to set the dim amount for a Dialog. However, I would prefer that the color was a translucent white instead of a translucent black (or is it dark gray?). Any ideas?

Upvotes: 4

Views: 5535

Answers (1)

inazaruk
inazaruk

Reputation: 74780

First, you need to create white 9-patch drawable. Similar to panel_background.9.png drawable (which is black) that can be found at path:

 <android-sdk-dir>/platforms/android-#/data/res/drawable-hdpi/panel_background.9.png

Let's assume your drawable will be called white_panel_background.9.png.


Option 1.

You can create your custom theme that extends Theme.Dialog and overrides windowBackground attribute with custom white 9-patch drawable.

 <style name="Theme.WhiteDialog" parent="@android:style/Theme.Dialog">
     <item name="android:windowBackground">@drawable/white_panel_background</item>  
 </style>

And then use this theme in your dialog:

Dialog dialog = Dialog(context, R.style.Theme_WhiteDialog);

Option 2.

If you've already created the dialog (with default theme), you still can update its window background:

Dialog dialog = ...;
...
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.white_panel_background);
dialog.getWindow().setBackgroundDrawable(drawable);

Note: you'll need to change text colors from white to black (so its visible on white background).
Note: you'll need to do some extra steps for AlertDialog as it uses its own styles for backgrounds. For more details see AlertDialog style.


enter image description here

Upvotes: 4

Related Questions