How to make custom dialog with rounded corners in android

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

this works for me


With the Androidx library and Material Components Theme you can override the getTheme() method:

import androidx.fragment.app.DialogFragment

class RoundedDialog: DialogFragment() {

    override fun getTheme() = R.style.RoundedCornersDialog

    //....

}

with:

<style name="RoundedCornersDialog" parent="@style/Theme.MaterialComponents.Dialog">
    <item name="dialogCornerRadius">16dp</item>
</style>

Or you can use the MaterialAlertDialogBuilder included in the Material Components Library:

import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class RoundedAlertDialog : DialogFragment() {

    //...

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return MaterialAlertDialogBuilder(requireActivity(), R.style.MaterialAlertDialog_rounded)
                .setTitle("Test")
                .setMessage("Message")
                .setPositiveButton("OK", null)
                .create()
    }

}

with:

<style name="MaterialAlertDialog_rounded" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/DialogCorners</item>
</style>

<style name="DialogCorners">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
</style>

enter image description here

If you don't need a DialogFragment just use the MaterialAlertDialogBuilder.


You need to do the following:

  • Create a background with rounded corners for the Dialog's background:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    
        <solid android:color="#fff" />
    
        <corners
            android:bottomLeftRadius="8dp"
            android:bottomRightRadius="8dp"
            android:topLeftRadius="8dp"
            android:topRightRadius="8dp" />
    
    </shape>
    
  • Now in your Dialog's XML file in the root layout use that background with required margin:

    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:background="@drawable/dialog_background"
    
  • finally in the java part you need to do this:

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(layoutResId);
    View v = getWindow().getDecorView();
    v.setBackgroundResource(android.R.color.transparent);
    

This works perfectly for me.


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