micky
micky

Reputation: 73

how to set background image to textview in android?

i want to set background image to textview with its height and width.means if the textview size small than it set it small image if its size big than it draw large image to that text view.

Upvotes: 0

Views: 4120

Answers (2)

2red13
2red13

Reputation: 11217

maybe a xml drawable is helpful, depends to waht kind of image you want to load, store the following xml in your drawable folder and declare the cml as background for your textview

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape>
        <solid
        android:color="#ef4444"/>
        <stroke
        android:width="1dp"
        android:color="#992f2f"/>
        <corners
        android:radius="4dp"/>
        <padding
        android:left="4dp"
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp"/>
    </shape>
</item>
<item>  
    <shape> 
        <gradient
        android:startColor="#ef4444"
        android:endColor="#992f2f"
        android:angle="270"/>
        <stroke
        android:width="1dp"
        android:color="#992f2f"/>
        <corners
        android:radius="4dp"/>
        <padding
        android:left="4dp"
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp"/>
    </shape>
</item>

Upvotes: 0

Gabriel Negut
Gabriel Negut

Reputation: 13960

Try a 9-patch image. See here for details.

Upvotes: 1

Related Questions