Bottom Navigation With Fragment

 Bottom Navigation With Fragment :

1. Create All Fragment :

2.Drawable Icon 

3.menu create

4.bottom_item.xml:



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

    <item
        android:id="@+id/home"
        android:title="Home"
        android:icon="@drawable/baseline_home_24"/>


    <item
        android:id="@+id/notification"
        android:title="Notification"
        android:icon="@drawable/baseline_notifications_24"/>

    <item
        android:id="@+id/person"
        android:title="Person"
        android:icon="@drawable/baseline_person_24"/>

</menu>

5.activity.main.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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:background="@color/black"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/baseline_menu_24"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:layout_marginStart="30dp"
            android:text="@string/app_name"
            android:textSize="20sp"/>

    </androidx.appcompat.widget.Toolbar>


    <FrameLayout
        android:id="@+id/farmelayout"
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_above="@+id/bottomnavigation"
        android:layout_height="match_parent"/>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomnavigation"
        android:background="@color/white"
        android:layout_width="match_parent"
        app:itemRippleColor="#E91E63"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_item"/>

</RelativeLayout>



6.MainActivity.java:


package com.abdulahad.bottomnavigation;

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

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

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

public class MainActivity extends AppCompatActivity {

    BottomNavigationView bottomnavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //......................................................
        //.......................................................
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.farmelayout,new HomeFragment());
        fragmentTransaction.commit();

        bottomnavigation = findViewById(R.id.bottomnavigation);

        bottomnavigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {


                int itemId = item.getItemId();

                if (itemId==R.id.home){
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.farmelayout,new HomeFragment());
                    fragmentTransaction.commit();

                } else if (itemId==R.id.notification) {

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.farmelayout,new NotificationFragment());
                    fragmentTransaction.commit();

                } else if (itemId==R.id.person) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.farmelayout,new PersonFragment());
                    fragmentTransaction.commit();
                }


                return true;
            }
        });

        //......................................................
       //.......................................................








    }
}

Comments

Popular posts from this blog

Flutter Native Splash

Navigation And Bottom Font Style