Home > front end >  How can I make the layout background of AlertDialog transparent?
How can I make the layout background of AlertDialog transparent?

Time:05-26

I'm creating an app in Android Studio. I created an AlertDialog with the background of the layout set to this custom xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/connectioncardColor" />
        <corners android:bottomRightRadius="8dp"
            android:bottomLeftRadius="8dp"
            android:topRightRadius="8dp"
            android:topLeftRadius="8dp"/>
        <stroke
            android:color="@color/colorPrimaryDark"
            android:width="1dp"/>
    
    </shape>

The corners of the background are nice and rounded but behind the background is another layer of white. So there is bit of white in the corner, how to remove that?

White in corner

CodePudding user response:

Create your custom style for dialog

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

then add this style in builder

AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyDialog)

CodePudding user response:

Since I cant just post a link I will copy the answer too, but I found my answer here: How to make custom dialog with rounded corners in android

Create an XML file in drawable, say dialog_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/white"/>
    <corners
        android:radius="30dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

set it as the background in your layout XML:

android:background="@drawable/dialog_bg"

Set the background of the dialog's root view to transparent, because Android puts your dialog layout within a root view that hides the corners in your custom layout.

Java:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Kotlin:

dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
  • Related