Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlertDialog with rounded corners: rectangle seen below corners

I want a Dialog with rounded corners, but when the Dialog is seen there's a rectangle below it that's seen below the corners, like this:

enter image description here

I build the dialog using a custom DialogFragment:

public class MyDialogFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.playdialog, null));
    return builder.create();
}
}

The dialog layout (playdialog) has the following drawable as background:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

    <solid
        android:color="#AAAAAAAA" />

    <stroke
        android:width="2dp"
        android:color="#FF000000" />

    <corners android:radius="20dp" />
</shape>

As I said, I set this drawable as background:

android:background="@drawable/dialog_background"

I don't want that rectangle to be seen. How can I do it??

In this post the user had the same problem. I tried to use the solution that worked for him but it didn't work for me. I modified my DialogFragment like this:

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.playdialog, null));
    Dialog d = builder.create();
    d.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    return d;
}

The result is exactly the same. How can I remove that rectangle?

Thanks!

like image 864
PX Developer Avatar asked Feb 28 '13 19:02

PX Developer


People also ask

How do I set corner radius for alert dialog in Android?

You can simply use MaterialAlertDialogBuilder to create custom dialog with rounded corners. then create a Alert Dialog object in Java class like this : AlertDialog alertDialog = new MaterialAlertDialogBuilder(this,R. style.

How many styles of AlertDialog are there?

There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)

How do you give shape to AlertDialog in flutter?

To display rounded corners for AlertDialog in Flutter, specify the shape property of AlertDialog with RoundedRectangleBorder object with border radius specified. An AlertDialog with rounded corners having a border radius of 30 is shown in the following screenshot.

How do I change the font in alert box?

If you only want to change text format, you can just override alertDialogTheme attribute to change the theme for the AlertDialog .


1 Answers

I was shocked when I occured the same problem, the solution is also a little weird. Create your own custom drawable for eg see below.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@color/text_highlight" />

    <stroke
        android:width="5dp"
        android:color="@color/text_highlight" />

    <corners android:radius="12dp" />

    <stroke
        android:width="1dp"
        android:color="@android:color/transparent" />

</shape>

Add the following lines to your dialog:

 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

 // This is line that does all the magic
 dialog.getWindow().setBackgroundDrawableResource(                             
 R.drawable.dialog_rounded);
like image 66
Akshay Mukadam Avatar answered Nov 09 '22 17:11

Akshay Mukadam