How do I apply a style to all buttons of an Android application
Asked Answered
A

2

140

I have a style applied to my whole application:

AndroidManifest.xml:

<application android:theme="@style/ApplicationStyle" android:icon="@drawable/icon" android:label="@string/app_name">

And in my styles.xml:

 <style name="ApplicationStyle" parent="android:Theme">
  <item name="android:button">@style/CKButton</item>
 </style>
 <style name="CKButton" parent="android:style/Widget.Button">
  <item name="android:textSize">19sp</item>
  <item name="android:layout_margin">0dip</item>
  <item name="android:background">#ff0000</item>
 </style>

But the style doesn't get applied.

I'm sorry if I just used the false name in the ApplicationStyle - Item, but I have no clue where to look for the object names and simply assumed, that android:button applies to all buttons.

Aedile answered 9/3, 2010 at 16:53 Comment(1)
Please take a look at my answer on https://mcmap.net/q/168025/-how-to-apply-style-to-all-buttons-in-android-applicationBaptize
S
216

For Android styles, you reference the preset attributes that Android has laid out in R.attr. In this case, it looks like you want to to reference android:buttonStyle. I think this would work:

<style name="ApplicationStyle" parent="android:Theme">
  <item name="android:buttonStyle">@style/CKButton</item>
</style>
Sulamith answered 9/3, 2010 at 17:8 Comment(5)
God, I was looking for such functionality for ages. Got no idea why buttons' textColor does not follow main theme's textColor property. Thanks much!Corso
any one no how to do this for spinner?Tumescent
Just one note: if the CKButton style does not inherit @android:style/Widget.Button, the button will stop working.Aret
what is difference between parent="android:Widget.Button" and parent=@android:style/Widget.Button?Bibliotheca
Why layout editor do not preview changes as per the button style set in the themeSecrecy
A
26

For Material components theme it would be like this:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="materialButtonStyle">@style/AppTheme.Button</item>
</style>
Annalee answered 3/11, 2020 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.