3

I'm attempting to create a custom dialog with a semi transparent background. I managed to get it to work through code with:

getWindow().setBackgroundDrawableResource(R.color.bg_tran);

Where bg_tran == #A0000000. However I'd rather have it in XML as I use this for several different custom dialog classes. Using just android:background does not work. Nor setting the theme to @android:style/Theme.Translucent worked.

I found several articles talking about using the attribute android:windowBackground. However the android:windowBackground attribute does not exist. Meaning eclipse doesn't show me it as an option for auto complete. I've checked within my style sheet and my actual layout.xml. Since I'm compiling for 2.2 I thought that the issue and changed build target to 4.0.3. No fix. I've even tried explicitly using it anyway but it doesn't work. I searched the Android Dev website and can't even find the page which describes this attribute. Aside from occasionally mentioning in passing, there's nothing. Also looking up setBackgroundDrawableResource doesn't tell me what the equivalent XML attribute tag is. I'm incredibly confused. What am I missing?

Stack overflow references I used to learn above:
Transparent Dialog Theme
Android Dialog Transparent
How to make a custom dialog transparent

Update:
In case it wasn't clear, yes I've tried setting this up within a theme. Does not work. Here's my defined theme:

<style name="my_dialog_theme" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@color/bg_tran</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
</style>
Community
  • 1
  • 1
Ifrit
  • 6,791
  • 8
  • 50
  • 79
  • Why are you not using it as a theme? you can set this theme to all your dialogs.. – Ron May 07 '12 at 15:53
  • I am. Updated my question to reflect – Ifrit May 07 '12 at 16:06
  • I have used `android.R.style.Theme_Panel` when instantiating the `Dialog`. In the XML I used a custom `Drawable` as the background to do the white outline with rounded corners. – techi.services May 07 '12 at 18:02

2 Answers2

3

You need to define a custom theme for your dialog.

<style name="Theme.CustomDialog" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    ...
    ...
</style>

Update:

You can achieve what you want in your dialog by extending the Dialog class. Set the window background drawable in this derived class.

public class CustomDialog extends Dialog 
{
    public CustomDialog (final Context context)
    {
        super(context);

        // This is the layout XML file that describes your Dialog layout
        this.setContentView(R.layout.myDialogLayout);  
        getWindow().setBackgroundDrawableResource(R.color.bg_tran);
    }
}
Ron
  • 24,175
  • 8
  • 56
  • 97
  • I already am. The issue is that windowBackground is not working. Updated question to show my theme. – Ifrit May 07 '12 at 16:07
  • try adding `WindowIsTranslucent` too to the theme.should work. – Ron May 07 '12 at 17:16
  • Nope. Still not working. Where are you finding these tags? Cause none of these window* attributes are appearing as available options. Is there some magical list of all xml attributes? – Ifrit May 07 '12 at 17:31
  • Did you not read my question? The first thing I said was that I already got it to work through code by using the getWindow().setBackgroundDrawableResource() method. That's not the issue. I'm trying to get it through XML only. – Ifrit May 07 '12 at 17:50
  • And you want to make the solution scalable.. right..? Why only through xml method? – Ron May 07 '12 at 17:53
  • Separation of functionality and design. I do not want to always call the setBackground method in code everytime I use that layout. – Ifrit May 07 '12 at 17:56
  • Okay.. dont set the layout in `CustomDialog`. Set it in your class derived from this `CustomDialog` but apply all needed properties in this `CustomDialog`'s constructor.. – Ron May 07 '12 at 18:01
  • yeeaahhhh. Thought about doing that as a backup. Guess that means its not possible thru XML only – Ifrit May 07 '12 at 18:06
  • Its possible using xml by using ` – Ron May 07 '12 at 18:13
  • Would you acccept this as answer? – Ron May 20 '12 at 05:04
  • make sure theme DOES NOT have false – lannyf Aug 20 '15 at 17:52
0

I've experienced the same issue. android:windowBackground attribute just does not appear.you have to type the attribute complete and correct yourself then build your project. Voila, it will compile without any errors and it will work perfectly.

kaveh gh
  • 69
  • 8