userbb
userbb

Reputation: 1874

Change button text using <selector>

I am using selector to change color of button. And I was trying to do same with text property. Is it possible to change text in such way?

Upvotes: 7

Views: 4422

Answers (1)

hardartcore
hardartcore

Reputation: 17037

As far as I know, you can't do that because android:text="" takes string or string resource id as param. If you for example do something like this :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:text="SECOND" />
    <item android:state_focused="true" android:state_pressed="true" android:text="SECOND" />
    <item android:state_focused="false" android:state_pressed="true" android:text="SECOND" />
    <item android:text="FIRST" />
</selector>

, like the way we are changing text color or background, it will put the path to this selector as your button text. It will be something like : res/drawable/text_selector.xml or whenever you put this file.

So the only way to achieve this is to control it manually in button's OnClickListener.

Upvotes: 8

Related Questions