bfahm
bfahm

Reputation: 318

Could not use MaterialButtonToggleGroup though I can use MaterialButton?

I want to achieve this look in my app: look

My xml code in the activity looks something like that:

<com.google.android.material.button.MaterialButtonToggleGroup
  android:id="@+id/toggleGroup"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button1"
    ... />

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button2"
    ... />

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button3"
    ... />
</com.google.android.material.button.MaterialButtonToggleGroup>

I migrated to androidx, and used material components in my styles, but for some reason I get a runtime error that says:

Binary XML file: Error inflating class com.google.android.material.button.MaterialButtonToggleGroup

implementation "com.google.android.material:material:1.0.0"

Though, When using com.google.android.material.button.MaterialButton without a group it would work perfectly fine, meaning that I'm using the right styling and gradle implementation.

I tries also the following:

Here are my dependencies (In case some of them are causing some sort of confliction):

implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'androidx.cardview:cardview:1.0.0'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    //Room Persistance Library
    def room_version = "2.1.0"
    implementation "android.arch.persistence.room:runtime:$room_version"
    annotationProcessor "android.arch.persistence.room:compiler:$room_version"
    //Lifecycle Libraries from the Android Architecture Components
    def lifecyle_version = " 2.1.0"
    implementation "android.arch.lifecycle:extensions:$lifecyle_version"
    annotationProcessor "android.arch.lifecycle:compiler:$room_version"

    //External Libraries:
    implementation 'com.github.HITGIF:TextFieldBoxes:1.4.4'
    implementation "com.google.android.material:material:1.0.0"

Here is the style I'm using for that activity:

<style name="ThemeNewRoutine" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

Here is the activity element in the manifest file:

<activity
            android:name=".Views.NewRoutine.NewRoutineActivity"
            android:theme="@style/ThemeNewRoutine">

        </activity>

How come com.google.android.material.button.MaterialButton is working perfectly fine BUT com.google.android.material.button.MaterialButtonToggleGroup isn't.

Also, I found out that the following attributes also cannot be resolved when I use them in the style.xml file (even though the parent is Theme.MaterialComponents.Light):

I don't know if that would cause any confliction but my activity extends AppCompatActivity:

public class NewRoutineActivity extends AppCompatActivity

Upvotes: 5

Views: 4947

Answers (1)

Benjamin Menrad
Benjamin Menrad

Reputation: 988

I got the same problem in my app and could eventually fix it by just updating the material library to the latest version '1.2.0-alpha02'.

Unfortunately I've no idea why it didn't work with v1.0.0, as there are examples which do work with this version.

implementation 'com.google.android.material:material:1.2.0-alpha02'

Upvotes: 6

Related Questions