Skip to content

Instantly share code, notes, and snippets.

@thrashedbrain
Created January 15, 2021 04:42
Show Gist options
  • Save thrashedbrain/633498e5d2d56c52fda6a1a25d29e695 to your computer and use it in GitHub Desktop.
Save thrashedbrain/633498e5d2d56c52fda6a1a25d29e695 to your computer and use it in GitHub Desktop.
Minimal example of bottom navigation view
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigationView = findViewById(R.id.navView);
loadFragment(new RouletteFragment());
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_coin:
loadFragment(new CoinFragment());
return true;
case R.id.nav_dice:
loadFragment(new DiceFragment());
return true;
default:
loadFragment(new RouletteFragment());
return true;
}
}
});
}
private void loadFragment(Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragment);
ft.commit();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
app:menu="@menu/menu"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_roulette"
android:title="@string/nav_roulette_str"
android:icon="@drawable/ic_wheel"/>
<item
android:id="@+id/nav_coin"
android:title="@string/nav_coin_str"
android:icon="@drawable/ic_coin"/>
<item
android:id="@+id/nav_dice"
android:title="@string/nav_dice_str"
android:icon="@drawable/ic_dice"/>
</menu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment