Android Custom Dialog with transparent window Background breaks my button selector

FoamyGuy picture FoamyGuy · Oct 29, 2011 · Viewed 11.1k times · Source

I am making a custom dialog with a transparent window background set in the style. I have another button in my activity behind the dialog set with the same button_selector as the background, and it works fine, so I know the problem is something with the style and more specifically the windowBackground attribute.

Does anyone know how I can get a transparent window background for my custom dialog but still allow my button selector to work properly?

Included are pictures of how it looks with the button background set to @drawable/lightblue1, and @drawable/button_selector.

this is my style xml

<resources>
   <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
     <item name="android:windowBackground">@color/transparent</item>
     <item name="android:windowIsFloating">false</item>
     <item name="android:windowNoTitle">true</item>
  </style>
</resources>

If I remove the <item name="android:windowBackground">@color/transparent</item> line then my button selector works correctly, but my dialog is put inside the systems default dialog container background.

This is my button declaration xml. If i change @drawable/button_selector to one of the actual png files then it appears correctly, but with the selector my button background gets set to transparent.

  <Button
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/button_selector"
  android:layout_marginBottom="15dp"
  android:textSize="35sp"
  android:text="@string/btnText1">
  </Button>

Here is how I create the dialog from java:

 Dialog dialog = new Dialog(TimeClock.this, R.style.CustomDialogTheme);
 dialog.setContentView(R.layout.tag);
 dialog.show();

Here is the button_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/darkblue1" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/darkblue1" /> <!-- focused -->
     <item android:drawable="@drawable/lightblue1" /> <!-- default -->
 </selector>

EDIT: I ended up "faking" my dialog with a translucent activity so that I could get better control over its appearance.

with button set to the the png file with button set from the selector

Answer

Dan J picture Dan J · Oct 30, 2011

I don't understand exactly what is going wrong, but maybe it is worth defining your CustomDialogTheme based on an existing translucent theme? e.g.

<resources>
   <style name="CustomDialogTheme" parent="@android:style/Theme.Translucent.NoTitleBar">

It is also possible that you may need to set some of the extra style items like (taken from this question):

<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>

I was wondering if Android uses one of those settings to allow your button selector to work?