Saeed Hashemi
Saeed Hashemi

Reputation: 1024

Material toggle button problem on Android API 21

I use material toggle button group like this

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:singleSelection="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_topso"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_height="wrap_content"

  android:layout_width="wrap_content"          android:text="@string/topo" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_averages"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/average" />

    </com.google.android.material.button.MaterialButtonToggleGroup>

But when run the application, crash with this error:

 java.lang.IllegalStateException: Attempted to get ShapeAppearanceModel from a MaterialButton which has an overwritten background.
        at com.google.android.material.button.MaterialButton.getShapeAppearanceModel(MaterialButton.java:1169)
        at com.google.android.material.button.MaterialButtonToggleGroup.addView(MaterialButtonToggleGroup.java:244)
        at android.view.ViewGroup.addView(ViewGroup.java:3709)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)

I can't fix it!, how can I solve this issue?

Material version: '1.3.0-alpha03'

Style:

    <style name="MaterialAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:buttonStyle">@style/ButtonStyle</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="floatingActionButtonStyle">@style/FloatingButtonStyle</item>
</style>

Upvotes: 2

Views: 820

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 365028

You are using:

<com.google.android.material.button.MaterialButton
   android:background="@drawable/about"

Using the android:background in MaterialButton you are overriding the default background. It means the MaterialButton doesn't use its own MaterialShapeDrawable for the background (and the ShapeAppearanceModel).
It seems a bug but you can solve it removing the android:background in your MaterialButton.

Upvotes: 2

Related Questions