0

I have lots of textview in screen. I want to change color of textview whenever user click it and color should be remained same until user click other textview.

For this i am used this slector but it isn't working like that.

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:state_selected="false" android:color="#000"/>
    <item android:state_selected="true" android:color="#12fdfd"/>
</selector>

How should i do it? any help

enter image description here

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74

2 Answers2

0

Your TextView need to implemented Checkable interface. Try to use Known Indirect Subclasses of Checkable eg. CheckedTextView or implement your own TextView.

dieter_h
  • 2,707
  • 1
  • 13
  • 19
0

Replace the TextView by CheckedTextView then your selector must be as following

   <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item  android:state_checked="true"   android:color="#12fdfd"/>
     <item  android:color="#000"/>
  </selector>

use the setChecked(boolean bln) function for your CheckedTextView

 mCTxtView.setChecked(true);

You can use it as checkbox ui by setting drawable for checkedTextView

android:drawableLeft="@drawable/radio_drawable"

radio_drawable.xml

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