Search code examples
androidandroid-appcompatandroid-buttonandroid-themeandroid-styles

Theming class extends View (Button) with AppCompat


I have declared Button inside my XML:

<Button
    android:id="@+id/custom_id"
    android:theme="@style/BlueButtonTheme"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/custom_string"/>

most important line is styling with BlueButtonTheme:

<style name="BlueButtonTheme" parent="AppTheme">
    <item name="android:buttonStyle">@style/BlueButtonStyle</item>
    <item name="buttonStyle">@style/BlueButtonStyle</item>

    <item name="colorAccent">@color/blue_dark</item>
    <item name="colorControlActivated">@color/blue_dark</item>
    <item name="colorControlHighlight">@color/blue_dark</item>
    <item name="colorButtonNormal">@color/blue</item>

    <item name="android:colorAccent">@color/blue_dark</item>
    <item name="android:colorControlActivated">@color/blue_dark</item>
    <item name="android:colorControlHighlight">@color/blue_dark</item>
    <item name="android:colorButtonNormal">@color/blue</item>

    <item name="android:textColor">@color/white</item>
</style>

<style name="BlueButtonStyle" parent="Widget.AppCompat.Button.Colored">
    <item name="colorAccent">@color/blue_dark</item>
    <item name="colorControlActivated">@color/blue_dark</item>
    <item name="colorControlHighlight">@color/blue_dark</item>
    <item name="colorButtonNormal">@color/blue</item>

    <item name="android:colorAccent">@color/blue_dark</item>
    <item name="android:colorControlActivated">@color/blue_dark</item>
    <item name="android:colorControlHighlight">@color/blue_dark</item>
    <item name="android:colorButtonNormal">@color/blue</item>

    <item name="android:textColor">@color/white</item>
</style>

And Button is blue in all os versions, nice... Now I want to create custom MyButton extends Button, but when I set in XML:

<com.package.name.MyButton
    android:id="@+id/custom_id"
    android:theme="@style/BlueButtonTheme"
    ...

widget will stay gray in e.g. Android 4.1 (physical test device), but have white text and a bit blue when pressed/selected. 5.0 and above works. My button don't have any additional logic (for now), only set of constructors. How to apply working theme for this custom widget extending Button?


Solution

  • few hours of tries and 10 minutes after posting above question I've found proper answer: extends AppCompatButton (from support v7), not android.widget.Button