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.

Code Poet
  • 11,227
  • 19
  • 64
  • 97

5 Answers5

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>
user
  • 86,916
  • 18
  • 197
  • 190
  • 4
    This sounds so plausible and logical, yet it doesn't seem to work for me. Could I be missing something? – Code Poet Jun 06 '12 at 06: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 06 '12 at 07: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 07 '12 at 14:46
  • This gives me a: No resource found that matches the given name: attr 'android:editTextSyle' – AgentKnopf Mar 15 '13 at 06:42
  • @Zainodis I don't see how that would happen, that style is available(on which android version are you trying to use it?). Try to refresh or clean your project and see if you don't have other xml errors. – user Mar 15 '13 at 08:25
  • @Luksprog After lots of refreshing and cleaning the error disappeared, but re-appeared occasionally. My Min SDK Version is 10, Target SDK Version was 14 at the time (11 now). My main issue is, that the app looks different on SDK 10 devices than it looks on SDK 11/14 devices. And it also depends on which device it is. The Note has a dark background (while the Xperia S has a white background) in it's EditTexts, so dark text color wouldn't work, unless we also change the background color of the AutoCompleteTextView... It's a joy. We've now decided to control the complete look from top to bottom. – AgentKnopf Mar 15 '13 at 08:59
  • @Zainodis Welcome to the wonderful world of Android various looks across devices and API versions. If you want a unified look and feel for all platforms and devices then go with making your own complete theme(instead of relying on the sdk provided look(which manufacturers modify as they please)). – user Mar 15 '13 at 09:04
  • Hey @Luksprog, is the `android:editTextStyle` supposed to apply to `AutoCompleteTextViews` as well as `EditTextViews`? Because I tried and its not working for styling my `AutoCompleteTextView`. How would you style an `AutoCompleteTextView`? And I'm not talking about the drop down, I just want to apply a colored Holo underline to the `AutoCompleteTextView`. – Etienne Lawlor Jun 22 '13 at 19:14
  • 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 '13 at 04:56
  • thanks @Luksprog ya i just noticed the `autoCompleteTextViewStyle` attribute in `themes.xml`. – Etienne Lawlor Jun 23 '13 at 16:52
  • Okay I did exactly the same thing but nothing happened. I was using Theme.Light instead of Theme.Holo therefore I changed that as well Here is my code:- – Haseeb Jadoon Oct 30 '14 at 18:44
  • @Jadoon please post a new question mentioning all the details. Also make sure you actually apply that them to your application or activities. – user Oct 30 '14 at 19:05
  • I like this solution, but unfortunately it does not support every style attribute.. specifically of interest - margins are not available with this approach apparently - described here: http://stackoverflow.com/a/13365288/2162226 . "Short Answer: If you are specifying layout_margin in a custom style, this style must be explicitly applied to each individual view that you wish to have the specified margin" – Gene Bo Apr 14 '15 at 21:01
  • 5
    Doesn't work for me too. But if i change `` to ``, it's ok. Thanks! – Sam Sch Apr 12 '18 at 08:58
  • When using `AppCompat` remove the `android:` prefix. Apply to not only this but many other attributes. – Meow Cat 2012 Mar 08 '19 at 07:55
  • In 2019, it works smoothly at Android Studio 3.4.1. However I had to add `android:` prefix, So we have: `@style/App_EditTextStyle` . It's the only way to disable select handles in `EditText` controls, because *Android* just add the routines in API v.29 – Paulo Buchsbaum Aug 05 '19 at 18:16
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>
iflp
  • 1,742
  • 17
  • 25
  • 1
    Works for me too. But why it behaves like this ? – Eagle_Eye Mar 09 '16 at 17:50
  • 3
    Might have something to do with the `AppCompat` library. Does your activity extend `AppCompatActivity`? Oh the mysteries of Android development. – Matt Zukowski May 03 '16 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 06 '16 at 07:15
  • Thankkkss for this. – Adam Varhegyi Feb 20 '17 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 '17 at 09:16
  • Same story for me. I'm using both `AppCompatActivity` and the `AppCompatEditText`. Had to get rid of `android:` namespace... – Vucko Aug 20 '19 at 10:22
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>
Reynaldo Aguilar
  • 1,906
  • 1
  • 15
  • 29
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
<style name="App_Theme" parent="@android:style/Theme.Holo">    
    <item name="android:editTextBackground">@drawable/filled_roundededges_box_dark</item>
</style>
user1700099
  • 466
  • 5
  • 6