Showing posts with label sliding. Show all posts
Showing posts with label sliding. Show all posts

Monday, 13 May 2013

Android Sliding menu like facebook,gmail,google+,youtube

 Now a days most of the android application uses sliding menu. Many good apps like facebook, gmail,google+,youtube are using this type of menu.

To integrate sliding menu you need to use third party library. If you want to use actionbar in older version of android then sherlock library is also required.

Today I am going to explain how to use these library for sliding menu in your android application.

Output of the project is shown in below screenshot.






 You can download libraries and source code from following links.
      Sherlock Libraray
      Slidingmenu Library
      Project Source



activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Swipe left"
        android:textStyle="bold" />

</RelativeLayout>

menu.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/frag_menu"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.ketan.slidingexample.Menufragment" />

</RelativeLayout>

menulist.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="#000"
        android:dividerHeight="1dp"
        android:entries="@array/marray" />

</RelativeLayout>

 MainActivity.java

package com.ketan.slidingexample;

import android.os.Bundle;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;

public class MainActivity extends SherlockFragmentActivity implements Menufragment.MenuClickInterFace{
    SlidingMenu menu;
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text=(TextView)findViewById(R.id.text);
        ActionBar ab = getSupportActionBar();
        ab.setHomeButtonEnabled(true);
        ab.setDisplayHomeAsUpEnabled(true);
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.menu);
        menu.setSlidingEnabled(true);
       
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        menu.toggle();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onListitemClick(String item) {
        // TODO Auto-generated method stub
        text.setText(item);
    }
}


MenuFragment.java

package com.ketan.slidingexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;

public class Menufragment extends SherlockFragment {
    ListView list;
    MenuClickInterFace mClick;

    interface MenuClickInterFace {
        void onListitemClick(String item);
    }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        mClick = (MenuClickInterFace) activity;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        list = (ListView) getView().findViewById(R.id.list);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                String i=(String) arg0.getItemAtPosition(arg2);
                mClick.onListitemClick(i);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.menulist, container, false);
        return v;
    }

}




Again download libraries and source code from following links and Njoy!!!
      Sherlock Libraray
      Slidingmenu Library
      Project Source