Skip to content

Commit

Permalink
[Slider] Added LABEL_VISIBLE to label behavior and added a demo fragm…
Browse files Browse the repository at this point in the history
…ent in catalog.

Resolves #1316

PiperOrigin-RevId: 433570701
  • Loading branch information
pekingme authored and veganafro committed Mar 9, 2022
1 parent e7a691e commit ca8594d
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 53 deletions.
7 changes: 7 additions & 0 deletions catalog/java/io/material/catalog/slider/SliderFragment.java
Expand Up @@ -77,6 +77,13 @@ public Fragment createFragment() {
return new SliderScrollContainerDemoFragment();
}
});
additionalDemos.add(
new Demo(R.string.cat_slider_demo_label_behavior_title) {
@Override
public Fragment createFragment() {
return new SliderLabelBehaviorDemoFragment();
}
});
return additionalDemos;
}

Expand Down
@@ -0,0 +1,63 @@
/*
* Copyright 2019 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.slider;

import io.material.catalog.R;

import android.os.Bundle;
import androidx.appcompat.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.slider.RangeSlider;
import com.google.android.material.slider.Slider;
import io.material.catalog.feature.DemoFragment;

/**
* Fragment to display a few basic uses of the {@link Slider} widget with different label behaviors
* for the Catalog app.
*/
public class SliderLabelBehaviorDemoFragment extends DemoFragment {

@Nullable
@Override
public View onCreateDemoView(
@NonNull LayoutInflater inflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
View view =
inflater.inflate(
R.layout.cat_slider_demo_label_behavior, viewGroup, false /* attachToRoot */);

setUpSlider(view, R.id.switch_button_1, R.id.slider_1);
setUpSlider(view, R.id.switch_button_2, R.id.slider_2);
setUpSlider(view, R.id.switch_button_3, R.id.slider_3);
setUpSlider(view, R.id.switch_button_4, R.id.slider_4);

return view;
}

private void setUpSlider(
View view, @IdRes int switchId, @IdRes int sliderId) {
final RangeSlider slider = view.findViewById(sliderId);
SwitchCompat switchButton = view.findViewById(switchId);
switchButton.setOnCheckedChangeListener(
(buttonView, isChecked) -> slider.setEnabled(isChecked));
switchButton.setChecked(true);
}
}
@@ -0,0 +1,146 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 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.
-->
<ScrollView
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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/cat_slider_title_top_padding"
android:text="@string/cat_slider_label_floating"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.google.android.material.slider.RangeSlider
android:id="@+id/slider_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="@dimen/cat_slider_left_margin"
android:layout_marginStart="@dimen/cat_slider_left_margin"
android:valueFrom="0"
android:valueTo="10"
app:values="@array/initial_slider_values"
app:labelBehavior="floating"/>
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/cat_slider_title_top_padding"
android:text="@string/cat_slider_label_within_bounds"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.google.android.material.slider.RangeSlider
android:id="@+id/slider_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="@dimen/cat_slider_left_margin"
android:layout_marginStart="@dimen/cat_slider_left_margin"
android:valueFrom="0"
android:valueTo="10"
app:values="@array/initial_slider_values"
app:labelBehavior="withinBounds"/>
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/cat_slider_title_top_padding"
android:text="@string/cat_slider_label_gone"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.google.android.material.slider.RangeSlider
android:id="@+id/slider_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="@dimen/cat_slider_left_margin"
android:layout_marginStart="@dimen/cat_slider_left_margin"
android:valueFrom="0"
android:valueTo="10"
app:values="@array/initial_slider_values"
app:labelBehavior="gone"/>
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/cat_slider_title_top_padding"
android:text="@string/cat_slider_label_visible"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.google.android.material.slider.RangeSlider
android:id="@+id/slider_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="@dimen/cat_slider_left_margin"
android:layout_marginStart="@dimen/cat_slider_left_margin"
android:valueFrom="0"
android:valueTo="10"
app:values="@array/initial_slider_values"
app:labelBehavior="visible"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
31 changes: 18 additions & 13 deletions catalog/java/io/material/catalog/slider/res/values/strings.xml
Expand Up @@ -15,10 +15,11 @@
limitations under the License.
-->
<resources>
<string name="cat_slider_title" description="Title for the screen that showcases demonstrative usages of the Slider widget">Slider</string>
<string name="cat_slider_demo_continuous_title" description="Title for the continuous Slider widget demo">Continuous Slider demo</string>
<string name="cat_slider_demo_discrete_title" description="Title for the discrete Slider widget demo">Discrete Slider demo</string>
<string name="cat_slider_demo_scroll_container_title" description="Title for the Slider inside scrolling container demo">Slider in scrolling container demo</string>
<string name="cat_slider_title" description="Title for the screen that showcases demonstrative usages of the Slider widget [CHAR LIMIT=NONE]">Slider</string>
<string name="cat_slider_demo_continuous_title" description="Title for the continuous Slider widget demo [CHAR LIMIT=NONE]">Continuous Slider demo</string>
<string name="cat_slider_demo_discrete_title" description="Title for the discrete Slider widget demo [CHAR LIMIT=NONE]">Discrete Slider demo</string>
<string name="cat_slider_demo_scroll_container_title" description="Title for the Slider inside scrolling container demo [CHAR LIMIT=NONE]">Slider in scrolling container demo</string>
<string name="cat_slider_demo_label_behavior_title" description="Tile for the Slider with different label behaviors demo [CHAR LIMIT=NONE]">Slider label behavior demo</string>

