0

I have the following button:

 <Button                                    
     android:id="@+id/addexperience_button"                                    
     style="@style/MyButton"                                    
     android:layout_width="match_parent"                                    
     android:layout_height="wrap_content"                                    
     android:layout_alignLeft="@id/emptystate_image"                                    
     android:layout_alignRight="@id/emptystate_image"                                    
     android:layout_below="@id/emptystate_image"                                    
     android:text="@string/pickdetail_addexperience" />

with this styling:

  <style name="MyButton" parent="AppTheme">
        <item name="android:textColor">@color/white</item>
        <item name="android:paddingLeft">@dimen/single_spacing</item>
        <item name="android:paddingRight">@dimen/single_spacing</item>
        <item name="android:background">@drawable/bg_primarybutton</item>
    </style>

bg_primarybutton:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="6dp"
    android:insetLeft="4dp"
    android:insetRight="4dp"
    android:insetTop="6dp">
    <ripple android:color="?android:attr/colorControlHighlight">
        <item>
            <shape
                android:shape="rectangle"
                android:tint="@color/colorPrimary">
                <corners android:radius="18dp" />
            </shape>
        </item>
    </ripple>
</inset>

Now this button looks like this with 28.0.0-alpha1:

enter image description here

But looks like this with alpha2, alpha3 and rc01:

enter image description here

How come its ignoring my styling on a later version (esp. a RC?) Maybe there is something fundamentally wrong on my Button definition?

MichelReap
  • 5,630
  • 11
  • 37
  • 99

1 Answers1

3

The reason for that is because of your AppTheme being Theme.MaterialComponents.Light.NoActionBar, the inflater of your activity is inflating a MaterialButton instead of a Button and you can't change the background of a MaterialButton the same way of an old Button (see doc) .

If you want to have the same style as before, replace your MyButton style with

<style name="MyButton">
  <item name="android:textColor">@color/white</item>
  <item name="android:paddingLeft">@dimen/single_spacing</item>
  <item name="android:paddingRight">@dimen/single_spacing</item>
  <item name="backgroundTint">@color/colorPrimary</item>
  <item name="cornerRadius">18dp</item>
  <item name="rippleColor">?android:attr/colorControlHighlight</item>
</style>

Note that you should not extend your AppTheme when writing a style (that will be used in the style="@style/MyStyle attribute) for a View.

They introduced the auto inflation of MaterialButton with the alpha2. Open the class android.support.design.theme.MaterialComponentsViewInflater on alpha1 and alpha2 to see the difference.

If you DO NOT want this behavior of auto inflating MaterialButton change your AppThemeto be

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge"> 
pdegand59
  • 12,776
  • 8
  • 51
  • 58
  • saved my life, for some reason it also works if i use AppCompatButton. – MichelReap Sep 05 '18 at 06:47
  • One more thing, can you expand on this: "Note that you should not extend your AppTheme when writing a style (that will be used in the style="@style/MyStyle attribute) for a View." Why is that? – MichelReap Sep 05 '18 at 06:48