user4138608
user4138608

Reputation:

Using navigation drawer in more than one activity

I'm still learning to program on Android and this is my first time creating a navigation drawer. I know that there are other ways of creating the navigation drawer but this was, in my opinion, the easiest way for me to understand but now I don't know how can extend this in all of my activities.

This is the code:

Dashboard.java

    package com.example.alexandre.blueprint_apocalypse;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class Dashboard extends AppCompatActivity {

//Menu variables
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

//Buttons
public ImageButton profile_button;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    setTitle(R.string.dashboardLabel);

    //Create Menu
    mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_dashboard);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //Activate hamburguer button

    //Change Activity using buttons
    changeActivity();
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{

    if(mToggle.onOptionsItemSelected(item))
    {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/dashboard_profile_icon"
        android:id="@+id/dashoard_profile_button"
        android:layout_marginTop="77dp"
        style="?android:attr/borderlessButtonStyle"
        android:background="@null"
        android:scaleType="fitXY"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="47dp"
        android:layout_marginStart="47dp"
        android:contentDescription="" />
</RelativeLayout>

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity = "start">
</android.support.design.widget.NavigationView>

navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/menu_option_dashboard"
    android:title="Dashboard">
</item>

<item android:id="@+id/menu_option_profile"
    android:title="Profile">
</item>

<item android:id="@+id/menu_option_defenseHistory"
    android:title="Defense History">
</item>

<item android:id="@+id/menu_option_encyclopedia"
    android:title="Encyclopedia">
</item>

<item android:id="@+id/menu_option_worldScore"
    android:title="World Score">
</item>

How can I extend this to all of my activities?

Upvotes: 1

Views: 495

Answers (2)

Yonjuni
Yonjuni

Reputation: 178

Here is a link to an example which adds the navigation drawer to multiple activities

There is an activity called BaseActivity wish serves as a parent for all activities that are using the navigation drawer. So your Dashboard class could extend the new activity. The MainActivity in the example is an activity extending the BaseActivity.

Hope this helps.

Upvotes: 1

Jay Pandya
Jay Pandya

Reputation: 138

Add the navigation view to all the activities where you want the navigation drawer to be present. But you have to maintain the checked state of items in each drawer manually by navigationView.setCheckedItem(id);

Upvotes: 0

Related Questions