1 /* 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 17 package android.widget; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.accessibility.AccessibilityNodeInfo; 22 23 24 /** 25 * A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch 26 * the thumb and drag left or right to set the current progress level or use the arrow keys. 27 * Placing focusable widgets to the left or right of a SeekBar is discouraged. 28 * <p> 29 * Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to 30 * be notified of the user's actions. 31 * 32 * @attr ref android.R.styleable#SeekBar_thumb 33 */ 34 public class SeekBar extends AbsSeekBar { 35 36 /** 37 * A callback that notifies clients when the progress level has been 38 * changed. This includes changes that were initiated by the user through a 39 * touch gesture or arrow key/trackball as well as changes that were initiated 40 * programmatically. 41 */ 42 public interface OnSeekBarChangeListener { 43 44 /** 45 * Notification that the progress level has changed. Clients can use the fromUser parameter 46 * to distinguish user-initiated changes from those that occurred programmatically. 47 * 48 * @param seekBar The SeekBar whose progress has changed 49 * @param progress The current progress level. This will be in the range 0..max where max 50 * was set by {@link ProgressBar#setMax(int)}. (The default value for max is 100.) 51 * @param fromUser True if the progress change was initiated by the user. 52 */ onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)53 void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser); 54 55 /** 56 * Notification that the user has started a touch gesture. Clients may want to use this 57 * to disable advancing the seekbar. 58 * @param seekBar The SeekBar in which the touch gesture began 59 */ onStartTrackingTouch(SeekBar seekBar)60 void onStartTrackingTouch(SeekBar seekBar); 61 62 /** 63 * Notification that the user has finished a touch gesture. Clients may want to use this 64 * to re-enable advancing the seekbar. 65 * @param seekBar The SeekBar in which the touch gesture began 66 */ onStopTrackingTouch(SeekBar seekBar)67 void onStopTrackingTouch(SeekBar seekBar); 68 } 69 70 private OnSeekBarChangeListener mOnSeekBarChangeListener; 71 SeekBar(Context context)72 public SeekBar(Context context) { 73 this(context, null); 74 } 75 SeekBar(Context context, AttributeSet attrs)76 public SeekBar(Context context, AttributeSet attrs) { 77 this(context, attrs, com.android.internal.R.attr.seekBarStyle); 78 } 79 SeekBar(Context context, AttributeSet attrs, int defStyleAttr)80 public SeekBar(Context context, AttributeSet attrs, int defStyleAttr) { 81 this(context, attrs, defStyleAttr, 0); 82 } 83 SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)84 public SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 85 super(context, attrs, defStyleAttr, defStyleRes); 86 } 87 88 @Override onProgressRefresh(float scale, boolean fromUser, int progress)89 void onProgressRefresh(float scale, boolean fromUser, int progress) { 90 super.onProgressRefresh(scale, fromUser, progress); 91 92 if (mOnSeekBarChangeListener != null) { 93 mOnSeekBarChangeListener.onProgressChanged(this, progress, fromUser); 94 } 95 } 96 97 /** 98 * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also 99 * provides notifications of when the user starts and stops a touch gesture within the SeekBar. 100 * 101 * @param l The seek bar notification listener 102 * 103 * @see SeekBar.OnSeekBarChangeListener 104 */ setOnSeekBarChangeListener(OnSeekBarChangeListener l)105 public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) { 106 mOnSeekBarChangeListener = l; 107 } 108 109 @Override onStartTrackingTouch()110 void onStartTrackingTouch() { 111 super.onStartTrackingTouch(); 112 if (mOnSeekBarChangeListener != null) { 113 mOnSeekBarChangeListener.onStartTrackingTouch(this); 114 } 115 } 116 117 @Override onStopTrackingTouch()118 void onStopTrackingTouch() { 119 super.onStopTrackingTouch(); 120 if (mOnSeekBarChangeListener != null) { 121 mOnSeekBarChangeListener.onStopTrackingTouch(this); 122 } 123 } 124 125 @Override getAccessibilityClassName()126 public CharSequence getAccessibilityClassName() { 127 return SeekBar.class.getName(); 128 } 129 130 /** @hide */ 131 @Override onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info)132 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { 133 super.onInitializeAccessibilityNodeInfoInternal(info); 134 135 if (canUserSetProgress()) { 136 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS); 137 } 138 } 139 } 140