Skip to content

Commit

Permalink
[Slider] Add vertical support
Browse files Browse the repository at this point in the history
- added "orientation" attr allowing for vertical support
- updated tests and doc
- added demo

PiperOrigin-RevId: 695856298
  • Loading branch information
paulfthomas authored and hunterstich committed Nov 21, 2024
1 parent 7f8d63b commit 5bcda8a
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 108 deletions.
7 changes: 7 additions & 0 deletions catalog/java/io/material/catalog/slider/SliderFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public Fragment createFragment() {
return new SliderCornerDemoFragment();
}
});
additionalDemos.add(
new Demo(R.string.cat_slider_demo_vertical_title) {
@Override
public Fragment createFragment() {
return new SliderVerticalDemoFragment();
}
});
return additionalDemos;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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
*
* https://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 static com.google.android.material.slider.SliderOrientation.HORIZONTAL;
import static com.google.android.material.slider.SliderOrientation.VERTICAL;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.android.material.slider.Slider;
import io.material.catalog.feature.DemoFragment;

/**
* Fragment to display a few basic uses of the vertical {@link Slider} widget for the Catalog app.
*/
public class SliderVerticalDemoFragment extends DemoFragment {

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

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

Slider slider = view.findViewById(R.id.slider_vertical);
MaterialSwitch switchButton = view.findViewById(R.id.switch_button);
switchButton.setOnCheckedChangeListener(
(buttonView, isChecked) -> slider.setOrientation(isChecked ? VERTICAL : HORIZONTAL));

return view;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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.
-->
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="16dp">

<com.google.android.material.slider.Slider
android:id="@+id/slider_vertical"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_marginTop="64dp"
android:orientation="vertical"
android:value="8.09"
android:valueFrom="0.0"
android:valueTo="11.0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:thumbHeight="48dp"
app:trackCornerSize="12dp"
app:trackHeight="40dp"
app:trackIconActive="@drawable/baseline_music_note_24"
app:trackIconActiveColor="@color/m3_slider_active_tick_marks_color"
app:trackIconActiveSize="20dp"
app:trackIconInactive="@drawable/baseline_music_off_24"
app:trackIconInactiveColor="@color/m3_slider_inactive_tick_marks_color"
app:trackIconInactiveSize="20dp" />

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="128dp"
android:checked="true"
android:text="@string/cat_slider_vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<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="Title for the Slider with different label behaviors demo [CHAR LIMIT=NONE]">Slider label behavior demo</string>
<string name="cat_slider_demo_corner_title" description="Title for the Slider with different corner sizes demo [CHAR LIMIT=NONE]">Slider custom corners demo</string>
<string name="cat_slider_demo_vertical_title" description="Title for the vertical Slider demo [CHAR LIMIT=NONE]">Slider vertical 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 @@ -30,7 +31,8 @@
Discrete sliders allow users to select a specific value from a range.
</string>

<string name="cat_slider_enabled" description="">Enabled</string>
<string name="cat_slider_enabled" description="The label for a switch controlling enabled state[CHAR LIMIT=NONE]">Enabled</string>
<string name="cat_slider_vertical" description="The label for a switch controlling vertical orientation [CHAR LIMIT=NONE]">Vertical</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>
Expand Down
5 changes: 5 additions & 0 deletions docs/components/Slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ slider also has tick marks.

| Element | Attribute | Related method(s) | Default value |
|--------------------------------------------|------------------------------|--------------------------------------------------------------------------------------|--------------------------------------|
| **Orientation** | `android:orientation` | `setOrientation`<br/>`isVertical` | `horizontal` |
| **Min value** | `android:valueFrom` | `setValueFrom`<br/>`getValueFrom` | N/A |
| **Max value** | `android:valueTo` | `setValueTo`<br/>`getValueTo` | N/A |
| **Step size (discrete)** | `android:stepSize` | `setStepSize`<br/>`getStepSize` | N/A |
Expand Down Expand Up @@ -321,6 +322,10 @@ thing.
**Note:** `app:trackStopIndicatorSize` takes precedence over
`app:tickRadiusActive` and `app:tickRadiusInactive`.

**Note:** `vertical` orientation still uses `height` in the same way as for
`horizontal` orientation. In this context, `height` can be seen as track
thickness.

#### Thumb attributes

| Element | Attribute | Related method(s) | Default value |
Expand Down
Loading

0 comments on commit 5bcda8a

Please # to comment.