I have problem implementing Preference.
When i run my app and click on the preference tab nothing happens.
I don’t know what is wrong with the code, Because logcat don’t show anything.
And thanks in advance.
public class Reset extends PreferenceActivity{
private Preference myPreference;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPreference = findPreference("reset");
myPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference arg0) {
Builder alertDialog = new AlertDialog.Builder(Reset.this);
alertDialog.setMessage("Reset?");
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
} });
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
} });
alertDialog.show();
return false;
}
});
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference android:title="Reset Score" android:key="reset">
</Preference>
</PreferenceScreen>
Add this line to your code:
This code is working:
And remember to add your activity: Reset to the Manifest: