2

I use bottom bar in my application .but I want to change default item(tab) in bottom bar.I do not use any external library.just how I can set my favorite tab when application runs.

My codes:

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.action_home:
                selectedFragment = Fragment1.newInstance();
                break;
            case R.id.action_product:
                selectedFragment = Fragment2.newInstance();
                break;
            case R.id.action_order:
                selectedFragment = Fragment3.newInstance();
                break;
            case R.id.action_contact:
                selectedFragment = Fragment4.newInstance();
                break;
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.scrollView, selectedFragment);
        transaction.commit();
        return true;
    }

});
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54

4 Answers4

2
bottomNavigationView.setSelectedItemId(R.id.action_home);

This will work

Rnld
  • 68
  • 9
2

This should work (I use it with api 19+)

bottomNavigationView.getMenu().getItem(index).setChecked(true);
pgentili
  • 659
  • 5
  • 13
1
    for (int i = 0, size = menu.size(); i < size; i++) {
        MenuItem item = menu.getItem(i);
        item.setChecked(item.getItemId() == 0);
    }
0

If you want a specific item to be opened when the app is started. Just try this code:

bottomNavigationView.setSelectedItemId(R.id.your_id);

But it is for API 25 and higher only.

TREAF ALSHEMERI
  • 409
  • 5
  • 12