Femn Dharamshi
Femn Dharamshi

Reputation: 577

Change background colour onClick()

I have a ImageView and a tag_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <solid android:color="#272822">

    </solid>

    <stroke android:color="#686868" android:width="1dp">

    </stroke>

    <corners android:radius="8dp"/>
</shape>

What changes should i make so to : change the background colour of the background when the imageview is clicked. i have used this tag_layout as :

<ImageView
            android:background="@drawable/tag_layout"
            android:padding="5dp"
            android:scaleType="fitCenter"
            android:layout_marginRight="5dp"
            android:layout_width="0dp"
            android:layout_height="37dp"
            android:src="@drawable/zoomin"
            android:layout_weight="1"
            />

How do i assign on click colours in a xml file ?

Upvotes: 0

Views: 40

Answers (1)

Tiago Ornelas
Tiago Ornelas

Reputation: 1119

This should be enough

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true">
       <shape android:shape="rectangle">
           <solid android:color="#yourcoloronpressed"/>
           <stroke android:color="#686868" android:width="1dp"/>
           <corners android:radius="8dp"/>
       </shape>
   </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#272822"/>
            <stroke android:color="#686868" android:width="1dp"/>
            <corners android:radius="8dp"/>
        </shape>
    </item>
</selector>

Replace the yourcoloronpressed by your color.

You can also change the other atributes like stroke and corners on pressed, of course.

Upvotes: 1

Related Questions