MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // set selected item bottomNavigationView.getMenu().getItem(1).setChecked(true); viewPagerMain.setCurrentItem(1); // bind bottomNavigationView and viewPager bottomNavigationView.setOnNavigationItemSelectedListener(bottomNavigationView_OnNavigationItemSelectedListener); viewPagerMain.addOnPageChangeListener(viewPagerMain_OnPageChangeListener); } BottomNavigationView.OnNavigationItemSelectedListener bottomNavigationView_OnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.item_bn_graph: viewPagerMain.setCurrentItem(0); break; case R.id.item_bn_bmi: viewPagerMain.setCurrentItem(1); break; case R.id.item_bn_table: viewPagerMain.setCurrentItem(2); break; } return true; } }; ViewPager.OnPageChangeListener viewPagerMain_OnPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: bottomNavigationView.getMenu().getItem(0).setChecked(true); break; case 1: bottomNavigationView.getMenu().getItem(1).setChecked(true); break; case 2: bottomNavigationView.getMenu().getItem(2).setChecked(true); break; } } @Override public void onPageScrollStateChanged(int state) { } };
References
https://github.com/mhdr/AndroidSamples/tree/master/067