I'd like to show an additional button in a CheckBoxPreference
. I've managed this by subclassing CheckBoxPreference
and a custom layout (using this code as a basis). However, I struggle to add an OnClickListener
to this button in my PreferenceActivity
. I've tried using a global OnClickListener
-variable in the class extending CheckBoxPreference
as well as adding a getView()
-method to the PreferenceActivity
that is supposed to return the Preference's view (to be able to use findViewById(R.id.my_button)
, but both approaches don't work :-(.
Asked
Active
Viewed 333 times
0

Nick
- 3,504
- 2
- 39
- 78
-
In the example `findViewById` is used within the `onBindView` method, why can't you use it there? – dst Aug 06 '13 at 21:15
-
I don't know how to assign an onClickListener defined in the PreferenceActivity to the button in the custom preference class. I can define a *local* onClickListener fine in onBindView, but don't know how to do it across classes. – Nick Aug 07 '13 at 01:21
-
I assume you're able to access the `CheckBoxPreferenceSubclassWithButton`? Just as in the `setIcon` method in the example you've linked it should be possible to have an instance variable for the click listener (equivalent to `mIcon` in the example), then set that listener locally in `onBindView` and create a method `setListener` setting the listener instance variable and calling `notifyChanged`. – dst Aug 07 '13 at 01:37
-
This works perfectly, thanks! I only missed the call to `notifyChanged`. Would you like to post your comment as an answer? – Nick Aug 07 '13 at 08:14
1 Answers
1
Create an instance variable for the click listener within your subclass of CheckBoxPreference
, which holds the listener you wish to set. Create a setter for this variable, calling notifyChanged()
after setting the value. This will cause the onBindView
method to be called on listener changes.
Now you can use this variable from within onBindView
, set your listener to the button's view there (view.findViewById(R.id.yourbuttonid)
).

dst
- 3,307
- 1
- 20
- 27