14

I'm trying to change the style of the EditText? Is it possible to achieve that? If so I'll appreciate being told about it, otherwise what alternative ways are available.

Community
  • 1
  • 1

3 Answers3

29

You can use the attribute style="@style/your_style" that is defined for any widget.

To define your style you have to create a file called style.xml in the values folder (i.e. \res\values\styles.xml) and use the following syntax:

<style name="You.EditText.Style" parent="@android:style/Widget.EditText">
    <item name="android:textColor">@color/your_color</item>
    <item name="android:gravity">center</item>
</style>

The attribute parent="@android:style/Widget.EditText" is important because it will ensure that the style being defined extends the basic Android EditText style, thus only properties different from the default style need to be defined.

CJBS
  • 15,147
  • 6
  • 86
  • 135
Mario Lenci
  • 10,422
  • 5
  • 39
  • 50
  • 2
    Perfect! `parent="@android:style/Widget.EditText"` is what I was missing - without that, my EditTexts became read-only. – CJBS Aug 12 '14 at 23:48
4
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:colorBackground">@color/windowBackground</item>
    <item name="android:textColor">@color/textColor</item>
    <item name="colorAccent">@color/colorAccent</item>
    //apply Button style
    <item name="android:editTextStyle">@style/editTextStyle</item>
</style>

//Button Style
<style name="editTextStyle" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/colorWhite</item>
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:textSize">24sp</item>
    <item name="android:paddingTop">10dp</item>
    <item name="android:paddingBottom">10dp</item>
</style>
Nilesh Savaliya
  • 666
  • 9
  • 9
0

Yes, it is definitely possible. See http://developer.android.com/guide/topics/ui/themes.html (Raghu already postet that link) for more details.

You can also but a background in it, see this post: http://www.anddev.org/tutorial_buttons_with_niceley_stretched_background-t4369.html It only covers buttons, but it is exactly the same for edittexts.

Force
  • 6,312
  • 7
  • 54
  • 85