3

i am new to android programming .well in my application i have added the preference class and i am calling the xml preference which is stored in the xml folder of my application.this is the code for it

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <ListPreference android:title="colors" android:key="colors" android:summary="list of colors to choose from " android:entries="@array/list" android:entryValues="@array/lvalues"></ListPreference> </PreferenceScreen>

and in the java class i had written only one line that is this addPreferencesFromResource(R.xml.prefs);

now i am able to show the preference class and also the list some colors data.

my question is on selecting the list preference it should show me the selected preference in to the summary attribute .currently it is showing me as 'list of colors to choose from' what i want is to show the selected color say i have selected white the summary should show me as white.

please help me .

Thank you, maddy.

MADDY
  • 147
  • 4
  • 14

4 Answers4

5

you can actually do it in xml. Try this:

<ListPreference
android:key="pref_list"
android:title="A list of preferences"
android:summary="%s"
android:entries="@array/pref_list_entries"
android:entryValues="@array/pref_list_entries_values"
android:defaultValue="0" />

your selected value would replace "%s". For further clarification read this post

Community
  • 1
  • 1
NezSpencer
  • 640
  • 8
  • 12
4

In the other code example there is a little bug which causes that the old value is displayed and not the new one.

Here is a working version:

ListPreference splashList = (ListPreference) findPreference("splash");
splashList.setSummary(splashList.getEntry());

splashList.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  public boolean onPreferenceChange(Preference preference, Object newValue) {
    String nv = (String) newValue;

    if (preference.getKey().equals("splash")) {
      ListPreference splashList = (ListPreference) preference;
      splashList.setSummary(splashList.getEntries()[splashList.findIndexOfValue(nv)]);
    }
    return true;
  }

});
Jan
  • 41
  • 3
1

You dont need to do the extend listPreferences. in your PreferencesActivity file put as per my example:

final ListPreference defaultTown=(ListPreference)findPreference(getString(R.string.pref_default_town_key));         
    Log.i("try",defaultTown.getKey());

    defaultTown.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {            
            Log.i("theApp", "Selected = " + defaultTown.getValue());  
            defaultTown.setTitle(defaultTown.getValue());
            return true;         
        } 

    });     

on change the value will update the title when you enter the application you will need to populate the title with your current preference though instead of colors which is set at default in your xml

1

You need to extend ListPreference class and specify that in the xml file

class MyListPref extends ListPreference {

    public void onClick (DialogInterface dialog, int which) {
        this.setSummary(MyListPref.this.getEntry());
    }
}

If your Custom Preference class name is com.sample.MyListPref the xml entry will be

 <com.sample.MyListPref
    android:key="pref_list"
    android:title="List Preference"
    android:dialogTitle="List Pref Dialog"
    android:entries="@array/pref_items"
    android:entryValues="@array/pref_items_values"/>
Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
  • well sorry to say you but i am not able to understand how to do this .well my i code is exactly from this website http://mobileorchard.com/android-app-developmentusing-preferences/ – MADDY Feb 02 '12 at 10:31
  • i actually extended preferenceActivity so how to do this sir,my code is exactly the same as in this website – MADDY Feb 02 '12 at 10:33
  • well i put a button and on onclick event i called a intent which fires the prefs.java class.in that i add the code as public class PrefActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.prefs); } } and in firing the above mentioned preference xml file from xml resources. for this reason i am not able to understand where should i extend the listpreference as allready extended preferenceActivity.please if you can explain me sir it will be great help for me – MADDY Feb 02 '12 at 11:07