BottomNavigation View OnNavigationItemSelectedListener is deprecated

Viewed 16458

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.

5 Answers

Just use the OnItemSelectedListener interface:

kotlin

bottomNavigationView?.setOnItemSelectedListener {
    // do stuff

    return@setOnItemSelectedListener true
}

Java

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

    return true;
});
  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!!.

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

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
    }

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;
            }
        });
Related