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>