0

I need to add a button into a Preference Fragment Layout. I can succesfully get the button in the Preferences layout, but I cannot capture the event of it being clicked.

all other preferences are captured in the onSharedPreferenceChanged Callback, but the button click is not.

What is the best way to add a callback to the button, or have it return it's value in the onSharedPreferenceChanged callback?

I've tried almost all examples I could find on Stackoverflow, but most are designed with a preferenceActivity in mind, not a fragment.

One solution I tried did fire a callback event, but only when that callback was in the host Activity!

Fragment specific help on this appreciated.

2 Answers2

0

This should work in your fragment:

    Button prefBtn = ((Button) view.findViewById(R.id.prefBtn));
    prefBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            // Your code here
        }
    });

EDIT
You also need something like this in your fragment layout:

    <Button
        android:id="@+id/prefBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
Mario Huizinga
  • 780
  • 7
  • 14
  • Thanks for the response. I'm sure I have tried this and it won;t work, as there is no R.id.prefBtn, because the Fragment preference layout is in the 'xml' folder (as required), not the layout, so it doesn't exist to create. Can you confirm this work inside a fragment? – user3263740 Feb 01 '17 at 14:57
  • Yes the code comes directly from my own fragment. You need to give the button an id in the xml layout file of your fragment. – Mario Huizinga Feb 01 '17 at 15:40
  • Hi Mario, When I add the above the app crashes. The Button is returned as null and if I add the button XMl you have suggested, the fragment won;t even load. – user3263740 Feb 01 '17 at 20:16
  • Show your xml layout file, the onCreateView() of the fragment and the error you get in [logcat](https://developer.android.com/studio/debug/am-logcat.html). – Mario Huizinga Feb 02 '17 at 05:51
  • I will do that tonight Mario, but I can remember the error. It's essentially that the list I'm trying to cast the button object from, doesn't contain any Children, hence it returns null for the button object and crashes. – user3263740 Feb 02 '17 at 09:45
  • Hi Mario, I ended up using Jakar's solution at this link: http://stackoverflow.com/questions/5298370/how-to-add-a-button-to-a-preferencescreen-android It works just fine for Fragment Preference as well. Thanks for your help Mario. – user3263740 Feb 02 '17 at 20:56
0

For the whole item click:

preferenceManager.findPreference<Preference>("your_key")?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
    Timber.d("click item")
    return@OnPreferenceClickListener false
}

For only the button: u need a custom preference.

class FooterPreference(context: Context, attrs: AttributeSet): Preference(context, attrs) {

    override fun onBindViewHolder(holder: PreferenceViewHolder?) {
        super.onBindViewHolder(holder)
        holder?.itemView?.findViewById<Button>(R.id.btn)?.setOnClickListener {
            Timber.d("click btn")
        }
    }
}
<packageName.FooterPreference
        app:layout="@layout/layout_preference_footer" />
Krahmal
  • 195
  • 13