Home » Android » android AlertDialog with transparent background

android AlertDialog with transparent background

Posted by: admin June 15, 2020 Leave a comment

Questions:

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)
How to&Answers:

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

Answer:

    <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();

Answer:

connectionDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

Answer:

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.