NavigationView OnNavigationItemSelectedListener not being called












13















I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code



Activity Layout



<?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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_menu" />

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>


Activity onCreate()



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutID());

toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.common_menu);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
}

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
menuItem.setChecked(true);
// allow some time after closing the drawer before performing real navigation
// so the user can see what is happening
drawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
@Override
public void run() {
navigate(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
drawerLayout.closeDrawers();
return true;
}

});
usernameTextView = (TextView) findViewById(R.id.drawer_header_username);
usernameTextView.setText(getAppDContext().getAccount().getUsername());
}









share|improve this question

























  • Is there a reason for setting the listener twice on the NavigationView?

    – Luksprog
    Jul 14 '15 at 5:18











  • Oops. Pasted wrong code here. Corrected. Thanks for pointing it out.

    – NinjaCoder
    Jul 14 '15 at 5:30











  • Post your full layout

    – Gabriele Mariotti
    Jul 14 '15 at 21:45
















13















I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code



Activity Layout



<?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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_menu" />

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>


Activity onCreate()



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutID());

toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.common_menu);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
}

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
menuItem.setChecked(true);
// allow some time after closing the drawer before performing real navigation
// so the user can see what is happening
drawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
@Override
public void run() {
navigate(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
drawerLayout.closeDrawers();
return true;
}

});
usernameTextView = (TextView) findViewById(R.id.drawer_header_username);
usernameTextView.setText(getAppDContext().getAccount().getUsername());
}









share|improve this question

























  • Is there a reason for setting the listener twice on the NavigationView?

    – Luksprog
    Jul 14 '15 at 5:18











  • Oops. Pasted wrong code here. Corrected. Thanks for pointing it out.

    – NinjaCoder
    Jul 14 '15 at 5:30











  • Post your full layout

    – Gabriele Mariotti
    Jul 14 '15 at 21:45














13












13








13


2






I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code



Activity Layout



<?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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_menu" />

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>


Activity onCreate()



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutID());

toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.common_menu);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
}

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
menuItem.setChecked(true);
// allow some time after closing the drawer before performing real navigation
// so the user can see what is happening
drawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
@Override
public void run() {
navigate(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
drawerLayout.closeDrawers();
return true;
}

});
usernameTextView = (TextView) findViewById(R.id.drawer_header_username);
usernameTextView.setText(getAppDContext().getAccount().getUsername());
}









share|improve this question
















I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code



Activity Layout



<?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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_menu" />

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>


Activity onCreate()



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutID());

toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.common_menu);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
}

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
menuItem.setChecked(true);
// allow some time after closing the drawer before performing real navigation
// so the user can see what is happening
drawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
@Override
public void run() {
navigate(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
drawerLayout.closeDrawers();
return true;
}

});
usernameTextView = (TextView) findViewById(R.id.drawer_header_username);
usernameTextView.setText(getAppDContext().getAccount().getUsername());
}






android navigationview android-support-design






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 27 '18 at 16:03









naXa

13.8k892136




13.8k892136










asked Jul 14 '15 at 4:41









NinjaCoderNinjaCoder

1,17521640




1,17521640













  • Is there a reason for setting the listener twice on the NavigationView?

    – Luksprog
    Jul 14 '15 at 5:18











  • Oops. Pasted wrong code here. Corrected. Thanks for pointing it out.

    – NinjaCoder
    Jul 14 '15 at 5:30











  • Post your full layout

    – Gabriele Mariotti
    Jul 14 '15 at 21:45



















  • Is there a reason for setting the listener twice on the NavigationView?

    – Luksprog
    Jul 14 '15 at 5:18











  • Oops. Pasted wrong code here. Corrected. Thanks for pointing it out.

    – NinjaCoder
    Jul 14 '15 at 5:30











  • Post your full layout

    – Gabriele Mariotti
    Jul 14 '15 at 21:45

















Is there a reason for setting the listener twice on the NavigationView?

– Luksprog
Jul 14 '15 at 5:18





Is there a reason for setting the listener twice on the NavigationView?

– Luksprog
Jul 14 '15 at 5:18













Oops. Pasted wrong code here. Corrected. Thanks for pointing it out.

– NinjaCoder
Jul 14 '15 at 5:30





Oops. Pasted wrong code here. Corrected. Thanks for pointing it out.

– NinjaCoder
Jul 14 '15 at 5:30













Post your full layout

– Gabriele Mariotti
Jul 14 '15 at 21:45





Post your full layout

– Gabriele Mariotti
Jul 14 '15 at 21:45












3 Answers
3






active

oldest

votes


















56














When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)



<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>





share|improve this answer





















  • 1





    Thanks it is working for me.

    – Shivam Garg
    May 31 '16 at 5:45






  • 2





    Rockstar answer.. thanks

    – Siddharth
    Jun 7 '16 at 9:11






  • 2





    Wow. I have been looking for hours. Thank you.

    – NorthernLights
    Jun 19 '16 at 4:52






  • 1





    This fixed a problem I had when exiting a full screen video in a WebView using the back button. The FrameLayout was listed after the NavigationView in my XML file, and even though it was set as visibility GONE it was still covering up the NavigationView and preventing touches from being registered.

    – Soren Stoutner
    Aug 19 '16 at 4:50













  • THANKS!!!!!!!!!!!!!!!!!

    – alfo888_ibg
    Oct 25 '16 at 8:59



















7














for me this did the trick!



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



navigationView.bringToFront();






share|improve this answer
























  • This should be the accepted answer.

    – Iorek
    May 12 '17 at 2:49



















5














Your activity main layout should look like this:



<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigationDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_main_content" />

<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
style="@style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="@layout/header"
app:menu="@menu/menu_drawer"/>

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


In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder)
for example menu_drawer.xml :



<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav1"
android:checked="true"
android:icon="@drawable/logo"
android:title="Navigation item 1"/>
<item
android:id="@+id/nav2"
android:icon="@drawable/logo"
android:title="Navigation item 2"/>
</group>
</menu>


than your java code:



public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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

setUpToolbar();
setUpNavDrawer();
}

private void setUpNavDrawer() {
NavigationView view = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
view.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}


Check if this work for you. In my project works like a charm.






share|improve this answer


























  • Does it call onOptionsItemSelected when hamburger icon is selected? No. Also when I select a navigation item it is not calling the listener

    – NinjaCoder
    Jul 14 '15 at 18:00













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31397792%2fnavigationview-onnavigationitemselectedlistener-not-being-called%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









56














When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)



<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>





share|improve this answer





















  • 1





    Thanks it is working for me.

    – Shivam Garg
    May 31 '16 at 5:45






  • 2





    Rockstar answer.. thanks

    – Siddharth
    Jun 7 '16 at 9:11






  • 2





    Wow. I have been looking for hours. Thank you.

    – NorthernLights
    Jun 19 '16 at 4:52






  • 1





    This fixed a problem I had when exiting a full screen video in a WebView using the back button. The FrameLayout was listed after the NavigationView in my XML file, and even though it was set as visibility GONE it was still covering up the NavigationView and preventing touches from being registered.

    – Soren Stoutner
    Aug 19 '16 at 4:50













  • THANKS!!!!!!!!!!!!!!!!!

    – alfo888_ibg
    Oct 25 '16 at 8:59
















56














When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)



<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>





share|improve this answer





















  • 1





    Thanks it is working for me.

    – Shivam Garg
    May 31 '16 at 5:45






  • 2





    Rockstar answer.. thanks

    – Siddharth
    Jun 7 '16 at 9:11






  • 2





    Wow. I have been looking for hours. Thank you.

    – NorthernLights
    Jun 19 '16 at 4:52






  • 1





    This fixed a problem I had when exiting a full screen video in a WebView using the back button. The FrameLayout was listed after the NavigationView in my XML file, and even though it was set as visibility GONE it was still covering up the NavigationView and preventing touches from being registered.

    – Soren Stoutner
    Aug 19 '16 at 4:50













  • THANKS!!!!!!!!!!!!!!!!!

    – alfo888_ibg
    Oct 25 '16 at 8:59














