i am encapsulating stuff into a fragment at the moment and run into a problem that is hard to google. Inside my fragment are some buttons with onClick attributes but they are called on the Activity rather the fragment from the android system - this makes encapsulating a bit clumsy. Is there a way to have the reflection stuff from onClick to call on the fragment? The only solution to this I see at the moment is not to use onClick in the xml and set click-listeners inside the fragment via code.
-
See [How to handle button clicks using the xml onClick within Fragments?](http://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments) for a more in-depth discussion. – Jim Hurne Mar 31 '13 at 08:44
6 Answers
I spoke to some googlers @ #adl2011 - they recognize the problem and perhaps there will be a fix of that in the future. Until then - one should use .setOnClick in the Fragment.

- 39,001
- 44
- 144
- 244
-
22Huh, you are the first honest person on S.O. to say this. Everyone else is trying to cover up this problem with ridiculous explanations. – IgorGanapolsky May 15 '12 at 00:16
-
I dont know if there is a solution yet to this problem, but this becomes problamatic when trying to access a checkbox since OnClickListener wont work there. – aruwen Aug 01 '12 at 14:56
-
4Jeez when Google says they’ll fix it, they’ll fix it. There’s no need to remind them every 6 months! – Saifur Rahman Mohsin Oct 13 '15 at 07:26
-
2
-
When you open the fragment you want to handle the onClick event on, try replacing it rather than just adding it on top of an activity. – display name Aug 08 '18 at 20:03
-
It's been 10 years now . The only solution they have come up with is data binding . – Faisal Jan 17 '21 at 22:15
The problem is that when layout's are inflated it is still the hosting Activity that is receiving the button clicks, not the individual Fragments.
I prefer using the following solution for handling onClick events. This works for Activity and Fragments as well.
public class StartFragment extends Fragment implements OnClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_start, container, false);
Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.StartButton:
...
break;
}
}
}
Then problem is gone.

- 33,936
- 20
- 234
- 300
It works for me
Add import:
import android.view.View.OnClickListener;
Fragment.java
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return rootView;
}

- 77
- 1
- 1
ISSUE:
1.In XML onClick attribute will call activity's public method.
2.Fragment's public method not called.
3.Fragment reusability.
SOLUTION:
In Fragment's layout add this to the View.
android:onClick="onFragmentViewClick"
In each activity the fragment may belong..
public void onFragmentViewClick(View v) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (fragment != null && fragment.isVisible()) {
if (fragment instanceof SomeFragment) {
((SomeFragment) fragment).onViewClicked(v);
}
}
}
In the Fragment include this method..
public void onViewClicked(View v) {
switch (v.getId()) {
case R.id.view_id:
Log.e("onViewClicked", "yehhh!!");
break;
}
}

- 1,048,767
- 296
- 4,058
- 3,343

- 1,261
- 1
- 12
- 11
Try this...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_ipadditional_users, container, false);
FloatingActionButton button7 = (FloatingActionButton) rootView.findViewById(R.id.fab_ip_additional_user);
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), IPAdditionalUsersEdit.class);
startActivity(intent);
}
});
return rootView;

- 2,978
- 26
- 31
You could have the listener that is being called in the activity forward the call onto a listener in the fragment. You should have a reference to the fragment inside of the FragmentActivity to pass the call on. You will have to cast to call the method or have your fragment implement an interface you define. I know that isn't the best solution but it will work. You could also use the tag of a button to specify the method name to call if you wanted. Hope this helps a bit.

- 24,509
- 9
- 59
- 94
-
Yes... not a clean or convenient solution, trying to go bottom to top on falls... – Frederic Yesid Peña Sánchez Apr 11 '13 at 15:55