NavigationView OnNavigationItemSelectedListener not being called
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
add a comment |
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
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
add a comment |
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
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
android navigationview android-support-design
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
When you make XML layout, you should write down NavigationView
after BaseLayout (FrameLayout
, LinearLayout
, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
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 aWebView
using the back button. TheFrameLayout
was listed after theNavigationView
in my XML file, and even though it was set as visibilityGONE
it was still covering up theNavigationView
and preventing touches from being registered.
– Soren Stoutner
Aug 19 '16 at 4:50
THANKS!!!!!!!!!!!!!!!!!
– alfo888_ibg
Oct 25 '16 at 8:59
|
show 2 more comments
for me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
This should be the accepted answer.
– Iorek
May 12 '17 at 2:49
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
When you make XML layout, you should write down NavigationView
after BaseLayout (FrameLayout
, LinearLayout
, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
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 aWebView
using the back button. TheFrameLayout
was listed after theNavigationView
in my XML file, and even though it was set as visibilityGONE
it was still covering up theNavigationView
and preventing touches from being registered.
– Soren Stoutner
Aug 19 '16 at 4:50
THANKS!!!!!!!!!!!!!!!!!
– alfo888_ibg
Oct 25 '16 at 8:59
|
show 2 more comments
When you make XML layout, you should write down NavigationView
after BaseLayout (FrameLayout
, LinearLayout
, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
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 aWebView
using the back button. TheFrameLayout
was listed after theNavigationView
in my XML file, and even though it was set as visibilityGONE
it was still covering up theNavigationView
and preventing touches from being registered.
– Soren Stoutner
Aug 19 '16 at 4:50
THANKS!!!!!!!!!!!!!!!!!
– alfo888_ibg
Oct 25 '16 at 8:59
|
show 2 more comments
When you make XML layout, you should write down NavigationView
after BaseLayout (FrameLayout
, LinearLayout
, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
When you make XML layout, you should write down NavigationView
after BaseLayout (FrameLayout
, LinearLayout
, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
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 aWebView
using the back button. TheFrameLayout
was listed after theNavigationView
in my XML file, and even though it was set as visibilityGONE
it was still covering up theNavigationView
and preventing touches from being registered.
– Soren Stoutner
Aug 19 '16 at 4:50
THANKS!!!!!!!!!!!!!!!!!
– alfo888_ibg
Oct 25 '16 at 8:59
|
show 2 more comments
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 aWebView
using the back button. TheFrameLayout
was listed after theNavigationView
in my XML file, and even though it was set as visibilityGONE
it was still covering up theNavigationView
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
|
show 2 more comments
for me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
This should be the accepted answer.
– Iorek
May 12 '17 at 2:49
add a comment |
for me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
This should be the accepted answer.
– Iorek
May 12 '17 at 2:49
add a comment |
for me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
for me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
answered Mar 20 '17 at 15:19
nivaniva
10615
10615
This should be the accepted answer.
– Iorek
May 12 '17 at 2:49
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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