Change the color of the text in the android edit box in accordance with the focus

I use the following xml file to change the color of the editbox. But I would like to know how to change the color of the text in the edit box according to the focus. If the edit field is concentrated, I like to change the color of the text to black, and when it does not focus, I like to change the color to white.

How to implement this in an XML file? Any help would be really helpful for me in exploring the resources available for Android.

 <?xml version="1.0" encoding="UTF-8"?>
 <selector
     xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
    <shape>
        <gradient
            android:endColor="#FFFFFF"
            android:startColor="#FFFFFF"
            android:angle="90" />
        <stroke
            android:width="3dp"
            android:color="#0f0f0f" />
        <corners
            android:radius="10dp" />
    </shape>
</item>

<item android:state_focused="false">        
    <shape>
        <gradient
            android:endColor="#000000"
            android:startColor="#000000"
            android:angle="90" />
        <stroke
            android:width="3dp"
            android:color="#151515" />
        <corners
            android:radius="10dp" />
    </shape>
</item>

+3
source share
2 answers

you should use a ColorStateList instead of a list of states.

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

then just set this list of states to the textColor property.

+2
source

I think you are looking for a list of states .

0
source

All Articles