How to change fragment with the Bottom Navigation Activity?
It's pretty "simple".
- Create your Fragments. Let's say we want 3 fragments;
FragmentA
,FragmentB
andFragmentC
withFragmentA
being the first Fragment we want on the BottomNavigationView. In your Activity, go to the
onNavigationItemSelected
method and change the content to this:private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener(){ @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.frag_a: currentFragment = new FragmentA(); ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content, currentFragment); ft.commit(); return true; case R.id.frag_b: currentFragment = new FragmentB(); ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content, currentFragment); ft.commit(); return true; case R.id.frag_c: currentFragment = new FragmentC(); ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content, currentFragment); ft.commit(); return true; } return false; } };
In your
onCreate()
method, do this:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_client_profile); ft = getSupportFragmentManager().beginTransaction(); currentFragment = new FragmentA(); ft.replace(R.id.content, currentFragment); ft.commit(); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); }
If you do not add FragmentA
in the onCreate()
, the activity is blank when you first launch it.
If you are wondering what R.id.content
refers to, it is the Id of the Framelayout in your activity's layout. It initially contains a TextView
, delete the TextView so it looks like this:
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
Finally, ft
and currentFragment
are defined like this:
Fragment currentFragment = null;
FragmentTransaction ft;
Not sure about elegance, but this works.
The way I would do it is, I would first add three methods similar to this one (each for a single fragment. Replace the layout name and the fragment object to the appropriate fragment that is being switched to):
public void switchToFragment1() {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.your_fragment_layout_name, new Fragment1()).commit();
}
So your switch statement would end up looking like this:
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
switchToFragment1();
break;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
switchToFragment2();
break;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
switchToFragment3();
break;
}
As for switching the fragments by swiping to the sides, I believe you would need a ViewPager.