|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.material.catalog.button; |
| 17 | + |
| 18 | +import io.material.catalog.R; |
| 19 | + |
| 20 | +import android.annotation.SuppressLint; |
| 21 | +import android.os.Bundle; |
| 22 | +import android.view.LayoutInflater; |
| 23 | +import android.view.View; |
| 24 | +import android.view.View.OnClickListener; |
| 25 | +import android.view.ViewGroup; |
| 26 | +import android.widget.Button; |
| 27 | +import android.widget.LinearLayout.LayoutParams; |
| 28 | +import androidx.annotation.NonNull; |
| 29 | +import androidx.annotation.Nullable; |
| 30 | +import com.google.android.material.button.MaterialButton; |
| 31 | +import com.google.android.material.button.MaterialButtonGroup; |
| 32 | +import io.material.catalog.feature.DemoFragment; |
| 33 | + |
| 34 | +/** A fragment that displays a button group demo for adding and removing buttons at runtime. */ |
| 35 | +public class ButtonGroupRuntimeDemoFragment extends DemoFragment { |
| 36 | + |
| 37 | + private static final int MAX_COUNT = 10; |
| 38 | + private int buttonCount; |
| 39 | + private Button addButton; |
| 40 | + private Button removeButton; |
| 41 | + |
| 42 | + /** |
| 43 | + * Create a Demo View with {@link MaterialButtonGroup}, in which, buttons are added and removed at |
| 44 | + * runtime. |
| 45 | + */ |
| 46 | + @Nullable |
| 47 | + @Override |
| 48 | + public View onCreateDemoView( |
| 49 | + @NonNull LayoutInflater layoutInflater, |
| 50 | + @Nullable ViewGroup viewGroup, |
| 51 | + @Nullable Bundle bundle) { |
| 52 | + View view = |
| 53 | + layoutInflater.inflate( |
| 54 | + R.layout.cat_buttons_group_runtime_fragment, viewGroup, /* attachToRoot= */ false); |
| 55 | + |
| 56 | + MaterialButtonGroup buttonGroup = view.findViewById(R.id.cat_dynamic_button_group); |
| 57 | + addButton = view.findViewById(R.id.cat_add_button); |
| 58 | + removeButton = view.findViewById(R.id.cat_remove_button); |
| 59 | + updateControl(); |
| 60 | + addButton.setOnClickListener( |
| 61 | + new OnClickListener() { |
| 62 | + @SuppressLint("SetTextI18n") |
| 63 | + @Override |
| 64 | + public void onClick(View v) { |
| 65 | + MaterialButton button = new MaterialButton(view.getContext()); |
| 66 | + button.setText("Button"); |
| 67 | + buttonGroup.addView( |
| 68 | + button, |
| 69 | + -1, |
| 70 | + new LayoutParams( |
| 71 | + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1)); |
| 72 | + buttonCount++; |
| 73 | + updateControl(); |
| 74 | + } |
| 75 | + }); |
| 76 | + removeButton.setOnClickListener( |
| 77 | + v -> { |
| 78 | + buttonGroup.removeViewAt(buttonGroup.getChildCount() - 1); |
| 79 | + buttonCount--; |
| 80 | + updateControl(); |
| 81 | + }); |
| 82 | + |
| 83 | + return view; |
| 84 | + } |
| 85 | + |
| 86 | + private void updateControl(){ |
| 87 | + if(buttonCount == 0){ |
| 88 | + removeButton.setEnabled(false); |
| 89 | + }else if(buttonCount == MAX_COUNT){ |
| 90 | + addButton.setEnabled(false); |
| 91 | + }else{ |
| 92 | + addButton.setEnabled(true); |
| 93 | + removeButton.setEnabled(true); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments