Detect sliding drawer is open or closed in android

Creating toast message notification to display drawer opening & closing status.

Sliding drawer gives us two different type of functions first one is setOnDrawerOpenListener() to do any particular task on sliding drawer open event like display a toast message that sliding drawer is open successfully etc. The second function is setOnDrawerCloseListener() also applied to do some particular task on sliding drawer closing event. So here is the complete step by step tutorial for Detect sliding drawer is open or closed in android.

android-project-download-code-button

How to Detect sliding drawer is open or closed in android.

Note: There are two images i am using in this project colorful_arrow_icon.png and sliding_layout_image.png so download these image from below and put these images into res->drawable-hdpi folder.

drawable hdpi folder

Images to download :

colorful_arrow_icon

sliding_layout_image
Code for MainActivity.java file.

 package com.slidingdraweropenclose_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Toast;


public class MainActivity extends Activity {

 SlidingDrawer SD;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SD = (SlidingDrawer)findViewById(R.id.drawer);
 
 SD.setOnDrawerOpenListener(new OnDrawerOpenListener() {
 
 @Override
 public void onDrawerOpened() {
 
 Toast.makeText(MainActivity.this, "Sliding drawer open", Toast.LENGTH_LONG).show();
 
 }
 });
 
 SD.setOnDrawerCloseListener(new OnDrawerCloseListener() {
 
 public void onDrawerClosed() {
 
 Toast.makeText(MainActivity.this, "Sliding drawer close", Toast.LENGTH_LONG).show();
 
 }
 
 });
 
 }
 }

Code for activity_main.xml layout file.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.slidingdraweropenclose_android_examples.com.MainActivity"
 android:orientation="vertical" >

 <SlidingDrawer
 android:id="@+id/drawer"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:handle="@+id/handle"
 android:rotation="180"
 android:content="@+id/content">


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

 <LinearLayout
 android:id="@id/content"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:rotation="180"
 android:orientation="horizontal">
 
<!-- DEFINE ALL YOUR WIDGETS HERE TO ADD THEM IN SLIDING DRAWER LAYOUT. -->
 
 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:src="@drawable/sliding_layout_image"
 />
 
<!-- DEFINE ALL YOUR WIDGETS HERE TO ADD THEM IN SLIDING DRAWER LAYOUT. -->
 
 </LinearLayout>
 </SlidingDrawer>
</LinearLayout>

Screenshots:

Detect sliding drawer is open or closed in android

sliding drawer closed

Click Here To Download Detect sliding drawer is open or closed in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *