'Create MaterialButtonToggleGroup including MaterialButton(s) with customized style programmatically in Android

I'm trying to create a MaterialButtonToggleGroup with material buttons inside it and I wanna do that with custom styles.

I was able to create that using xml, however, I need to achieve the same result programmatically

here's what I did with xml (I'm using LinearLayout as a root view):

<com.google.android.material.button.MaterialButtonToggleGroup
  android:id="@+id/group_item_size"
  style="@style/ButtonsToggleGroupStyle"
  >

  <com.google.android.material.button.MaterialButton
    style="@style/ButtonStyle"
    android:text="Small"
    />

  <com.google.android.material.button.MaterialButton
    style="@style/ButtonStyle"
    android:text="Large"
    />

  <com.google.android.material.button.MaterialButton
    style="@style/ButtonStyle"
    android:text="Family"
    />

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

and the code for the styles:

      <style name="ButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
    <item name="cornerRadius">@dimen/margin_20dp</item>
    <item name="android:padding">@dimen/margin_8dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">1</item>
    <item name="android:textSize">@dimen/text_size_14sp</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textColor">@drawable/drawable_button_toggle_group_text_selector</item>
    <item name="android:backgroundTint">@drawable/drawable_button_toggle_group_selector</item>
  </style>

  <style name="ButtonsToggleGroupStyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:orientation">horizontal</item>
    <item name="android:layout_marginStart">@dimen/margin_32dp</item>
    <item name="android:layout_marginTop">@dimen/margin_12dp</item>
    <item name="android:layout_marginEnd">@dimen/margin_32dp</item>
  </style>

thanks in advance for your help 🙏🏽



Solution 1:[1]

Just use a xml layout (single_button_layout.xml) to define the single MaterialButton with your favorite style:

<com.google.android.material.button.MaterialButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/ButtonStyle"
    .../>

Then just use something like:

MaterialButtonToggleGroup group = findViewById(R.id.toggle_button_group);

for (....){

  MaterialButton button =
      (MaterialButton)  getLayoutInflater().inflate(R.layout.single_button_layout, group, false);
  button.setText(...);
  group.addView(button);

}

Solution 2:[2]

To do it all programmatically you could code the skeleton as follows:

MaterialButtonToggleGroup charpathButtongroup = new MaterialButtonToggleGroup(this, null, R.attr.materialButtonOutlinedStyle);
MaterialButton pathButton[] = new MaterialButton[2];
pathButton[0] = new MaterialButton(this, null, R.attr.materialButtonOutlinedStyle);
pathButton[1] = new MaterialButton(this, null, R.attr.materialButtonOutlinedStyle);
FrameLayout.LayoutParams charpathgroupParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
charpathButtongroup.setLayoutParams(charpathgroupParams);
game.addView(charpathButtongroup);
charpathButtongroup.addView(pathButton[0]);
charpathButtongroup.addView(pathButton[1]);

Be sure to set appropriate params for the MaterialButtonToggleGroup before adding views to it or you will get the following exception:

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

Attributes can be added to your LayoutParams and MaterialButtons programmatically as follows:

enter image description here

I prefer doing everything programmatically for my MMORG game, no layout folder.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabriele Mariotti
Solution 2