first you must get view from the button on your 1st fragment, using this code:
btnChFrag = info.findViewById(R.id.button1);
(info is the view of your fragment layout)
then you need to set onclicklistener on that button
btnChFrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 newFragment = new Fragment2();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, newFragment);
ft.commit();
}
change R.id.container with your container on your MainActivity view
so your code on fragment1 will be:
public class Fragment1 extends Fragment {
private Button btnChFrag
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View info = inflater.inflate(R.layout.info, container, false);
btnChFrag = info.findViewById(R.id.button1);
//set button onclicklistener here
return info;
}
-----Edit------
sorry I totally forgot you want to do it with button, my bad
this is the code of fragment1
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View info = inflater.inflate(R.layout.info, container, false);
Button btn1 = (Button)rootView.findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)Fragment1.this.getActivity()).Tab.setCurrentItem(2);
}
});
return info;
}
tried it, and it works :)