2

I am relatively new to android i want to add a navigation drawer to my app so i add a new activity and pick navigation drawer activity

activity class

import android.os.Bundle;
import android.view.View;
import android.view.Menu;

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

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class HomeActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        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_slideshow)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

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

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

and when i run the app and try to click on another item nothing happens it remains on the home fragment most of the info i've seen so far is old before version 3.6.3 and others just work What I've tried so far - uninstall and reinstall android studio - update to the latest android studio (4 at the moment) - created other fragments - stared new navigation drawer project run it..same thing

been on this the past few days nothing i try seems to work

Heading

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

2

you can implement onClick on the items of navigation drawer.

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener

and add this method to handle click listener:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {

   case R.id.nav_home: {
  //do somthing
        break;
    }  

  case R.id.nav_gallery: {
  //do somthing
        break;
    }  

   case R.id.nav_slideshow: {
  //do somthing
        break;
    }  
}
//close navigation drawer
drawer.closeDrawer(GravityCompat.END);
return true;
}

and other way to handle click :

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            int id=menuItem.getItemId();
            //it's possible to do more actions on several items, if there is a large amount of items I prefer switch(){case} instead of if()
            if (id==R.id.nav_home){
                Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_SHORT).show();
            }

           if (id==R.id.nav_gallery){
                Toast.makeText(getApplicationContext(), "nav_gallery", Toast.LENGTH_SHORT).show();
            }

           if (id==R.id.nav_slideshow){
                Toast.makeText(getApplicationContext(), "slideshow", Toast.LENGTH_SHORT).show();
            }
            //This is for maintaining the behavior of the Navigation view
            NavigationUI.onNavDestinationSelected(menuItem,navController);
            //This is for closing the drawer after acting on it
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    });
Farzad Kamali
  • 751
  • 2
  • 6
  • 24
  • thank you for your response i got it to work seems the problem was with xml like stated here [link](https://stackoverflow.com/questions/57929874/clicking-on-item-of-navigation-drawer-doesn-t-open-fragments?rq=1) – Paul Uchiha Jun 04 '20 at 13:47
0

you can easily close the drawer by using the line below by handling it in on click of each item of drawer:

drawer_layout.closeDrawer(GravityCompat.END)
Bita Mirshafiee
  • 600
  • 8
  • 14