Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomNavigation View OnNavigationItemSelectedListener is deprecated

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.

like image 745
Tonnie Avatar asked May 21 '21 17:05

Tonnie


People also ask

Is BottomNavigationView deprecated?

BottomNavigationView. OnNavigationItemSelectedListener. Stay organized with collections Save and categorize content based on your preferences. This interface is deprecated.

What is onNavigationItemSelected?

onNavigationItemSelected(MenuItem item) Called when an item in the navigation menu is selected.

Is it possible to set bottomnavigationview onitemselectedlistener?

This does not require an BottomNavigationView.OnNavigationItemSelectedListener to be set. This method is deprecated. Use NavigationBarView.setOnItemSelectedListener (OnItemSelectedListener) instead.

What is bottomnavigationview in Android?

com.google.android.material.bottomnavigation.BottomNavigationView. Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation. Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap.

Is the setonnavigationitemselectedlistener method deprecated?

Sign in to your account After upgrade to 1.4.0-beta01, the setOnNavigationItemSelectedListener method is marked as deprecated. So what is the alternate?

Is the bottomnavigationview class deprecated?

Its deprecated according to github sources: BottomNavigationView.setOnNavigationItemSelectedListener In its comment you can read: @deprecated Use {@link NavigationBarView#setOnItemSelectedListener(OnItemSelectedListener)} * instead.


5 Answers

Just use the OnItemSelectedListener interface:

kotlin

bottomNavigationView?.setOnItemSelectedListener {
    // do stuff

    return@setOnItemSelectedListener true
}

Java

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

    return true;
});
like image 171
Ali Moghadam Avatar answered Oct 20 '22 18:10

Ali Moghadam


  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!!.

like image 41
Umesh Yadav Avatar answered Oct 20 '22 18:10

Umesh Yadav


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

like image 42
Ali Salehi Avatar answered Oct 20 '22 17:10

Ali Salehi


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
    }
like image 21
Melikbekyan Ashot Avatar answered Oct 20 '22 19:10

Melikbekyan Ashot


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;
            }
        });
like image 28
Kumar Santanu Avatar answered Oct 20 '22 19:10

Kumar Santanu