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"/>
Related
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"
/>
Is there any way that i can set an attribute globally and request focus whenever an error is set?
Suppose i am validating a lot of EditText fields and setting an Error for each of them, after setting each other i have to request a focus:
editText.setError("Error");
editText.requestFocus();
However there exist a lot of fields and i am required to do call .requestFocus() in every single one. Is there any way to set it globally or setting explicitly is the only way around?
you can create one function for that and call that function in your whole app by giving class reference.
public static void EdittextError(EditText editText, String Error) {
editText.setError(Error);
editText.requestFocus(); }
Pass edit text obj. and Error String in function parameter.
Is there any way to set it globally or setting explicitly is the only way around?
No, there's no special attribute in system widget, so you have to set it by hand or extend EditText and override setError() to automatically request focus when called and then use MyEditText in your layouts
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.
I have a preference settings for which i have configured a dialog box pop up on click of preference using the below code.
Dialog passwordDialog = new Dialog(this);
passwordDialog.setContentView(R.layout.password_dialog);
passwordDialog.setTitle("Set new password.");
passwordDialog.setCancelable(true);
passwordDialog.show();
The dialog is coming fine. However In the dialog i have a password edit text and a confirm password edit text. If these matches with each other i need to save the password to preference. I do not know how to save the value to a preference on ok click in my custom dialog. Please let me know how to do this. Thank you for your time and help.
You can retrieve the preferences, and then commit information to it :
SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
preferences.edit().putString("passwordKey", editText.getText().toString()).commit();
To add information to a preference, start by calling edit() (returns an editor), add the value you want (key/value, like a Map), and never forget to call commit() (to commit the changes).
You can then access your value using
preferences.getString("passwordKey", defaultValue);
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.