NavigationView OnNavigationItemSelectedListener not being called
Solution 1:
When you make XML layout, you should write down NavigationView
after BaseLayout (FrameLayout
, LinearLayout
, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
Solution 2:
For me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
Solution 3:
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.