Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText Style.xml change FOCUS EditText

Good afternoon,

Guys I did a style file for all fields style.xml EditText now when the field gaining focus, I want to change the style. How do?

like image 236
Globsecure Avatar asked Mar 05 '26 10:03

Globsecure


1 Answers

You have to define the drawables for each state of your edit text. You create an xml file in your drawables folder that defines all the state drawables for your edit text. The XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

You make one item for each state drawable you want to specify. So for a drawable for state_focused you'd just do:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/myFocusedEditText"
        android:state_focused="true"/>
    <item
        android:drawable="@drawable/myEnabledEditText"
        android:state_enabled="true"/>
</selector>

Name this XML file, custom_edit_text.xml, make sure it's in the drawables folder. Then all you have to do is define in your styles.xml

<style name="my_edit_text">
    <item name="android:drawable">@drawable/custom_edit_text</item>
</style>

Good luck!

like image 131
Zaid Daghestani Avatar answered Mar 06 '26 23:03

Zaid Daghestani



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!