Quentin Beuvelet
Quentin Beuvelet

Reputation: 241

Handles of EditText are underlined

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"
     ...
/>

Upvotes: 2

Views: 763

Answers (2)

rafsanahmad007
rafsanahmad007

Reputation: 23881

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...

Upvotes: 3

Shanmugavel GK
Shanmugavel GK

Reputation: 368

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

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

Upvotes: -1

Related Questions