Skip to content

Instantly share code, notes, and snippets.

@Velmm
Created December 27, 2017 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Velmm/4a0d8e6bd7cfca3efadcd2d5c7d60082 to your computer and use it in GitHub Desktop.
Save Velmm/4a0d8e6bd7cfca3efadcd2d5c7d60082 to your computer and use it in GitHub Desktop.
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