how to use Bottom navigation in android
Step1:-
Put Frame Layout in Activity where you need to pass fragment.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_container"
>
</FrameLayout>
Step2: for Bottom navigation
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomnavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/white"
app:menu="@menu/nav_menu" />
Step3:- MainActivity.java
package com.example.firebasedatabase;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import com.example.firebasedatabase.Adapters.ProductAdapter;
import com.example.firebasedatabase.Fragments.Booking_Fragment;
import com.example.firebasedatabase.Fragments.Home_Fragment;
import com.example.firebasedatabase.Fragments.Profile_Fragment;
import com.example.firebasedatabase.models.ProductModel;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
NavigationView nav;
BottomNavigationView bnv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
drawerLayout = findViewById(R.id.drawer_layout);
toolbar= findViewById(R.id.toolbar);
nav= findViewById(R.id.nav_view);
bnv = findViewById(R.id.bottomnavigation);
toggle= new ActionBarDrawerToggle(MainActivity.this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,new Home_Fragment()).commit();
bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
androidx.fragment.app.Fragment temp=null;
switch (item.getItemId())
{
case R.id.nav_home: temp= new Home_Fragment();
break;
case R.id.nav_profile : temp=new Profile_Fragment();
break;
case R.id.nav_logout: temp=new Booking_Fragment();
break;
}
Log.d("cheking","sfd" + item.getItemId());
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,temp).commit();
return true;
}
});
}
}
Step3:- 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/mainmenu"
android:title="Home"/>
<item android:id="@+id/nav_profile" android:title="Profile"
android:icon="@drawable/mainmenu"/>
<item android:id="@+id/nav_logout" android:icon="@drawable/mainmenu"
android:title="Logout"/>
</menu>