AndroidX : BottomNavigationBar with Constraint Layout and Fragments

implementation 'com.google.android.material:material:1.1.0-alpha10'
Adding vector asset to drawable folder
Choosing from different Icons
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_icon1"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home"/>
<item
android:id="@+id/nav_icon2"
android:icon="@drawable/ic_format_list_bulleted_black_24dp"
android:title="List"
/>
<item
android:id="@+id/nav_icon3"
android:icon="@drawable/ic_camera_alt_black_24dp"
android:title="Scan"
></item>
<item
android:id="@+id/nav_icon4"
android:icon="@drawable/ic_notifications_active_black_24dp"
android:title="Notification"
></item>

</menu>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"

app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintTop_toTopOf="parent" />
private BottomNavigationView.OnNavigationItemSelectedListener itemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;

switch (item.getItemId()){
case R.id.nav_icon1:
selectedFragment = new ListFragment();
break;
case R.id.nav_icon2:
selectedFragment = new HomeFragment();
break;
case R.id.nav_icon3:
selectedFragment = new CameraFragment();
break;
case R.id.nav_notification:
selectedFragment = new NotificationFragment();
break;

}

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();

return true;

}
};
BottomNavigationBar With Fragments and Constraint Layout.

--

--

Software Engineer• AI/ML• Mobile & Web Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store