Click here to Skip to main content
15,790,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am Android Studio beginner and I wish to get some guidance from experienced one. So I've created a project with bottom navigation activity and have 1 default layout which is activity_main.xml. With those ready bottom navigation, I created layouts for each of them by adding a new empty fragment.

I have Home, MainActivity, Notifications, Profile, Settings under my bundle folder (under java). Layout folder has activity_main.xml, fragment_home.xml, fragment_notifications.xml, fragment_profile.xml, fragment_settings.xml.

I want to make fragment_home.xml layout to be first displayed in the container of activity_main.xml

What I understand about the layout is, activity_main.xml now acting like a master container where it's elements (such as button, textinput) will keep on displayed even after I change the view screen by clicking one of the menu from bottom navbar.

This is what I have in activity_main.xml :

<!-- I make the tags short just for sample purpose -->

<?xml version="1.0" encoding="utf-8"?>
	<!-- I want to replace this TextView with fragment_home.xml view -->
    <TextView />
	<!-- This is the bottom navigation that is already being included -->
    < />

This is my :

public class MainActivity extends AppCompatActivity {
    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                    // I believe that I have to do something here to get the view?
                    getSupportFragmentManager().beginTransaction().replace(, new Home()).commit();
                    return true;
                    getSupportFragmentManager().beginTransaction().replace(, new Profile()).commit();
                    return true;
                    getSupportFragmentManager().beginTransaction().replace(, new Notifications()).commit();
                    return true;
                    getSupportFragmentManager().beginTransaction().replace(, new Settings()).commit();
                    return true;
            return false;

    protected void onCreate(Bundle savedInstanceState) {
        BottomNavigationView navView = findViewById(;
        mTextMessage = findViewById(;


What I have tried:

I tried replacing
<TextView />
<include layout="@layout/fragment_home" />
from activity_main.xml but like I said, it will be displayed in every screen when switched.

How should I load the home fragment as default view and replaced with other view based on the navigation selection? Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900