How to change android button text color globally in theme

35,034

Solution 1

 <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="android:textColor">#yourcolor</item>
    <item name="android:buttonStyle">@style/ButtonColor</item>
    <item name="colorButtonNormal">@color/buttonColor</item>
</style>



<style name="ButtonColor" parent="@android:style/Widget.Button">
   <item name="android:textColor">@color/yourcolor</item>
</style> 

android:textColor This should help you change the text color globally.

Solution 2

With the new Theme.MaterialComponents theme you can define the materialButtonStyle attribute in your app theme.

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ....

    <item name="materialButtonStyle">@style/MyButtonTheme</item>
  </style>

In this way you can customize globally the style of all buttons in your app. You can override the theme attributes from the default style using the materialThemeOverlay attribute.

Something like:

  <style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/ButtonStyleTextColor</item>
  </style>

  <style name="ButtonStyleTextColor">
    <!-- For filled buttons, your theme's colorPrimary provides the default background color of the component, and -->
    <!--the text color is colorOnPrimary -->
    <item name="colorPrimary">@color/my_color</item>
    <item name="colorOnPrimary">@color/my_color2</item>

  </style>

Currently the materialThemeOverlay attribute requires version 1.1.0 of material components for android library.

Solution 3

For anyone that stumbles onto this, I recommend checking out a similar question about Material Design Button Styles. With a theme, your "accent color" will be used to color your button.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="android:buttonStyle">@style/Widget.AppCompat.Button.Colored</item>
    <item name="buttonStyle">@style/Widget.AppCompat.Button.Colored</item>
</style>

Solution 4

If you just need to change the text colour of buttons. You can modify the textAppearanceButton style attribute of your main theme.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorButtonNormal">@color/buttonColor</item>
    <item name="android:textAppearanceButton">@style/TextAppearance.AppCompat.Button.Custom</item>
</style>

And declare your new textAppearance style as follows

<style name="TextAppearance.AppCompat.Button.Custom">
    <item name="android:textColor">@color/mycustomcolor</item>
</style>
Share:
35,034
Zapnologica
Author by

Zapnologica

I am an enthusiastic developer indulging in the world of programming. in love with C# .net

Updated on March 31, 2021

Comments

  • Zapnologica
    Zapnologica about 3 years

    How can I change all my buttons text color?

    I know I can set the background color like follows :

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
        <item name="colorButtonNormal">@color/buttonColor</item>
    </style>
    

    How can I do this for the button Text?

  • Zapnologica
    Zapnologica about 8 years
    Will this only be for buttons? or editText and textView
  • Sumanth Jois
    Sumanth Jois about 8 years
    i have edited the code. this code will only apply for button's. The older one , apply's for all. ThankYou. I hope this was helpful.
  • levibostian
    levibostian over 6 years
    Works, but use @style/Widget.AppCompat.Button as the parent style for ButtonColor to get Material Design AppCompat style.
  • zeus
    zeus over 5 years
    in this answer <item name="android:textColor">#yourcolor</item> seam to not only apply to button but also to everything
  • ban-geoengineering
    ban-geoengineering over 3 years
    This doesn't change the colour of the buttons' text.
  • ban-geoengineering
    ban-geoengineering over 3 years
    This doesn't change the colour of the buttons' text.
  • JustGotStared
    JustGotStared over 2 years
    works like magic.