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

4

24

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.

Madlin answered 3/9, 2015 at 13:36 Comment(3)
that's weird... the same setup (although EditTextDefault should probably inherit from @android:style/Widget.EditText instead of android:Widget.EditText) works for me without the need to specify style tag for EditTextMcadams
@blipinsk You are rightLandsman
You can also use this line in your default theme <item name="editTextColor">@color/black</item>Blandish
D
64

instead of this..

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

use this..

<item name="editTextStyle">@style/EditTextDefault</item>
Declension answered 30/11, 2015 at 23:55 Comment(2)
It's a resource style, which support the api you're running, you can use both.Declension
Thanks a lot! Can I use that same logic to others componentes?Dyslogia
L
5

What Should You Do

Use this @android:style/Widget.EditText instead parent="android:Widget.EditText" .I hope it will helps you .

Please read This SO Answer Set a consistent style to all EditText

<style name="EditTextDefault" parent="@android:style/Widget.EditText">
Landsman answered 3/9, 2015 at 14:5 Comment(0)
E
4

if your are using appcompat >v22 you can try using

<style name="EditText.Colored" parent="Theme.AppCompat">
    <item name="colorControlNormal">@color/colorNormal</item>
    <item name="colorControlActivated">@color/colorActivated</item>
    <item name="colorControlHighlight">@color/colorControlHighlight</item>
</style>

and in your layout.xml set the android:theme propertie to the edittext

<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:theme="@style/EditText.Colored"
    android:hint="@string/hint_enter_login"
    android:inputType="textEmailAddress" />
Einhorn answered 3/9, 2015 at 14:13 Comment(1)
Do you know if there is a way to set EditText theme in a app theme?Galaxy
A
1

In addition to fixing the reference to the Widget.EditText:

<style name="EditTextDefault" parent="@android:style/Widget.EditText">

You need to create a "theme" and apply it in your manifest. Like this:

<application
    ...
    android:theme="@android:style/DefaultAppTheme" >
</application>
Ania answered 3/9, 2015 at 14:9 Comment(1)
Can you develop the answer so it reffers to create a generic theme only for EditText? you are overriding the general app theme with the EditText, and it does not make any sense.Doradorado

© 2022 - 2024 — McMap. All rights reserved.