Add rounded corner to Alert Dialog

Fabio :

I'm trying to add rounded corners to AlertDialog, but I don't understand the logic of the shape file (which doesn't work). I'm using it as background in the RelativeLayout of the AlertDialog, but it seems that it gets ignored. This is the shape file:

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

This is the alert dialog xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/dialog_rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".UserList"
    android:background="@drawable/shape_dialog">
    <TextView
        android:id="@+id/dialog_titile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Scegli un'operazione"
        android:textAlignment="center"
        android:padding="5dp"
        android:textColor="@android:color/black"
        android:background="#D3D3D3"
        android:textSize="26dp" />

    <TextView
        android:id="@+id/dialog_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vuoi aprire o eliminare il test?"
        android:textAlignment="center"
        android:padding="15dp"
        android:textSize="26dp"
        android:layout_marginLeft="80dp"
        android:background="@android:color/white"
        android:textColor="@android:color/black"
        android:layout_below="@id/dialog_titile" />
    <Button
        android:id="@+id/dialog_neutral_btn"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text="Indietro"
        android:layout_below="@id/dialog_tv"
        android:textColor="@android:color/black"
        android:background="@drawable/button_bg_3" />
    <Button
        android:id="@+id/dialog_positive_btn"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Apri"
        android:layout_alignBaseline="@id/dialog_neutral_btn"
        android:layout_alignParentRight="true"
        android:background="@drawable/button_bg_3"
        android:layout_marginRight="20dp"/>
    <Button
        android:id="@+id/dialog_negative_btn"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Elimina"
        android:layout_toLeftOf="@id/dialog_positive_btn"
        android:layout_alignBaseline="@id/dialog_neutral_btn"
        android:background="@drawable/button_bg_3"
        android:layout_marginRight="10dp" />
</RelativeLayout>

And this is the result What did I miss?

Tamir Abutbul :

I have checked your shape and it looks good on my phone, I have this shape that works the same way + have gradiant :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="-90"
    android:centerColor="#F2F2F2"
    android:endColor="#ADA996"
    android:startColor="#DBDBDB" />
<stroke
    android:width="2dp"
    android:color="#000000" />
<corners android:radius="8dp" />

<padding
    android:bottom="4dp"
    android:left="4dp"
    android:right="4dp"
    android:top="4dp" />
</shape>
  • Try to use the shape above, If it will not work anyway try to use your shape on a single button - if you will see your shape working correctly you will know that your problem is coming from your layout file.

  • And maybe all you need to do Invalidate Caches/Restart, it is possible that you have no mistakes but your app is using old shape coming from the cache

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126287&siteId=1