blob: 5676881cee7679d32fccc17adc0c54a1e4712d9b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17package android.widget;
18
Artur Satayeved5a6ae2019-12-10 17:47:54 +000019import android.compat.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.util.AttributeSet;
Maxim Bogatov32e59d52015-04-30 16:57:33 -070022import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24
25/**
26 * A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch
27 * the thumb and drag left or right to set the current progress level or use the arrow keys.
RoboErik5b071432015-02-11 13:52:05 -080028 * Placing focusable widgets to the left or right of a SeekBar is discouraged.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 * <p>
30 * Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to
31 * be notified of the user's actions.
32 *
33 * @attr ref android.R.styleable#SeekBar_thumb
34 */
Philip Milneab104ba2013-04-19 03:53:38 +000035public class SeekBar extends AbsSeekBar {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37 /**
38 * A callback that notifies clients when the progress level has been
39 * changed. This includes changes that were initiated by the user through a
40 * touch gesture or arrow key/trackball as well as changes that were initiated
41 * programmatically.
42 */
43 public interface OnSeekBarChangeListener {
RoboErik5b071432015-02-11 13:52:05 -080044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 /**
46 * Notification that the progress level has changed. Clients can use the fromUser parameter
47 * to distinguish user-initiated changes from those that occurred programmatically.
RoboErik5b071432015-02-11 13:52:05 -080048 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * @param seekBar The SeekBar whose progress has changed
Keyvan Amiri86fb2a22016-09-29 17:53:24 -070050 * @param progress The current progress level. This will be in the range min..max where min
51 * and max were set by {@link ProgressBar#setMin(int)} and
52 * {@link ProgressBar#setMax(int)}, respectively. (The default values for
53 * min is 0 and max is 100.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * @param fromUser True if the progress change was initiated by the user.
55 */
56 void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);
RoboErik5b071432015-02-11 13:52:05 -080057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 /**
59 * Notification that the user has started a touch gesture. Clients may want to use this
RoboErik5b071432015-02-11 13:52:05 -080060 * to disable advancing the seekbar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 * @param seekBar The SeekBar in which the touch gesture began
62 */
63 void onStartTrackingTouch(SeekBar seekBar);
RoboErik5b071432015-02-11 13:52:05 -080064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 /**
66 * Notification that the user has finished a touch gesture. Clients may want to use this
RoboErik5b071432015-02-11 13:52:05 -080067 * to re-enable advancing the seekbar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 * @param seekBar The SeekBar in which the touch gesture began
69 */
70 void onStopTrackingTouch(SeekBar seekBar);
71 }
72
Mathew Inwood978c6e22018-08-21 15:58:55 +010073 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private OnSeekBarChangeListener mOnSeekBarChangeListener;
RoboErik5b071432015-02-11 13:52:05 -080075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 public SeekBar(Context context) {
77 this(context, null);
78 }
RoboErik5b071432015-02-11 13:52:05 -080079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 public SeekBar(Context context, AttributeSet attrs) {
81 this(context, attrs, com.android.internal.R.attr.seekBarStyle);
82 }
83
Alan Viverette617feb92013-09-09 18:09:13 -070084 public SeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
85 this(context, attrs, defStyleAttr, 0);
86 }
87
88 public SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
89 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
91
92 @Override
Mathew Inwood978c6e22018-08-21 15:58:55 +010093 @UnsupportedAppUsage
RoboErik5b071432015-02-11 13:52:05 -080094 void onProgressRefresh(float scale, boolean fromUser, int progress) {
95 super.onProgressRefresh(scale, fromUser, progress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 if (mOnSeekBarChangeListener != null) {
RoboErik5b071432015-02-11 13:52:05 -080098 mOnSeekBarChangeListener.onProgressChanged(this, progress, fromUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
100 }
101
102 /**
103 * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also
104 * provides notifications of when the user starts and stops a touch gesture within the SeekBar.
RoboErik5b071432015-02-11 13:52:05 -0800105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * @param l The seek bar notification listener
RoboErik5b071432015-02-11 13:52:05 -0800107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * @see SeekBar.OnSeekBarChangeListener
109 */
110 public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
111 mOnSeekBarChangeListener = l;
112 }
RoboErik5b071432015-02-11 13:52:05 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 @Override
115 void onStartTrackingTouch() {
Adam Powell10298662011-08-14 18:26:30 -0700116 super.onStartTrackingTouch();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 if (mOnSeekBarChangeListener != null) {
118 mOnSeekBarChangeListener.onStartTrackingTouch(this);
119 }
120 }
RoboErik5b071432015-02-11 13:52:05 -0800121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 @Override
123 void onStopTrackingTouch() {
Adam Powell10298662011-08-14 18:26:30 -0700124 super.onStopTrackingTouch();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 if (mOnSeekBarChangeListener != null) {
126 mOnSeekBarChangeListener.onStopTrackingTouch(this);
127 }
128 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800129
130 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800131 public CharSequence getAccessibilityClassName() {
132 return SeekBar.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800133 }
Maxim Bogatov32e59d52015-04-30 16:57:33 -0700134
135 /** @hide */
136 @Override
137 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
138 super.onInitializeAccessibilityNodeInfoInternal(info);
139
140 if (canUserSetProgress()) {
141 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS);
142 }
143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144}