alexward1230
alexward1230

Reputation: 589

Android: Preference button clicked?

I have a shared preference screen with check box's list preferences etc. Now I would like to add a button in the shared preference screen that when I click it a dialog pops up. I do this and it looks perfect to make the button on the preference screen:

<Preference
        android:key="key"
        android:summary="make pop up dialog"
        android:title="dialog" />

But now I don't know how to get when it is clicked I tried and I cant use onSharedPreferenceChanged because no preferences are changed its just clicked. So what would I do to get when the preference button is clicked? Thanks for the help.

Upvotes: 5

Views: 2840

Answers (1)

IvanRF
IvanRF

Reputation: 7265

First, your PreferenceActivity must implement Preference.OnPreferenceClickListener. Then, under the onCreate function call

findPreference(YOUR_KEY_PREF).setOnPreferenceClickListener(this);

and add this function

@Override
public boolean onPreferenceClick(Preference preference) {
    String key = preference.getKey();
    if(key.equals(YOUR_KEY_PREF)){
        showYourDialog();
        return true;
    }
    return false;
}

In your case, YOUR_KEY_PREF is "key"

Upvotes: 6

Related Questions