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