0

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 ?

Amir Sadegh
  • 111
  • 1
  • 2
  • 7

3 Answers3

3

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

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
m.Ling
  • 296
  • 1
  • 6
3
 <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>
Kirti
  • 691
  • 8
  • 27
0

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

Community
  • 1
  • 1
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64