1

there are loads of tutorials online about how to use fragments with navigation drawers, but I want to know how to open navigation drawers with it. As in if you select any individual option of a navigation drawer, it'll take you to another activity with the same option of a navigation drawer within the activity.

MainActivity.java

package com.example.fyptrial3;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawer;

    EditText etUsername, etPassword;


    public void OnLogin(View view) {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String type = "login";

        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username, password);
    }

    private AppBarConfiguration mAppBarConfiguration;


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);


        Toolbar toolbar = findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);




        final DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_map,
                R.id.nav_news, R.id.nav_share, R.id.nav_login)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        navigationView.bringToFront();

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                switch (menuItem.getItemId()) {
                    case R.id.nav_home:
                        Toast.makeText(getApplicationContext(), "Home is selected", Toast.LENGTH_LONG).show();
                        break;

                    case R.id.nav_gallery:
                        Toast.makeText(getApplicationContext(), "Members are selected",Toast.LENGTH_LONG).show();
                       Intent memberstart=new Intent(MainActivity.this,GalleryActivity.class);
                        startActivity(memberstart);
                        break;

                    case R.id.nav_map:
                        Toast.makeText(getApplicationContext(), "Slideshow is selected", Toast.LENGTH_LONG).show();
                        break;

                    case R.id.nav_share:
                        Toast.makeText(getApplicationContext(), "Share is selected", Toast.LENGTH_LONG).show();
                        //Intent sharestart=new Intent(MainActivity.this,this.class);
                       // startActivity(sharestart);
                        break;
                }

                drawer.closeDrawers();

                return false;
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //youtube.com/watch?v=zwabHRv2taA

    //In order to change it to Irish, I used this button to show that when it is clicked
    //the version of the Navigation Drawer as Gaeilge will pop up

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() ==R.id.main_settings) {
            Intent gaeilgemainstart=new Intent(MainActivity.this,MainActivityGaeilge.class);
            startActivity(gaeilgemainstart);

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
John B
  • 43
  • 1
  • 7
  • `Activity` within `Activity` not possible – Md. Asaduzzaman Jan 23 '20 at 16:36
  • I'm not trying to make it within another activity. Just trying to bring it to another activity @md-asaduzzaman I'm just saying if you click one of the options in the navigation drawer, it will take you to another activity with the same navigation drawer options available, but just with a different layout – John B Jan 23 '20 at 16:37
  • Have you looked at this answer? https://stackoverflow.com/a/26526858/769265 – David Wasser Jan 23 '20 at 16:54

2 Answers2

1

Inside your mobile_navigation.xml add the following code.

    <activity
    android:id="@+id/nav_custom"
    android:name="net.larntech.nav.CustomActivity"
    android:label="@string/menu_custom"
    tools:layout="@layout/activity_custom"
    />

Here is a video tutorial illustrating the same thing

https://youtu.be/5VsRFJjyMjU

Richard Kamere
  • 749
  • 12
  • 10
  • Although this is a shortest path to activate an activity with drawer item, it has one drawback: when you leave the activity with back system button the drawer remains unfolded. – Serge Sep 06 '22 at 23:24
0

you can add NavigationView to your activities by add this to your activities XML

  <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_main"
        app:menu="@menu/activity_main_drawer" />

and add this to your activities

public class YourActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_activity);
        setTheme(R.style.full_screen);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));
        toggle.setHomeAsUpIndicator(R.drawable.side_menu);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id = menuItem.getItemId();
        switch (id) {
            case R.id.home:
                Intent intent = new Intent(HomeActivity.this, HomeActivity.class);
                startActivity(intent);
                break;
            case R.id.active_order:
                Intent active = new Intent(HomeActivity.this, ActiveOrderActivity.class);
                startActivity(active);
                break;
                case R.id.history_order:
                Intent history = new Intent(HomeActivity.this, HistoryOrderActivity.class);
                startActivity(history);
                break;
            case R.id.profile:
                Intent profile = new Intent(HomeActivity.this, ProfileActivity.class);
                startActivity(profile);
                break;
            case R.id.chat:
                Intent chat = new Intent(HomeActivity.this, ChatActivity.class);
                startActivity(chat);
                break;
            case R.id.logout:
                showPopup();
                break;
        }
        return true;
    }
 }

i hope this will help you