Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of EditText's underline

I have tried to change the underline color of my EditText with the help of this thread.

I did the same thing - this is the EditText with the android:background set as @drawable/edt_bg_selector.

<EditText
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:ems="10"
         android:id="@+id/editText4"
         android:layout_weight="1"
         android:elevation="0dp"
         android:background="@drawable/edt_bg_selector" />

My edt_bg_selector:

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

... edt_bg_normal.xml ...

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1px"
                android:color="@color/colorWhite" />

            <solid android:color="#00FFFFFF" />

            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />
        </shape>
    </item>
</layer-list>

... and the last, edt_bg_selected.xml .

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1px"
                android:color="@color/colorWhite" />

            <solid android:color="#00FFFFFF" />

            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />
        </shape>
    </item>
</layer-list>

With this code I can see the color of underline has changed to white in Design Editor of Android Studio. But when I run this project on android device, the changes haven't been made. Selected EditText's underline is my primaryColor and unselected EditText is black.

Android studio design editor:

Android studio design editor

Project run on android device:

android device

Does this have anything to do with the selected theme of my application? Or what did I miss?

like image 396
MoOoG Avatar asked Mar 03 '17 17:03

MoOoG


People also ask

How do you underline in EditText?

Custom Subject Modification UnderlineAdd an EditText to activity_main. xml, and add the android:theme="@style/MyEditText" attribute. The effect is as follows: As you can see, the color of the cursor and underline will be changed, but the spacing will remain.

How do you change the color of the TextInputLayout underline?

You can apply a custom style to change the colors. To change the hint color you have to use these attributes: hintTextColor and android:textColorHint . To change the bottom line color you have to use the attribute: boxStrokeColor .


1 Answers

Add these attributes to your activity theme:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorControlNormal">@android:color/white</item>
    <item name="colorControlActivated">@android:color/white</item>
    <item name="colorControlHighlight">@android:color/white</item>
</style>
like image 162
Nika Kurdadze Avatar answered Sep 29 '22 20:09

Nika Kurdadze