Thursday, 27 March 2014

Simple SlidingDrawer Functionality in Android Application Development

This blog described about most popular android feature sliding between two screen using tab or menu list icon. This feature can performed by using SlidingDrawer widget in android app it can be slide manually also.
Below is the simple code of SlidingDrawer functionality.

main.xml

<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:orientation="vertical"   
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sliding Drawer"
        android:textColor="#336633"
        android:layout_marginTop="20dp"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:textStyle="bold" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        >
       
        <SlidingDrawer
        android:id="@+id/slidingDrawer1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:layout_marginBottom="5dp">

        <Button
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="->" />

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="   C" />

            <CheckBox
                android:id="@+id/checkBox2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text=" C++" />

            <CheckBox
                android:id="@+id/checkBox3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Java" />

            <CheckBox
                android:id="@+id/checkBox4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text=".Net" />

            <CheckBox
                android:id="@+id/checkBox5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text=" PHP" />

            <CheckBox
                android:id="@+id/checkBox6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Ruby" />

            <CheckBox
                android:id="@+id/checkBox7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Python" />
           
        </LinearLayout>
    </SlidingDrawer>
       
    </LinearLayout>
</LinearLayout>


SlidingDrawerActivity.java

package com.rakesh.tiwari.slidingdrawer;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

public class SlidingDrawerActivity extends Activity {
    SlidingDrawer slidDrawer;
    Button slidBtn;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        slidDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
        slidBtn = (Button) findViewById(R.id.handle);
        slidDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {

            @Override
            public void onDrawerOpened() {
                // TODO Auto-generated method stub

                slidBtn.setText("<-");
            }
        });
        slidDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {

            @Override
            public void onDrawerClosed() {
                // TODO Auto-generated method stub

                slidBtn.setText("->");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sliding_drawer, menu);
        return true;
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.slidingdrawer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rakesh.tiwari.slidingdrawer.SlidingDrawerActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


SlidingDrawer App Development
SlidingDrawer App Development

SlidingDrawer Functionality
SlidingDrawer Functionality

SlidingDrawer Widget
SlidingDrawer Widget

SlidingDrawer Feature
SlidingDrawer Feature