Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
How to change fragment with the Bottom Navigation Activity?

I created a new project with the "Bottom Navigation Activity":

enter image description here

This is the generated code:

package com.aaron.waller.mrpolitik;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

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:
                    mTextMessage.setText(R.string.title_home);
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
            }
            return true;
        }

    };

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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}

How can I change to new Fragments with the Bottom Bar? For example I have 3 Fragments: Fragment1 Fragment2 and Fragment3 And I want to change to them with the 3 buttons from the Bottom Bar. Also I want that I can switch the Fragments by swiping my finger left and right how can I do that?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

0

It's pretty "simple".

  1. Create your Fragments. Let's say we want 3 fragments; FragmentA, FragmentB and FragmentC with FragmentA being the first Fragment we want on the BottomNavigationView.
  2. 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;
     }
    
    };
    
  3. 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.

about 4 years ago · Santiago Trujillo Report

0

The best way is to use a ViewPager with a FragmentPagerAdapter. Since it cashes the fragments inside it. Use setOnNavigationItemSelectedListener with the BottomNavigationView to listen for the user's clicks. And use viewPager.setCurrentItem(..) to move between pages.

Creating a new fragment everytime the user clicks on an item in the bottom navigation view isn't a good solution (especially when the user clicks on the item of the screen that he is currently at, the solution above will create a new fragment even for this case)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!