Yang
Yang

Reputation: 6892

android: customized text selector

I wanted to design a customized text selector that changed the text color when user clicks the TextView. But got the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.view.InflateException: Binary XML file line #55: Error inflating class

here is what I have: drawable/text_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_enabled="false" 
        android:state_focused="true" 
        android:drawable="@color/black" /> 
  <item android:state_pressed="true" 
        android:drawable="@color/blue" /> 
  <item android:state_focused="true" 
        android:drawable="@color/black" /> 
</selector> 

layout/textview.xml

<TextView android:id = "@+id/last_page_button"
    android:text="@string/last_page_button_string" 
    android:gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:background="#ffffff"
    android:textColor = "@drawable/text_selector"
    android:layout_weight="1" />

values/color.xml

<resources> 
    <color name="white">#ffffffff</color> 
    <color name="black">#ff000000</color> 
    <color name="blue">#ffccddff</color>

Upvotes: 9

Views: 12709

Answers (2)

ess.crazy
ess.crazy

Reputation: 296

use selector as follows:

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_selected="true"

    android:color="@color/c1" />
<item
    android:color="@color/c2" />

Upvotes: 0

Alex Volovoy
Alex Volovoy

Reputation: 68444

You can't assign drawable to textColor. It has to be a Color.

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"

        android:color="@color/color1" />
    <item
        android:color="@color/color2" />
</selector>

create a folder color in your res , save this file as mycolor.xml and assign it to textColor as @color/mycolor

Upvotes: 24

Related Questions