SlidingDrawer


public class SlidingDrawer
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.SlidingDrawer


This class was deprecated in API level 17.
This class is not supported anymore. It is recommended you base your own implementation on the source code for the Android Open Source Project if you must use it in your application.

SlidingDrawer hides content out of the screen and allows the user to drag a handle to bring the content on screen. SlidingDrawer can be used vertically or horizontally. A special widget composed of two children views: the handle, that the users drags, and the content, attached to the handle and dragged with it. SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer should only be used inside of a FrameLayout or a RelativeLayout for instance. The size of the SlidingDrawer defines how much space the content will occupy once slid out so SlidingDrawer should usually use match_parent for both its dimensions. Inside an XML layout, SlidingDrawer must define the id of the handle and of the content:

 <SlidingDrawer
     android:id="@+id/drawer"
     android:layout_width="match_parent"
     android:layout_height="match_parent"

     android:handle="@+id/handle"
     android:content="@+id/content">

     <ImageView
         android:id="@id/handle"
         android:layout_width="88dip"
         android:layout_height="44dip" />

     <GridView
         android:id="@id/content"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

 </SlidingDrawer>
 

Summary

Nested classes

interface SlidingDrawer.OnDrawerCloseListener

Callback invoked when the drawer is closed. 

interface SlidingDrawer.OnDrawerOpenListener

Callback invoked when the drawer is opened. 

interface SlidingDrawer.OnDrawerScrollListener

Callback invoked when the drawer is scrolled. 

XML attributes

android:allowSingleTap Indicates whether the drawer can be opened/closed by a single tap on the handle. 
android:animateOnClick Indicates whether the drawer should be opened/closed with an animation when the user clicks the handle. 
android:bottomOffset Extra offset for the handle at the bottom of the SlidingDrawer. 
android:content Identifier for the child that represents the drawer's content. 
android:handle Identifier for the child that represents the drawer's handle. 
android:orientation Orientation of the SlidingDrawer. 
android:topOffset Extra offset for the handle at the top of the SlidingDrawer. 

Inherited XML attributes

Constants

int ORIENTATION_HORIZONTAL

int ORIENTATION_VERTICAL

Inherited constants

Inherited fields

Public constructors

SlidingDrawer(Context context, AttributeSet attrs)

Creates a new SlidingDrawer from a specified set of attributes defined in XML.

SlidingDrawer(Context context, AttributeSet attrs, int defStyleAttr)

Creates a new SlidingDrawer from a specified set of attributes defined in XML.

SlidingDrawer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Creates a new SlidingDrawer from a specified set of attributes defined in XML.

Public methods

void animateClose()

Closes the drawer with an animation.

void animateOpen()

Opens the drawer with an animation.

void animateToggle()

Toggles the drawer open and close with an animation.

void close()

Closes the drawer immediately.

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

View getContent()

Returns the content of the drawer.

View getHandle()

Returns the handle of the drawer.

boolean isMoving()

Indicates whether the drawer is scrolling or flinging.

boolean isOpened()

Indicates whether the drawer is currently fully opened.

void lock()

Locks the SlidingDrawer so that touch events are ignores.

boolean onInterceptTouchEvent(MotionEvent event)

Implement this method to intercept all touch screen motion events.

boolean onTouchEvent(MotionEvent event)

Implement this method to handle touch screen motion events.

void open()

Opens the drawer immediately.

void setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)

Sets the listener that receives a notification when the drawer becomes close.

void setOnDrawerOpenListener(SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener)

Sets the listener that receives a notification when the drawer becomes open.

void setOnDrawerScrollListener(SlidingDrawer.OnDrawerScrollListener onDrawerScrollListener)

Sets the listener that receives a notification when the drawer starts or ends a scroll.

void toggle()

Toggles the drawer open and close.

void unlock()

Unlocks the SlidingDrawer so that touch events are processed.

Protected methods

void dispatchDraw(Canvas canvas)

Called by draw to draw the child views.

void onFinishInflate()

Finalize inflating a view from XML.

void onLayout(boolean changed, int l, int t, int r, int b)

Called from layout when this view should assign a size and position to each of its children.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

Inherited methods

XML attributes

android:allowSingleTap

Indicates whether the drawer can be opened/closed by a single tap on the handle. (If false, the user must drag or fling, or click using the trackball, to open/close the drawer.) Default is true.

May be a boolean value, such as "true" or "false".

android:animateOnClick

Indicates whether the drawer should be opened/closed with an animation when the user clicks the handle. Default is true.

May be a boolean value, such as "true" or "false".

android:bottomOffset

Extra offset for the handle at the bottom of the SlidingDrawer.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:content

Identifier for the child that represents the drawer's content.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:handle

Identifier for the child that represents the drawer's handle.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:orientation

Orientation of the SlidingDrawer.

Must be one of the following constant values.

ConstantValueDescription
horizontal0Defines an horizontal widget.
vertical1Defines a vertical widget.

android:topOffset

Extra offset for the handle at the top of the SlidingDrawer.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Constants

