How to set selected item in BottomNavigationView

I am trying to set the default element for the created activity, but it does not work? This is my code:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_userhome); mTextMessage = (TextView) findViewById(R.id.message); profile = (FrameLayout) findViewById(R.id.profile); mall = (FrameLayout) findViewById(R.id.mall); dietplan =(FrameLayout) findViewById(R.id.dietplan); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setSelectedItemId(R.id.dietplan); navigation.setOnNavigationItemSelectedListener (mOnNavigationItemSelectedListener); } 

But it seems that navigation.setSelectedItemId(R.id.dietplan); does not work. Please help me set the default lower nav item:

This is my stack trace (logcat):

 FATAL EXCEPTION: main Process: gym.android.ommsoftware.gym, PID: 1915 java.lang.RuntimeException: Unable to start activity ComponentInfo{gym.android.ommsoftware.gym/gym.android.ommsoftware.gym.Userhome}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at gym.android.ommsoftware.gym.Userhome.onCreate(Userhome.java:57) at android.app.Activity.performCreate(Activity.java:5541) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method) 
+13
android bottomnavigationview
source share
11 answers

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; } } 
+10
source share

Instead of the one you need setChecked(true) this element. Try this code

 mBottomNavigationView=(BottomNavigationView)findViewById(R.id.bottom_nav); mBottomNavigationView.getMenu().findItem(R.id.item_id).setChecked(true); 

BottomNavigationView item is highlighted in the BottomNavigationView .

+24
source share

Kotlin Extension Option Abshishek Answer :

 internal fun BottomNavigationView.checkItem(actionId: Int) { menu.findItem(actionId)?.isChecked = true } // use bottom_navigation.checkItem(R.id.navigation_home) 

This does not call OnNavigationItemSelectedListener .

+2
source share

You can use:

  navigationView?.menu?.findItem(drawableMenuItem.id)?.isChecked = true 

and it will not be the OnNavigationItemSelectedListener event.

+2
source share

It works for me

Action Layout:

 <android.support.design.widget.BottomNavigationView android:id="@+id/bottomNavigation" android:layout_width="match_parent" android:layout_height="wrap_content" app:itemBackground="@color/colorPrimary" app:itemIconTint="@color/tabs" app:itemTextColor="@color/tabs" app:menu="@menu/bottom_navigation_main" /> 

color / tabs.xml:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/not_active" android:state_checked="false"/> <item android:color="@color/active" android:state_checked="true"/> </selector> 

Click callback:

 @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.action_tab0: setFragment(f0); break; case R.id.action_tab1: setFragment(f1); break; } return true; // not false! } 
+1
source share

You can also set the selection to the BottomNavigatioView using the index as follows:

 public void selectBottomNavigationOption(int index) { switch (index) { case 0: index = R.id.action_1; break; case 1: index = R.id.action_2; break; case 2: index = R.id.action_3; break; case 3: index = R.id.action_4; break; } bottomNavigationView.setSelectedItemId(index); } 
+1
source share

Add android:enabled="true" to your BottomNavigation menu items.

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_id" android:enabled="true" android:icon="@drawable/ic_my_icon" android:title="@string/menu_title"/> </menu> 

And in onCreate() configure listeners using bottomNavigationView.setOnNavigationItemSelectedListener(mListener) .

And set the desired item to select by doing bottomNavigationView.selectedItemId = R.id.menu_id .

This will call onNavigationItemSelected() from the NavigationItemSelectedListener each time an action is created.

+1
source share

If you want the selected element to be ignored in the view and not returned as β€œselected”, you can return false after processing the click, in some cases and in some projects you may need to open the action instead of a fragment and this will make the lower element selected after closing the action.

 private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item -> when (item.itemId) { R.id.navigation_shipment -> { currentItem = TAB_INDEX_SHIPMENT val intent = Intent(this, BookShipmentActivity::class.java) startActivity(intent) return@OnNavigationItemSelectedListener false } } false } 
0
source share
 bottomNavigationView.setSelectedItemId(R.id.action_item1); 

where action_item1 is the identifier of the menu item.

0
source share

In my case, I want to highlight the bottomNavigationView element when I click a button from another fragment. Element highlighting works fine when I click on a tab element to switch between fragments. But I would need to include fragments from the local button inside my fragment. In such cases, I cannot access the bottomNavigationView, which is available in MainActivity from my fragment. The application crashes whenever I try to do as shown below from my snippet

Option 1: (crashed)

 void onButtonClick() { View view = MainActivity.bottomNavigationView.findViewById(R.id.navigation_home); view.performClick(); } 

Option 2: (crashed)

  bottomNavigationView.setSelectedItemId(R.id.homeTab) 

Option 3: (crashed)

  MainActivity.mBottomBar.selectTabAtPosition(2); 
0
source share

For your information: for fragment, onCreateView

 BottomNavigationView mBottomNavigationView = getActivity().findViewById(R.id.bottomNavigationView); mBottomNavigationView.setSelectedItemId(R.id.your_item); 
0
source share

All Articles