Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Color for area outside rectangle's curved edges

I am making a rectangle for the background of a RelativeLayout and was wondering how I could make the area outside of the rounded edges of this rectangle to also be darker_grey. Currently, only the inside of this rectangle is darker_grey

<?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/darker_gray" />
    <stroke android:width="1dip" android:color="@android:color/black"/>
    <corners android:radius="20dp" />
</shape>
like image 227
Paradox Avatar asked Jan 02 '23 22:01

Paradox


2 Answers

You can do it by switching to a layer-list drawable, and layering your shape on top of a solid colour, like so:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@android:color/darker_gray" />
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="@android:color/darker_gray" />
            <stroke android:width="1dip" android:color="@android:color/black"/>
            <corners android:radius="20dp" />
        </shape>
    </item>
</layer-list>
like image 67
Logan Pickup Avatar answered Jan 05 '23 16:01

Logan Pickup


Another way to implement background color to any view using shape:

<ImageView
        android:layout_width="200dp" 
        android:layout_height="200dp"
        android:background="@android:color/darker_gray"
        android:src="@drawable/shape"/>

Please decide view width and height based on your requirement.

It will look like:

enter image description here

like image 45
Jitesh Mohite Avatar answered Jan 05 '23 17:01

Jitesh Mohite