ORIENTATION_HORIZONTAL

Added in API level 3
public static final int ORIENTATION_HORIZONTAL

Constant Value: 0 (0x00000000)

ORIENTATION_VERTICAL

Added in API level 3
public static final int ORIENTATION_VERTICAL

Constant Value: 1 (0x00000001)

Public constructors

SlidingDrawer

Added in API level 3
public SlidingDrawer (Context context, 
                AttributeSet attrs)

Creates a new SlidingDrawer from a specified set of attributes defined in XML.

Parameters
context Context: The application's environment.

attrs AttributeSet: The attributes defined in XML.

SlidingDrawer

Added in API level 3
public SlidingDrawer (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Creates a new SlidingDrawer from a specified set of attributes defined in XML.

Parameters
context Context: The application's environment.

attrs AttributeSet: The attributes defined in XML.

defStyleAttr int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

SlidingDrawer

Added in API level 21
public SlidingDrawer (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Creates a new SlidingDrawer from a specified set of attributes defined in XML.

Parameters
context Context: The application's environment.

attrs AttributeSet: The attributes defined in XML.

defStyleAttr int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

defStyleRes int: A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

Public methods

animateClose

Added in API level 3
public void animateClose ()

Closes the drawer with an animation.

animateOpen

Added in API level 3
public void animateOpen ()

Opens the drawer with an animation.

animateToggle

Added in API level 3
public void animateToggle ()

Toggles the drawer open and close with an animation.

close

Added in API level 3
public void close ()

Closes the drawer immediately.

getAccessibilityClassName

Added in API level 23
Deprecated in API level 17
public CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getContent

Added in API level 3
public View getContent ()

Returns the content of the drawer.

Returns
View The View reprenseting the content of the drawer, identified by the "content" id in XML.

getHandle

Added in API level 3
public View getHandle ()

Returns the handle of the drawer.

Returns
View The View reprenseting the handle of the drawer, identified by the "handle" id in XML.

isMoving

Added in API level 3
public boolean isMoving ()

Indicates whether the drawer is scrolling or flinging.

Returns
boolean True if the drawer is scroller or flinging, false otherwise.

isOpened

Added in API level 3
public boolean isOpened ()

Indicates whether the drawer is currently fully opened.

Returns
boolean True if the drawer is opened, false otherwise.

lock

Added in API level 3
public void lock ()

Locks the SlidingDrawer so that touch events are ignores.

See also:

onInterceptTouchEvent

Added in API level 3
public boolean onInterceptTouchEvent (MotionEvent event)

Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.

Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action MotionEvent#ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.

Parameters
event MotionEvent: The motion event being dispatched down the hierarchy.

Returns
boolean Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

onTouchEvent

Added in API level 3
public boolean onTouchEvent (MotionEvent event)

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
event MotionEvent: The motion event.

Returns
boolean True if the event was handled, false otherwise.

open

Added in API level 3
public void open ()

Opens the drawer immediately.

setOnDrawerCloseListener

Added in API level 3
public void setOnDrawerCloseListener (SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)

Sets the listener that receives a notification when the drawer becomes close.

Parameters
onDrawerCloseListener SlidingDrawer.OnDrawerCloseListener: The listener to be notified when the drawer is closed.

setOnDrawerOpenListener

Added in API level 3
public void setOnDrawerOpenListener (SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener)

Sets the listener that receives a notification when the drawer becomes open.

Parameters
onDrawerOpenListener SlidingDrawer.OnDrawerOpenListener: The listener to be notified when the drawer is opened.

setOnDrawerScrollListener

Added in API level 3
public void setOnDrawerScrollListener (SlidingDrawer.OnDrawerScrollListener onDrawerScrollListener)

Sets the listener that receives a notification when the drawer starts or ends a scroll. A fling is considered as a scroll. A fling will also trigger a drawer opened or drawer closed event.

Parameters
onDrawerScrollListener SlidingDrawer.OnDrawerScrollListener: The listener to be notified when scrolling starts or stops.

toggle

Added in API level 3
public void toggle ()

Toggles the drawer open and close. Takes effect immediately.

unlock

Added in API level 3
public void unlock ()

Unlocks the SlidingDrawer so that touch events are processed.

See also:

Protected methods

dispatchDraw

Added in API level 3
protected void dispatchDraw (Canvas canvas)

Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

Parameters
canvas Canvas: This value cannot be null.

onFinishInflate

Added in API level 3
protected void onFinishInflate ()

Finalize inflating a view from XML. This is called as the last phase of inflation, after all child views have been added.

Even if the subclass overrides onFinishInflate, they should always be sure to call the super method, so that we get called.
If you override this method you must call through to the superclass implementation.

onLayout

Added in API level 3
protected void onLayout (boolean changed, 
                int l, 
                int t, 
                int r, 
                int b)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed boolean: This is a new size or position for this view

l int: Left position, relative to parent

t int: Top position, relative to parent

r int: Right position, relative to parent

b int: Bottom position, relative to parent

onMeasure

Added in API level 3
protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.