Set selected item in Android BottomNavigationView
Seems to be fixed in SupportLibrary 25.1.0 :)
Edit: It seems to be fixed, that the state of the selection is saved, when rotating the screen.
How to Set selected item in BottomNavigationView
Just share my working source code
In Xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView
android:background="@color/colorWhite"
android:id="@+id/gfPrlBnvBtmView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation_main" />
</LinearLayout>
In Java,
public class TestActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener
{
private BottomNavigationView mBtmView;
private int mMenuId;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.test);
mBtmView = (BottomNavigationView) findViewById(R.id.gfPrlBnvBtmView);
mBtmView.setOnNavigationItemSelectedListener(this);
mBtmView.getMenu().findItem(R.id.action_yoga).setChecked(true);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// uncheck the other items.
mMenuId = item.getItemId();
for (int i = 0; i < mBtmView.getMenu().size(); i++) {
MenuItem menuItem = mBtmView.getMenu().getItem(i);
boolean isChecked = menuItem.getItemId() == item.getItemId();
menuItem.setChecked(isChecked);
}
switch (item.getItemId()) {
case R.id.action_food: {
}
break;
case R.id.action_medical: {
}
break;
case R.id.action_yoga: {
}
break;
case R.id.action_postures: {
}
break;
}
return true;
}
}
Set initially selected item index/id in BottomNavigationView
Set the selected menu item ID using setSelectedItemId
:
bottomNavigationView.setSelectedItemId(R.id.item_id);
This method started being available from Android Support Library 25.3.0.
How to change selected item in BottomNavigationView
I think the problem is that you check on a wrong BottomNavigationView
, the following line in onCreateView
is actually inflating a new instance of view without attaching it to the screen:
BottomNavigationView bottom = inflater.inflate(R.layout.activity_main, container, false).findViewById(R.id.mainactivity_bottom_navi);
Instead of inflating a new view, try access the view from its activity:
BottomNavigationView bottom = getActivity().findViewById(R.id.mainactivity_bottom_navi);
Set no item pre-selected in Bottom Navigation view
I combined the solution mentioned by @Ashish Kumar and resolved my query and
private void customizeBottomBar() {
mBottomNavigation.getMenu().getItem(0)
.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_reserve_normal));
changeMenuItemCheckedStateColor(mBottomNavigation, getUnCheckedColor(), getUnCheckedColor());
}
/**
* Method to change the color state of bottom bar view
* @param bottomNavigationView - BottomNavigation view instance
* @param checkedColorHex int value of checked color code
* @param uncheckedColorHex int value of unchecked color code
*/
void changeMenuItemCheckedStateColor(BottomNavigationView bottomNavigationView,
int checkedColorHex, int uncheckedColorHex) {
int[][] states = new int[][]{
new int[]{-android.R.attr.state_checked}, // unchecked
new int[]{android.R.attr.state_checked}, // checked
};
int[] colors = new int[]{
uncheckedColorHex,
checkedColorHex
};
ColorStateList colorStateList = new ColorStateList(states, colors);
bottomNavigationView.setItemTextColor(colorStateList);
bottomNavigationView.setItemIconTintList(colorStateList);
}
Bottom NavigationView change items programmatically
From API 25.3.0,
you can use setSelectedItemId
BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.item_4th);
Related Topics
Write to /Res/Drawable/ on the Fly
Low-Latency Audio Playback on Android
Navigation Drawer: Set as Always Opened on Tablets
Version Conflict Updating to 8.4.0
How to Draw a Route, Along an Existing Road, Between Two Points
How to Send Broadcast from One App to Another App
Android Studio Gradle Dsl Method Not Found: 'Android()' -- Error(17,0)
Appropriate Multiplication of Matrices for Rotation/Translation
How to Read "Adb Shell Dumpsys Alarm" Output
How to Make Custom Dialog with Rounded Corners in Android
Custom Cut/Copy Action Bar for Edittext That Shows Text Selection Handles
Is 'Shouldoverrideurlloading' Really Deprecated? What How to Use Instead
Getting the Battery Current Values for the Android Phone
Starting the Android Emulator in Sdk Tools, Revision 12
Change Actionbar Color Programmatically More Than Once
Android Autocompletetextview with Custom Adapter Filtering Not Working