# MaterialButtonGroup

[View Source](https://github.com/material-components/material-components-android/blob/1.14.0/lib/java/com/google/android/material/button/MaterialButtonGroup.java)

*** ** * ** ***


```
public class MaterialButtonGroup
```

<br />

Known direct subclasses [MaterialButtonToggleGroup](https://developer.android.com/reference/com/google/android/material/button/MaterialButtonToggleGroup), [MaterialSplitButton](https://developer.android.com/reference/com/google/android/material/button/MaterialSplitButton)

|---|---|
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonToggleGroup` | A common container for a set of related, toggleable `https://developer.android.com/reference/com/google/android/material/button/MaterialButton`s. |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialSplitButton` | A container for two `https://developer.android.com/reference/com/google/android/material/button/MaterialButton`s that together create a split button. |

*** ** * ** ***

A common container for a set of related `https://developer.android.com/reference/com/google/android/material/button/MaterialButton`s. The `https://developer.android.com/reference/com/google/android/material/button/MaterialButton`s in this group will be shown on a single line.

This layout currently only supports child views of type `https://developer.android.com/reference/com/google/android/material/button/MaterialButton`. Buttons can be added to this view group via XML, as follows:

```
<com.google.android.material.button.MaterialButtonGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toggle_button_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_private"/>
    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_team"/>
    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_everyone"/>
    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_custom"/>

</com.google.android.material.button.MaterialButtonGroup>
```

Buttons can also be added to this view group programmatically via the addView methods.

MaterialButtonGroup is a LinearLayout. Using `
android:layout_width="MATCH_PARENT"` and removing `android:insetBottom``
android:insetTop` on the children is recommended if using `VERTICAL`.

For more information, see the [component developer guidance](https://github.com/material-components/material-components-android/blob/master/docs/components/Button.md) and [design guidelines](https://material.io/components/buttons/overview).

## Summary

| ### Nested types |
|---|
| `public class https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup.LayoutParams` A LinearLayout.LayoutParams implementation for `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup`. |

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/java/lang/Object` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#OVERFLOW_BUTTON_TAG()` |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#OVERFLOW_MODE_MENU() = 1` A value for `overflowMode`. |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#OVERFLOW_MODE_NONE() = 0` A value for `overflowMode`. |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#OVERFLOW_MODE_WRAP() = 2` A value for `overflowMode`. |

| ### Public fields |
|---|---|
| `int` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#overflowMode()` |
| `int` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#spacing()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#MaterialButtonGroup(Context)(Context context)` |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#MaterialButtonGroup(Context,AttributeSet)(Context context, AttributeSet attrs)` |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#MaterialButtonGroup(Context,AttributeSet,int)( Context context, AttributeSet attrs, int defStyleAttr )` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#addView(View,int,ViewGroup.LayoutParams)(View child, int index, LayoutParams params)` This override prohibits Views other than `https://developer.android.com/reference/com/google/android/material/button/MaterialButton` to be added. |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup.LayoutParams` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#generateLayoutParams(AttributeSet)(AttributeSet attrs)` |
| `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getChildOriginalShapeAppearanceModel(int)(int index)` Returns the original `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` for the `https://developer.android.com/reference/com/google/android/material/button/MaterialButton` child at the given index. |
| `https://developer.android.com/reference/com/google/android/material/shape/CornerSize` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getInnerCornerSize()()` Returns the inner corner size of the group. |
| `Drawable` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getOverflowButtonIcon()()` Returns the icon shown for the overflow button, if present. |
| `int` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getOverflowMode()()` Returns the overflow mode. |
| `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getShapeAppearance()()` Returns the `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` of the group. |
| `int` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getSpacing()()` Returns the spacing (in pixels) between each button in the group. |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#onViewRemoved(View)(View child)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setEnabled(boolean)(boolean enabled)` Enables this `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup` and all its `https://developer.android.com/reference/com/google/android/material/button/MaterialButton` children |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setInnerCornerSize(com.google.android.material.shape.CornerSize)(https://developer.android.com/reference/com/google/android/material/shape/CornerSize cornerSize)` Sets the inner corner size of the group. |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setOrientation(int)(int orientation)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setOverflowButtonIcon(Drawable)(Drawable icon)` Sets the icon to show for the overflow button. |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setOverflowButtonIconResource(int)(int iconResourceId)` Sets the icon to show for the overflow button. |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setOverflowMode(int)(int overflowMode)` Sets the overflow mode. |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setShapeAppearance(com.google.android.material.shape.ShapeAppearanceModel)(https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel shapeAppearance)` Sets the `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` of the group. |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setSpacing(int)(int spacing)` Sets the spacing between each button in the group. |

| ### Protected methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#checkLayoutParams(ViewGroup.LayoutParams)(LayoutParams p)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#dispatchDraw(Canvas)(Canvas canvas)` |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup.LayoutParams` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#generateDefaultLayoutParams()()` |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup.LayoutParams` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#generateLayoutParams(ViewGroup.LayoutParams)(LayoutParams p)` |
| `int` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getChildDrawingOrder(int,int)(int childCount, int i)` We keep track of which views are pressed and checked to draw them last. |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#onLayout(boolean,int,int,int,int)(boolean changed, int l, int t, int r, int b)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#onMeasure(int,int)(int widthMeasureSpec, int heightMeasureSpec)` |

## Constants

### OVERFLOW_BUTTON_TAG

```
public static final Object OVERFLOW_BUTTON_TAG
```

### OVERFLOW_MODE_MENU

```
public static final int OVERFLOW_MODE_MENU = 1
```

A value for `overflowMode`. It indicates that the buttons that don't fit to the group size will be hidden in the group and contained in a popup menu.

### OVERFLOW_MODE_NONE

```
public static final int OVERFLOW_MODE_NONE = 0
```

A value for `overflowMode`. It indicates that there's no handling to the buttons that don't fit to the group size.

### OVERFLOW_MODE_WRAP

```
public static final int OVERFLOW_MODE_WRAP = 2
```

A value for `overflowMode`. It indicates that the buttons that don't fit to the group size will be displayed in another row under it.

## Public fields

### overflowMode

```
public int overflowMode
```

### spacing

```
public int spacing
```

## Public constructors

### MaterialButtonGroup

```
public MaterialButtonGroup(Context context)
```

### MaterialButtonGroup

```
public MaterialButtonGroup(Context context, AttributeSet attrs)
```

### MaterialButtonGroup

```
public MaterialButtonGroup(
    Context context,
    AttributeSet attrs,
    int defStyleAttr
)
```

## Public methods

### addView

```
public void addView(View child, int index, LayoutParams params)
```

This override prohibits Views other than `https://developer.android.com/reference/com/google/android/material/button/MaterialButton` to be added. It also makes updates to the add button shape and margins.

### generateLayoutParams

```
public MaterialButtonGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
```

### getChildOriginalShapeAppearanceModel

```
public ShapeAppearanceModel getChildOriginalShapeAppearanceModel(int index)
```

Returns the original `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` for the `https://developer.android.com/reference/com/google/android/material/button/MaterialButton` child at the given index.

### getInnerCornerSize

```
public CornerSize getInnerCornerSize()
```

Returns the inner corner size of the group.

### getOverflowButtonIcon

```
public Drawable getOverflowButtonIcon()
```

Returns the icon shown for the overflow button, if present.

ref com.google.android.material.R.styleable#MaterialButtonGroup_overflowButtonIcon

| Returns |
|---|---|
| `Drawable` | the overflow button icon, if present. |

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setOverflowButtonIcon(Drawable)` |   |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setOverflowButtonIconResource(int)` |   |

### getOverflowMode

```
public int getOverflowMode()
```

Returns the overflow mode.

### getShapeAppearance

```
public ShapeAppearanceModel getShapeAppearance()
```

Returns the `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` of the group.

### getSpacing

```
public int getSpacing()
```

Returns the spacing (in pixels) between each button in the group.

### onViewRemoved

```
public void onViewRemoved(View child)
```

### setEnabled

```
public void setEnabled(boolean enabled)
```

Enables this `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup` and all its `https://developer.android.com/reference/com/google/android/material/button/MaterialButton` children

| Parameters |
|---|---|
| `boolean enabled` | boolean to setEnable `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup` |

### setInnerCornerSize

```
public void setInnerCornerSize(CornerSize cornerSize)
```

Sets the inner corner size of the group.

Can set as an `https://developer.android.com/reference/com/google/android/material/shape/AbsoluteCornerSize` or RelativeCornerSize. Don't set relative corner size larger than 50% or absolute corner size larger than half height to avoid corner overlapping.

| Parameters |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/shape/CornerSize cornerSize` | the inner corner size of the group |

### setOrientation

```
public void setOrientation(int orientation)
```

### setOverflowButtonIcon

```
public void setOverflowButtonIcon(Drawable icon)
```

Sets the icon to show for the overflow button.

ref com.google.android.material.R.styleable#MaterialButtonGroup_overflowButtonIcon

| Parameters |
|---|---|
| `Drawable icon` | Drawable to use for the overflow button's icon. |

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setOverflowButtonIconResource(int)` |   |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getOverflowButtonIcon()` |   |

### setOverflowButtonIconResource

```
public void setOverflowButtonIconResource(int iconResourceId)
```

Sets the icon to show for the overflow button.

ref com.google.android.material.R.styleable#MaterialButtonGroup_overflowButtonIcon

| Parameters |
|---|---|
| `int iconResourceId` | drawable resource ID to use for the overflow button's icon. |

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#setOverflowButtonIcon(Drawable)` |   |
| `https://developer.android.com/reference/com/google/android/material/button/MaterialButtonGroup#getOverflowButtonIcon()` |   |

### setOverflowMode

```
public void setOverflowMode(int overflowMode)
```

Sets the overflow mode.

### setShapeAppearance

```
public void setShapeAppearance(ShapeAppearanceModel shapeAppearance)
```

Sets the `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` of the group.

| Parameters |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel shapeAppearance` | The new `https://developer.android.com/reference/com/google/android/material/shape/ShapeAppearanceModel` of the group. |

### setSpacing

```
public void setSpacing(int spacing)
```

Sets the spacing between each button in the group.

| Parameters |
|---|---|
| `int spacing` | the spacing (in pixels) between each button in the group |

## Protected methods

### checkLayoutParams

```
protected boolean checkLayoutParams(LayoutParams p)
```

### dispatchDraw

```
protected void dispatchDraw(Canvas canvas)
```

### generateDefaultLayoutParams

```
protected MaterialButtonGroup.LayoutParams generateDefaultLayoutParams()
```

### generateLayoutParams

```
protected MaterialButtonGroup.LayoutParams generateLayoutParams(LayoutParams p)
```

### getChildDrawingOrder

```
protected int getChildDrawingOrder(int childCount, int i)
```

We keep track of which views are pressed and checked to draw them last. This prevents visual issues with overlapping strokes.

### onLayout

```
protected void onLayout(boolean changed, int l, int t, int r, int b)
```

### onMeasure

```
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
```