Androidx Bottom Navigation with Fragments

main.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
	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/_coordinator"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<com.google.android.material.appbar.AppBarLayout
		android:id="@+id/_app_bar"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:theme="@style/AppTheme.AppBarOverlay">
		<androidx.appcompat.widget.Toolbar
			android:id="@+id/_toolbar"
			android:layout_width="match_parent"
			android:layout_height="?attr/actionBarSize"
			android:background="?attr/colorPrimary"
			app:popupTheme="@style/AppTheme.PopupOverlay"/>
	</com.google.android.material.appbar.AppBarLayout>
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:orientation="vertical"
		app:layout_behavior="@string/appbar_scrolling_view_behavior">
		<FrameLayout
			android:id="@+id/frame1"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:padding="8dp"
     android:layout_weight="1"/>
		<com.google.android.material.bottomnavigation.BottomNavigationView
			android:id="@+id/bottomnavigation1"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"/>
	</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

home_fragment.xml

<LinearLayout
	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"
	android:orientation="vertical">
	<TextView
		android:id="@+id/textview1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:padding="8dp"
		android:text="Home fragment"
		android:textSize="20sp"
		android:textColor="#000000"/>
</LinearLayout>

chat_fragment.xml

<LinearLayout
	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"
	android:orientation="vertical">
	<TextView
		android:id="@+id/textview1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:padding="8dp"
		android:text="Chat Fragment"
		android:textSize="20sp"
		android:textColor="#000000"/>
</LinearLayout>

profile_fragment.xml

<LinearLayout
	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"
	android:orientation="vertical">
	<TextView
		android:id="@+id/textview1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:padding="8dp"
		android:text="Profile Fargment"
		android:textSize="20sp"
		android:textColor="#000000"/>
</LinearLayout>

MainActivity.java

package com.my.newproject6;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.bottomnavigation.BottomNavigationView;


public class MainActivity extends  AppCompatActivity  { 


    private Toolbar _toolbar;
    private AppBarLayout _app_bar;
    private CoordinatorLayout _coordinator;

    private FrameLayout frame1;
    private BottomNavigationView bottomnavigation1;
    @Override
    protected void onCreate(Bundle _savedInstanceState) {
        super.onCreate(_savedInstanceState);
        setContentView(R.layout.main);
        initialize(_savedInstanceState);
        initializeLogic();
    }

    private void initialize(Bundle _savedInstanceState) {

        _app_bar = (AppBarLayout) findViewById(R.id._app_bar);
        _coordinator = (CoordinatorLayout) findViewById(R.id._coordinator);
        _toolbar = (Toolbar) findViewById(R.id._toolbar);
        setSupportActionBar(_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        _toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View _v) {
                    onBackPressed();
                }
            });
        frame1 = (FrameLayout) findViewById(R.id.frame1);
        bottomnavigation1 = (BottomNavigationView) findViewById(R.id.bottomnavigation1);

        bottomnavigation1.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    final int _itemId = item.getItemId();
                    switch((int)_itemId) {
                        case ((int)0): {
                                getSupportFragmentManager().beginTransaction().replace(R.id.frame1, new HomeFragmentActivity()).commit();
                                break;
                            }
                        case ((int)1): {
                                getSupportFragmentManager().beginTransaction().replace(R.id.frame1, new ChatFragmentActivity()).commit();
                                break;
                            }
                        case ((int)2): {
                                getSupportFragmentManager().beginTransaction().replace(R.id.frame1, new ProfileFragmentActivity()).commit();
                                break;
                            }
                        default: {
                                getSupportFragmentManager().beginTransaction().replace(R.id.frame1, new HomeFragmentActivity()).commit();
                                break;
                            }
                    }
                    return true;
                }
            });
    }

    private void initializeLogic() {
        bottomnavigation1.getMenu().add(0, 0, 0, "Home").setIcon(R.drawable.ic_home);
        bottomnavigation1.getMenu().add(0, 1, 0, "Chat").setIcon(R.drawable.ic_chat);
        bottomnavigation1.getMenu().add(0, 2, 0, "Profile").setIcon(R.drawable.ic_profile);
        getSupportFragmentManager().beginTransaction().replace(R.id.frame1, new HomeFragmentActivity()).commit();
    }

    @Override
    protected void onActivityResult(int _requestCode, int _resultCode, Intent _data) {

        super.onActivityResult(_requestCode, _resultCode, _data);

        switch (_requestCode) {

            default:
                break;
        }
    }


}

HomeFragmentActivity.java

package com.my.newproject6;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import java.util.HashMap;


public class HomeFragmentActivity extends  Fragment  { 


    private ArrayList<HashMap<String, Object>> data = new ArrayList<>();

    private TextView textview1;
    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater _inflater, @Nullable ViewGroup _container, @Nullable Bundle _savedInstanceState) {
        View _view = _inflater.inflate(R.layout.home_fragment, _container, false);
        initialize(_savedInstanceState, _view);
        initializeLogic();
        return _view;
    }

    private void initialize(Bundle _savedInstanceState, View _view) {

        textview1 = (TextView) _view.findViewById(R.id.textview1);
    }

    private void initializeLogic() {
    }

    @Override
    public void onActivityResult(int _requestCode, int _resultCode, Intent _data) {

        super.onActivityResult(_requestCode, _resultCode, _data);

        switch (_requestCode) {

            default:
                break;
        }
    }


}

ChatFragmentActivity

package com.my.newproject6;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public class ChatFragmentActivity extends  Fragment  { 


    private TextView textview1;
    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater _inflater, @Nullable ViewGroup _container, @Nullable Bundle _savedInstanceState) {
        View _view = _inflater.inflate(R.layout.chat_fragment, _container, false);
        initialize(_savedInstanceState, _view);
        initializeLogic();
        return _view;
    }

    private void initialize(Bundle _savedInstanceState, View _view) {

        textview1 = (TextView) _view.findViewById(R.id.textview1);
    }

    private void initializeLogic() {
    }

    @Override
    public void onActivityResult(int _requestCode, int _resultCode, Intent _data) {

        super.onActivityResult(_requestCode, _resultCode, _data);

        switch (_requestCode) {

            default:
                break;
        }
    }


}

ProfileFragmentActivity.java

package com.my.newproject6;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public class ProfileFragmentActivity extends  Fragment  { 


    private TextView textview1;
    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater _inflater, @Nullable ViewGroup _container, @Nullable Bundle _savedInstanceState) {
        View _view = _inflater.inflate(R.layout.profile_fragment, _container, false);
        initialize(_savedInstanceState, _view);
        initializeLogic();
        return _view;
    }

    private void initialize(Bundle _savedInstanceState, View _view) {

        textview1 = (TextView) _view.findViewById(R.id.textview1);
    }

    private void initializeLogic() {
    }

    @Override
    public void onActivityResult(int _requestCode, int _resultCode, Intent _data) {

        super.onActivityResult(_requestCode, _resultCode, _data);

        switch (_requestCode) {

            default:
                break;
        }
    }


}