Skip to content

Commit 456f135

Browse files
drchenveganafro
authored andcommitted
[Button] Fix progress indicator is not shown when set as the icon
MaterialButton is using TextViewCompat.setCompoundDrawablesRelative() under the hood to implement setIcon() operation. TextViewCompat won't call its compound drawables' setVisible() method, on which we rely to start progress indicator drawables automatically. Calls setVisible() explicitly to start progressing. Also adds a demo to catalog. Resolves #2095 PiperOrigin-RevId: 383483364
1 parent f6237e3 commit 456f135

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

catalog/java/io/material/catalog/progressindicator/ProgressIndicatorStandaloneDemoFragment.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import androidx.annotation.NonNull;
2525
import androidx.annotation.Nullable;
2626
import androidx.annotation.StyleRes;
27+
import com.google.android.material.button.MaterialButton;
2728
import com.google.android.material.chip.Chip;
2829
import com.google.android.material.progressindicator.CircularProgressIndicatorSpec;
2930
import com.google.android.material.progressindicator.IndeterminateDrawable;
@@ -50,17 +51,20 @@ public View onCreateDemoView(
5051

5152
CircularProgressIndicatorSpec spec =
5253
new CircularProgressIndicatorSpec(getContext(), /*attrs=*/ null, 0, getSpecStyleResId());
54+
Chip chip = view.findViewById(R.id.cat_progress_indicator_chip);
55+
chip.setChipIcon(IndeterminateDrawable.createCircularDrawable(getContext(), spec));
56+
5357
IndeterminateDrawable<CircularProgressIndicatorSpec> progressIndicatorDrawable =
5458
IndeterminateDrawable.createCircularDrawable(getContext(), spec);
55-
56-
Chip chip = view.findViewById(R.id.cat_progress_indicator_chip);
57-
chip.setChipIcon(progressIndicatorDrawable);
59+
MaterialButton button = view.findViewById(R.id.cat_progress_indicator_button);
60+
button.setIcon(progressIndicatorDrawable);
5861

5962
SwitchMaterial chipIconSwitch =
6063
view.findViewById(R.id.cat_progress_indicator_standalone_chip_switch);
6164
chipIconSwitch.setOnCheckedChangeListener(
6265
(buttonView, isChecked) -> {
6366
chip.setChipIconVisible(isChecked);
67+
button.setIcon(isChecked ? progressIndicatorDrawable : null);
6468
});
6569
return view;
6670
}

catalog/java/io/material/catalog/progressindicator/res/layout/cat_progress_indicator_standalone_fragment.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
android:ellipsize="end"
3333
android:text="@string/cat_progress_indicator_standalone_indeterminate_chip"/>
3434

35+
<com.google.android.material.button.MaterialButton
36+
android:id="@+id/cat_progress_indicator_button"
37+
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:ellipsize="end"
41+
android:text="@string/cat_progress_indicator_standalone_indeterminate_button"/>
42+
3543
<com.google.android.material.switchmaterial.SwitchMaterial
3644
android:id="@+id/cat_progress_indicator_standalone_chip_switch"
3745
android:layout_width="wrap_content"

catalog/java/io/material/catalog/progressindicator/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@
120120
description="An example of using progress indicator drawable as chip icon [CHAR LIMIT=NONE]">
121121
Indeterminate Chip
122122
</string>
123+
<string name="cat_progress_indicator_standalone_indeterminate_button"
124+
description="An example of using progress indicator drawable as button icon [CHAR LIMIT=NONE]">
125+
Indeterminate Button
126+
</string>
123127
<string name="cat_progress_indicator_standalone_switch_label"
124128
description="A material switch to toggle progress indicator drawable on chip [CHAR LIMIT=NONE]">
125129
Show Icon

lib/java/com/google/android/material/button/MaterialButton.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ private void updateIcon(boolean needsIconReset) {
761761
int width = iconSize != 0 ? iconSize : icon.getIntrinsicWidth();
762762
int height = iconSize != 0 ? iconSize : icon.getIntrinsicHeight();
763763
icon.setBounds(iconLeft, iconTop, iconLeft + width, iconTop + height);
764+
icon.setVisible(true, needsIconReset);
764765
}
765766

766767
// Forced icon update

0 commit comments

Comments
 (0)