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
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.
BottomNavigationView. OnNavigationItemSelectedListener. Stay organized with collections Save and categorize content based on your preferences. This interface is deprecated.
onNavigationItemSelected(MenuItem item) Called when an item in the navigation menu is selected.
This does not require an BottomNavigationView.OnNavigationItemSelectedListener to be set. This method is deprecated. Use NavigationBarView.setOnItemSelectedListener (OnItemSelectedListener) instead.
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.
Sign in to your account After upgrade to 1.4.0-beta01, the setOnNavigationItemSelectedListener method is marked as deprecated. So what is the alternate?
Its deprecated according to github sources: BottomNavigationView.setOnNavigationItemSelectedListener In its comment you can read: @deprecated Use {@link NavigationBarView#setOnItemSelectedListener(OnItemSelectedListener)} * instead.
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.
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With