EditText background base line color changing based on its focus in android

后端 未结 5 1196
时光取名叫无心
时光取名叫无心 2020-12-16 11:14

I need to design EditText with base line showed in below image and it will be change to some other color when it receives focus.!

相关标签:
5条回答
  • 2020-12-16 11:15

    I needed the same thing, and I solved it in the following way:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_focused="true" android:state_window_focused="true">
       <shape>
         <stroke android:color="@color/blue_500" android:width="2dp" />
         <corners android:radius="45dp"/>
       </shape>
    </item>
    <item android:state_focused="false" android:state_window_focused="false">
      <shape>
        <stroke android:color="@color/grey_600" android:width="2dp"/>
        <corners android:radius="45dp"/>
      </shape>
    </item>
    </selector>
    
    0 讨论(0)
  • 2020-12-16 11:21

    You can add theme for EditText

    <style name="EditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorControlNormal">@color/primary</item>
        <item name="colorControlActivated">@color/primary</item>
        <item name="colorControlHighlight">@color/primary</item>
    </style>
    

    And you can use in EditText as

    <EditText
        android:id="@+id/edtCode"
        android:layout_width="match_parent"
        android:layout_height="40dp"
       .......................
        android:theme="@style/EditTextTheme">
    
    </EditText>
    
    0 讨论(0)
  • 2020-12-16 11:30

    For changing the baseline of EditText when it is on focus you can simply go to the colors.xml file in your project and change the "colorAccent" value.

    res/values/colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    ...
    <color name="colorAccent">#DESIRED_COLOR</color>
    </resources>`
    
    0 讨论(0)
  • 2020-12-16 11:33

    Try Holo Color generator for custom colors.

    Android Holo Colors

    You can create Selector and set it as background of the EditText

    res/drawable/edit_text.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:state_window_focused="false"
            android:state_enabled="true"
            android:drawable="@drawable/textfield_default" />
    
        <item
            android:state_window_focused="false"
            android:state_enabled="false"
            android:drawable="@drawable/textfield_disabled" />
    
        <item
            android:state_pressed="true"
            android:drawable="@drawable/textfield_pressed" />
    
        <item
            android:state_enabled="true"
            android:state_focused="true"
            android:drawable="@drawable/textfield_selected" />
    
        <item
            android:state_enabled="true"
            android:drawable="@drawable/textfield_default" />
    
        <item
            android:state_focused="true"
            android:drawable="@drawable/textfield_disabled_selected" />
    
        <item
            android:drawable="@drawable/textfield_disabled" />
    
    </selector>
    

    layout :

    <EditText    
        android:id="@+id/mobilenumber"
        ....
        android:background="@drawable/edit_text" />
    
    0 讨论(0)
  • 2020-12-16 11:39

    use this in your editText style

    <item name="backgroundTint">@color/focus_tint_list</item>
    

    focus_tint_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:state_focused="true" android:color="?attr/colorAccent"/>
    
       <item android:state_focused="false" android:color="#999999"/>
    
    </selector>
    
    0 讨论(0)
提交回复
热议问题