rupesh
rupesh

Reputation: 2891

Navigation drawer in all Activity

Hi all i want to put navigation drawer in all activity.

Layout file:

     <?xml version="1.0" encoding="utf-8"?>
<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/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="275dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/black"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:paddingLeft="5dp" />

</android.support.v4.widget.DrawerLayout>

In other activity i am extending this class. But this this not working only this is the drawer logo is coming in the other activity page.

Please tell what i am doing wrong here. Thanks in advance.

I find the solution. Thanks for your kind help.

Update:

Please find my answer below.

Upvotes: 0

Views: 1182

Answers (5)

sirmagid
sirmagid

Reputation: 1130

this work for me

public class MyDrawer extends AppCompatActivity {
ActionBarDrawerToggle toggle;
protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;
@Override
public void setContentView(final int layoutResID) {
    fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.mydrawer, null);
    frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);
    getLayoutInflater().inflate(layoutResID, frameLayout, true);
    super.setContentView(fullLayout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout3);
    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            drawer.closeDrawers();
            int itemId = menuItem.getItemId();
            Toast.makeText(getApplicationContext(), menuItem.getTitle().toString(),
                    Toast.LENGTH_LONG).show();
            //navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
            return true;
        }
    });
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if (toggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/drawer_framelayout">
<FrameLayout
    android:id="@+id/drawer_frame2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main_drawer"
android:background="#fefefd" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

for use:

public class yourclass extends MyDrawer {

is .setOnItemClickListener work?yes

<android.support.v4.widget.DrawerLayout>
<FrameLayout>
    your main content stuff here
</android.support.v4.widget.DrawerLayout>
<FrameLayout>
    your main content stuff here(.setOnItemClickListener)

Upvotes: 0

rupesh
rupesh

Reputation: 2891

Create a layout drawer_layout:

<?xml version="1.0" encoding="utf-8"?>
<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/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="275dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/black"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:paddingLeft="5dp" />

drwaer_custom_layout_file This is for each single row in drawer.(Customize based on requirement):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp" >

<LinearLayout
    android:id="@+id/itemLayoutColor"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/drawer_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/sku_search"
        android:paddingLeft="15dp" />

    <TextView
        android:id="@+id/drawer_itemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingLeft="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/white" />
</LinearLayout>

<View
    android:id="@+id/dividerView"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#191919"
    android:paddingLeft="15dp" >
</View>

Create an Adapter class. (Remove the element which is not relevant for you):

public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {

Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
int selectedPosition;


public CustomDrawerAdapter(Context context, int layoutResourceID,
        List<DrawerItem> listItems, int selectedPosition) {
    super(context, layoutResourceID, listItems);
    this.context = context;
    this.drawerItemList = listItems;
    this.layoutResID = layoutResourceID;
    this.selectedPosition = selectedPosition;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    DrawerItemHolder drawerHolder;
    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        drawerHolder = new DrawerItemHolder();

        view = inflater.inflate(layoutResID, parent, false);
        drawerHolder.ItemName = (TextView) view
                .findViewById(R.id.drawer_itemName);
        drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
        drawerHolder.itemLayoutColor = (LinearLayout) view
                .findViewById(R.id.itemLayoutColor);
        // drawerHolder.dividerView = (View) view
        // .findViewById(R.id.dividerView);
        view.setTag(drawerHolder);

    } else {
        drawerHolder = (DrawerItemHolder) view.getTag();

    }

    DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
    drawerHolder.ItemName.setTypeface(tfNormal);
    drawerHolder.ItemName.setText(dItem.getItemName());
    if (dItem.getImgResID() != 0) {
        drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
                dItem.getImgResID()));

    } else {

        drawerHolder.ItemName.setTextColor(context.getResources().getColor(
                R.color.black));
        drawerHolder.itemLayoutColor.setBackgroundColor(context
                .getResources().getColor(R.color.pGray));
        drawerHolder.icon.setVisibility(View.GONE);
        // drawerHolder.dividerView.setBackgroundColor(Color.GREEN);
    }
    if(selectedPosition == position){
        drawerHolder.itemLayoutColor.setBackgroundColor(context
                .getResources().getColor(R.color.lightyellow));
    }
    return view;
}

private static class DrawerItemHolder {
    TextView ItemName;
    ImageView icon;
    LinearLayout itemLayoutColor;
    // View dividerView;
}
}

Create a class that will extends Activity write below methods:

