Implementing a Sliding Drawer with Android SlidingDrawer

The effect shown above (commonly seen in many apps) can be achieved using SlidingDrawer. Introduced in Android 1.5, the android.widget.SlidingDrawer class provides a simple way to create sliding drawer UI components.

SlidingDrawer Attributes

  • android:allowSingleTap: Indicates whether the drawer can be opened or closed by clicking the handle.
  • android:animateOnClick: Determines whether an animation plays when the handle is pressed to open/close the drawer.
  • android:content: The view that contains the hidden contant.
  • android:handle: The view that acts as the handle for the drawer.

Layout Example

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/f">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal" />

    <SlidingDrawer
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="vertical">

        <ImageView
            android:id="@id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/music_list_btn" />

        <LinearLayout
            android:id="@id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/t">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Hidden content" />

        </LinearLayout>

    </SlidingDrawer>

</LinearLayout>

Programmatic Control

The SlidingDrawer class also provides methods to listen for drawer events:

SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.drawer);

drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
    public void onDrawerOpened() {
        // Drawer is now open
    }
});

drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
    public void onDrawerClosed() {
        // Drawer is now closed
    }
});

drawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {
    public void onScrollStarted() {
        // Scrolling started
    }
    public void onScrollEnded() {
        // Scrolling ended
    }
});

With just the layout XML, you can achieve the sliding drawer effect as shown in the image. The event listeners allow you to react to drawer state changes and animations.

Tags: Android SlidingDrawer UI Android Development

Posted on Mon, 17 Aug 2026 16:07:36 +0000 by gudushen