I cant get this to work I want the sign out Button
on this preferences
screen to have a ClickListener
This is how it looks like:
Here´s the code and the buttonView
is always NULL:
class PreferencesFragment : PreferenceFragmentCompat() {
lateinit var activity: Context
private var prefs: SharedPreferences = BleApplication.getInstance().getDefaultSharedPreferences()
override fun onAttach(context: Context?) {
super.onAttach(context)
activity = requireActivity()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val buttonView = view.findViewById<View>(R.id.btn_sign_out)
if (buttonView != null) {
buttonView.setOnClickListener {
Toast.makeText(getActivity(), "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
// Hide the divider
/* setDivider(ColorDrawable(Color.TRANSPARENT))
setDividerHeight(0)*/
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.app_prefs)
}
}
I also tried the kotlinx.android.synthetic but same problem there
Here´s the xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:layout="@layout/pref_category_text"
android:title="@string/pref_category_remote_battery_title">
<SwitchPreferenceCompat
android:key="@string/pref_key_category_remote_battery_switch"
android:title="@string/pref_category_remote_battery_switch_title"
android:summary="@string/pref_category_remote_battery_switch_summ"/>
</PreferenceCategory>
<PreferenceCategory
android:layout="@layout/pref_category_text"
android:title="@string/pref_category_sign_out_title">
<Preference
android:key="@string/pref_key_category_signed_out"
android:widgetLayout="@layout/pref_category_sign_out_button"
android:title="@string/pref_category_sign_out_button_title"
android:summary="@string/pref_category_sign_out_buttom_summ"/>
</PreferenceCategory>
</PreferenceScreen>
Here is the "@layout/pref_category_sign_out_button"
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn_sign_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:text="@string/pref_category_sign_out_title" />
</LinearLayout>
Since your
Fragment
extends fromPreferenceFragmentCompat
, you should not try to set aView.OnClickListener
but overridePreferenceFragmentCompat.onPreferenceTreeClick()
instead. According to the documentation, this method is ...Code example in Java:
Code example in Kotlin (I hope I can trust Android Studio :P)
This will enable you to catch click events for the
Preference
but not for theButton
.In order to do that as well, one can use a custom
Preference
and overrideonBindViewHolder(PreferenceViewHolder)
. SincePreferenceViewHolder
- similar toRecyclerView.ViewHolder
- has a fielditemView
which contains the inflated layout, here is a good opportunity to set our ownView.OnClickListener
.SignOutPreference
extends fromTwoStatePreference
(in the com.android.support:preference-v7 library) because replacing theCheckBox
widget with your customButton
requires only to set theandroid:widgetLayout
attribute, just like you do in your code snippet:SignOutPreference.java