Created
January 15, 2021 04:42
-
-
Save thrashedbrain/633498e5d2d56c52fda6a1a25d29e695 to your computer and use it in GitHub Desktop.
Minimal example of bottom navigation view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment