Skip to content Skip to sidebar Skip to footer

How To Create A Navigation Drawer On A Fragment In Android

I need some suggestions in my design. I have an Activity class that loads multiple fragments. One of the fragment has Navigation drawer. When the user clicks any of the items in th

Solution 1:

You will have to make a class which is having a navigation drawer in it and then you can extend the particular class wherever you want. if you want it on fragments just extend that particular activity holding those fragments with that class with navigation drawer.

Solution 2:

Use following code, activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"android:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" >

<FrameLayout
android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent" >

<ListView
android:id="@+id/drawer"android:layout_width="240dp"android:layout_height="match_parent"android:layout_gravity="start"android:background="#FFF"android:choiceMode="singleChoice"/>
</LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
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;

publicclassMainActivityextendsFragmentActivity {
    final String[] data ={"one","two","three"};
    final String[] fragments ={
            "Your.package.Name.FragmentOne",
            "Your.package.Name.FragmentTwo",
            "Your.package.Name.FragmentThree"};
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         ArrayAdapteradapter=newArrayAdapter(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

         finalDrawerLayoutdrawer= (DrawerLayout)findViewById(R.id.drawer_layout);
         finalListViewnavList= (ListView) findViewById(R.id.drawer);
         navList.setAdapter(adapter);
         navList.setOnItemClickListener(newOnItemClickListener(){
                 @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, finalint pos,long id){
                         drawer.setDrawerListener( newDrawerLayout.SimpleDrawerListener(){
                                 @OverridepublicvoidonDrawerClosed(View drawerView){
                                         super.onDrawerClosed(drawerView);
                                         FragmentTransactiontx= getSupportFragmentManager().beginTransaction();
                                         tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
                                         tx.commit();
                                 }
                         });
                         drawer.closeDrawer(navList);
                 }
         });
         FragmentTransactiontx= getSupportFragmentManager().beginTransaction();
         tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
         tx.commit();
    }

}

And make your layouts for fragments in this case FragmentOne, FragmentTwo, FragmentThree

Solution 3:

You will have to make a class which is having a navigation drawer in it and then you can extend the particular class wherever you want .... if u want it on fragments then just extend that particular activity holding those fragments with that class with navigation drawer.

No,the activity on which you are replacing fragments should have navigation drawer.that is it.

Post a Comment for "How To Create A Navigation Drawer On A Fragment In Android"