How to implement TextView color selector when OnClick listener is installed in its parent layout?

The xml layout is as follows. I have a RelativeLayout that contains a TextView. The OnClick listener is set to RelativeLayout. RelativeLayout has a selector background. I want when the user clicks on the RelativeLayout, the background of the RelativeLayout should change, and the color of the TextView text will also change. Although I set the color selector for the TextView, only the RelativeLayout selector works. The color selector in TextView does not work. How can I implement changing the background and RelativeLayout of the background and TextView text when the user clicks the layout? Thanks.

<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/background_selector" android:id="@+id/mylayout" > <TextView android:id="@+id/mytextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:typeface="sans" android:textSize="18sp" android:textStyle="bold" android:singleLine="true" android:ellipsize="end" android:textColor="@drawable/color_blue_selector" /> </RelativeLayout> 
+4
source share
3 answers
+3
source

Isn't it easy

 relativeLayout.setBackgroundResource(R.color.brown); textView.setTextColor(R.color.white); 

... having brown and white values ​​in res / values ​​/color.xml, defined as

 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="white">#FFFFFF</color> <color name="brown">#2E2B27</color> </resources> 
+1
source

Perhaps you could use OnTouchListener and respond to Down and Down events by changing the color of the text

0
source

All Articles