How to change android button text color globally in theme
M

7

37

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?

Mirk answered 28/3, 2016 at 11:29 Comment(1)
Check thisEpidiascope
E
53
 <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.

Etherege answered 28/3, 2016 at 11:32 Comment(4)
Will this only be for buttons? or editText and textViewMirk
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.Etherege
Works, but use @style/Widget.AppCompat.Button as the parent style for ButtonColor to get Material Design AppCompat style.Idette
in this answer <item name="android:textColor">#yourcolor</item> seam to not only apply to button but also to everythingKickapoo
F
17

With the new Theme.MaterialComponents theme you can define the materialButtonStyle attribute in your app theme, to customize globally the style of all buttons in your app.

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
       <!-- .... --> 
       <item name="materialButtonStyle">@style/Widget.App.Button</item>
  </style>

With

<style name="Widget.App.Button" parent="Widget.Material3.Button">
    <!-- button style customization example -->
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
    <item name="android:textAppearance">@style/TextAppearance.App.Button</item>
    <item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item>
</style>

Use Widget.MaterialComponents.Button as parent if you are using Material2 theme.

If you would like to override only some attributes like colors from the default style use the materialThemeOverlay attribute.

Something like:

  <style name="Widget.App.Button" parent="Widget.Material3.Button">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
  </style>

  <style name="ThemeOverlay.App.Button">
    <!-- 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>
Fenn answered 19/8, 2019 at 22:0 Comment(1)
works like magic.Aquinas
D
11

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>
Danika answered 28/3, 2016 at 12:12 Comment(0)
O
11

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>
Obelize answered 22/2, 2017 at 15:36 Comment(0)
F
1

Since MaterialComponents Buttons use colorPrimary by default you can simply set colorOnPrimary in your theme when using Theme.MaterialComponents.Light, don't know if it's the same with Material3.

<resources>
    <!-- Base application theme. -->
    <style name="Base.Theme.MyApplication" parent="Theme.MaterialComponents.Light">
        <!-- Customize your light theme here. -->
        ...
        <item name="colorOnPrimary">@color/colorOnPrimary</item>
    </style>

    <style name="Theme.MyApplication" parent="Base.Theme.MyApplication" />
</resources>
Flightless answered 27/6, 2022 at 1:6 Comment(0)
B
-1

Just using AppCompatButton by setting this attribute :

"app:backgroundTint="@color/wildberries"

And make sure your activity extends AppCompatActivity. I just use it in my project. It works like a charm in both 5.X and pre-5.X.

Belldas answered 12/6, 2017 at 6:47 Comment(1)
This doesn't change the colour of the buttons' text.Handcart
T
-3
<Button  
     app:backgroundTint="#fece2f" //any color
 />

This is best way if you don't want it to be defined globally

Thermion answered 19/5, 2020 at 17:32 Comment(1)
This doesn't change the colour of the buttons' text.Handcart

© 2022 - 2024 — McMap. All rights reserved.