2

I am trying to set up a Navigation Activity in my application. The problem is that I cannot switch between the Menu fragments. It looks like the menu items are not clickable.

I tried to set up the Navigation Activity in the same way I have already set up a menu (Action Bar) before but it's just not working. Also with on Click Listener, the items don't react. My question is now on how to make the onNavigationItemSelected() method work?

Sorry for the mass of code.

public class NavActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.nav, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_home: {
            Toast.makeText(NavActivity.this, "Anmeldung Fehlgeschlagen", Toast.LENGTH_LONG).show();
            return true;
        }

        case R.id.nav_gallery:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

app_bar_nav.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"
    app:srcCompat="@android:drawable/ic_dialog_email" />

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

activity_nav_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:App="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_menu_camera"
        android:title="@string/menu_home"
        android:clickable="true"/>
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="@string/menu_gallery"
        android:clickable="true"/>
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="@string/menu_slideshow"
        App:showAsAction="always"
        android:clickable="true"/>
    <item
        android:id="@+id/nav_tools"
        android:icon="@drawable/ic_menu_manage"
        android:title="@string/menu_tools"
        android:clickable="true"/>
</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="@string/menu_share"
            android:clickable="true"/>
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="@string/menu_send"
            android:clickable="true"/>
    </menu>
</item>

mobile_navigation.xml

<navigation 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/mobile_navigation"
app:startDestination="@+id/nav_home">

<fragment
    android:id="@+id/nav_home"
    android:name="com.example.login.ui.home.HomeFragment"
    android:label="@string/menu_home"
    tools:layout="@layout/fragment_home"
    android:clickable="true"/>

<fragment
    android:id="@+id/nav_gallery"
    android:name="com.example.login.ui.gallery.GalleryFragment"
    android:label="@string/menu_gallery"
    tools:layout="@layout/fragment_gallery" />

<fragment
    android:id="@+id/nav_slideshow"
    android:name="com.example.login.ui.slideshow.SlideshowFragment"
    android:label="@string/menu_slideshow"
    tools:layout="@layout/fragment_slideshow" />

<fragment
    android:id="@+id/nav_tools"
    android:name="com.example.login.ui.tools.ToolsFragment"
    android:label="@string/menu_tools"
    tools:layout="@layout/fragment_tools" />

<fragment
    android:id="@+id/nav_share"
    android:name="com.example.login.ui.share.ShareFragment"
    android:label="@string/menu_share"
    tools:layout="@layout/fragment_share" />

<fragment
    android:id="@+id/nav_send"
    android:name="com.example.login.ui.send.SendFragment"
    android:label="@string/menu_send"
    tools:layout="@layout/fragment_send" />

acitivty_nav.xml

<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_nav"
    app:menu="@menu/activity_nav_drawer" />

<include
    layout="@layout/app_bar_nav"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

AnonRocketman
  • 178
  • 2
  • 16
  • The Navigation framework should be handling the `Fragment` switch automatically. Did you implement the `OnNavigationItemSelectedListener` on an attempt to debug? Please [edit] your question to add the layout that contains the ``. – Mike M. Sep 03 '19 at 14:50
  • The drawer in a `` must be listed last in order for it to receive touch events properly. Move the `` to after the `` in `activity_nav`. If you did not modify that file yourself initially, then it's likely due to an issue in the 3.5 upgrade for Android Studio that causes XML to be rearranged incorrectly. If that's your case, have a look at [this post](https://stackoverflow.com/q/57591080) to see how to fix that. And please see [this post](https://stackoverflow.com/a/57702735) on the last linked duplicate for more information about the Navigation stuff. – Mike M. Sep 03 '19 at 15:02
  • 1
    It works! thanks for your help and time! Cheers – AnonRocketman Sep 03 '19 at 15:26

1 Answers1

-1

I have updated my answer please check I have added listener

public class NavActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);

    navigationView.setNavigationItemSelectedListener(this);

    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.nav, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_home: {
            Toast.makeText(NavActivity.this, "Anmeldung Fehlgeschlagen", Toast.LENGTH_LONG).show();
            return true;
        }

        case R.id.nav_gallery:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39