I am trying to set up a simple SettingsFragment with one of the Preference customized with an layout xml and set OnClickListener on this customised Preference. For this moment none of the way work. What I miss please ?
I have tryed implements of OnPreferenceClickListener and OnPreferenceTreeClickListener but without succes.
This is my delete_btn_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/delete_account_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.package.SettingsActivity">
<Button
android:id="@+id/delete_account_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="@dimen/size_b"
android:text="@string/delete_account"
android:textColor="@android:color/white"
android:background="@drawable/background_btn"
android:layout_margin="@dimen/size_s" />
</LinearLayout>
And this is my Preference element in <PreferenceScreen .../>
...
<PreferenceCategory app:title="@string/account_category">
<Preference
app:icon="@drawable/ic_delete_primary_dark_24"
app:key="delete_account"
app:title="@string/delete_account"/>
</PreferenceCategory>
...
And this is my SettingsFragment whit one "...ClickListener" witch don't work
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
Preference test = SettingsFragment.this.findPreference("delete_account");
if (test != null) {
test.setLayoutResource(R.layout.delete_btn_layout);
test.setOnPreferenceClickListener(preference -> {
ViewWidgets.showSnackBar(1,getView(),"delete ?");
// do some action
return true;
});
} else {
ViewWidgets.showSnackBar(1,getView(),"Error, Please retray.");
}
}
Any help please.
For those who may encounter a similar problem, I describe the solution that I applied.
The event detection of the click, I inserted in the activity (class SettingsActivity
) that holds the SettingsFragment. With the "deleteUserAccount" method which initializes the process:
public void deleteUserAccount(View view) {
openDialog();
}
The referance for this event I inserted in the button in the old way with the "onClick" method:
<Button
android:id="@+id/delete_account_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="@dimen/size_b"
android:text="@string/delete_account"
android:textColor="@android:color/white"
android:background="@drawable/background_google_btn"
android:layout_margin="@dimen/size_s"
android:onClick="deleteUserAccount"/>