Search code examples
javaandroidandroid-layoutandroid-dialog

Android Dialog with rounded corners - still showing the background without corners radius


I want to make rounded corners dialog; but after I was done, it appears like this>>

Result Design

Java

AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(this);
dialogBuilder.setView(R.layout.complain_dialog);
final AlertDialog alertDialog= dialogBuilder.create();
alertDialog.show();

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
app:cardBackgroundColor="#FFF"
app:cardCornerRadius="15dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="15dp"
        android:background="@color/black_overlay" />

</android.support.v7.widget.CardView>

The problem is: why the dialog is still shown int the background without corners radius?

After searching for a solution to this problem, I found some of these solutions>>

1- Android Dialog - Rounded Corners and Transparency

2- Android custom alert dialog with rounded corners

3- Android dialog background with corner radius has layered background

Java- After test the above solutions

Dialog dialog= new Dialog(getContext());
dialog.setContentView(R.layout.complain_dialog);
dialog.getWindow().setBackgroundDrawable(new 
ColorDrawable(Color.TRANSPARENT)); 
dialog.show();

Result after test the solutions

Result Design

Now the dialog is not appear at all! Can any one give me solution for this problem? Thank you in advance.


Solution

  • Solution with AlertDialog with same layout (tested On Kitkat).

     AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(this);
     dialogBuilder.setView(R.layout.temp);
     final AlertDialog alertDialog= dialogBuilder.create();
     alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
     alertDialog.show();
    

    To appear Dialog as same you need set the width of dialog. Here is a reference. Otherwise it will take the Content width . Do all dialog.getWindow() operation before setting Content view to Dialog.