3

This doesn't work, because there is no buttonStyle item name. It has only button.

My example:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme" parent="@android:style/Theme.Black">
        <item name="android:button">@style/ThemeButton</item>
    </style>

    <style name="ThemeButton" parent="@android:style/Widget.Button">
        <item name="android:background">@drawable/button</item>
    </style>

</resources>

But this doesn't work either.

5
  • Yes, there is buttonStyle item. Are you sure that you are using Theme.Black in your application?
    – Michał Z.
    Feb 3, 2013 at 18:26
  • Yes. But there no buttonStyle, see my screen: s43.radikal.ru/i100/1302/f1/012f48561ebd.png
    – Maximus
    Feb 3, 2013 at 18:31
  • 5
    It doesn't matter that eclipse is not showing it. It is there, try to type it and run your application.
    – Michał Z.
    Feb 3, 2013 at 18:32
  • Ok, when I use it force it works. Thanks...
    – Maximus
    Feb 3, 2013 at 18:33
  • It's not forcing it. Eclipse isn't perfect sometimes :)
    – Michał Z.
    Feb 3, 2013 at 18:34

1 Answer 1

6

I override the button style for every theme when using different themes for different API levels. Consider the following example.

Your application's theme for API 14+ is a theme which derives from android:Theme.Holo.Light.DarkActionBar.

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

android:Theme.Holo.Light.DarkActionBar doesn't have its own buttonStyle, but its parent Theme.Holo.Light does. And the button style is the following:

<item name="buttonStyle">@style/Widget.Holo.Light.Button</item>

But your application's theme for API 21 derives from android:Theme.Material.Light.DarkActionBar

<style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar">
    <!-- API 21 theme customizations can go here. -->
</style>

The button style of android:Theme.Material.Light is the following:

<item name="buttonStyle">@style/Widget.Material.Light.Button</item>

Widget.Button is a grandfather of Widget.Holo.Light.Button and Widget.Material.Light.Button I mentioned above. So if you derive from Widget.Button in your application, you will lose the customizations Google engineers provided for Widget.Material.Light.Button and Widget.Holo.Light.Button.

I recorded a few examples to demonstrate this.

Android button style. Widget.Button for all API levels.

The old-fashioned button for all API levels http://youtu.be/hKWUFgvw-Gs

styles.xml in the folder values looks like this:

<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:buttonStyle">@style/Button</item>
</style>

<style name="Button" parent="@android:style/Widget.Button"></style>

Every button style derives from the button style of its theme.

The button has the Material animation http://youtu.be/8Wp1TWjjha0

styles.xml in the folder values-v21 looks like this:

<resources>

    <style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>

    <style name="BaseButton" parent="@android:style/Widget.Material.Light.Button"></style>

</resources>

styles.xml in the folder values-v14 looks like this:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    </style>

    <style name="BaseButton" parent="@android:style/Widget.Holo.Light.Button"></style>

</resources>

styles.xml in the folder values looks like this:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="android:Theme.Light"></style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:buttonStyle">@style/Button</item>
    </style>

    <style name="Button" parent="@style/BaseButton">
        <item name="android:text">Lorem Ipsum</item>
    </style>

</resources>

Material Design for Android 5.0 (API level 21)

How to record your screen on Android 4.4.4

Video bitrates

2
  • why does this stop working with API level 16? I can't set a style for all buttons
    – Michael
    Aug 14, 2015 at 1:48
  • @Michail, can you please post your code? On Github, for example Aug 14, 2015 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.