@Override
public void setContentView(int layoutResID) {
    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(
            R.layout.navigation_drawer_layout, null);
    actContent = (FrameLayout) mDrawerLayout
            .findViewById(R.id.content_frame);
    getLayoutInflater().inflate(layoutResID, actContent, true);
    super.setContentView(mDrawerLayout);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
    }
    return true;
}

protected void navigationDrawer(DrawerLayout mDrawerLayout,
        ListView mDrawerList, int selectedPosition) {

    this.selectedPosition = selectedPosition;

    activity = (Activity) NavigationDrawerBaseActivity.this;
    actionBar = getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);


    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    this.mDrawerLayout = mDrawerLayout;
    this.mDrawerList = mDrawerList;

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    // set up the drawer's list view with items and click listener
    mDataList = new ArrayList<DrawerItem>();

    **********Here add the items in the list***********
   i.e.
    mDataList.add(new DrawerItem(DrawerConstant.LOGOUT,
            R.drawable.ic_signout));

    adapter = new CustomDrawerAdapter(this,
            R.layout.drawer_custom_single_layout, mDataList,
            selectedPosition);

    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    mDrawerLayout, /* DrawerLayout object */
    R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
    R.string.drawer_open, /* "open drawer" description for accessibility */
    R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}

public class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

            if (position != selectedPosition) {
                selectItem(position);
    }
}

protected void selectItem(int position) {
    // update the main content by replacing fragments

    switch (position) {
    case 0:
        // Call the another activity
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
        break;
    case 1:
        // Call the another activity
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
        break;
    case 2:
        // Call the another activity
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
        break;

    default:
        break;
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
  }
 }

Add the below code in Activity wherever you want to put Navigation Drawer:

  • Extends the NavigationDrawerBaseActivity class.

  • After setContentView call below method:

    // set Naviagtion Drawer
    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);
    super.navigationDrawer(mDrawerLayout, mDrawerList, 1);
    
  • Add below method in same Activity:

     @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      if (mDrawerToggle.onOptionsItemSelected(item)) {
        }
     }
    

Upvotes: 1

cheko506
cheko506

Reputation: 368

You can add an abstract method to get the layout for the activity which it's extending your navigation drawer: public abstract int NavigatonDrawerBaseActivity(); and make your class an abstract class (here use setContentView(NavigatonDrawerBaseActivity())). Then in the class which it's extending the Navigation drawer implements the method and pass it your xml layout (not use the setContentView method):

@Override
public int NavigatonDrawerBaseActivity() {

    return R.layout.yourLayout;
}

And finally in your layout you need to add the same code of your NavigationDrawerBaseActivity xml:

This works form me using fragments. Hope it helps.

Upvotes: 0

Syed Nazar Muhammad
Syed Nazar Muhammad

Reputation: 625

I have achieved the asked scenario in the following procedure.

Xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/venue_bg"
    android:orientation="vertical" >

    <!-- The navigation drawer -->

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayoutMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

        <ExpandableListView
            android:id="@+id/ExpandableList"
            android:layout_width="@dimen/drawer_size"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@drawable/left_drawer_item_gradient"
            android:choiceMode="singleChoice"
            android:clickable="true"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />

        <!-- content layout -->

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

setcontentView inside NavigationDrawerBaseActivity

@Override
    public void setContentView(int layoutID) 
    {
        // TODO Auto-generated method stub

        fullLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main_menu_activity_container, null);

        pocketFrame = (FrameLayout) fullLayout.findViewById(R.id.frameLayoutContent);

        getLayoutInflater().inflate(layoutID, pocketFrame, true);

        super.setContentView(fullLayout);
    }

In your NavigationDrawerBaseActivity make a method which sets setcontentView & take layout as an argument, & in every extended child class override that method & pass layout in argument. In xml @+id/ExpandableList is a list which is in left & @+id/frameLayoutContent acts like a container for incoming argument layouts, this will give you the replica of fraqgments replacing in a container.

Note: This is just a work around, recommended is Fragments.

Upvotes: 0

Deniz
Deniz

Reputation: 12530

Create a BaseActivity class that implements the drawer, and let all your activities extend this BaseActivity.

@Override
public void setContentView(int layoutResID) {
    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); 
    actContent = (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);

    setContentView(mDrawerLayout);
    getLayoutInflater().inflate(layoutResID, actContent, true);
}

Set the drawer layout as main content

Upvotes: 0

Related Questions