Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom navigation On navigation item select listener not working

I want to build bottom navigation with activity for my app and after completing my bottom nav. layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/Bottomnav"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:layout_gravity="bottom"
            android:background="#FFFFFF"
            app:menu="@menu/bottomnav_menus">

        </android.support.design.widget.BottomNavigationView>
    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

Next I gone into my Main activity for implementing onnavigationitemselectlistner, but after doing this method without any error my bottom nav. not working.

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId())
        {
            case R.id.live:
                Intent intent = new Intent(MainActivity.this, LiveScore.class);
                startActivity(intent);
                return true;
        }
        return false;
    }
});

my menu items :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:title=""
        android:id="@+id/home"
        android:icon="@drawable/baseline_home_black_24dp"
        android:orderInCategory="1"/>

    <item
        android:title=""
        android:id="@+id/live"
        android:icon="@drawable/baseline_live_tv_black_24dp"
        android:orderInCategory="2"/>
    <item
        android:title=""
        android:id="@+id/video"
        android:icon="@drawable/baseline_video_library_black_24dp"
        android:orderInCategory="3"/>
</menu>

Please Comment If you want any other info.

like image 237
Harsh Tiwari Avatar asked Jan 14 '19 11:01

Harsh Tiwari


3 Answers

make sure you are adding below code

navView.setOnNavigationItemSelectedListener(new ...);
navView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
            @Override
            public void onNavigationItemReselected(@NonNull MenuItem item) {
                Toast.makeText(MainActivity.this, "Reselected", Toast.LENGTH_SHORT).show();
            }
        });

after

 NavigationUI.setupWithNavController(navView, navController);  

this

like image 188
sandeep mali Avatar answered Nov 10 '22 13:11

sandeep mali


Try below, it works fine for me

menu xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_home"
    android:icon="@drawable/ic_home_blue_48dp"
    android:title="Home" />
<item
    android:id="@+id/action_menu"
    android:icon="@drawable/ic_apps_black_24dp"
    android:title="Menu"
     />
<item
    android:id="@+id/action_msg"
    android:icon="@drawable/ic_chat_black_24dp"
    android:title="Message Inbox"
   />
</menu>

xml

 <android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary"
    app:itemIconTint="@drawable/bottom_nav_colors"
    app:itemTextColor="@drawable/bottom_nav_colors"
    app:menu="@menu/bottom_navigation_items"/>

bottom listener

BottomNavigationView bottomNavigationView;
bottomNavigationView.setOnNavigationItemSelectedListener(new 
BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item)
        {
            Fragment selectedFragment = null;
            switch (item.getItemId())
            {
                case R.id.action_home:
                   //perform action
                    break;
                case R.id.action_menu:
                    //perform action
                    break;
                case R.id.action_msg:
                   //perform action
                    break;
            }
            FragmentTransaction transaction = 
    getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container, selectedFragment);
            transaction.commit();
            return true;
        }
    });
like image 34
Android Geek Avatar answered Nov 10 '22 14:11

Android Geek


replace this

  switch (item.getItemId())
        {
            case R.id.live:
                Intent intent = new Intent(MainActivity.this, LiveScore.class);
                startActivity(intent);
                return true;

        }
        return false;

by this

  switch (item.getItemId())
            {
                case R.id.live:
                       Intent intent = new Intent(MainActivity.this,LiveScore.class);
                    startActivity(intent);
                    break;
            }
            return true;
like image 1
Mohammad Sayed Avatar answered Nov 10 '22 12:11

Mohammad Sayed