Question

There are only so many types of Preference types we can set in PreferenceActivity. I am trying to run a specific function once a certain preference is clicked. Is there a way to override a preferenceScreen or editText onClick?

Was it helpful?

Solution

Yes you can. Here's a simple example of how to get the preference reference and execute custom code on a click event.

public class ActivityPreferences extends PreferenceActivity
{
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        addPreferencesFromResource( R.xml.preferences );

        Preference myPref = findPreference( "MY_PREF" );
        myPref.setOnPreferenceClickListener( new OnPreferenceClickListener()
        {
            public boolean onPreferenceClick( Preference pref )
            {
                  // Run your custom method
            }
        } );
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top