33

I am trying out the Android's BottomNavigationView implementation as per Material Design

However, on the MainActivity code I am getting a warning that OnNavigationItemSelectedListener is deprecated - see the below snapshot

enter image description here

Have tried get an alternative method to work with the BottomNavigationView but I cannot find it.

Looking for help from anyone with a way out but in the meantime I have matched my BottomView's menu items ids with the fragment destination ids and I successfully achieved Navigation but with a limitation of not being able to update my toolbar title with the Fragment's name.

Tonnie
  • 4,865
  • 3
  • 34
  • 50

6 Answers6

65

Just use the OnItemSelectedListener interface:

kotlin

bottomNavigationView?.setOnItemSelectedListener { item ->
    // do stuff

    return@setOnItemSelectedListener true
}

Java

bottomNavigationView.setOnItemSelectedListener(item -> {
    // do stuff

    return true;
});
Boken
  • 4,825
  • 10
  • 32
  • 42
Ali Moghadam
  • 1,026
  • 1
  • 9
  • 8
18
  binding!!.bottomNavigationView.setOnItemSelectedListener{
        when (it.itemId) {
            R.id.home_menu -> {
                openFragment(HomeFragment.newInstance("", ""))
                return@setOnItemSelectedListener true
            }
            R.id.deals -> {
                openFragment(DealFragment.newInstance("", ""))
                return@setOnItemSelectedListener true
            }
            R.id.history -> {
                openFragment(HistoryFragment.newInstance("", ""))
                return@setOnItemSelectedListener true
            }
            R.id.page_2 -> {
                openFragment(AccountFragment.newInstance("", ""))
                return@setOnItemSelectedListener true
            }
        }
        false
    }

Try this!!.

Umesh Yadav
  • 1,042
  • 9
  • 17
5

You can use Bubble Navigation insted of BottomNavigationView.

Bubble Navigation is a light-weight library to easily make beautiful Navigation Bars with a ton of customization options.

enter image description here

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Ali Salehi
  • 164
  • 2
  • 9
4

This is a solution for Kotlin. Make sure to return@setOnItemSelectedListener true, this line of code change the color of menu items in your navigation view.

bottomNavigationView.setOnItemSelectedListener {
        when (it.itemId) {
            R.id.firstId -> {
                // Write your code here
            }
            R.id.secondID-> {
                // Write your code here
            }
        }
        return@setOnItemSelectedListener true
    }
4

OnNavigationItemSelectedListener is now deprecate use setOnItemSelectedListener below some example -

bottomNav.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.bottom_m_home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.bottom_m_reward:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.bottom_m_wallet:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.bottom_m_share:
                        viewPager.setCurrentItem(3);
                        break;
                    default:
                        viewPager.setCurrentItem(0);
                }
                return true; // return true;
            }
        });
Kumar Santanu
  • 603
  • 1
  • 7
  • 14
2

I couldn't get setOnItemSelectedListener to work since I was also using setupWithNavController so I switched to addOnDestinationChangedListener and it worked.

navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
     if(destination.getId()==R.id.menu_item_id){
      //navigate to other activity
     }
})

SOURCE

francis
  • 3,852
  • 1
  • 28
  • 30