Anthony
Anthony

Reputation: 3074

Custom drawable background corners radius not working

I am making a custom drawable background for a dialog, I cannot seen to get the corner radius working with the XML (see photo)

You can see the corner radius at the top left & top right is not blue or showing correctly.

Here is the drawable XML code:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:bottom="50dp">
        <shape android:shape="rectangle">
            <size android:height="100dp" />
            <solid android:color="@color/new_booking_via_address" />
            <corners
                android:bottomLeftRadius="0dp"
                android:bottomRightRadius="0dp"
                android:radius="10dp"
                android:topLeftRadius="10dp"
                android:topRightRadius="10dp" />
        </shape>
    </item>

    <item android:top="100dp">
        <shape android:shape="rectangle">
            <size android:height="200dp" />
            <solid android:color="@color/White" />
            <corners
                android:bottomLeftRadius="10dp"
                android:bottomRightRadius="10dp"
                android:radius="10dp"
                android:topLeftRadius="0dp"
                android:topRightRadius="0dp" />
        </shape>
    </item>
</layer-list>

Here is the layout XML code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320dp"
    android:layout_margin="20dp"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="320dp"
        android:background="@drawable/dialog_bg"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/transparent"
            android:animateLayoutChanges="true"
            android:gravity="center"
            android:orientation="vertical">

            <de.hdodenhof.circleimageview.CircleImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:background="@drawable/ic_perm_identity_white_48dp" />

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_margin="20dp"
            android:layout_height="wrap_content"
            android:text="Here is some text which the user may read to then grant a permission"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:animateLayoutChanges="true"
            android:orientation="vertical">

            <Button
                android:id="@+id/grantPermissionButton"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_gravity="right|center"
                android:gravity="right|center"
                android:text="Grant permission" />

            <Button
                android:id="@+id/notNowButton"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_gravity="right|center"
                android:gravity="right|center"
                android:text="Not now" />

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

Example

Upvotes: 0

Views: 1519

Answers (1)

Pztar
Pztar

Reputation: 4759

In code set the background of the Dialog to be transparent.

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

Use the CardView attributes to round your corners. With app:cardCornerRadius="10dp" so you have this at the root:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="320dp"
    android:layout_margin="20dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:cardCornerRadius="10dp">

Upvotes: 2

Related Questions