Monday, June 4, 2012

Android SlidingDrawer Example




SlidingDrawer hides content out of the screen and allows the user to drag a handle to bring the content on screen. SlidingDrawer can be used vertically or horizontally. A special widget composed of two children views: the handle, that the users drags, and the content, attached to the handle and dragged with it. SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer should only be used inside of a FrameLayout or a RelativeLayout for instance. The size of the SlidingDrawer defines how much space the content will occupy once slid out so SlidingDrawer should usually use match_parent for both its dimensions. Inside an XML layout, SlidingDrawer must define the id of the handle and of the content.
Two important methods of SlidingDrawer :

In res/layout/main.xm layout add SlidingDrawer View as :
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/drawer" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    android:handle="@+id/handle"  
    android:background="@drawable/a07"
    android:content="@+id/content">  
  
    <ImageView android:id="@id/handle" 
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" 
        android:src="@drawable/arrow_up">
    </ImageView>  
    <ListView android:id="@id/content" 
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" 
        android:background="#ff00C0">
    </ListView>  
  
</SlidingDrawer>


In SlidingDrawerActivity implements OnItemClickListener, OnDrawerOpenListener and OnDrawerCloseListener as:
package com.imrankhanandroid.SlidingDrawerExp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

public class SlidingDrawerActivity extends Activity implements 
OnItemClickListener,OnDrawerOpenListener,OnDrawerCloseListener {
    private SlidingDrawer drawer;  
    private ImageView handle;  
    private ListView content;  
    private String[] strcntycode={"IN","US","UK","JAP","PK","CH"};
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.
        FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        drawer = (SlidingDrawer) this.findViewById(R.id.drawer);  
        handle = (ImageView) this.findViewById(R.id.handle);  
        content = (ListView) this.findViewById(R.id.content);  
        content.setAdapter(new ArrayAdapter(this,  
                android.R.layout.simple_list_item_1, strcntycode));
        content.setOnItemClickListener(this); 
        drawer.setOnDrawerOpenListener(this);  
        drawer.setOnDrawerCloseListener(this);  
    }

	@Override
	public void onItemClick(AdapterView arg0, 
                    View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
        Toast.makeText(this, "Clicked Position " + (arg2 + 1)+"Code is :"
        +strcntycode[arg2],Toast.LENGTH_SHORT).show();  
	}

	@Override
	public void onDrawerOpened() {
		// TODO Auto-generated method stub
		handle.setImageResource(R.drawable.arrow_down);
	}

	@Override
	public void onDrawerClosed() {
		// TODO Auto-generated method stub
		handle.setImageResource(R.drawable.arrow_up);
	}
}

Download Source Code: SlidingDrawerExample









1 comments:

Anonymous said...

;
;';]lhiiiiiiiiiiiii

Post a Comment

 
Powered by Blogger