9

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?

MoOoG
  • 293
  • 2
  • 5
  • 20

4 Answers4

9
<EditText
    android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Something or Other"
            android:backgroundTint="@android:color/holo_green_light" />

Note: only works with AP lvl 21 or higher.

vinod
  • 1,126
  • 13
  • 18
9

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>
Nika Kurdadze
  • 2,502
  • 4
  • 18
  • 28
3

Programmatically just implement this line:

inputText.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.error), PorterDuff.Mode.SRC_ATOP);

inputText is your EditText object.

Vipul Asri
  • 8,903
  • 3
  • 46
  • 71
  • I tried it and it works too. There is only one thing - on Android 6.0 Marshmallow (API 23) context.getResources().getColor() is deprecated so I used ContextCompat.getColor(). The solution of my problem is in my comment above Thank you – MoOoG Mar 03 '17 at 17:58
  • I think that when you lose focus , it stays with that color ( it should back to the default color) – Royi Namir Jan 22 '18 at 11:20
0

Ok, I have no idea what happened but the code, I posted above, works. I haven't changed anything, but I restarted the Android studio.

Before I posted this, I did the project rebuild under Build > Rebuild Project and it didn't help.

If somebody can explain why this happened or what should I do in this case, I would be grateful, but the problem is solved.

MoOoG
  • 293
  • 2
  • 5
  • 20