Hello fellow programmers, I have a little problem with Preferences activity.
http://developer.android.com/reference/android/preference/PreferenceActivity.html
I've got just one preference category and a listPreference:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="#string/basic_settings" >
<ListPreference
android:defaultValue="70"
android:entries="#array/listArray"
android:entryValues="#array/listValues"
android:key="updates_interval"
android:persistent="true"
android:summary="#string/SOME_SUMMARY"
android:title="#string/SOME_TITLE" />
</PreferenceCategory>
I need to have the selected value (the default one or the user defined one) written in the summary of the listPreference, for example:
We will have at least 70 characters.
How can I do this from the code?
Any help is appreciated
Try like this..
Preference customPref = (Preference) findPreference("updates_interval");<-- your preferences key
customPref.setSummary("desired string");
here is a short example:
Preference etp = (Preference) findPreference("the_pref_key");
etp.setSummary("New summary");
This requires that you display your preferences either from a PreferenceActivity or from a PreferenceFragment, since findPreference() is a method of these classes. You most likely do that already.
To change the summary every time the user changes the actual preference, use a OnPreferenceChangeListener and check if the relevant key changed in the callback. After it has changed, just edit the summary like above.
You can create a subclass of ListPreference in which you set an OnPreferenceChangedListener from which you will have access to the new value, and set the text on your ListPreference. I think the setSummary() function on the ListPreference will update the text under the name of the preference. If that doesn't work you can also override getView() to implement your own custom view for the Preference on which you can set the text directly.
Related
I have a modified EditTextPreference with using SimpleSummaryProvider:
<NumberPreference
android:key="pref_key"
android:defaultValue="0"
android:title="Some title"
app:useSimpleSummaryProvider="true"/>
If I change this preference programmatically (for example in the onSharedPreferenceChanged event), then the summary in the graphical interface will not update. Is there any way to notify the Preference widget that it needs to read the value again and display it in the summary? Without manually assigning a specific value to the summary?
I am using PreferenceFragmentCompat and a modified EditTextPreference with a modified EditTextPreferenceDialogFragmentCompat.
I looked at the Android sources (EditTextPreferenceDialogFragmentCompat.java) - how the dialog for changing settings notifies EditTextPreference about a value change. Did as well and it worked :)
if (editTextPreference().callChangeListener(value)) {
editTextPreference().setText(value);
}
I have implemented a custom ListPreference and managed to load a list of items along with checkboxes for each without an issue. However, I need to add a “Select All” checkbox on top in order to select all list of items. How would I achieve this with the following source I have implemented?
The layout:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/Title_LOCATIONS">
<com.gm.settings.LocationsListPreference
android:defaultValue="null"
android:key="list_locations"
android:title="#string/LocationsListPreference_title"
android:dialogTitle="#string/LocationsListPreference_title"
android:summary="#string/LocationsListPreference_summary"
/>
The class:
public class LocationsListPreference extends ListPreference {
}
I have implemented the class by following a tutorial and it works fine. But it uses a default layout i think and if I were to add this addition checkbox, how would I achieve this?
Update:
I want to know as to how i can add the "Select All" checkbox to the layout? Or should i create a custom layout? Please provide a sample code. (Because i feel the way it is right now, i dont have the control over this checkbox)
What you could do is add a CheckBoxPreference in your PreferenceCategory and attach to it a OnPreferenceChangedListener that sets all of the values to being checked.
An example could probably look a little something like this:
<CheckBoxPreference
android:key="select_all"
android:defaultValue="false"
android:title="Select All"
/>
<com.gm.settings.LocationsListPreference
android:defaultValue="null"
android:key="list_locations"
android:title="#string/LocationsListPreference_title"
android:dialogTitle="#string/LocationsListPreference_title"
android:summary="#string/LocationsListPreference_summary"
/>
And then in your PreferenceFragment (or PreferenceActivity), you would have the following:
SharedPreferences shareprefs = getPreferenceManager().getSharedPreferences();
LocationsListPreference listPreference = getPreference("list_locations");
CheckBoxPreference selectAll = getPreference("select_all");
selectAll.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
{
public boolean onPreferenceChanged(Preference preference, Object newValue)
{
//Do something with your listPreference and/or your sharedPrefs
}
}
Hope this helps, and if you get to a road block, I think this post does a slightly better job at explaining some of the concepts. Good luck!
Found a stackoverflow post which might help others if they come across this kind of implementation:
You can build your custom ListPreference layout.
Cheers!
I have two preference xml files. The first one (pref2.xml) contains 2 preferences:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="key"
android:title="title"
android:defaultValue="false"
/>
<CheckBoxPreference
android:key="key2"
android:title="title"
android:defaultValue="false"
/>
</PreferenceScreen>
and the other one (pref1.xml) contains 1 preference:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="key"
android:title="title"
android:defaultValue="false"
/>
</PreferenceScreen>
in my preference activity I am trying to change them:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref1);
this.getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.pref2);
final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
Log.d("LOG", "size of sharedPreferences"+adapter.getCount()+"(should be 2)");
actually here i get the right output and everything is displayed correctly. But I want to change the displayed preferences concerning one preference. Therefore I implemented the OnSharedPreferenceChangeListener in the preference activity:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
super.onSharedPreferenceChanged(sharedPreferences, key);
//switch the widget type
if (key.equals("key")){
this.getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.pref1);
final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
Log.d("LOG", "size of sharedPreferences "+adapter.getCount()+" (should be 1)");
}
}
Well, the pref1 preferences are displayed correctly, but the output of
"size of sharedPreferences 2"
indicated that in the background there are still the old preferences applied. If i iterate over the listAdapter i get also the old preferences.
Any idea how I could solve this?
I found out that the listadapter is some how outdated. If I get the count of items via getPreferenceScreen().getPreferenceCount() I get the right amount. But how do I access these preferences ?
I'm fairly sure that problem lies with your having two preferences with a key of "key" even though they are on different screens. A preference is identified by its key and this needs to be unique to avoid conflicts. The listener only knows which preference is being changed from the key. I would change the keys to have unique values.
EDIT: Well, wasn't right :S
...
Okey, I finally found the solution. After changing the PreferenceScreen I need to bind manually the listView again:
getPreferenceScreen().bind((ListView)findViewById(android.R.id.list));
I am tryig to write an Android Honeycomb application and I am having trouble subclassing Preference: http://developer.android.com/reference/android/preference/Preference.html
I want to make a similar layout with title and summary but also a progress bar.
I have created the layout and added the custom preference class but I can't seem to get hold of the instance of it to set the values of the items in it.
It seems that the preference key doesn't work for the custom class.
Here is my preference definition compared to the standard preference class:
<Preference
android:key="int_free_storage"
android:title="Free Space"
android:summary="free storage value here"/>
<com.hamid.storageether.SpacePreference
android:key="int_space_test"
android:title="Test"
android:summary="This is my custom preference"/>
My my preference subclass then sets my XML layout as it's layout resource in its constructor
setLayoutResource(R.layout.space_pref_layout);
it also overrides the setTitle and setSummary methods....
In my main PreferenceActivity I try to get hold of my Preference by it's key but no luck it seems, since the preference never gets updated:
// These Two work
Preference intTotal = (Preference)findPreference("int_total_storage");
Preference intFree = (Preference)findPreference("int_free_storage");
intTotal.setSummary("Standard Preference Summary 1");
intFree.setSummary("Standard Preference Summary 2");
// My subclass doesn't - It just displays the default text defined in the layout xml.
SpacePreference intTest = (SpacePreference)findPreference("int_test_space");
intTest.setTitle("Testtttyyy");
intTest.setSummary("Test Summary");
Could someone please point me towards where I may be going wrong?
Is this code copied straight from the program or retyped? If copied, then your key is "int_space_test" in XML and "int_test_space" in code. It should be throwing a null pointer exception on the next line where you use intTest if that's the case.
I have Listpreferences in my app. They don't appear to be setting to their defaults right after installation - they appear to be null. I'm trying to figure out why my default preferences are not being set right after installation. In my main code I have:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
InUnits = sp.getString("List1", "defValue");
InAngs = sp.getString("List2", "defValue");
OutUnits = sp.getString("List3", "defValue");
OutAngs = sp.getString("List4", "defValue");
Right after the above code executes, each variable contains "defValue" instead of the actual values I have assigned in my ListPreference below.
My preference xml file is called, "settings.xml". Here's what one of the ListPreferences there looks like:
<ListPreference
android:key="List1"
android:title="Input: Alph"
android:summary="Choose Alph or Ralph"
android:entries="#array/inputAlph"
android:entryValues="#array/input_Alph_codes"
android:dialogTitle="Input Alph"
android:defaultValue="ININ"/>
Here's what some of my strings.xml file looks like:
<string-array name="inputUnits">
<item>Alph</item>
<item>Ralph</item>
</string-array>
<string-array name="input_Alph_codes">
<item>ININ</item>
<item>INMM</item>
</string-array>
When I go to menu, and then settings, I can see my defaults checked (radiobuttoned). Then when I go back from the settings menu to my main screen - all is well - for life! ...then each var above is assigned the proper default value.
This only happens when I first install my app on the phone. After I go to the settings screen once and then right out of it, the app is fine and accepts any setting changes.
By the way, as you can see, "List1" is the android:key within a file called settings.xml in my res/xml folder.
They don't appear to be setting to
their defaults right after
installation - they appear to be null.
That's what's supposed to happen.
I'm trying to figure out why my
default preferences are not being set
right after installation.
They're not supposed to be. The preference XML you have listed there is only used for populating a PreferenceActivity, nothing more. Until the user opens the PreferenceActivity, the preferences will be null, and the defaults you supply to the SharedPreferences getters will be returned.
UPDATE
You can use setDefaultValues() on PreferenceManager to assign the defaults from your preference XML to a SharedPreferences. However, be careful of the timing -- this will do disk I/O, and therefore ideally is performed on a background thread.
Set the default values to SharedPreferences from your preference XML.
PreferenceManager.setDefaultValues(Context context, int resourceId, boolean readAgain)
PreferenceManager.setDefaultValues
You can specify a default value like this
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.getString("thePrefKey", "theDefaultValue");
The android:defaultValue="..." in the "layout" settings.xml is only a visual help for user