Android uses selector to change the font color and background color in TextView

  • 2020-12-21 18:09:46
  • OfStack

This article shows how Android uses selector to modify the font color and background color in TextView. To share for your reference, the details are as follows:

selector in android everyone is familiar with, it can be very convenient to use it to implement, control in different actions, color equivalent change. Here I'm going to say one of the applications in TextView.

I think we all know that Button button is a special TextView in the source code, so we often use TextView to complete the button, just add 1 android:clickable="true".

When TextView uses selector, there are two situations, one is the normal judgment on the TextView control, such as press, focus, etc., and the other is the judgment on these actions of the TextView outer control, and then the action is returned to TextView.

1, normal judgment on TextView control, press, focus and other actions

This is relatively simple. In order to change the background color and text color of the control, we use two xml files. The code is as follows:

tbackground.xml modifies the background


<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!--  The default background image -->
  <!--<item android:drawable="@color/white" />-->
  <!--  Background image with no focus  -->
  <item android:state_window_focused="false" android:drawable="@color/white" />
  <item android:state_focused="false" android:state_pressed="true"  android:drawable="@color/btnbackBlue" />
</selector>

So I'm going to make a point here, and you can see that I've highlighted the background image (color) by default, and why is that, because you put this one first, whenever it runs, it runs first, and when it runs, it doesn't run anymore, so all of this stuff is gone. If you want to set the default, put this line of code at the bottom.

ttextcolor.xml modifies text


<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!--  Background image with no focus  -->
  <item android:state_window_focused="false" android:color="@color/black" />
  <item android:state_focused="false" android:state_pressed="true"  android:color="@color/white" />
  <!--  The default background image -->
  <item android:color="@color/black" />
</selector>

I put the default value at the end of the text, and I'm going to say 1 here, we're going to use android:drawable for the background and android:color for the color of the text, otherwise it's going to get an error, why? Think about it. Ha ha...


<TextView
    android:id="@+id/txt_collection_cancel"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text=" cancel "
    android:textColor="@drawable/ttextcolor"
    android:gravity="center"
    android:background="@drawable/tbackground"
    android:clickable="true"/>

2. These actions of the TextView outer control are then passed back to TextView.

This also happens a lot. We usually add an LinearLayout or RelativeLayout to the outer layer. And we're going to give the click event to this outer control. At this point, you want to change the background of the outer control and the text color of the TextView control. Now at this point, and we're still doing this, you'll see that TextView doesn't react, why, because it doesn't get an event, and then there's one property that it uses which is android:duplicateParentState

The official explanation is that "if this property is set, the drawing state (cursor, press down, etc.) will be fetched directly from the parent container. Note that only the drawing state is obtained, not the event. That is, Button has the effect of being clicked when you click on 1, but does not execute the click event. Look at the code below:


<RelativeLayout
    android:id="@+id/rela_collection_add"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@drawable/tbackground"
    android:clickable="true">
    <View
      android:id="@+id/line_collection_add"
      android:layout_width="fill_parent"
      android:layout_height="1dp"
      android:background="@color/gray"
      android:layout_gravity="center_vertical"
      android:layout_alignParentBottom="true"
      />
    <TextView
      android:id="@+id/txt_collection_add"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:text=" New favorites "
      android:textColor="@drawable/ttextcolor"
      android:textSize="@dimen/ActionBar_title_size"
      android:duplicateParentState="true"
      android:gravity="center"
      android:layout_above="@+id/line_collection_add"
      />
</RelativeLayout>

While we are changing the background of the outer control, we are also changing the color of the TextView text.

I hope this article has been helpful in Android programming.


Related articles: