Sliding Drawer in Anroid Studio

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.    
  12.    
  13.     <SlidingDrawer  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="fill_parent"  
  16.             android:content="@+id/content"  
  17.             android:handle="@+id/handle" android:id="@+id/slidingDrawer"  
  18.             android:layout_centerVertical="true"  
  19.             android:layout_centerHorizontal="true">  
  20.    
  21.         <Button  
  22.                 android:id="@+id/handle"  
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"  
  25.    
  26.                 android:background="@drawable/up"/>  
  27.    
  28.         <LinearLayout  
  29.                 android:id="@+id/content"  
  30.                 android:layout_width="fill_parent"  
  31.                 android:layout_height="fill_parent"  
  32.                 android:orientation="vertical">  
  33.    
  34.           <ImageView  
  35.                   android:layout_height="fill_parent"  
  36.                   android:layout_width="fill_parent"  
  37.                   android:background="@drawable/img"  
  38.                  >  
  39.           </ImageView>  
  40.         </LinearLayout>  
  41.     </SlidingDrawer>   
  42. </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.    
  7. public class MainActivity extends Activity {  
  8.    
  9.   SlidingDrawer sliding;  
  10.     Button button;  
  11.    
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.    
  17.         sliding=(SlidingDrawer)findViewById(R.id.slidingDrawer);  
  18.         button=(Button)findViewById(R.id.handle);  
  19.    
  20.         sliding.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {  
  21.             @Override  
  22.             public void onDrawerClosed() {  
  23.                 button.setBackgroundResource(R.drawable.down);  
  24.             }  
  25.         });  
  26.    
  27.     sliding.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {  
  28.         @Override  
  29.         public void onDrawerOpened() {  
  30.             button.setBackgroundResource(R.drawable.up);  
  31.         }  
  32.     });  
  33.     }  
  34.  } 
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.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="17" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.slidingdrawerexample.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.    
  27. </manifest> 
Step 4
 
Output Screen
 
sl1.jpg
 
Step 5
 
When you slide the button:
 
sl2.jpg


Similar Articles