Side Navigation Drawer Item click not working
Asked Answered
T

4

22

PLEASE READ FIRST BEFORE ANYONE MARK IT AS DUPLICATE QUESTION BECAUSE IT IS NOT.

I have used Navigation Drawer, Bottom Navigation bar and a custom Action bar.

Bottom navigation bar and action bar are working fine. navigation drawer also shows menu present under it but the items are not clickable.

I have tried all the answers related to

navigation drawer item click not working

but still my problem has not been solved yet. there is no error in the debug section.

Image of output

Image of output

Dashboard Activity

package com.vicky.sampleApp;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;

public class Dashboard extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    //android.app.ActionBar actionbar;
    TextView textview;
    TextView textviewTitle;
    private ActionBar toolbar;
    private DrawerLayout drawer;
    private ActionBarDrawerToggle toggle;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);

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

        setActionBarText("HomePage");

         drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
         toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar1, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.bringToFront();
        drawer.addDrawerListener(toggle);
        toggle.syncState();

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

        toolbar = getSupportActionBar();

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);



    }


    private void setActionBarText(String text){
    toolbar = getSupportActionBar();
    toolbar.setBackgroundDrawable(getResources().getDrawable(R.drawable.default_appbar_theme));//line under the action bar
    View viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_title_text_layout, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.CENTER);
    textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
    textviewTitle.setText(text);
    toolbar.setCustomView(viewActionBar, params);
    toolbar.setDisplayShowCustomEnabled(true);
    toolbar.setDisplayShowTitleEnabled(false);
    toolbar.setHomeButtonEnabled(true);
}
/*
private NavigationView.OnNavigationItemSelectedListener mSideNavigationItemSelectedListener
        = new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
            case R.id.nav_camera:
                Toast.makeText(Dashboard.this, "Camera", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Dashboard.this,AboutUs.class);
                startActivity(intent);
                break;
            case R.id.nav_gallery:
                Toast.makeText(Dashboard.this, "Gallery", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_slideshow:
                Toast.makeText(Dashboard.this, "Slideshow", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_manage:
                Toast.makeText(Dashboard.this, "Slideshow", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_share:
                Toast.makeText(Dashboard.this, "Nav_share", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_send:
                Toast.makeText(Dashboard.this, "nav_send", Toast.LENGTH_SHORT).show();
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
};
*/
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    setActionBarText("Home");
                    return true;
                case R.id.navigation_appointments:
                    setActionBarText("Appointments");
                    return true;
                case R.id.navigation_category:
                    setActionBarText("Category");
                    return true;
                case R.id.navigation_profile:
                    setActionBarText("Profile");
                    return true;
            }
            return false;
        }
    };

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            Toast.makeText(this, "Camera", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this,AboutUs.class);
            startActivity(intent);
        } else if (id == R.id.nav_gallery) {
            Toast.makeText(this, "Gallery", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_slideshow) {
            Toast.makeText(this, "Slideshow", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_manage) {
            Toast.makeText(this, "Nav_manage", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_share) {
            Toast.makeText(this, "Nav_share", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_send) {
            Toast.makeText(this, "nav_send", Toast.LENGTH_SHORT).show();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}

dashboard.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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <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" />

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

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/CustomTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/CustomTheme.PopupOverlay" />

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

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:labelVisibilityMode="labeled"
            app:itemBackground="@color/colorPrimary"
            android:layout_gravity="bottom"
            app:itemIconTint="@drawable/selector_bottom_nav_bar"
            app:itemTextColor="@drawable/selector_bottom_nav_bar"
            app:menu="@menu/navigation"/>

    </FrameLayout>

</android.support.v4.widget.DrawerLayout>
Threepence answered 4/7, 2018 at 10:13 Comment(2)
BEFORE ANYONE MARK IT AS DUPLICATE PLEASE READ THE QUESTION. sure sirHerve
Sir did you find the reason why this is not working? I have added images to the question for the reference please have a look at them also.Threepence
D
61

Just put your NavigationView after FrameLayout. Check below code:

<?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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/CustomTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/CustomTheme.PopupOverlay" />

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

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:labelVisibilityMode="labeled"
            app:itemBackground="@color/colorPrimary"
            android:layout_gravity="bottom"
            app:itemIconTint="@drawable/selector_bottom_nav_bar"
            app:itemTextColor="@drawable/selector_bottom_nav_bar"
            app:menu="@menu/navigation"/>

    </FrameLayout>

    <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" />

</android.support.v4.widget.DrawerLayout>
Discomfit answered 4/7, 2018 at 10:38 Comment(4)
Thankyou very much sir, it was really a silly mistake, few moments back only i transferred that section at the top. SIR may i know the reason why this problem occured...Threepence
Because FrameLayout overlaps NavigationView.Discomfit
@VickySalunkhe You know what. They make same mistake in AndroidStudio's template Drawer Navigation project! Thank god I found this answer :)Sepulture
This answer is such a lifesaver. Spent so much time only to find out that my NavigationView had to be on the bottom of the xml layout. Android Y U DO THIS?!!!Gibberish
S
19

I was searching for different keywords but landed on this page every time. I had the issue that I could not click the navigation menu items.

I thought it would be easy to find a solution for those who are struggling with this strange behavior which is auto-generated by android studio 4.0 while creating a project with a navigation drawer.

I spent hours but at the end I have to find it my own way and that way was too simple. Posting in the hope of helping someone. My Layout was the following on which the item click was not working.

<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.navigation.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" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>

I changed to this , Just put the navigation view on the bottom and it worked.

 <?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.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" />
</androidx.drawerlayout.widget.DrawerLayout>
Santalaceous answered 27/6, 2020 at 9:13 Comment(2)
you saved me hours of searching, still it's a shame that Android Studio creates the first version by default when you create a new Drawer Activity...Swage
This one helped mePossession
S
4

I too had the same problem. Just you have to put the NavigationView in the last part of the XML code.

Silvestro answered 7/7, 2021 at 6:0 Comment(0)
H
2

You should call or include the frame layout before next, the navigation refer to the code

<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <com.google.android.material.navigation.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" />

</androidx.drawerlayout.widget.DrawerLayout>
Howe answered 28/12, 2020 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.