88

I'm trying to make all EditText's in my application have a consistent look. I'm aware that I can do something like this:

<style name="App_EditTextStyle">
    <item name="android:background">@drawable/filled_roundededges_box_dark</item>
    <item name="android:textColor">#808080</item>
    <item name="android:layout_height">45dip</item>
</style>

Then I can make a particular EditText have this style by doing this:

<EditText ...
    style="@style/App_EditTextStyle
    ...>

But this way I have to remember to set the style individually for each and every EditText in my application which is tedious, if not error prone.

Is there some way I could make this a part of a theme or something? This is so I don't have to associate this style to every EditText. Something like this fictitious code block:

<style name="App_Theme" parent="@android:style/Theme.Holo">
   ... 
   <item name="android:EditTextSyle">@style/App_EditTextStyle</item>
   ...
<style>

And then in my AndroidManifest.xml I have something like:

<application
    ....
    android:theme="@style/App_Theme">

And Voila! all my EditText's have the consistent style without me having to specify the style for each instance.

5 Answers 5

167

Override the attribute pointing to the EditText style(named editTextStyle :) ) in your custom theme:

<style name="App_Theme" parent="@android:style/Theme.Holo">
   <item name="android:editTextStyle">@style/App_EditTextStyle</item>
</style>

and make your custom style to extend Widget.EditText:

<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
    <item name="android:background">@drawable/filled_roundededges_box_dark</item>
    <item name="android:textColor">#808080</item>
    <item name="android:layout_height">45dip</item>
</style>

Edit:

If you're using the much newer AppCompat related themes use the editTextStyle attribute without the android prefix:

<style name="App_Theme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="editTextStyle">@style/App_EditTextStyle</item>
</style>
16
  • 4
    This sounds so plausible and logical, yet it doesn't seem to work for me. Could I be missing something?
    – Code Poet
    Jun 6, 2012 at 6:43
  • 1
    @ManishGupta How exactly doesn't work? I've tested the styles above and they work, BUT the layout_height/height can't be put in there because android probably checks for those attributes before the theme's styles and it will complain about them missing. But a custom drawable + different text color work with the above.
    – user
    Jun 6, 2012 at 7:50
  • I have things set up exactly as you describe. There is no point in me reposting. I'm giving this to you anyway. I am missing something somewhere. What you prescribe is cold logic and should work. I'll revisit this after cooling off. Thanks.
    – Code Poet
    Jun 7, 2012 at 14:46
  • 3
    @toobsco42 edittextStyle doesn't apply to an AutoCompleTextView(although it's an EditText). Have a look at the autoCompleteTextViewStyle attribute in the theme.xml which points to a Widget.AutoCompleTextView style in the style.xml file.
    – user
    Jun 23, 2013 at 4:56
  • 5
    Doesn't work for me too. But if i change <item name="android:editTextStyle"> to <item name="editTextStyle">, it's ok. Thanks!
    – Sam Sch
    Apr 12, 2018 at 8:58
56

@Luksprog answer is correct but not working for me. After some experimentation, I found out that removing the android namespace from editTextStyle made it work for me.

<style name="App_Theme" parent="@android:style/Theme.Holo">
   <item name="editTextStyle">@style/App_EditTextStyle</item>
</style>

and make your custom style to extend Widget.EditText or if using the AppCompat theme Widget.AppCompat.EditText:

<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
    <item name="android:background">@drawable/filled_roundededges_box_dark</item>
    <item name="android:textColor">#808080</item>
    <item name="android:layout_height">45dip</item>
</style>
6
  • 1
    Works for me too. But why it behaves like this ?
    – Eagle_Eye
    Mar 9, 2016 at 17:50
  • 3
    Might have something to do with the AppCompat library. Does your activity extend AppCompatActivity? Oh the mysteries of Android development. May 3, 2016 at 20:45
  • 1
    @MattZukowski did some digging based on your comment - probable culprit: From AppCompat v7:21+ onwards the standard theme attributes needs to be used without the "android:" prefix in the styles.
    – iflp
    Oct 6, 2016 at 7:15
  • Thankkkss for this. Feb 20, 2017 at 10:41
  • Works fine even without using Widget.AppCompat.EditText .. But I can not access Widget.AppCompat.EditText. Says "can not resolve symbol '@android:style/Widget.AppCompat.EditText'"
    – Neri
    Aug 23, 2017 at 9:16
18

First, define the style for your EditText. Make sure that the parent style is android:Widget.EditText

<style name="CustomEditTextStyle" parent="android:Widget.EditText">

    <item name="android:textColor">#0F0F0F</item>
    <!-- ... More items here if needed ... -->
</style>

After that, override the attribute android:editTextStyle in your custom theme. Be aware, if you are using the support library, you will also need to override the attribute editTextStyle (without the android namespace).

<style name="App_Theme" parent="...">
   <item name="android:editTextStyle">@style/CustomEditTextStyle</item>
   <item name="editTextStyle">@style/CustomEditTextStyle</item> <!-- For compatibility with the support library -->
</style>
0
1

If you just need to set a few simple parameters, like text color, the Android namespace has a few parameters that will do that without the need to declare a separate style for the edit text. Eg

<style name="MyStyle" parent="android:Theme.Material.NoActionBar">
    <item name="colorPrimary">@color/black</item>
    <item name="colorPrimaryDark">@color/white</item>
    <item name="colorAccent">@color/white</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:editTextColor">@color/black</item>
    <item name="android:editTextBackground">@color/black</item>
    ....
</style>
1
  • For newer version of AppCompat use editTextColor and remove android:.
    – Armin
    Aug 13, 2019 at 11:37
-1
<style name="App_Theme" parent="@android:style/Theme.Holo">    
    <item name="android:editTextBackground">@drawable/filled_roundededges_box_dark</item>
</style>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.