Introduction

This article explains how to use a SlidingDrawer in your activity. A SlidingDrawer hides the content from the screen and allows the user to bring content onto the screen. You can use a sliding drawer vertically or horizontally or both. A SlidingDrawer can only be used inside a RelativeLayout and FrameLayout. In the XML file, you will use a SlidingDrawer with one button to handle the layout and an imageview that you want to drag.
In a Java class file, you will set the SlidingDrawer on setOnDarwablwCloseListener that closes the sliding drawer when you will click on the handle button. Inside the onDrawableClose you will set the handle button on the setBackgroundResource that sets the background resource on button click. After this, to open the sliding drawer you will set the SlidingDawer on setDawableOpenListener that opens the drawer when you will click on the handle button.
Step 1
Create an XML file and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity"
  10. android:background="#d1e7df">
  11. <SlidingDrawer
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. android:content="@+id/content"
  15. android:handle="@+id/handle" android:id="@+id/slidingDrawer"
  16. android:layout_centerVertical="true"
  17. android:layout_centerHorizontal="true">
  18. <Button
  19. android:id="@+id/handle"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:background="@drawable/up"/>
  23. <LinearLayout
  24. android:id="@+id/content"
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:orientation="vertical">
  28. <ImageView
  29. android:layout_height="fill_parent"
  30. android:layout_width="fill_parent"
  31. android:background="@drawable/img"
  32. >
  33. </ImageView>
  34. </LinearLayout>
  35. </SlidingDrawer>
  36. </RelativeLayout>
Step 2
Create a Java class file and write this:
  1. package com.slidingdrawerexample;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.SlidingDrawer;
  6. public class MainActivity extends Activity {
  7. SlidingDrawer sliding;
  8. Button button;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. sliding=(SlidingDrawer)findViewById(R.id.slidingDrawer);
  14. button=(Button)findViewById(R.id.handle);
  15. sliding.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
  16. @Override
  17. public void onDrawerClosed() {
  18. button.setBackgroundResource(R.drawable.down);
  19. }
  20. });
  21. sliding.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
  22. @Override
  23. public void onDrawerOpened() {
  24. button.setBackgroundResource(R.drawable.up);
  25. }
  26. });
  27. }
  28. }
Step 3
Android Menifest file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.slidingdrawerexample"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="17" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.slidingdrawerexample.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 4
Output Screen
sl1.jpg
Step 5
When you slide the button:
sl2.jpg