skip to Main Content

I have ,

  1. Two fragments (Fragment1,Fragment2).
  2. Two buttons (Btn1 in frag1 , Btn2 in frag2).
  3. A bottom navigation view.

On clicking the Btn in frag1 , we will be directed to frag2.

On clicking the Btn in frag2 , we will be directed to frag1.

while directing from frag(1->2 or 2->1), the icon on the bottom navigation view stays the same

Is there a way to highlight that icon with respect to the loaded fragment?

On Click Listener for btn in frag1

this.getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.body_container, new Fragment2()).commit();

On Click Listener for btn in frag2

this.getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.body_container, new Fragment1()).commit();

OnItemSelectedListener for bottom nav in MainActivity Code

((BottomNavigationView)findViewById(R.id.bottom_nav)).setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener(){
            Fragment clickedFragment = null;
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) { 
                    case R.id.one:  clickedFragment = new Fragment1();  break;
                    case R.id.two:  clickedFragment = new Fragment2();  break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.body_container, clickedFragment).commit();
                return true;
            }
        });

3

Answers


  1. Chosen as BEST ANSWER
    1. Create a class temp in the same package as the main activity is in and add the below field to it.

      public static BottomNavigationView bottomNavigationView;
      

    Just go through the code, I explained in comments.

    1. Main Activity Code

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
      
           BottomNavigationView bottomNavView = temp.bottomNavigationView = findViewById(R.id.bottom_nav);
      
           //This line, initially when the app is opened, displays the fragment1 by default
           getSupportFragmentManager().beginTransaction().replace(R.id.body_container, new Fragment1()).commit();
      
           bottomNavView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener(){
               @Override
               public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                   Fragment clickedFragment = null;
                   switch (item.getItemId()) {
                       case R.id.one:  clickedFragment = new Fragment1();  break;
                       case R.id.two:  clickedFragment = new Fragment2();  break;
                   }
                   //This line replaces the fragment when user clicks on icon of bottom navigation view
                   getSupportFragmentManager().beginTransaction().replace(R.id.body_container, clickedFragment).commit();
                   return true;
               }
           });
       }
      

    3.OnViewCreated in Fragment1

    @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            view.findViewById(R.id.btn_to_frag2).setOnClickListener(l->{
                temp.bottomNavigationView.setSelectedItemId(R.id.two);
            });
        }
    

    4.OnViewCreated in Fragment2

    @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            view.findViewById(R.id.btn_to_frag1).setOnClickListener(l->{
                temp.bottomNavigationView.setSelectedItemId(R.id.one);
            });
        }
    

  2. The setOnItemSelectedListener is only triggered whenever the selected item of the BottomNavigationView changes. What you are currently doing is changing the current displayed fragment. Instead of changing the fragment, you should set the selected item of the BottomNavigationView instead. Something like this should work

    ((BottomNavigationView) findViewById(R.id.bottom_nav)).setSelectedItemId(R.id.your_menu_item_id);
    
    Login or Signup to reply.
  3. Just make sure that the setOnItemSelectedListener has a return value of true
    It works fine without setting the setSelectedItemId

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search