Android, subclass Preference class - android

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.

Related

defaultValue returned as empty string for Custom Preference class used in Preference XML

I am trying to create a time selection preference by extending the DialogPreference. I have specified a android:defaultValue in the Preference XML. Everything works well, but when I try to read the value of the preference from code, I get an empty string. Ideally, if the preference is not set, I should get the value as 5:00 which is mentioned asandroid:defaultValue in the Preference XML.
When I retrieve the preference, how can I get the value specified as android:defaultValue in the
Preference XML, if user has not yet set the preference?
<com.sample.package.TimeDialogPreference
android:defaultValue="5:00"
android:key="#string/pref_key_time"
android:title="#string/pref_title_time"
android:summary="#string/pref_summary_time"
android:dialogLayout="#layout/preference_time"
/>

Android - How to change texts in the preferences activity dynamically?

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.

Android PreferenceScreen Preferences

I am new to Android development. I have developed a preferences activity in my Android Application. I wanted one Preference to open up a regular Activity. I created a preference object in my XML file and captured the onclick event in order to open the activity.
Code below:
<PreferenceCategory android:title="School">
<Preference
android:key="txtSchoolListPreference"
android:title="Select School"
android:clickable="true" />
</PreferenceCategory>
// Get selected school text box
Preference SelectedSchool =(Preference)findPreference("txtSchoolListPreference");
SelectedSchool.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
// Show the login intent
Intent i = new Intent(Settings.this,SchoolList.class);
i.putExtra(One.APP_ACTIVITY_NAME,One.APP_ACTIVITY_SETTINGS);
startActivityForResult(i, One.APP_ACTIVITY_SCHOOLLIST);
return true;
}
});
Everything works great but I would like to add the circle arrow-down icon to the preference but I don't know how.
Does anyone have any idea how I can add the circle arrow-down preference to the preference I have added to the page?
Getting the PreferenceActivity to show like the system's current theme is a bit more involved. Than just showing the Android vanilla arrow. You'll have to create a subclass of DialogPreference (code on github). Use the code from EditTextPreference (code on github) as a template on how to create your subclass. As it appears you'll be starting another Activity this will be easier than creating one that displays another dialog (was not that easy in my experience).
To include your preference in the Preference Resource xml file use the fully qualified name with a leading capital letter. For example class Foo in package com.stackoverflow would appear as <Com.stackoverflow.Foo>. This is similar to how custom view widgets are used in xml layouts.
The reason you have to do it this way is the arrow is an internal resource so we have to go to some extremes to use the internal resource.

Android: SharedPreference: Defaults not set at startup

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

Using EditTextPreference with 2 user input fields

I would like to use EditTextPreference to show 2 input fields instead of 1. For instance, a username and password field should be shown. I don't want to use a dialog for each one. How can this be done? In the WiFi settings there is one that does this, when you want connect to a protected network, a dialog shows to set a password for the credential storage with 2 fields.
You can use a DialogPreference and create your own layout for the input fields you require.
DialogPreference is abstract so you'll need to create your own subclass of it, adding an implementation of onDialogClosed() to save the values from the dialog as Preferences.
You can still reference your own class in a Preferences XML file by using the class as the XML tag. For example:
<com.yourdomain.YourDialogPreference
android:title="Title"
android:summary="Summary"
android:key="dialog_preference"/>

Categories

Resources