I have a TextView in a layout, on click of text I want to changed the color, I know I can use text.setTextColor(Color.parseColor("#FFFFFF"));
but the problem is that the color does not save and after pressing back the color does not get changed, what can I do ?

- 111
- 1
- 2
- 7
3 Answers
You can create a selector to you textView
http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList
or you can register ontouchlistener
on you button
http://developer.android.com/reference/android/view/View.OnTouchListener.html
and with help of the MotionEvent you can make switch case of the actions for when you are pressing and when you are releasing the button
http://developer.android.com/reference/android/view/MotionEvent.html

- 13,409
- 16
- 61
- 96

- 296
- 1
- 6
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:color="#ffffffff"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!--default -->
</selector>

- 691
- 8
- 27
-
it causes this error `java.lang.ClassNotFoundException: android.view.selector in loader dalvik.system.PathClassLoader[/data/app/com.emergencycalls-1.apk]` – Amir Sadegh Sep 03 '13 at 07:48
-
@something u r missing to use – Kirti Sep 03 '13 at 07:50
-
tell me what exactly u r doing? – Kirti Sep 03 '13 at 08:52
-
it seys item tag needs drawable – Amir Sadegh Sep 03 '13 at 10:19
-
add above xml file to u r texview android:drawable="@drawable/xmlfilename" – Kirti Sep 03 '13 at 10:33
-
clean the project once and check – Kirti Sep 03 '13 at 10:35
Well!
You can define the Text color for all 4 state, similarly as you defined background for the Button. For example:
file name: /res/color/text_color.xml
XML file saved at res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#440000"/>
<item android:state_focused="true" android:color="#004400"/>
<item android:state_pressed="true" android:color="#000044"/>
<item android:color="#444"/>
</selector>
Take this file into your Button Style(\res\values\styles.xml) like this:
<style name="button_text" parent="@android:style/Widget.Button">
<item name="android:textColor">@color/text_color.xml</item> <!-- Here is specified text color -- >
</style>
Now add this style to your button
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/button_text">
</Button>
For detail check out this

- 1
- 1

- 6,200
- 6
- 45
- 64
-
I want to change the text color not setting a background, in first solution your code needs an image as background – Amir Sadegh Sep 03 '13 at 10:32
-
@AmirSadeghsorry i miss understood the question, now I have updated the answer .I'll surely help u – Shakeeb Ayaz Sep 03 '13 at 15:54