Don't loop or get reference to the button.
Simply make your activity implement OnClickListener and set it as click listeners for the buttons in all your fragments .. In the fragment
yourBtn.setOnClickListener(((YourActivityType) getActivity()));
another solution without the implements keyword
is to have a public method in your activity, for example, setButtonAction(Button btn)
public void setButtonAction(Button btn) {
btn.setOnClickListener(myClickListener);
}
and declare myClickListener as variable in youractivity
Same idea without having your activity to implement clickLitener