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.

TimD1
  • 982
  • 15
  • 26
Maximus
  • 471
  • 1
  • 10
  • 25

1 Answers1

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

Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138