-1

I've been trying to get the click events for menu in navigation drawer to go through, but it doesn't work.

Here's my main_activity

package com.example.administrator.myapplication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

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


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.nav_item_one: {
            Intent intent = new Intent(this, first_activity.class);
            this.startActivity(intent);
            Toast.makeText(Beer.this, "This is my navigation Toast message!",
                    Toast.LENGTH_LONG).show();
            break;
        }
        case R.id.nav_item_two:{
            Intent intent = new Intent(this, second_activity.class);
            this.startActivity(intent);
            break;
        }
    }
    return true;
}


private void setNavigationViewListener() {
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}
}

Here's the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:theme="@style/AppTheme"
tools:context=".MainActivity">

<include
    layout="@layout/app_bar_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<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"
    android:focusable="true"
    app:elevation="20dp"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer">

</android.support.design.widget.NavigationView>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/main_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="8dp"
            android:text="@string/main_heading"
            android:textAlignment="center"
            android:textColor="@color/pink"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/header_image"
            android:layout_width="wrap_content"
            android:layout_height="248dp"
            android:layout_below="@id/main_heading"
            android:layout_marginBottom="8dp"
            android:contentDescription="@string/main"
            android:outlineAmbientShadowColor="@color/colorPrimary"
            android:src="@drawable/image_header" />

        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/header_image"
            android:background="?android:attr/listDivider" />

        <TextView
            android:id="@+id/main_description_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/divider"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="8dp"
            android:text="@string/beer_description_paragraph_1" />

    </RelativeLayout>
</ScrollView>


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

here's the activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:androud="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
    <item android:id="@+id/nav_item_one"
        android:title="@string/item_one"/>
    <item android:id="@+id/nav_item_two"
        android:title="@string/item_two"/>
</group>

I can see all the items in the drawer and when I click on an item, the drawer closes but that's it, the associated action is not going through.

Ajay Bhargav
  • 7
  • 1
  • 4
  • You have a `View` in front of the `NavigationView`. That is, you have something listed after it in the ``. The `` must be listed last in order to function correctly. I would also mention that `DrawerLayout` usually only has one main content `View`. You currently have two – the ``d `app_bar_main` layout, and the ``. This will likely cause unexpected, incorrect behavior for you down the line. Those should be combined into one. – Mike M. Jan 16 '19 at 17:39
  • 1
    thanks buddy. this helped! also, I'll look into reducing the child views. – Ajay Bhargav Jan 16 '19 at 17:57

1 Answers1

0

You need to use the same id declared in the activity_main_drawer menu. So it would be:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.nav_beer: {
            Intent intent = new Intent(this, first_activity.class);
            this.startActivity(intent);
            Toast.makeText(Beer.this, "This is my navigation Toast message!",
                    Toast.LENGTH_LONG).show();
            break;
        }
        case R.id.nav_brandy:{
            Intent intent = new Intent(this, second_activity.class);
            this.startActivity(intent);
            break;
        }
    }
    return true;
}
Sdghasemi
  • 5,370
  • 1
  • 34
  • 42