简体   繁体   中英

Transparent background for custom AlertDialog

Strangely, this was working fine, this is how it looks on my app released on Google Play:

在此输入图像描述

And this is how it looks now:

在此输入图像描述

All I did was migrate to AndroidX.

Here is my dialog layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialogLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/rounded_corners">

        <Button
            android:id="@+id/btnAdd2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:text="@string/import_video"
            android:textColor="@color/dark_text" />

    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corners">

        <Button
            android:id="@+id/btnAdd1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:text="@string/add_student"
            android:textColor="@color/dark_text" />

    </FrameLayout>

</LinearLayout>

and here is how I inflate it:

View dialogView = View.inflate(getApplicationContext(), R.layout.dialog_main, null);
LinearLayout dialogLinear = dialogView.findViewById(R.id.dialogLinear);
//Here is where is set the background to transparent
dialogLinear.setBackgroundColor(0x00000000);

final AlertDialog alertD = new AlertDialog.Builder(this).create();
Button btnAdd1 = dialogView.findViewById(R.id.btnAdd1);
Button btnAdd2 = dialogView.findViewById(R.id.btnAdd2);
btnAdd1.setTypeface(mCustom_font_Bold);
btnAdd2.setTypeface(mCustom_font_Bold);           

btnAdd1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //Do stuff
    }
});

btnAdd2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //Do stuff
    }
});

alertD.setView(dialogView);
if (alertD.getWindow() != null) {
    alertD.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
alertD.show();

I searched and couldn't find why this would be happening. Can someone please give me some advise?


I have tried setting it in in the layout itself.


Edit 1:

After trying the answer below, it now looks like this, instead of above:

在此输入图像描述


I resolved it by adding the following styles:

<style name="NewDialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:background">@android:color/transparent</item>
</style>

Setting the style like this:

final AlertDialog alertD = new AlertDialog.Builder(this, R.style.NewDialog).create();

And to fix what happened in Edit 1, i changed my LinearLayout to a RelativeLayout

  1. You Can Create a theme and assign that theme to your AlertDialog

Define theme in your styles.xml

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

And Assign that theme to your AlertDialog

final AlertDialog alertD = new AlertDialog.Builder(this,R.style.CustomDialog).create();

Or

2.You should try and use Dialog instead of AlertDialog As Explained in this Answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM