Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing text color in edit box of android according to the focus

I am using the below xml file to change the color of editbox. But i would like to know how to change the text color in the editbox according to the focus. If the editbox is focussed i like to change the text color to black and when it is not focussed i like to change the color to white.

How do i implement this in the xml file? Any help would be really useful for me in learning the android drawable resources.

 <?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>

like image 664
Ron Avatar asked Jan 28 '26 22:01

Ron


1 Answers

you should use ColorStateList instead of State List.

<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 assign this state list to textColor property.

like image 177
woodshy Avatar answered Jan 30 '26 14:01

woodshy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!