3

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.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
William Stewart
  • 841
  • 1
  • 14
  • 27

2 Answers2

3

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.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • So is there no way with the original ListPreference to have an onClick method for a specific item in a ListPreference? – William Stewart May 30 '11 at 21:53
  • I don't think so, since the creation of the dialog and the selection is all handled by code you do not control. It might sound like a pain, but it's actually not that bad. The extra code is minimal and encapsulated in the custom class. – dmon May 30 '11 at 21:57
  • Ok then, I'm just trying to understand how the custom class will work. I have no idea what I would put into the onPrepareDialogBuilder method to get what I need. Sorry if its simple but my minds gone blank, any hints? – William Stewart May 30 '11 at 21:58
  • There's a sample of a custom preference in the samples folder. See code [here](http://hi-android.info/docs/resources/samples/ApiDemos/src/com/example/android/apis/preference/MyPreference.html). – dmon May 30 '11 at 22:03
  • Ok I might understand now. Could I pass a int parameter in the constructor which is a reference to the position of the item in the list, and if this item is clicked bring up my dialog? So for example, if i have 4 items in the list, and I want the last one to bring up a dialog which I can also pass in the constructor, I can pass the int 3 then put something in the onClick() method to check if the item clicked is in position 3 then show a dialog? – William Stewart May 30 '11 at 22:19
  • The way I would do it is to provide a resource int pointing to a String constant. Then, in the onPrepareDialogBuilder add this custom string to the list when you build it and save the index (size -1 ) so you know when it was clicked in the onClick. Unless you want to place the "custom" string somewhere specifically (e.g. in the middle of the list) I think this would be the easiest. – dmon May 30 '11 at 22:23
  • How exactly do I add the String to the list, and in the onClick how will it find out which index was clicked? – William Stewart May 30 '11 at 22:36
  • Copy the `onPrepareDialogBuilder` then get the entries by calling `getEntries()` and add a new item to the array at the end, then pass that into the `builder.setSingleChoiceItems()`. And then the onclick has `which`, which is the index of the item that was clicked. Note that you will probably have to store your data in a somewhat encoded fashion so that you can support the custom values, as the default implementation only stores the integer value of the selected item. – dmon May 30 '11 at 22:55
  • This is what I have at the moment, is this what you are getting at? `@Override protected void onPrepareDialogBuilder(Builder builder) { CharSequence entry = text; entries = this.getEntries(); entries[entries.length-1] = entry; builder.setSingleChoiceItems(entries, 0, new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { if (which == entries.length) { } } }); }` – William Stewart May 30 '11 at 23:17
  • Sorry, kinda hard to read when these comments don't do new lines. – William Stewart May 30 '11 at 23:18
  • Right, so its figured out that I've clicked the right item, what would be the best way to then display a dialog with an EditText inside it to allow them to enter the value of X? Show it inside this custom class, or show it back in the PreferenceActivity class? – William Stewart May 30 '11 at 23:32
  • Yep, show the dialog in the onclick and save the value returned. I've never done a custom dialog with an EditText inside, but it's definitely doable (see [here](http://about-android.blogspot.com/2010/02/create-custom-dialog.html)). – dmon May 30 '11 at 23:40
  • So I assume I need to create a new Activity containing the AlertDialog reference this activity inside the onClick()? Also as it is I assume the custom preference class is ready to implement and test, but it just wont do anything when the item is clicked. How do I use it within the XML which has my live wallpaper settings? – William Stewart May 30 '11 at 23:46
0

Override onPreferenceTreeClick() in your PreferenceActivity and compare the preference it gives to the one you want to do something for.

Geobits
  • 22,218
  • 6
  • 59
  • 103