Rounded corners with background image for text view in android studio

407 Views Asked by At

I have a text view and and I want its background to be a image and also its corners rounded. I have tried that by creating a drawable file but the background image gets spilled out.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:padding="5dp"
    android:orientation="horizontal">
    <TextView
    
android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/colorPrimaryDark"
        android:background="@drawable/drawable_file"
        />
</LinearLayout>

drawable_file.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@drawable/img" />
    </item>
    <item>
        <shape>
            <stroke
                android:width="1dp" />
           
            <corners android:radius="50dp" />
        </shape>
    </item>
</layer-list>

Output:

Example Output:

1

There are 1 best solutions below

1
Lares234 On

As in official Android documentation for LayerList

A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.

Meaning your drawable needs to be placed after the rounded background to be visible.