Skip to content

Commit

Permalink
[ProgressIndicator] Added demo for visibility control.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 592008983
  • Loading branch information
pekingme authored and hunterstich committed Dec 19, 2023
1 parent ba0b332 commit 5d62e9e
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 6 deletions.
Expand Up @@ -19,6 +19,7 @@

import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import dagger.Provides;
import dagger.android.ContributesAndroidInjector;
import dagger.multibindings.IntoSet;
Expand Down Expand Up @@ -53,10 +54,24 @@ public Fragment createFragment() {
};
}

@NonNull
public List<Demo> getSharedAdditionalDemos() {
List<Demo> additionalDemos = new ArrayList<>();
additionalDemos.add(
new Demo(R.string.cat_progress_indicator_visibility_demo_title) {
@Nullable
@Override
public Fragment createFragment() {
return new ProgressIndicatorVisibilityDemoFragment();
}
});
return additionalDemos;
}

@Override
@NonNull
public List<Demo> getAdditionalDemos() {
List<Demo> additionalDemos = new ArrayList<>();
List<Demo> additionalDemos = getSharedAdditionalDemos();
additionalDemos.add(
new Demo(R.string.cat_progress_indicator_demo_indeterminate_title) {
@Override
Expand Down
@@ -0,0 +1,149 @@
/*
* Copyright 2023 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.
*/
package io.material.catalog.progressindicator;

import io.material.catalog.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.android.material.progressindicator.CircularProgressIndicator;
import com.google.android.material.progressindicator.LinearProgressIndicator;
import com.google.android.material.slider.Slider;
import io.material.catalog.feature.DemoFragment;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
* This is the fragment to demo different visibility change behaviors of {@link
* LinearProgressIndicator} and {@link CircularProgressIndicator}.
*/
public class ProgressIndicatorVisibilityDemoFragment extends DemoFragment {

public static final int SHOW_NONE = 0;
public static final int SHOW_OUTWARD = 1;
public static final int SHOW_INWARD = 2;
public static final int HIDE_NONE = 0;
public static final int HIDE_OUTWARD = 1;
public static final int HIDE_INWARD = 2;
public static final int HIDE_ESCAPE = 3;

private static final Map<String, Integer> showBehaviorCodes = new HashMap<>();
private static final Map<String, Integer> hideBehaviorCodes = new HashMap<>();

static {
showBehaviorCodes.put("none", SHOW_NONE);
showBehaviorCodes.put("outward", SHOW_OUTWARD);
showBehaviorCodes.put("inward", SHOW_INWARD);
hideBehaviorCodes.put("none", HIDE_NONE);
hideBehaviorCodes.put("outward", HIDE_OUTWARD);
hideBehaviorCodes.put("inward", HIDE_INWARD);
hideBehaviorCodes.put("escape", HIDE_ESCAPE);
}

@Override
@NonNull
public View onCreateDemoView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle) {

View view =
layoutInflater.inflate(
R.layout.cat_progress_indicator_visibility_fragment,
viewGroup,
false /* attachToRoot */);

initialize(view);

return view;
}

public void initialize(@NonNull View view) {
LinearProgressIndicator linearIndicator = view.findViewById(R.id.linear_indicator);
CircularProgressIndicator circularIndicator = view.findViewById(R.id.circular_indicator);
Slider progressSlider = view.findViewById(R.id.progress_slider);
MaterialSwitch determinateSwitch = view.findViewById(R.id.determinate_mode_switch);

progressSlider.addOnChangeListener(
(slider, value, fromUser) -> {
if (!linearIndicator.isIndeterminate()) {
linearIndicator.setProgressCompat((int) value, true);
}
if (!circularIndicator.isIndeterminate()) {
circularIndicator.setProgressCompat((int) value, true);
}
});
determinateSwitch.setOnCheckedChangeListener(
(v, isChecked) -> {
if (isChecked) {
float progress = progressSlider.getValue();
linearIndicator.setProgressCompat((int) progress, true);
circularIndicator.setProgressCompat((int) progress, true);
} else {
linearIndicator.setProgressCompat(0, false);
circularIndicator.setProgressCompat(0, false);
linearIndicator.setIndeterminate(true);
circularIndicator.setIndeterminate(true);
}
});

AutoCompleteTextView showBehaviorInput = view.findViewById(R.id.showBehaviorDropdown);
showBehaviorInput.setOnItemClickListener(
(parent, view12, position, id) -> {
String selected = (String) showBehaviorInput.getAdapter().getItem(position);
int showBehaviorCode = showBehaviorCodes.get(selected.toLowerCase(Locale.US));
linearIndicator.setShowAnimationBehavior(showBehaviorCode);
circularIndicator.setShowAnimationBehavior(showBehaviorCode);
});
AutoCompleteTextView hideBehaviorInput = view.findViewById(R.id.hideBehaviorDropdown);
hideBehaviorInput.setOnItemClickListener(
(parent, view1, position, id) -> {
String selected = (String) hideBehaviorInput.getAdapter().getItem(position);
int hideBehaviorCode = hideBehaviorCodes.get(selected.toLowerCase(Locale.US));
linearIndicator.setHideAnimationBehavior(hideBehaviorCode);
circularIndicator.setHideAnimationBehavior(hideBehaviorCode);
});

Button showButton = view.findViewById(R.id.showButton);
showButton.setOnClickListener(
v -> {
if (linearIndicator.getVisibility() != View.VISIBLE) {
linearIndicator.show();
}
if (circularIndicator.getVisibility() != View.VISIBLE) {
circularIndicator.show();
}
});
Button hideButton = view.findViewById(R.id.hideButton);
hideButton.setOnClickListener(
v -> {
if (linearIndicator.getVisibility() == View.VISIBLE) {
linearIndicator.hide();
}
if (circularIndicator.getVisibility() == View.VISIBLE) {
circularIndicator.hide();
}
});
}
}
@@ -0,0 +1,116 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical"
android:padding="16dp"
android:showDividers="middle"
android:divider="@drawable/layout_divider">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cat_progress_indicator_linear" />
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/linear_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cat_progress_indicator_circular" />
<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/circular_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"/>

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/determinate_mode_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cat_progress_indicator_set_to_determinate_mode"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cat_progress_indicator_determinate_progress"/>
<com.google.android.material.slider.Slider
android:id="@+id/progress_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:valueFrom="0"
android:valueTo="100"
android:stepSize="1"/>

<com.google.android.material.textfield.TextInputLayout
style="?attr/textInputOutlinedExposedDropdownMenuStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/cat_progress_indicator_show_behavior">
<AutoCompleteTextView
android:id="@+id/showBehaviorDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:text="@string/cat_progress_indicator_initial_behavior"
android:hint="@string/cat_progress_indicator_show_behavior"
app:simpleItems="@array/show_behaviors_array"/>
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="?attr/textInputOutlinedExposedDropdownMenuStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/cat_progress_indicator_hide_behavior">
<AutoCompleteTextView
android:id="@+id/hideBehaviorDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:text="@string/cat_progress_indicator_initial_behavior"
android:hint="@string/cat_progress_indicator_hide_behavior"
app:simpleItems="@array/hide_behaviors_array"/>
</com.google.android.material.textfield.TextInputLayout>

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:text="@string/cat_progress_indicator_show"/>
<Button
android:id="@+id/hideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:text="@string/cat_progress_indicator_hide"/>
</androidx.appcompat.widget.LinearLayoutCompat>

</LinearLayout>

Expand Up @@ -19,6 +19,10 @@
description="Title for the screen that showcases demonstrative usages of the MaterialProgressIndicator widget [CHAR LIMIT=NONE]">
Progress Indicator
</string>
<string name="cat_progress_indicator_visibility_demo_title" translatable="false"
description="Title of demo for the progress indicators with different visibility change behaviors [CHAR LIMIT=NONE]">
Progress Indicator Visibility Demo
</string>
<string name="cat_progress_indicator_demo_indeterminate_title"
description="Title of demo for the progress indicators in indeterminate mode [CHAR LIMIT=NONE]">
Indeterminate Progress Indicator Demo
Expand Down Expand Up @@ -60,19 +64,31 @@
description="Text label above a slider for adjusting circular indicator's size [CHAR LIMIT=NONE]">
Circular Indicator Size (20-200) dp
</string>
<string name="cat_progress_indicator_hide"
<string name="cat_progress_indicator_hide_behavior" translatable="false"
description="Hint label of a dropdown to select hide behavior [CHAR LIMIT=NONE]">
Hide Behavior
</string>
<string name="cat_progress_indicator_show_behavior" translatable="false"
description="Hint label of a dropdown to select show behavior [CHAR LIMIT=NONE]">
Show Behavior
</string>
<string name="cat_progress_indicator_initial_behavior" translatable="false"
description="Show/hide behavior preset in the dropdown fields [CHAR LIMIT=NONE]">
None
</string>
<string name="cat_progress_indicator_hide" translatable="false"
description="Button label to hide progress indicators [CHAR LIMIT=NONE]">
Hide
</string>
<string name="cat_progress_indicator_show"
<string name="cat_progress_indicator_show" translatable="false"
description="Button label to show progress indicators [CHAR LIMIT=NONE]">
Show
</string>
<string name="cat_progress_indicator_determinate_progress" translatable="false"
description="Text label above a slider for changing indicators' progress [CHAR LIMIT=NONE]">
Progress (0-100)
</string>
<string name="cat_progress_indicator_set_to_determinate_mode"
<string name="cat_progress_indicator_set_to_determinate_mode" translatable="false"
description="Set the displayed progress indicators to the determinate mode. [CHAR LIMIT=NONE]">
Determinate Mode
</string>
Expand All @@ -83,11 +99,11 @@

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

<string name="cat_progress_indicator_linear"
<string name="cat_progress_indicator_linear" translatable="false"
description="A linear progress progress indicator [CHAR LIMIT=NONE]">
Linear type
</string>
<string name="cat_progress_indicator_circular"
<string name="cat_progress_indicator_circular" translatable="false"
description="A circular progress progress indicator [CHAR LIMIT=NONE]">
Circular type
</string>
Expand Down Expand Up @@ -142,4 +158,16 @@
description="A material switch to toggle progress indicator drawable on chip [CHAR LIMIT=NONE]">
Show Icon
</string>

<string-array name="show_behaviors_array" translatable="false">
<item>Inward</item>
<item>Outward</item>
<item>None</item>
</string-array>
<string-array name="hide_behaviors_array" translatable="false">
<item>Inward</item>
<item>Outward</item>
<item>Escape</item>
<item>None</item>
</string-array>
</resources>

0 comments on commit 5d62e9e

Please sign in to comment.