Search code examples
javaandroidlive-wallpaperlistpreference

Android, show dialog when ListPreference item is clicked


Basically I have a ListPreference to allow a user to change the X position of some text on my Live Wallpaper.

It contains 4 entries: top, middle, bottom and manually input X. The first 3 options are no problem, I simply get the SharedPreferences in my WallpaperService class and check if they are top, middle or bottom and change the position corresponding to their choice.

However, the last option is proving more difficult, what I want to do is have an EditText alert box popup when the user clicks the "Manually input X" ListPreference item so they can enter a value for X. I just cant figure out how to make the alert popup from clicking that specific List element.


Solution

  • You probably want to create a custom ListPreference. Basically you want to extend from ListPreference (see original here), and provide a custom protected void onPrepareDialogBuilder(Builder builder), in which you provide the additional "custom" list item and the onclick to handle the selection of the "custom" entry.

    Note that I keep saying "custom" because it would be a best practice to make this class as reusable as possible.