Home > Software engineering >  Unable to navigate items on menu drawer
Unable to navigate items on menu drawer

Time:07-09

I am trying to navigate different activity using menu drawer but nothing happens after clicking on any item of the menu drawer. It would be great if anyone could help me in finding out what I am missing. I have attached my code below.

Thanks in advance

Here's the code :

MainActivity.java

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    public DrawerLayout drawerLayout;
    public ActionBarDrawerToggle actionBarDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawerLayout = findViewById(R.id.my_drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override

    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {


  // int id = onMenuItemSelected()
        int id = item.getItemId();
        switch (id) {
            case R.id.nav_dashboard:
                Intent intent = new Intent(getApplicationContext(), Dashboard.class);
                startActivity(intent);
                break;
            case R.id.nav_configuration:
                Intent configuration = new Intent(getApplicationContext(), Configuration.class);
                startActivity(configuration);
                break;
            case R.id.nav_saved:
                Intent saved =new Intent(getApplicationContext(), SavedConfig.class);
                startActivity(saved);
                break;
            case R.id.nav_console:
                Intent console = new Intent(getApplicationContext(), Console.class);
                startActivity(console);
                break;

        }
        
        return true;
    }
}

activity_main.xml

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

<!--the root view must be the DrawerLayout-->
<androidx.drawerlayout.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/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="128dp"
            android:gravity="center"
            android:text="broker"
            android:textSize="18sp" />

    </LinearLayout>

    <!--this the navigation view which draws
        and shows the navigation drawer-->
    <!--include the menu created in the menu folder-->
    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu" />

</androidx.drawerlayout.widget.DrawerLayout>

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="HardcodedText">

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

    <item
        android:id="@ id/nav_configuration"
        android:title="Configuration" />

    <item
        android:id="@ id/nav_saved"
        android:title="Saved Config" />

    <item
        android:id="@ id/nav_console"
        android:title="Console" />


    <item
        android:id="@ id/nav_settings"
        android:title="Settings" />

</menu>

CodePudding user response:

Add a ID to your NavigationView

<com.google.android.material.navigation.NavigationView 
    android:id="@ id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/menu" />

Then onCreate

NavigationView navigationView= findViewById(R.id.navigation);
navigationView.setNavigationItemSelectedListener(this);
  • Related