Skip to content

Commit 5d62e9e

Browse files
pekingmehunterstich
authored andcommitted
[ProgressIndicator] Added demo for visibility control.
PiperOrigin-RevId: 592008983
1 parent ba0b332 commit 5d62e9e

File tree

4 files changed

+314
-6
lines changed

4 files changed

+314
-6
lines changed

catalog/java/io/material/catalog/progressindicator/ProgressIndicatorFragment.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import androidx.fragment.app.Fragment;
2121
import androidx.annotation.NonNull;
22+
import androidx.annotation.Nullable;
2223
import dagger.Provides;
2324
import dagger.android.ContributesAndroidInjector;
2425
import dagger.multibindings.IntoSet;
@@ -53,10 +54,24 @@ public Fragment createFragment() {
5354
};
5455
}
5556

57+
@NonNull
58+
public List<Demo> getSharedAdditionalDemos() {
59+
List<Demo> additionalDemos = new ArrayList<>();
60+
additionalDemos.add(
61+
new Demo(R.string.cat_progress_indicator_visibility_demo_title) {
62+
@Nullable
63+
@Override
64+
public Fragment createFragment() {
65+
return new ProgressIndicatorVisibilityDemoFragment();
66+
}
67+
});
68+
return additionalDemos;
69+
}
70+
5671
@Override
5772
@NonNull
5873
public List<Demo> getAdditionalDemos() {
59-
List<Demo> additionalDemos = new ArrayList<>();
74+
List<Demo> additionalDemos = getSharedAdditionalDemos();
6075
additionalDemos.add(
6176
new Demo(R.string.cat_progress_indicator_demo_indeterminate_title) {
6277
@Override
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2023 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.progressindicator;
17+
18+
import io.material.catalog.R;
19+
20+
import android.os.Bundle;
21+
import android.view.LayoutInflater;
22+
import android.view.View;
23+
import android.view.ViewGroup;
24+
import android.widget.AutoCompleteTextView;
25+
import android.widget.Button;
26+
import androidx.annotation.NonNull;
27+
import androidx.annotation.Nullable;
28+
import com.google.android.material.materialswitch.MaterialSwitch;
29+
import com.google.android.material.progressindicator.CircularProgressIndicator;
30+
import com.google.android.material.progressindicator.LinearProgressIndicator;
31+
import com.google.android.material.slider.Slider;
32+
import io.material.catalog.feature.DemoFragment;
33+
import java.util.HashMap;
34+
import java.util.Locale;
35+
import java.util.Map;
36+
37+
/**
38+
* This is the fragment to demo different visibility change behaviors of {@link
39+
* LinearProgressIndicator} and {@link CircularProgressIndicator}.
40+
*/
41+
public class ProgressIndicatorVisibilityDemoFragment extends DemoFragment {
42+
43+
public static final int SHOW_NONE = 0;
44+
public static final int SHOW_OUTWARD = 1;
45+
public static final int SHOW_INWARD = 2;
46+
public static final int HIDE_NONE = 0;
47+
public static final int HIDE_OUTWARD = 1;
48+
public static final int HIDE_INWARD = 2;
49+
public static final int HIDE_ESCAPE = 3;
50+
51+
private static final Map<String, Integer> showBehaviorCodes = new HashMap<>();
52+
private static final Map<String, Integer> hideBehaviorCodes = new HashMap<>();
53+
54+
static {
55+
showBehaviorCodes.put("none", SHOW_NONE);
56+
showBehaviorCodes.put("outward", SHOW_OUTWARD);
57+
showBehaviorCodes.put("inward", SHOW_INWARD);
58+
hideBehaviorCodes.put("none", HIDE_NONE);
59+
hideBehaviorCodes.put("outward", HIDE_OUTWARD);
60+
hideBehaviorCodes.put("inward", HIDE_INWARD);
61+
hideBehaviorCodes.put("escape", HIDE_ESCAPE);
62+
}
63+
64+
@Override
65+
@NonNull
66+
public View onCreateDemoView(
67+
@NonNull LayoutInflater layoutInflater,
68+
@Nullable ViewGroup viewGroup,
69+
@Nullable Bundle bundle) {
70+
71+
View view =
72+
layoutInflater.inflate(
73+
R.layout.cat_progress_indicator_visibility_fragment,
74+
viewGroup,
75+
false /* attachToRoot */);
76+
77+
initialize(view);
78+
79+
return view;
80+
}
81+
82+
public void initialize(@NonNull View view) {
83+
LinearProgressIndicator linearIndicator = view.findViewById(R.id.linear_indicator);
84+
CircularProgressIndicator circularIndicator = view.findViewById(R.id.circular_indicator);
85+
Slider progressSlider = view.findViewById(R.id.progress_slider);
86+
MaterialSwitch determinateSwitch = view.findViewById(R.id.determinate_mode_switch);
87+
88+
progressSlider.addOnChangeListener(
89+
(slider, value, fromUser) -> {
90+
if (!linearIndicator.isIndeterminate()) {
91+
linearIndicator.setProgressCompat((int) value, true);
92+
}
93+
if (!circularIndicator.isIndeterminate()) {
94+
circularIndicator.setProgressCompat((int) value, true);
95+
}
96+
});
97+
determinateSwitch.setOnCheckedChangeListener(
98+
(v, isChecked) -> {
99+
if (isChecked) {
100+
float progress = progressSlider.getValue();
101+
linearIndicator.setProgressCompat((int) progress, true);
102+
circularIndicator.setProgressCompat((int) progress, true);
103+
} else {
104+
linearIndicator.setProgressCompat(0, false);
105+
circularIndicator.setProgressCompat(0, false);
106+
linearIndicator.setIndeterminate(true);
107+
circularIndicator.setIndeterminate(true);
108+
}
109+
});
110+
111+
AutoCompleteTextView showBehaviorInput = view.findViewById(R.id.showBehaviorDropdown);
112+
showBehaviorInput.setOnItemClickListener(
113+
(parent, view12, position, id) -> {
114+
String selected = (String) showBehaviorInput.getAdapter().getItem(position);
115+
int showBehaviorCode = showBehaviorCodes.get(selected.toLowerCase(Locale.US));
116+
linearIndicator.setShowAnimationBehavior(showBehaviorCode);
117+
circularIndicator.setShowAnimationBehavior(showBehaviorCode);
118+
});
119+
AutoCompleteTextView hideBehaviorInput = view.findViewById(R.id.hideBehaviorDropdown);
120+
hideBehaviorInput.setOnItemClickListener(
121+
(parent, view1, position, id) -> {
122+
String selected = (String) hideBehaviorInput.getAdapter().getItem(position);
123+
int hideBehaviorCode = hideBehaviorCodes.get(selected.toLowerCase(Locale.US));
124+
linearIndicator.setHideAnimationBehavior(hideBehaviorCode);
125+
circularIndicator.setHideAnimationBehavior(hideBehaviorCode);
126+
});
127+
128+
Button showButton = view.findViewById(R.id.showButton);
129+
showButton.setOnClickListener(
130+
v -> {
131+
if (linearIndicator.getVisibility() != View.VISIBLE) {
132+
linearIndicator.show();
133+
}
134+
if (circularIndicator.getVisibility() != View.VISIBLE) {
135+
circularIndicator.show();
136+
}
137+
});
138+
Button hideButton = view.findViewById(R.id.hideButton);
139+
hideButton.setOnClickListener(
140+
v -> {
141+
if (linearIndicator.getVisibility() == View.VISIBLE) {
142+
linearIndicator.hide();
143+
}
144+
if (circularIndicator.getVisibility() == View.VISIBLE) {
145+
circularIndicator.hide();
146+
}
147+
});
148+
}
149+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2023 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:app="http://schemas.android.com/apk/res-auto"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:clipChildren="false"
22+
android:clipToPadding="false"
23+
android:orientation="vertical"
24+
android:padding="16dp"
25+
android:showDividers="middle"
26+
android:divider="@drawable/layout_divider">
27+
<TextView
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:text="@string/cat_progress_indicator_linear" />
31+
<com.google.android.material.progressindicator.LinearProgressIndicator
32+
android:id="@+id/linear_indicator"
33+
android:layout_width="match_parent"
34+
android:layout_height="wrap_content"
35+
android:indeterminate="true"/>
36+
37+
<TextView
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:text="@string/cat_progress_indicator_circular" />
41+
<com.google.android.material.progressindicator.CircularProgressIndicator
42+
android:id="@+id/circular_indicator"
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content"
45+
android:layout_gravity="center"
46+
android:indeterminate="true"/>
47+
48+
<com.google.android.material.materialswitch.MaterialSwitch
49+
android:id="@+id/determinate_mode_switch"
50+
android:layout_width="match_parent"
51+
android:layout_height="wrap_content"
52+
android:text="@string/cat_progress_indicator_set_to_determinate_mode"/>
53+
<TextView
54+
android:layout_width="wrap_content"
55+
android:layout_height="wrap_content"
56+
android:text="@string/cat_progress_indicator_determinate_progress"/>
57+
<com.google.android.material.slider.Slider
58+
android:id="@+id/progress_slider"
59+
android:layout_width="match_parent"
60+
android:layout_height="wrap_content"
61+
android:valueFrom="0"
62+
android:valueTo="100"
63+
android:stepSize="1"/>
64+
65+
<com.google.android.material.textfield.TextInputLayout
66+
style="?attr/textInputOutlinedExposedDropdownMenuStyle"
67+
android:layout_width="match_parent"
68+
android:layout_height="wrap_content"
69+
android:hint="@string/cat_progress_indicator_show_behavior">
70+
<AutoCompleteTextView
71+
android:id="@+id/showBehaviorDropdown"
72+
android:layout_width="match_parent"
73+
android:layout_height="wrap_content"
74+
android:inputType="none"
75+
android:text="@string/cat_progress_indicator_initial_behavior"
76+
android:hint="@string/cat_progress_indicator_show_behavior"
77+
app:simpleItems="@array/show_behaviors_array"/>
78+
</com.google.android.material.textfield.TextInputLayout>
79+
80+
<com.google.android.material.textfield.TextInputLayout
81+
style="?attr/textInputOutlinedExposedDropdownMenuStyle"
82+
android:layout_width="match_parent"
83+
android:layout_height="wrap_content"
84+
android:hint="@string/cat_progress_indicator_hide_behavior">
85+
<AutoCompleteTextView
86+
android:id="@+id/hideBehaviorDropdown"
87+
android:layout_width="match_parent"
88+
android:layout_height="wrap_content"
89+
android:inputType="none"
90+
android:text="@string/cat_progress_indicator_initial_behavior"
91+
android:hint="@string/cat_progress_indicator_hide_behavior"
92+
app:simpleItems="@array/hide_behaviors_array"/>
93+
</com.google.android.material.textfield.TextInputLayout>
94+
95+
<androidx.appcompat.widget.LinearLayoutCompat
96+
android:layout_width="match_parent"
97+
android:layout_height="wrap_content"
98+
android:orientation="horizontal">
99+
<Button
100+
android:id="@+id/showButton"
101+
android:layout_width="wrap_content"
102+
android:layout_height="wrap_content"
103+
android:layout_weight="1"
104+
android:layout_margin="2dp"
105+
android:text="@string/cat_progress_indicator_show"/>
106+
<Button
107+
android:id="@+id/hideButton"
108+
android:layout_width="wrap_content"
109+
android:layout_height="wrap_content"
110+
android:layout_weight="1"
111+
android:layout_margin="2dp"
112+
android:text="@string/cat_progress_indicator_hide"/>
113+
</androidx.appcompat.widget.LinearLayoutCompat>
114+
115+
</LinearLayout>
116+

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
description="Title for the screen that showcases demonstrative usages of the MaterialProgressIndicator widget [CHAR LIMIT=NONE]">
2020
Progress Indicator
2121
</string>
22+
<string name="cat_progress_indicator_visibility_demo_title" translatable="false"
23+
description="Title of demo for the progress indicators with different visibility change behaviors [CHAR LIMIT=NONE]">
24+
Progress Indicator Visibility Demo
25+
</string>
2226
<string name="cat_progress_indicator_demo_indeterminate_title"
2327
description="Title of demo for the progress indicators in indeterminate mode [CHAR LIMIT=NONE]">
2428
Indeterminate Progress Indicator Demo
@@ -60,19 +64,31 @@
6064
description="Text label above a slider for adjusting circular indicator's size [CHAR LIMIT=NONE]">
6165
Circular Indicator Size (20-200) dp
6266
</string>
63-
<string name="cat_progress_indicator_hide"
67+
<string name="cat_progress_indicator_hide_behavior" translatable="false"
68+
description="Hint label of a dropdown to select hide behavior [CHAR LIMIT=NONE]">
69+
Hide Behavior
70+
</string>
71+
<string name="cat_progress_indicator_show_behavior" translatable="false"
72+
description="Hint label of a dropdown to select show behavior [CHAR LIMIT=NONE]">
73+
Show Behavior
74+
</string>
75+
<string name="cat_progress_indicator_initial_behavior" translatable="false"
76+
description="Show/hide behavior preset in the dropdown fields [CHAR LIMIT=NONE]">
77+
None
78+
</string>
79+
<string name="cat_progress_indicator_hide" translatable="false"
6480
description="Button label to hide progress indicators [CHAR LIMIT=NONE]">
6581
Hide
6682
</string>
67-
<string name="cat_progress_indicator_show"
83+
<string name="cat_progress_indicator_show" translatable="false"
6884
description="Button label to show progress indicators [CHAR LIMIT=NONE]">
6985
Show
7086
</string>
7187
<string name="cat_progress_indicator_determinate_progress" translatable="false"
7288
description="Text label above a slider for changing indicators' progress [CHAR LIMIT=NONE]">
7389
Progress (0-100)
7490
</string>
75-
<string name="cat_progress_indicator_set_to_determinate_mode"
91+
<string name="cat_progress_indicator_set_to_determinate_mode" translatable="false"
7692
description="Set the displayed progress indicators to the determinate mode. [CHAR LIMIT=NONE]">
7793
Determinate Mode
7894
</string>
@@ -83,11 +99,11 @@
8399

84100
<!-- Descriptions of various examples of progress indicators -->
85101

86-
<string name="cat_progress_indicator_linear"
102+
<string name="cat_progress_indicator_linear" translatable="false"
87103
description="A linear progress progress indicator [CHAR LIMIT=NONE]">
88104
Linear type
89105
</string>
90-
<string name="cat_progress_indicator_circular"
106+
<string name="cat_progress_indicator_circular" translatable="false"
91107
description="A circular progress progress indicator [CHAR LIMIT=NONE]">
92108
Circular type
93109
</string>
@@ -142,4 +158,16 @@
142158
description="A material switch to toggle progress indicator drawable on chip [CHAR LIMIT=NONE]">
143159
Show Icon
144160
</string>
161+
162+
<string-array name="show_behaviors_array" translatable="false">
163+
<item>Inward</item>
164+
<item>Outward</item>
165+
<item>None</item>
166+
</string-array>
167+
<string-array name="hide_behaviors_array" translatable="false">
168+
<item>Inward</item>
169+
<item>Outward</item>
170+
<item>Escape</item>
171+
<item>None</item>
172+
</string-array>
145173
</resources>

0 commit comments

Comments
 (0)