56












56








56







When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)



<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>





share|improve this answer















When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)



<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 27 '18 at 16:05









naXa

13.8k892136




13.8k892136










answered Jul 16 '15 at 6:46









이종일이종일

70158




70158








  • 1





    Thanks it is working for me.

    – Shivam Garg
    May 31 '16 at 5:45






  • 2





    Rockstar answer.. thanks

    – Siddharth
    Jun 7 '16 at 9:11






  • 2





    Wow. I have been looking for hours. Thank you.

    – NorthernLights
    Jun 19 '16 at 4:52






  • 1





    This fixed a problem I had when exiting a full screen video in a WebView using the back button. The FrameLayout was listed after the NavigationView in my XML file, and even though it was set as visibility GONE it was still covering up the NavigationView and preventing touches from being registered.

    – Soren Stoutner
    Aug 19 '16 at 4:50













  • THANKS!!!!!!!!!!!!!!!!!

    – alfo888_ibg
    Oct 25 '16 at 8:59














  • 1





    Thanks it is working for me.

    – Shivam Garg
    May 31 '16 at 5:45






  • 2





    Rockstar answer.. thanks

    – Siddharth
    Jun 7 '16 at 9:11






  • 2





    Wow. I have been looking for hours. Thank you.

    – NorthernLights
    Jun 19 '16 at 4:52






  • 1





    This fixed a problem I had when exiting a full screen video in a WebView using the back button. The FrameLayout was listed after the NavigationView in my XML file, and even though it was set as visibility GONE it was still covering up the NavigationView and preventing touches from being registered.

    – Soren Stoutner
    Aug 19 '16 at 4:50













  • THANKS!!!!!!!!!!!!!!!!!

    – alfo888_ibg
    Oct 25 '16 at 8:59








1




1





Thanks it is working for me.

– Shivam Garg
May 31 '16 at 5:45





Thanks it is working for me.

– Shivam Garg
May 31 '16 at 5:45




2




2





Rockstar answer.. thanks

– Siddharth
Jun 7 '16 at 9:11





Rockstar answer.. thanks

– Siddharth
Jun 7 '16 at 9:11




2




2





Wow. I have been looking for hours. Thank you.

– NorthernLights
Jun 19 '16 at 4:52





Wow. I have been looking for hours. Thank you.

– NorthernLights
Jun 19 '16 at 4:52




1




1





This fixed a problem I had when exiting a full screen video in a WebView using the back button. The FrameLayout was listed after the NavigationView in my XML file, and even though it was set as visibility GONE it was still covering up the NavigationView and preventing touches from being registered.

– Soren Stoutner
Aug 19 '16 at 4:50







This fixed a problem I had when exiting a full screen video in a WebView using the back button. The FrameLayout was listed after the NavigationView in my XML file, and even though it was set as visibility GONE it was still covering up the NavigationView and preventing touches from being registered.

– Soren Stoutner
Aug 19 '16 at 4:50















THANKS!!!!!!!!!!!!!!!!!

– alfo888_ibg
Oct 25 '16 at 8:59





THANKS!!!!!!!!!!!!!!!!!

– alfo888_ibg
Oct 25 '16 at 8:59













7














for me this did the trick!



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



navigationView.bringToFront();






share|improve this answer
























  • This should be the accepted answer.

    – Iorek
    May 12 '17 at 2:49
















7














for me this did the trick!



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



navigationView.bringToFront();






share|improve this answer
























  • This should be the accepted answer.

    – Iorek
    May 12 '17 at 2:49














7












7








7







for me this did the trick!



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



navigationView.bringToFront();






share|improve this answer













for me this did the trick!



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



navigationView.bringToFront();







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 20 '17 at 15:19









nivaniva

10615




10615













  • This should be the accepted answer.

    – Iorek
    May 12 '17 at 2:49



















  • This should be the accepted answer.

    – Iorek
    May 12 '17 at 2:49

















This should be the accepted answer.

– Iorek
May 12 '17 at 2:49





This should be the accepted answer.

