blob: a6486a827af5f2a7f8c98e624d652a3e929e008f [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
19import android.content.Context;
20import android.util.AttributeSet;
Philip Milne989709a2012-06-22 16:09:04 -070021import android.util.ValueModel;
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -080022import android.view.accessibility.AccessibilityEvent;
23import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25
26
27/**
28 * A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch
29 * the thumb and drag left or right to set the current progress level or use the arrow keys.
30 * Placing focusable widgets to the left or right of a SeekBar is discouraged.
31 * <p>
32 * Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to
33 * be notified of the user's actions.
34 *
35 * @attr ref android.R.styleable#SeekBar_thumb
36 */
Philip Milne989709a2012-06-22 16:09:04 -070037public class SeekBar extends AbsSeekBar implements ValueEditor<Integer> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39 /**
40 * A callback that notifies clients when the progress level has been
41 * changed. This includes changes that were initiated by the user through a
42 * touch gesture or arrow key/trackball as well as changes that were initiated
43 * programmatically.
44 */
45 public interface OnSeekBarChangeListener {
46
47 /**
48 * Notification that the progress level has changed. Clients can use the fromUser parameter
49 * to distinguish user-initiated changes from those that occurred programmatically.
50 *
51 * @param seekBar The SeekBar whose progress has changed
52 * @param progress The current progress level. This will be in the range 0..max where max
53 * was set by {@link ProgressBar#setMax(int)}. (The default value for max is 100.)
54 * @param fromUser True if the progress change was initiated by the user.
55 */
56 void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);
57
58 /**
59 * Notification that the user has started a touch gesture. Clients may want to use this
60 * to disable advancing the seekbar.
61 * @param seekBar The SeekBar in which the touch gesture began
62 */
63 void onStartTrackingTouch(SeekBar seekBar);
64
65 /**
66 * Notification that the user has finished a touch gesture. Clients may want to use this
67 * to re-enable advancing the seekbar.
68 * @param seekBar The SeekBar in which the touch gesture began
69 */
70 void onStopTrackingTouch(SeekBar seekBar);
71 }
72
Philip Milne989709a2012-06-22 16:09:04 -070073 private ValueModel<Integer> mValueModel = ValueModel.EMPTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private OnSeekBarChangeListener mOnSeekBarChangeListener;
Philip Milne989709a2012-06-22 16:09:04 -070075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 public SeekBar(Context context) {
77 this(context, null);
78 }
79
80 public SeekBar(Context context, AttributeSet attrs) {
81 this(context, attrs, com.android.internal.R.attr.seekBarStyle);
82 }
83
84 public SeekBar(Context context, AttributeSet attrs, int defStyle) {
85 super(context, attrs, defStyle);
86 }
87
88 @Override
89 void onProgressRefresh(float scale, boolean fromUser) {
90 super.onProgressRefresh(scale, fromUser);
91
92 if (mOnSeekBarChangeListener != null) {
93 mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser);
Philip Milne989709a2012-06-22 16:09:04 -070094 if (fromUser) {
95 mValueModel.set(getProgress());
96 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
98 }
99
Philip Milne989709a2012-06-22 16:09:04 -0700100 @Override
101 public ValueModel<Integer> getValueModel() {
102 return mValueModel;
103 }
104
105 @Override
106 public void setValueModel(ValueModel<Integer> valueModel) {
107 mValueModel = valueModel;
108 setProgress(mValueModel.get());
109 }
110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
112 * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also
113 * provides notifications of when the user starts and stops a touch gesture within the SeekBar.
114 *
115 * @param l The seek bar notification listener
116 *
117 * @see SeekBar.OnSeekBarChangeListener
118 */
119 public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
120 mOnSeekBarChangeListener = l;
121 }
122
123 @Override
124 void onStartTrackingTouch() {
Adam Powell10298662011-08-14 18:26:30 -0700125 super.onStartTrackingTouch();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 if (mOnSeekBarChangeListener != null) {
127 mOnSeekBarChangeListener.onStartTrackingTouch(this);
128 }
129 }
130
131 @Override
132 void onStopTrackingTouch() {
Adam Powell10298662011-08-14 18:26:30 -0700133 super.onStopTrackingTouch();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 if (mOnSeekBarChangeListener != null) {
135 mOnSeekBarChangeListener.onStopTrackingTouch(this);
136 }
137 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800138
139 @Override
140 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
141 super.onInitializeAccessibilityEvent(event);
142 event.setClassName(SeekBar.class.getName());
143 }
144
145 @Override
146 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
147 super.onInitializeAccessibilityNodeInfo(info);
148 info.setClassName(SeekBar.class.getName());
149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150}