Revathi
Revathi

Reputation: 307

Textview onclick bg color change in android

If we click any of the Textview within a Listview, background color should have to change and onclick release, it should have to change to transparent color in android.Is there anyway?

Upvotes: 2

Views: 8732

Answers (2)

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

Create a textview_selector.xml in drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed_color"
          android:state_pressed="true" />    
    <item android:drawable="@drawable/normal_color" />
</selector>

Define colors in color.xml in res>values:

<?xml version="1.0" encoding="utf-8"?>
<resources>

     <drawable name="pressed_color">#FF0000</drawable> <!--custom color for pressed state -->
    <drawable name="normal_color">#00FFFFFF</drawable> <!--transperent color for normal state -->
</resources>

Now,you need to use it where you define textviews as a part of your listitem.

<TextView 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/textview1"
      android:gravity="center"
      android:text="This is sample!"
      android:background="@drawable/textview_selector"   
      android:clickable="true"   
/>      

Upvotes: 14

Jviaches1
Jviaches1

Reputation: 40

Visit http://developer.android.com/guide/topics/ui/themes.html ! There is very good explanation regarding concept you looking for.

Upvotes: 1

Related Questions