1

i am trying to implement a BottomNavigationView, been successful so far. Currently trying to implement the fragment to fragment movement, which is also successful, but somehow when i move from one fragment[radio] to another[stream] the navigationbar is supposed to highlight the icon[stream] but its not happening is there a way i can set the highlight properties through the fragment itself ? Below is the code and snapshot of my application:

FragmentOne

FragmentTwo

MainActivity.java

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.br.tron.bottombar.RadioFragment;
import com.br.tron.bottombar.StreamFragment;
import com.br.tron.bottombar.InfoFragment;

public class MainActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigationView;
    private Fragment fragment;
    private FragmentManager fragmentManager;

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

        fragmentManager = getSupportFragmentManager();
        fragment = new RadioFragment();
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.main_container, fragment).commit();

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationBar);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.nav_button_one:
                        fragment = new RadioFragment();
                        break;
                    case R.id.nav_button_two:
                        fragment = new StreamFragment();
                        break;
                    case R.id.nav_button_three:
                        fragment = new InfoFragment();
                        break;
                }
                final FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.main_container, fragment).commit();
                return true;
            }
        });
    }

    public void performStreamClick(){
        View view = bottomNavigationView.findViewById(R.id.main_container);
        view.performClick();
    }
}

RadioFragment.java

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class RadioFragment extends Fragment implements Button.OnClickListener  {

    Button buttonman;
    View rootView;

    Activity a;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof Activity) {
            a = (Activity) context;
        }
    }

    public RadioFragment(){
    };

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_player, container, false);
        buttonman = (Button)rootView.findViewById(R.id.buttonman);
        buttonman.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        /*Fragment fragment = new StreamFragment();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();*/
        ((MainActivity)a).performStreamClick();
    }
}

StreamFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class StreamFragment extends Fragment {

    public StreamFragment(){};

    @Override
    public View onCreateView(final LayoutInflater inflater,final ViewGroup container,final Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.fragment_stream, container, false);

    }

}
Khaledonia
  • 2,054
  • 3
  • 15
  • 33
IteratioN7T
  • 363
  • 8
  • 21

1 Answers1

2

You didn't initialize buttonman

In RadioFragment.java

Button buttonman;
View rootView;    
Activity a;

@Override
public void onAttach(Context context) {
super.onAttach(context);

if (context instanceof Activity){
    a=(Activity) context;
}
}

public RadioFragment(){
};

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_player, container, false);
    buttonman = (Button)rootView.findViewById(R.id.yourbuttonid); // initialize here
    buttonman.setOnClickListener(this);
    return rootView;

}
@Override
public void onClick(View v) {
   ((MainActivity)a).performStreamClick();

}

in MainActivity

public void performStreamClick(){
   View view = bottomNavigationView.findViewById(R.id.nav_button_two);
view.performClick();
}

NOTE: you can also do it through interfce

Explanation

You have added

Fragment fragment = new StreamFragment(); 
FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.replace(R.id.main_container, fragment); 
fragmentTransaction.addToBackStack(null); 
fragmentTransaction.commit(); 

This will simply push StreamFragment in main_container framelayout without notify bottomNavigationView.

Here

View view = bottomNavigationView.findViewById(R.id.nav_button_two);
view.performClick();

This line will programmatically perform click on nav_button_two in bottomNavigationView.. then all event will handle by bottomNavigationView .. Then it will highlight the stream icon.

IteratioN7T
  • 363
  • 8
  • 21
Mayur Raval
  • 3,250
  • 6
  • 34
  • 57
  • That was so silly of me.. its working but the navigationView is not highlighting the stream icon which i am supposed to move to any suggestions ? – IteratioN7T Jan 06 '17 at 08:53
  • @IterationN07 ,see added answer. i think you need to set position in navigation menu instead of insert it manually – Mayur Raval Jan 06 '17 at 09:03
  • @IteratioN07, set bottomviewnavgation to 2 position. it will be highlight the stream icon – Mayur Raval Jan 06 '17 at 09:14
  • i implemented your second suggestion performStreamClick(); now by using this i am stuck at the same fragment ... check the question i have updated the code with your suggested code. Also changed the theme of query too. – IteratioN7T Jan 06 '17 at 09:40
  • @IteratioN07, can you post your `XML` ? and bottombar navigation items `xml` – Mayur Raval Jan 06 '17 at 09:41
  • Working perfect .. can you just give me a simple idea on whats happening behind the scenes ? – IteratioN7T Jan 06 '17 at 09:54
  • Your solution vs mine(whats different here?) Fragment fragment = new StreamFragment(); FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.main_container, fragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); – IteratioN7T Jan 06 '17 at 09:57
  • @IteratioN07, see added explanation. – Mayur Raval Jan 06 '17 at 10:03
  • 1
    @IteratioN07, by the way, you fill this is bad idea, then we can also do `setselection` on stream item in `bottomnaviationview` .. see this link http://stackoverflow.com/questions/40202294/set-selected-item-in-android-bottomnavigationview – Mayur Raval Jan 06 '17 at 10:09
  • thats neat .. before i close out i have a query! should i use bundle to send data from one fragment to other ? whats your opinion ? – IteratioN7T Jan 06 '17 at 10:19
  • @IteratioN07, If you are using `Fragments` indepedent than it will communicate through `Activity` we can use interface for it or callbacks .. if you open one `Fragment` from another `Fragment` then we can use `Bundle` for it. – Mayur Raval Jan 06 '17 at 10:23