Bottom Navigation View

 

STEP 1: Create Menu (xml)


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home" />

<item
android:id="@+id/nav_db"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="Dashboard" />

<item
android:id="@+id/nav_noti"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="Notification" />

</menu>


STEP 2: Main Activity (xml)


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation_menu" />

</RelativeLayout>


STEP 3: Main Activity (java)


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

private FrameLayout fragment_container;
private BottomNavigationView bottom_navigation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment_container = findViewById(R.id.fragment_container);
bottom_navigation = findViewById(R.id.bottom_navigation);

bottom_navigation.setOnNavigationItemSelectedListener(nav_listener);

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
//app start thay tyare direct je fragment show karavavu hoy te mate...

}

private BottomNavigationView.OnNavigationItemSelectedListener nav_listener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

Fragment selectedFragment = null;

switch (item.getItemId()) {

case R.id.nav_home:
selectedFragment = new HomeFragment();
break;

case R.id.nav_db:
selectedFragment = new DashboardFragment();
break;

case R.id.nav_noti:
selectedFragment = new NotificationFragment();
break;
}

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