flipkart

Friday, November 7, 2014

Navigation Drawer in Android

Navigation Drawer is list of layout but it most time always hidden when user swipe left edge side to right it will come and when user will touch it will hidden based on user selecting it will open information.

Now i will explain Navigation Drawer with example one by one file i will explain

activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ListView android:id="@+id/listview1"
        android:layout_width="140dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:background="#244848"/>
</android.support.v4.widget.DrawerLayout>

ListView will show name of the pages and FrameLayout is information of selected one nothing but fragment.

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:background="#488844"

    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40px"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:id="@+id/textview1"/>
</LinearLayout>

When ever select one it will open new fragment that fragment consists currently only one textview .

MainActivity.java

package com.vamsi.navidrawer;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
       String[] menu;
       DrawerLayout dLayout;
       ListView dList;
       ArrayAdapter<String> adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        menu = new String[]{"BALL","BAT","CUP","CRICKET","WICKET","GROUND","OUT","WIN"};
          dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
          dList = (ListView) findViewById(R.id.listview1);
          adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
          dList.setAdapter(adapter);
      dList.setSelector(android.R.color.darker_gray);
          dList.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
          dLayout.closeDrawers();
          Bundle args = new Bundle();
          args.putString("details", menu[position]);
          Fragment detail = new Fragment1();
          detail.setArguments(args);
            FragmentManager fragmentManager = getFragmentManager();
          fragmentManager.beginTransaction().replace(R.id.framelayout, detail).commit();
        }
          });
  }

}

This is my mainactivity of project this having initialization of elements and other stuff

Fragment1.java 

package com.vamsi.navidrawer;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment1 extends Fragment {
    TextView text;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
        View view = inflater.inflate(R.layout.fragment1, container, false);
        String menu = getArguments().getString("details");
        text= (TextView) view.findViewById(R.id.textview1);
        text.setText(menu);
        return view;
    }

}

This class is user when ever user click it open this fragment 

following screen-shots above project 


                When ever user swipe left to right following screen will come


                                      when ever user select based it show following fragment





Thank you studying my post please put comments it will be very use full for me 

No comments:

Post a Comment