Skip to content

Commit

Permalink
[ButtonGroup] Added more variants in catalog.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 666940948
  • Loading branch information
pekingme authored and leticiarossi committed Aug 27, 2024
1 parent 967dcd5 commit 2901dca
Show file tree
Hide file tree
Showing 10 changed files with 567 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.button.MaterialButtonGroup;
import com.google.android.material.materialswitch.MaterialSwitch;
import io.material.catalog.feature.DemoFragment;
Expand All @@ -37,8 +36,6 @@
/** A fragment that displays a button group demo for the Catalog app. */
public class ButtonGroupDemoFragment extends DemoFragment {

private int defaultInset;

/** Create a Demo View with different types of {@link MaterialButtonGroup}. */
@Nullable
@Override
Expand All @@ -49,25 +46,21 @@ public View onCreateDemoView(
View view =
layoutInflater.inflate(
R.layout.cat_buttons_group_fragment, viewGroup, /* attachToRoot= */ false);

ViewGroup content = view.findViewById(R.id.button_group_content);
layoutInflater.inflate(getIconOnlyButtonGroupContent(), content, /* attachToRoot= */ true);
layoutInflater.inflate(getLabelOnlyButtonGroupContent(), content, /* attachToRoot= */ true);
layoutInflater.inflate(getMixedButtonGroupContent(), content, /* attachToRoot= */ true);

List<MaterialButtonGroup> buttonGroups =
DemoUtils.findViewsWithType(view, MaterialButtonGroup.class);

defaultInset = getResources().getDimensionPixelSize(R.dimen.mtrl_btn_inset);

MaterialSwitch verticalOrientationToggle = view.findViewById(R.id.orientation_switch_toggle);
verticalOrientationToggle.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
for (MaterialButtonGroup buttonGroup : buttonGroups) {
int orientation = isChecked ? VERTICAL : HORIZONTAL;
buttonGroup.setOrientation(orientation);
for (int i = 0; i < buttonGroup.getChildCount(); ++i) {
int inset = getInsetForOrientation(orientation);
MaterialButton button = (MaterialButton) buttonGroup.getChildAt(i);
button.setInsetBottom(inset);
button.setInsetTop(inset);
adjustParams(button.getLayoutParams(), orientation);
}

buttonGroup.requestLayout();
}
});
Expand All @@ -83,12 +76,18 @@ public View onCreateDemoView(
return view;
}

private int getInsetForOrientation(int orientation) {
return orientation == VERTICAL ? 0 : defaultInset;
@LayoutRes
protected int getIconOnlyButtonGroupContent() {
return R.layout.cat_button_group_content_icon_only;
}

@LayoutRes
protected int getLabelOnlyButtonGroupContent() {
return R.layout.cat_button_group_content_label_only;
}

private static void adjustParams(LayoutParams layoutParams, int orientation) {
layoutParams.width =
orientation == VERTICAL ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT;
@LayoutRes
protected int getMixedButtonGroupContent() {
return R.layout.cat_button_group_content_mixed;
}
}
20 changes: 13 additions & 7 deletions catalog/java/io/material/catalog/button/ButtonsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.material.catalog.R;

import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import dagger.Provides;
import dagger.android.ContributesAndroidInjector;
Expand Down Expand Up @@ -57,13 +58,7 @@ public Fragment createFragment() {
@Override
public List<Demo> getAdditionalDemos() {
return Arrays.asList(
new Demo(R.string.cat_buttons_group) {
@Nullable
@Override
public Fragment createFragment() {
return new ButtonGroupDemoFragment();
}
},
getButtonGroupDemo(),
new Demo(R.string.cat_buttons_toggle_group) {
@Nullable
@Override
Expand All @@ -73,6 +68,17 @@ public Fragment createFragment() {
});
}

@NonNull
protected Demo getButtonGroupDemo() {
return new Demo(R.string.cat_buttons_group) {
@Nullable
@Override
public Fragment createFragment() {
return new ButtonGroupDemoFragment();
}
};
}

/** The Dagger module for {@link ButtonsFragment} dependencies. */
@dagger.Module
public abstract static class Module {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="8dp"
android:text="@string/cat_icon_only"
android:textAppearance="?attr/textAppearanceSubtitle1"/>
<com.google.android.material.button.MaterialButtonGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:gravity="center_horizontal"
android:spacing="4dp">
<Button
style="?attr/materialIconButtonFilledStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_paint_bucket"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonFilledStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_plus"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonFilledStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_components"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonFilledStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_accelerator"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonFilledStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_dialog"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
</com.google.android.material.button.MaterialButtonGroup>
<com.google.android.material.button.MaterialButtonGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:gravity="center_horizontal"
android:spacing="4dp">
<Button
style="?attr/materialIconButtonFilledTonalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_paint_bucket"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonFilledTonalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_plus"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonFilledTonalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_components"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonFilledTonalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_accelerator"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonFilledTonalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_dialog"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
</com.google.android.material.button.MaterialButtonGroup>
<com.google.android.material.button.MaterialButtonGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:gravity="center_horizontal"
android:spacing="4dp">
<Button
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_paint_bucket"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_plus"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_components"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_accelerator"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_dialog"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
</com.google.android.material.button.MaterialButtonGroup>
<com.google.android.material.button.MaterialButtonGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:gravity="center_horizontal"
android:spacing="4dp">
<Button
style="?attr/materialIconButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_paint_bucket"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_plus"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_components"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_accelerator"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
<Button
style="?attr/materialIconButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cat_icon_button_label_dialog"
app:icon="@drawable/ic_star_checkable_24"
app:iconPadding="0dp"/>
</com.google.android.material.button.MaterialButtonGroup>
</merge>
Loading

0 comments on commit 2901dca

Please sign in to comment.