Preference Activity on Preference Click Listener

Related searches

I am building a Preference Activity where most of the preferences in the list will be executing code and not modifying a SharedPreference directly. My preferences.xml file looks like this.

<PreferenceCategory
    android:title="Connection" >

    <Preference
        android:id="@+id/settings_connectToNewComputer"
        android:key="connectToNewComputer"
        android:summary="Currently connected to:"
        android:title="Connect to new computer" />

    <Preference
        android:id="@+id/removeDevice"
        android:key="removeDevice"
        android:summary="Remove this device from the computer's whitelist"
        android:title="Remove this device from computer" />

</PreferenceCategory>

<PreferenceCategory
    android:title="About" >

    <Preference
        android:id="@+id/settings_About"
        android:key="about"
        android:summary="About me and my thanks to those who made this app great"
        android:title="About Hue Pro" />

    <Preference
        android:id="@+id/contact"
        android:key="contact"
        android:summary="Contact me with comments, bugs, and suggestions for updates"
        android:title="Contact me" />

</PreferenceCategory>

My goal is to have a block of code executed when a one of these preferences are clicked. Similar to the "Clear search history" in the Google Play settings preference menu. (http://i.imgur.com/qnHbJX9.png)

Does anyone know how to make this possible?

I have to add that I have tried using findPreference("KeyNameHere") but it always returns null.

Thank you!


Edit:

I added in this code and implemented OnPreferenceClickListener:

@Override
public boolean onPreferenceClick(Preference preference) {
    return false;
}

But this method never gets called. Is there another way to do this?


Edit 2:

I have found that if I take out the PreferenceCategory tags so I am left with this:

<Preference
    android:id="@+id/settings_connectToNewComputer"
    android:key="connectToNewComputer"
    android:summary="Currently connected to:"
    android:title="Connect to new computer" />

<Preference
    android:id="@+id/removeDevice"
    android:key="removeDevice"
    android:summary="Remove this device from the computer's whitelist"
    android:title="Remove this device from computer" />

<Preference
    android:id="@+id/settings_About"
    android:key="about"
    android:summary="About me and my thanks to those who made this app great"
    android:title="About Hue Pro" />

<Preference
    android:id="@+id/contact"
    android:key="contact"
    android:summary="Contact me with comments, bugs, and suggestions for updates"
    android:title="Contact me" />

and call this:

getPreferenceScreen().setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            return false;
        }
    });

then I actually get a response from the click event. The only down side is I have to remove the preference grouping. Anyone know why this is and any way to fix it?

Implement OnPreferenceClickListener and in the onPreferenceClick

@Override
public boolean onPreferenceClick (Preference preference)
{
    String key = preference.getKey();
    // do what ever you want with this key
}

Preference.OnPreferenceClickListener, Preference : The Preference that was clicked. Returns. boolean, True if the click was handled. onPreferenceClick(Preference preference) Called when a preference has been clicked.

Maybe this could not be useful for OP, but could be useful for someone else. I'd like to write a sort of summary; in general, you can follow mainly three ways: 1) you can find your preference somewhere in your code with

Preference examplePreference = findPreference(KEY_EXAMPLE_PREFERENCE);

and then you can add a click listener and override its on click method with

examplePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {
        // handle click here
    }
});

This has to be done for every preference whose clicks you want to listen to 2) You can implement Preference.OnPreferenceClickListener interface in your settings fragment/activity and override onPreferenceClick just once, by using a switch construct or a if-else if-else if-... construct and merging all the single handlings; it should be something like:

@Override
public boolean onPreferenceClick(Preference preference) {
    switch (preference.getKey()) {
        case KEY_EXAMPLE_PREFERENCE: {
            // handle click here
        }
        break;
        case ...
    }
}

Then, you still have to find each preference but you can simply call on each of them

setOnPreferenceClickListener(this);

(I think the OP's implementation didn't work (his method wasn't called) because of this last part) we pass "this" as parameter because we implemented the click listener interface

3) (which I think is the easiest) you can override

onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)

