TextView state_pressed/state_focused/state_selected style change
Asked Answered
G

2

7

I'm trying to change a TextView style based on its state. My styles.xml contains:

<style name="text_normal_ops">  
    <item name="android:gravity">left</item>  
    <item name="android:textColor">@color/text_usual_color</item> 
    <item name="android:textStyle">bold</item>  
</style>  
<style name="text_normal_ops_pressed">  
    <item name="android:gravity">left</item>  
    <item name="android:textColor">@color/text_pressed</item>  
    <item name="android:textStyle">bold</item>  
</style>

My selector (text_ops.xml)is defined as:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true" style="@style/text_normal_ops_pressed" />  
    <item android:state_focused="true" style="@style/text_normal_ops_pressed" />  
    <item android:state_selected="true" style="@style/text_normal_ops_pressed" />  
    <item style="@style/text_normal_ops"/>  
</selector>

But when I apply this to my textview (style="@drawable/text_ops") it does not work. Any tips?
Thanks

Granadilla answered 9/11, 2011 at 12:1 Comment(1)
have you kept ur styles.xml in res->values folder???Perinephrium
C
8

In android as per my knowledge there is only two state-list 1. Color State List Resource 2. StateListDrawable. If you are using style in it then please recheck the doc

Please check below link for more info

  1. http://developer.android.com/guide/topics/resources/color-list-resource.html
  2. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Collation answered 9/11, 2011 at 12:13 Comment(1)
Thanks Maneesh, I guess that was the problem. Do you know of any work-around?Granadilla
A
0

The problem is the line style="@drawable/text_ops" this should be style="@style/text_ops".

I haven't tried using a selector for styles but it would be cool if it works that way.

Asiatic answered 9/11, 2011 at 12:11 Comment(1)
Yeah, I know what you mean. I also think it is kind of weird to put the styles selector file in the drawables folder, but I don't think it is possible to include the selector inside values/styles.xml.Granadilla

© 2022 - 2024 — McMap. All rights reserved.