Preference element in xml and the click event

If you have an EditTextPreference tag in a xml file you can catch the click event implementing OnSharedPreferenceChangeListener but it doesn’t work if you have a Preference tag in the xml file.

Consider the following code inside a xml file for the preferences:


You can catch the click event using one of these ways:

  • implementing an intent in the xml file, for example, in order to open a web page the code is:
  • implementing a Preference.OnPreferenceClickListener listener, for example, in your activity or fragment add the following:

2 Replies to “Preference element in xml and the click event”

    1. L’esempio che ho riportato è parte di un progetto in cui findPreference è un metodo di PreferenceFragment dove non è deprecato, ma effettivamente ho verificato che in altre classi come PreferenceActivity è deprecato con la motivazione: “This method was deprecated in API level 11. This function is not relevant for a modern fragment-based PreferenceActivity.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.