material-components / material-components/material-components-android

[MaterialButtonToggleGroup] Updating corner radius programatically

Open
#4,223 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
It would be nice to have an ability to update the corner radius and/or treatment of MaterialButtonToggleGroup programatically (post creation) instead of being locked in to whatever values were set in the XML. The issue is that even if you change your button's ShapeAppearance after initial inflation, the MaterialButtonToggleGroup would not take the new values into consideration because it only updates those values when Views are added, which could be seen here

```
void updateChildShapes() {
for (int i = 0; i < childCount; i++) {
MaterialButton button = getChildButton(i);
...
ShapeAppearanceModel.Builder builder = button.getShapeAppearanceModel().toBuilder();
CornerData newCornerData = getNewCornerData(i, firstVisibleChildIndex, lastVisibleChildIndex);
updateBuilderWithCornerData(builder, newCornerData);

button.setShapeAppearanceModel(builder.build());
}
}

@Nullable
private CornerData getNewCornerData(
int index, int firstVisibleChildIndex, int lastVisibleChildIndex) {
CornerData cornerData = originalCornerData.get(index);
...
}

/**
* This override prohibits Views other than {@link MaterialButton} to be added. It also makes
* updates to the add button shape and margins.
*/
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
....
ShapeAppearanceModel shapeAppearanceModel = buttonChild.getShapeAppearanceModel();
originalCornerData.add(
new CornerData(
shapeAppearanceModel.getTopLeftCornerSize(),
shapeAppearanceModel.getBottomLeftCornerSize(),
shapeAppearanceModel.getTopRightCornerSize(),
shapeAppearanceModel.getBottomRightCornerSize()));
...
}

@Override
public void onViewRemoved(View child) {
...
int indexOfChild = indexOfChild(child);
if (indexOfChild >= 0) {
originalCornerData.remove(indexOfChild);
}
...
}

```

**Describe the solution you'd like**
Ideally the solution would involve either a new function in MaterialButtonToggleGroup to invalidate cornerData (which is alright) or additional function to set ShapeAppearance to the MaterialButtonToggleGroup itself. Unfortunately it's probably not possible to do it fully automatically.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MaterialButtonToggleGroup entry point and read updateChildShapes(), addView(), and onViewRemoved(), especially how originalCornerData is captured and reused. Determine an API or invalidation path for post-creation corner-radius or shape changes, then verify that updating a child shape is reflected in the group’s corner treatment.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.