Create Bottom Navigation in Android

 //--------------------------------------------Code Detail and Working---------------------------------------------------------

Mamu create

//============================java Code Start =========================================



<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
       
android:title="action_Home"
       
android:id="@+id/Home"
       
android:icon="@drawable/home"
       
/>
    <item
       
android:title="Action_setting"
       
android:id="@+id/view"
       
android:icon="@drawable/ic_baseline_settings_24"/>

</menu>



//======================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

Calling the fragments

//============================java Code Start =========================================

loadFragment(new HomeFragment());

  
binding.bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
      
@Override
      
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
          
switch (item.getItemId()){
              
case R.id.Home:
                   loadFragment(
new HomeFragment());
                   break;

               default
:
                   loadFragment(
new SettingFragment());
                   break;
          
}
          
return true;
      
}
   })
;

private void loadFragment(Fragment f){
    FragmentTransaction  ft =
            getSupportFragmentManager().beginTransaction()
;
   
ft.replace(R.id.frament_container, f);
   
ft.commit();
}

 

//======================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

XML side code create bottom navigation

//============================java Code Start ========================================= 

<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=".bottomnav">

    <FrameLayout
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:id="@+id/frament_container"/>
    <com.google.android.material.bottomnavigation.BottomNavigationView
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/bottom_nav"
       
android:layout_alignParentBottom="true"
       
app:menu="@menu/buttom_nav"
       
/>

</RelativeLayout>

 //======================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

bottom navigation Back state Save

//============================java Code Start =========================================

@Override public void onBackPressed() {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.
fragment_container);
    if
(!(fragment instanceof HomeFragment)) {
        loadFragment(
new HomeFragment());
   
}
   
else { super.onBackPressed();
   
}
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.