Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class MainActivity extends AppCompatActivity {
private ActionBar toolbar;
private BottomNavigationView bottomNavigationView;
private String[] menus ={"Latest","Favorite","Cart","Profile"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = getSupportActionBar();
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigationview);
bottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
toolbar.setTitle(menus[0]);
loadFragment(new LatestFragment());
}
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()){
case R.id.item1:
fragment = new LatestFragment();
toolbar.setTitle(menus[0]);
loadFragment(fragment);
return true;
case R.id.item2:
toolbar.setTitle(menus[1]);
fragment = new FavoriteFragment();
loadFragment(fragment);
return true;
case R.id.item3:
toolbar.setTitle(menus[2]);
fragment = new CartFragment();
loadFragment(fragment);
return true;
case R.id.item4:
toolbar.setTitle(menus[3]);
fragment = new AccountFragment();
loadFragment(fragment);
return true;
}
return false;
};
};
private void loadFragment(Fragment fragment){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout,fragment);
fragmentTransaction.commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.