in your preference fragment/activity without implementing any other interface and there you can copy the switch of the if-else if-... construct of option 2); the main advantage in that you shouldn't need to find each preference and to call on them setOnPreferenceClickListener.

Hope this will be useful for someone!

android.preference.PreferenceActivity, preference.Preference.OnPreferenceClickListener. These examples are extracted from open source projects. You can vote up the ones you like or vote down the� This page provides Java code examples for android.preference.Preference.OnPreferenceClickListener. The examples are extracted from open source Java projects.

Just override:

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {

    String key = preference.getKey();
    ...

    return super.onPreferenceTreeClick(preferenceScreen, preference);
}

Java Code Examples for android.preference.Preference , Preference.OnPreferenceClickListener. The following code examples are extracted from open source projects. You can click to vote up the examples that are� androidx.activity.result.contract. Overview; Called when a preference has been clicked. true if the click was handled:

You could also find the preference and set the click listener.

Preference connectToNewComputer= findPreference("connectToNewComputer");
connectToNewComputer.setOnPreferenceClickListener(this);

Java Code Examples of android.preference.Preference , The following are top voted examples for showing how to use android.preference.Preference.OnPreferenceClickListener. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

I came up with my own (what I believe is really messed up) solution; but it works.

for(int x = 0; x < getPreferenceScreen().getPreferenceCount(); x++){
        PreferenceCategory lol = (PreferenceCategory) getPreferenceScreen().getPreference(x);
        for(int y = 0; y < lol.getPreferenceCount(); y++){
            Preference pref = lol.getPreference(y);
            pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){

                @Override
                public boolean onPreferenceClick(Preference preference) {
                    return false;
                }

            });
        }
    }

So what I have learned is there is a hierarchical system that works like: PreferenceScreen has children PreferenceCategory has children Preference, as you can see in the XML file. My problem was I could not set the preferences' onClickListeners directly from the PreferenceScreen. So I made two for loops that will get down to each Preference and set an OnPreferenceClickListener for each and every one of them. Messy, but works finally.

Overview; Android Platform; Android Support Library; AndroidX; AndroidX Test; AndroidX Constraint Layout; Architecture Components; Jetpack Compose UI; Android Automotive Library

Using the about item’s key value R.string.pref-support_about, we will first find its Preference object. We will call getPreferenceManager ().findPreference (key) where key is the string value of about setting. We then set the listener using setOnPreferenceClickListener and pass an object of OnPreferenceClickListener.

This was previously done in the settings activity, however the fragment doesn't have quite the same access. To enable a long click listener in the settings fragment, we must first get the ListView that is used to display the settings. As luck would have it, there is no direct access.

Comments
  • Thank you for the quick response! Please refer to my first edit in the original post.
  • Did you implement OnPreferenceClickListener then?
  • I have tried to implement that, but the problem I ran into was I could never load any of the Preference objects from the code. As I wrote in my original post, findPreference("KeyNameHere") always returns null so I couldn't ever find a way to implement OnPreferenceClickListener.
  • When you implement OnPreferenceClickListener, the method onPreferenceClick(Preference preference) will give the preference clicked. So all you have to do is to call preference.getKey() to get the String name of this preference.
  • I see what you mean now, based on your edit. Okay, so I tried this. I added implements onPreferenceClickListener onto my class and implemented and overrode the method onPreferenceClick(Preference arg0). But this method never gets called when I click on any of the preferences. And I have no idea why...
  • Thanks, it helped me
  • Thanks, real answer here!
  • +1 Your solution hinted me towards what I chose in my app. I make a String array of keys and then get Preference for each one and set the onPreferenceClick. It's a code of few lines. Thanks.
  • though this may seem right, but the method that was use was deprecated. If you have an updated answer please edit your post. Note getPreferenceScreen() is deprecated. Thanks
  • Recently I noticed that my preferences aren't working anymore. It took a lot of time to find out -- onPreferenceClick is not called anymore and only overriding onPreferenceTreeClick helped.