Make TextView orange when focusing?

I have a text view, I set it to be interactive and interactive - how do I select it to highlight orange (like a button) when the user focuses it with the scroll wheel, etc.?

TextView tv = ...;
tv.setClickable(true);
tv.setFocusable(true);

thank

+3
source share
2 answers

This is pretty easy. Here is the solution.

You have a TextView element with its background attribute set to @drawable/tbselectoras follows.

<TextView android:text="My text" 
    android:id="@+id/tv01"
    android:layout_width="300dip"
    android:layout_height="150dip"
    android:layout_gravity="center_horizontal"  
    android:background="@drawable/tbselector"/>

The last attribute android:backgroundis essential for you.

Now you are creating tbselector.xmlin a subdirectory drawable. It looks like this.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/bgdefault"
        android:state_focused="false"
        android:state_selected="false"/>
    <item
        android:drawable="@drawable/bgselected"
        android:state_focused="true"
        android:state_selected="false"/>
</selector>

Now you create bgdefault.xmlin a subdirectory drawablethat looks like this.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="200dip"
        android:height="150dip"
        android:color="#00FF00"/>
    <solid
        android:color="#00FF00"/>
</shape>

, bgselected.xml drawable, , , .

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="200dip"
        android:height="150dip"
        android:color="#FFFF00"/>
    <solid
        android:color="#FFFF00"/>
</shape>

, TextView. , XML . - , .

, .

+10

, OnFocusChangeListener TextView, setBackgroundColor(int color)

0

Source: https://habr.com/ru/post/1764028/


All Articles