Android set edit text style globally in theme doesn't work

exlwzicx picture exlwzicx · Sep 3, 2015 · Viewed 19.2k times · Source

I am facing with problem. I am trying to make edit texts in my app look the same with the help of theme.
I have generated styles with the help of online tools (9-patch images) and tried to set it in my theme like this

   <!-- Base application theme. -->
    <style name="DefaultAppTheme" parent="Theme.AppCompat">
        <!-- Main theme colors -->
        <!--   your app branding color for the app bar -->
        <item name="colorPrimary">@color/blue</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">@color/darkblue</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="colorAccent">@color/cornflowerblue</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:editTextStyle">@style/EditTextDefault</item>
    </style>

My style for edit text

<style name="EditTextDefault" parent="android:Widget.EditText">
        <item name="android:background">@drawable/edit_text_holo_light</item>
        <item name="android:textColor">@color/light_blue</item>
        <item name="android:layout_marginLeft">@dimen/margin_edit_text_default</item>
        <item name="android:layout_marginRight">@dimen/margin_edit_text_default</item>
        <item name="android:layout_marginTop">@dimen/margin_edit_text_default</item>
    </style>

But this doesn't work if use edit text without specifying style manually for each edit text.

This doesn't work

<EditText
    android:id="@+id/edit_text_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/edit_text_password"
    android:hint="@string/hint_enter_login"
    android:inputType="textEmailAddress" />

This works

<EditText
    android:id="@+id/edit_text_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/edit_text_password"
    style="@style/EditTextDefault"
    android:hint="@string/hint_enter_login"
    android:inputType="textEmailAddress" />

I know that layout_* params will not affect if they are used in theme, but other attributes should work.

Please help with this problem.

Answer

Jongz Puangput picture Jongz Puangput · Dec 1, 2015

instead of this..

<item name="android:editTextStyle">@style/EditTextDefault</item>

use this..

<item name="editTextStyle">@style/EditTextDefault</item>