Click here to Skip to main content
15,891,633 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 :

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

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout>
	<!-- I want to replace this TextView with fragment_home.xml view -->
    <TextView />
	
	<!-- This is the bottom navigation that is already being included -->
    <com.google.android.material.bottomnavigation.BottomNavigationView />
</androidx.constraintlayout.widget.ConstraintLayout>


This is my MainActivity.java :

Java
public class MainActivity extends AppCompatActivity {
    private TextView mTextMessage;

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

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    // I believe that I have to do something here to get the view?
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, new Home()).commit();
                    return true;
                case R.id.navigation_profile:
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, new Profile()).commit();
                    return true;
                case R.id.navigation_notifications:
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, new Notifications()).commit();
                    return true;
                case R.id.navigation_settings:
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, new Settings()).commit();
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        mTextMessage = findViewById(R.id.message);
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}


What I have tried:

I tried replacing
<TextView />
with
<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.
Posted

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