– Iorek
May 12 '17 at 2:49











5














Your activity main layout should look like this:



<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigationDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_main_content" />

<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
style="@style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="@layout/header"
app:menu="@menu/menu_drawer"/>

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


In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder)
for example menu_drawer.xml :



<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav1"
android:checked="true"
android:icon="@drawable/logo"
android:title="Navigation item 1"/>
<item
android:id="@+id/nav2"
android:icon="@drawable/logo"
android:title="Navigation item 2"/>
</group>
</menu>


than your java code:



public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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

setUpToolbar();
setUpNavDrawer();
}

private void setUpNavDrawer() {
NavigationView view = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
view.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}


Check if this work for you. In my project works like a charm.






share|improve this answer


























  • Does it call onOptionsItemSelected when hamburger icon is selected? No. Also when I select a navigation item it is not calling the listener

    – NinjaCoder
    Jul 14 '15 at 18:00


















5














Your activity main layout should look like this:



<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigationDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_main_content" />

<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
style="@style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="@layout/header"
app:menu="@menu/menu_drawer"/>

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


In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder)
for example menu_drawer.xml :



<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav1"
android:checked="true"
android:icon="@drawable/logo"
android:title="Navigation item 1"/>
<item
android:id="@+id/nav2"
android:icon="@drawable/logo"
android:title="Navigation item 2"/>
</group>
</menu>


than your java code:



public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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

setUpToolbar();
setUpNavDrawer();
}

private void setUpNavDrawer() {
NavigationView view = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
view.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}


Check if this work for you. In my project works like a charm.






share|improve this answer


























  • Does it call onOptionsItemSelected when hamburger icon is selected? No. Also when I select a navigation item it is not calling the listener

    – NinjaCoder
    Jul 14 '15 at 18:00
















5












5








5







Your activity main layout should look like this:



<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigationDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_main_content" />

<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
style="@style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="@layout/header"
app:menu="@menu/menu_drawer"/>

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


In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder)
for example menu_drawer.xml :



<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav1"
android:checked="true"
android:icon="@drawable/logo"
android:title="Navigation item 1"/>
<item
android:id="@+id/nav2"
android:icon="@drawable/logo"
android:title="Navigation item 2"/>
</group>
</menu>


than your java code:



public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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

setUpToolbar();
setUpNavDrawer();
}

private void setUpNavDrawer() {
NavigationView view = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
view.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}


Check if this work for you. In my project works like a charm.






share|improve this answer















Your activity main layout should look like this:



<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigationDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_main_content" />

<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
style="@style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="@layout/header"
app:menu="@menu/menu_drawer"/>

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


In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder)
for example menu_drawer.xml :



<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav1"
android:checked="true"
android:icon="@drawable/logo"
android:title="Navigation item 1"/>
<item
android:id="@+id/nav2"
android:icon="@drawable/logo"
android:title="Navigation item 2"/>
</group>
</menu>


than your java code:



public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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

setUpToolbar();
setUpNavDrawer();
}

private void setUpNavDrawer() {
NavigationView view = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
view.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}


Check if this work for you. In my project works like a charm.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 7 '16 at 9:10









Siddharth

6,492863110




6,492863110










answered Jul 14 '15 at 9:17









FaresFares

8415




8415













  • Does it call onOptionsItemSelected when hamburger icon is selected? No. Also when I select a navigation item it is not calling the listener

    – NinjaCoder
    Jul 14 '15 at 18:00





















  • Does it call onOptionsItemSelected when hamburger icon is selected? No. Also when I select a navigation item it is not calling the listener

    – NinjaCoder
    Jul 14 '15 at 18:00



















Does it call onOptionsItemSelected when hamburger icon is selected? No. Also when I select a navigation item it is not calling the listener

– NinjaCoder
Jul 14 '15 at 18:00







Does it call onOptionsItemSelected when hamburger icon is selected? No. Also when I select a navigation item it is not calling the listener

– NinjaCoder
Jul 14 '15 at 18:00




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31397792%2fnavigationview-onnavigationitemselectedlistener-not-being-called%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen