2

I encountered a problem recently, which was to change the default underline bar of an edittext from a default blackish color to white.

I used this solution, which seemed to me the best practices by changing the style of the EditText :

EditText

<android.support.v7.widget.AppCompatEditText
    android:id="@+id/nom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/Activity_Main_EditText_Nom"
    android:imeOptions="actionNext"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:textColor="@android:color/white"
    android:textColorHint="@color/activityBackground"
    android:theme="@style/EditTextStyle"
    android:visibility="gone"
/>

style.xml

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDisablePreview">true</item>
    <item name="android:windowBackground">@color/activityBackground</item>
    <item name="windowActionBar">false</item>
    <item name="actionMenuTextColor">@android:color/white</item>
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
    <item name="colorControlNormal">@android:color/white</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>
</style>

It worked ! But it also added to the handles of the EditText an underline bar...

Underlined handlers

Any ideas ?


Answer

By discussing with @rafsanahmad007, we finally got to a final solution.

style.xml

<resources>

    <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDisablePreview">true</item>
        <item name="android:windowBackground">@color/activityBackground</item>
        <item name="windowActionBar">false</item>
        <item name="actionMenuTextColor">@android:color/white</item>
        <item name="android:windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="MyTheme.EditText" parent="MyTheme">
        <item name="colorControlNormal">@android:color/white</item>
        <item name="colorControlActivated">@color/colorAccent</item>
        <item name="colorControlHighlight">@color/colorAccent</item>
    </style>

</resources>

Apply MyTheme to application, as usual. When you want to tweak an EditText just add to it :

EditText

<EditText
     ...
     android:theme="@style/MyTheme.EditText"
     ...
/>
Community
  • 1
  • 1
  • 1
    You can check this answer http://stackoverflow.com/questions/37824790/how-to-remove-underline-below-edittext-indicator – Vishesh Chandra Apr 18 '17 at 08:10
  • @visheshchandra Thanks for this link, those handlers/indicators/cursors have so many names I didn't come across this one! Unfortunately, changing the color filter of the EditText brings some problems, like disable the color change from white to colorAccent when it's selected and such. – Quentin Beuvelet Apr 18 '17 at 09:05
  • General tip: unless you want to subclass `EditText`, you don't need to use `AppCompatEditText`. – Sufian Apr 18 '17 at 09:30
  • @Sufian True, I was using some `app:` attributes before, so I needed `AppCompatEditText`, but now I may switch back to `EditText`. – Quentin Beuvelet Apr 18 '17 at 09:47
  • It could be because you're using `android:theme`. Does the handles' underline go away if you set `style` instead of `android:theme`? – Sufian Apr 18 '17 at 10:08
  • @Sufian They does go away, but the EditText's underline is not white anymore. Same problem with Rafsanahmad007's answer below. – Quentin Beuvelet Apr 18 '17 at 10:26
  • @QuentinBeuvelet does [this answer](http://stackoverflow.com/a/40889531/1276636) help? – Sufian Apr 18 '17 at 11:04
  • @Sufian Nope, sorry – Quentin Beuvelet Apr 18 '17 at 11:56

2 Answers2

2

Problem is with your style themes.. parent

use below code:

<android.support.v7.widget.AppCompatEditText
    android:theme="@style/MyStyle.EditText"/>

Now in your styles.xml

 <style name="MyStyle.EditText">
    <item name="editTextStyle">@style/MyEditTextStyle</item>
</style>

<style name="MyEditTextStyle" parent="Widget.AppCompat.EditText">
    <item name="colorControlNormal">@android:color/white</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>
</style>

try not to use the theme parent directly

EDIT

add the color property in base theme also

 <style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">@android:color/white</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>
</style>

make sure you select the theme in your activity in manifest...

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
-1

Use this doc.I think this URL will helpfull for your problem.

http://www.materialdoc.com/edit-text/

Shanmugavel GK
  • 360
  • 2
  • 11