<string name="cat_slider_description" description="Body text describing the Slider widget within the design system [CHAR LIMIT=NONE]">
Sliders let users select from a range of values by moving the slider thumb.
Expand All @@ -28,14 +29,18 @@
Discrete sliders allow users to select a specific value from a range.
</string>

<string name="cat_slider_set" description="The label for a set button">Set</string>
<string name="cat_slider_title_1" description="The title for the first demonstration slider">This one goes to eleven</string>
<string name="cat_slider_title_2" description="The title for a demonstration slider">From 100 to 1000</string>
<string name="cat_slider_title_3" description="The title for a demonstration slider">Negative numbers!</string>
<string name="cat_slider_title_4" description="The title for a demonstration slider">With a label formatter</string>
<string name="cat_slider_title_5" description="The title for a demonstration slider">I can have decimal numbers?</string>
<string name="cat_slider_title_6" description="The title for a demonstration slider">Without tick marks</string>
<string name="cat_slider_start_touch_description" description="Indicates the slider is now being touched">Slider started being touched</string>
<string name="cat_slider_stop_touch_description" description="Indicates the slider stopped being touched">Slider stopped being touched</string>
<string name="cat_slider_set" description="The label for a set button [CHAR LIMIT=NONE]">Set</string>
<string name="cat_slider_title_1" description="The title for the first demonstration slider [CHAR LIMIT=NONE]">This one goes to eleven</string>
<string name="cat_slider_title_2" description="The title for a demonstration slider [CHAR LIMIT=NONE]">From 100 to 1000</string>
<string name="cat_slider_title_3" description="The title for a demonstration slider [CHAR LIMIT=NONE]">Negative numbers!</string>
<string name="cat_slider_title_4" description="The title for a demonstration slider [CHAR LIMIT=NONE]">With a label formatter</string>
<string name="cat_slider_title_5" description="The title for a demonstration slider [CHAR LIMIT=NONE]">I can have decimal numbers?</string>
<string name="cat_slider_title_6" description="The title for a demonstration slider [CHAR LIMIT=NONE]">Without tick marks</string>
<string name="cat_slider_start_touch_description" description="Indicates the slider is now being touched [CHAR LIMIT=NONE]">Slider started being touched</string>
<string name="cat_slider_stop_touch_description" description="Indicates the slider stopped being touched [CHAR LIMIT=NONE]">Slider stopped being touched</string>
<string name="cat_slider_label_floating" description="The title for the demonstration slider with floating labels [CHAR LIMIT=NONE]">This one has floating labels</string>
<string name="cat_slider_label_within_bounds" description="The title for the demonstration slider with labels within bounds [CHAR LIMIT=NONE]">This one has labels within bounds</string>
<string name="cat_slider_label_gone" description="The title for the demonstration slider with gone labels [CHAR LIMIT=NONE]">This one shows no labels</string>
<string name="cat_slider_label_visible" description="The title for the first demonstration slider [CHAR LIMIT=NONE]">This one always shows labels</string>

</resources>
1 change: 1 addition & 0 deletions docs/components/Slider.md
Expand Up @@ -145,6 +145,7 @@ The modes of `app:labelBehavior` are:
view
* `withinBounds` - draws the label floating within the bounds of this view
* `gone` - prevents the label from being drawn
* `visible` - always draws the label

### Setting a `LabelFormatter`

Expand Down

0 comments on commit ca8594d

Please sign in to comment.