8

I have a custom AlertDialog and I want to make it's background completely transparent. Normally to make an activity completely transparent, I do the following

  • set background to #00000000 in the xml layout

  • in the manifest set android:theme="@android:style/Theme.Holo.Dialog" for the activity.

  • In onCreate add getWindow().setBackgroundDrawable(new ColorDrawable(0)).

    But now that I am dealing with a Dialog, how do I accomplish transparency?

Here is the dialog code:

LayoutInflater inflater = getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.activity_mine1,
        (ViewGroup) findViewById(R.layout.mine1));
    mine1 = new AlertDialog.Builder(this);
    mine1.setView(dialoglayout);
    mine1.show();

And my xml is just a relativeLayout with other child views:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000" >

    ...

</RelativeLayout>

Note: I have already looked at some similar posts here, but they don't seem to work.

My real reason is that the background that I really want to use, is not rectangular. I get it to work in an activity. But I want to use a dialog instead.

EDIT:

Further playing around, I have this style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomDialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
</resources>

Which I add as

new AlertDialog.Builder(this, R.style.CustomDialog)
Community
  • 1
  • 1
Cote Mounyo
  • 13,817
  • 23
  • 66
  • 87

4 Answers4

8
    <style name="CustomAlertDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:width">300dip</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

            Dialog connectionDialog = new Dialog(this, R.style.CustomAlertDialog);
            connectionDialog.setContentView(set your view here);
            connectionDialog.show();
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • Yours accomplishes as much as my own `style.xml`. The background is barely transparent; it's very dark; you must look really hard to see that there is some weak transparency going on. I am looking for `#00000000`. Also, for some reason there is a white border around the background. – Cote Mounyo Jul 10 '13 at 19:47
  • width attribute 300dip is not taking effect. – M. Usman Khan Jan 21 '16 at 16:37
5

Use Dialog instead of AlertDialog.Builder and so use setContentView instead of setView.

Konsol Labapen
  • 2,424
  • 2
  • 15
  • 12
4
connectionDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Mani
  • 3,394
  • 3
  • 30
  • 36
0

Looks like its related to android:backgroundDimEnabled and/or android:backgroundDimAmount in your style. Take a look at this answer for more info: Translucent Activity filling the entire screen

You might want to try setting android:backgroundDimEnabledto false.

Community
  • 1
  • 1
invertigo
  • 6,336
  • 5
  • 39
  • 64