Is it possible to load an String-list or HashMap into a ListPreference inside a SettingsActivity or do I need to start a whole knew activity?
And if, how can I register a click onto an Item inside a SettingsActivity?
note: I want to load a HashMap, the List should show the key value and if the user clicks on the Item, he should edit the value behind the key value. There also will be an "add" button so the user can add new keys and values by himself.
I figured out, that I need to start a new activity from my SettingsActivity.
I did this by creating a new Activity and adding this to my preferencex.xml:
<PreferenceCategory
android:title="#string/pref_color_picker"
android:key="pref_key_color_picker">
<Preference android:title="#string/default_colors">
<intent android:action="android.intent.action.VIEW"
android:targetPackage="my.package.app"
android:targetClass="my.package.app.theActivity"/>
</Preference>
</PreferenceCategory>
Related
I'm creating a PreferencesActivity where I have 3 preference items. When I click on each of them, I load contacts to pick one. After selecting a contact, I save it's name and phone number and I show it in the preference item.
But now I have to add the option to delete a contact. This would be done deleting the content of the preference, this is, clearing the strings. But my real question is, how to do this?
As there is not a normal layout, I cannot add a element for deleting that data, nor I can't do a long click on it to provide this option.
So my question is, working with preferences, what would be the way to provide the user at UI level the option to delete data from selected preference?
PD: I'm not asking about how to clear preferences data, but how to provide the user a option to clear selected preference data in the user interface (button, long click, etc...).
preferences.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="#string/contact">
<Preference
android:key="Contact1"
android:title="Contact 1"/>
<Preference
android:key="Contact2"
android:title="Contact 2"/>
<Preference
android:key="Contact3"
android:title="Contact 3"/>
</PreferenceCategory>
</PreferenceScreen>
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.edit().clear().commit(); //remove all your prefs :)
if you want to clear a specific data..then do it with the given key
eg. if you are saving a password of the user..and on click of logout button you want to clear it.just do this way
pref.edit().putString("password","").commit();
if you want to remove it then
pref.edit().remove("password").commit();
I have a bit of confusion about setting default values of shared preferences... for example, I have a PreferenceActivity with two or more PreferenceFragment and each of the preference fragmens has its own *preference_fragmentX.xml* file used to build the preference view. When I set the default values with
PreferenceManager.setDefaultValues(getActivity(), R.xml.?????, false);
Which XML should I specify? Should I build another XML preference file containing all the preferences of the *preference_fragmentX.xml* files with the default values and use this one in setDefaultValues? For example, I may build an app that registers for a broadcast event and I'm not sure the user opens the preference activity before the broadcast event is fired for the first time, so I'd like to initialize default preferences in the broadcast event, or in another activity started by it.. so, how do I initialize all the preferences of all the fragment panes?
It works if I build a default_preferences.xml file which contains all the preference keys defined in all the preference fragments XML files (I specify only the a PreferenceScreen root tag and all the preference tags under it, without specifying all the other tags like PreferenceCategory.. furthermore for each preference I specify only the key and the default vale attributes), but I don't know if it is the correct way to do this task, since I must replicate part of the other XML files:
preferences_fragment1.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/my_preference_category">
<RingtonePreference
android:key="preference_ringtone"
android:ringtoneType="alarm|notification"
android:showSilent="true"
android:summary="#string/preference_ringtone_summary"
android:title="#string/preference_ringtone_title" />
<ListPreference
android:key="preference_list"
android:entries="#array/list_items"
android:entryValues="#array/list_values"
android:summary="#string/preference_list_summary"
android:title="#string/preference_list_title" />
</PreferenceCategory>
</PreferenceScreen>
default_preference.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:key="preference_ringtone"/>
<ListPreference
android:defaultValue="5"
android:key="preference_list"/>
</PreferenceScreen>
I will start with an example... If you go to Settings > Applications > Manage applications, a new screen will open with a list of the installed applications: if you click on any application of the list, it will open a new screen containing information about the application.
Well, some settings of my application must be managed through a list, and this list should behave like the above example. I have already created a PreferenceActivity with some categories, each of which has some items: when I click on one of these items, I would like it to open a new screen where the new data is placed on a list, just like the list of the applications of the above example. Moreover, when I click on any entry of this list, it will open a new screen in order to set some data.
How should I proceed? Should I create an activity for each screen?
Android was created this way, according to the documentation "An activity is a single, focused thing that the user can do.", so yes, you should have an activity for each screen.
This changed a little with Honeycomb with the introduction of Fragments, but if you're not developing for tablets you should keep the one page, one activity mindset on Android.
Generally you have each activity call by another, the caller is pushed onto a stack (unless the calling activity ask's to be removed) and goes dormant until it returns
Basically you create an Intent in Activity A to start Activity B, you can pass data by using startActivityForResult with extras in the intents Example: How to pass data between activities
When you press the back button then that previous activity becomes active again and the result handler you set up can get any return data.
You might also look at fragments in the support API if you want to provide tablet support that looks and behaves better.
That is propably the best way to do it, at least if you're not working on a wizard style activity.
Use a ListActivity to show your list, and pass data to and from this activity using intents.
I was able to implement this at work, I don't remember right now in the head how I implemented it, was long time ago. If nobody has a good answer for you I will post it tomorrow, however: I remember putting a Preference, which will act as a button, then I added a preferenceClickListener in order to open a new PreferenceScreen on click.
But like I said, I'll post it for you tomorrow if you don't get a satisified answer.
Good luck!
UPDATE:
?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Personal"
android:key="personal_category">
<Preference
android:key="birth"
android:title="Birth"
android:summary="Choose your birthday"/>
<PreferenceScreen
android:key="height_imp"
android:title="Height"
android:summary="Enter your height">
<EditTextPreference
android:key="foot"
android:title="Foot"
android:summary="foot"
android:numeric="integer"
android:dialogTitle="Foot"/>
<EditTextPreference
android:key="inch"
android:title="Inch"
android:summary="inch"
android:numeric="integer"
android:dialogTitle="Inch"/>
</PreferenceScreen>
<EditTextPreference
android:key="weight"
android:title="Weight"
android:summary="Enter your weight"
android:numeric="integer"
android:dialogTitle="Weight"/>
</PreferenceCategory>
</PreferenceScreen>
That's it! When you click on it, it will take you to the second PreferenceScreen and so on, then finally when you need to customize your layout you'll need to open an Activity.
You could then use a Preference and add onPreferenceClick:
#Override
public boolean onPreferenceClick(Preference preference) {
if(preference == birth){
startActivity(new Intent(getBaseContext(), Birth.class));
}
if(preference == height_imp){
PreferenceScreen a = (PreferenceScreen) preference;
a.getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
return false;
}
return true;
}
If you need to change the background or something else with the preferenceScreen, then add a preferenceClickListener as well: height_imp = (PreferenceScreen)getPreferenceScreen().findPreference("height_imp");
height_imp.setOnPreferenceClickListener(this);
See... if once the user wants to return from certain point to previous position... if you had created a seperate activity for each of them... the present activity will be popped off the stack... letting the previous activity to be displayed...If you are changing the content of the list for every new screen...instead of creating new activity... then it will be difficult for the user to come back... you should again and again change the content of adapter..
So I think.. creating seperate activity for each screen is better..( and you can use same [any custom layout if you have]layout file for all activities..)
i'm a new android developer and i hope that you guys could help me.
I'm developing a preference screen like above:
<PreferenceCategory android:title="#string/security_title" >
<EditTextPreference
android:name="Password"
android:defaultValue=""
android:key="passwordPref"
android:password="true"
android:summary="#string/security_sumary"
android:title="#string/security_addPassword" />
<PreferenceScreen
android:key="secondPrefScreenPref"
android:summary="Click here to go to the second Preference Screen"
android:title="Second Preference Screen" >
<ListPreference android:entries="#android:id/list" android:key="listPasswordKey" />
</PreferenceScreen>
</PreferenceCategory>
In a password field,the user are going to type his passw and when he clicks OK button, the program are gonna save it in my database. This part is ok!
But when he clicks on a second pref screen i want to list, from database, all your passwords saved.
I know how to load the data, but i don't know how to call my method when the user clicks on a SecondPrefScreen and how to show them in a preferences.xml
can anyone help me?
thx
You will have to create a widget that will load the data and list it for you. Essentially, create an object that extends DialogPreference and give it a child list view that will list your security credentials.
I am displaying my "Settings" screen through a class that extends PreferenceFragment. I need to know when a particular setting is clicked so that I can handle the Intent to launch at runtime, as opposed to declaring in xml like this:
<PreferenceScreen
android:title="#string/terms_conditions"
android:summary=""
android:layout="#layout/settings_item">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.somecompany.com/m/EULA.aspx" />
</PreferenceScreen>
I need to fire a different Intent depending on other variables involved.
So, what I need is to know when the user clicks the "Terms and Conditions" within the settings fragment.
Have the class in which you need to know about config changes implement
SharedPreferences.onSharedPreferenceChangeListener
and install it as a listener in your SharedPreferences via
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(listener);
Then handle the changes is
onSharedPreferenceChanged()
The relevant documentation is about SharedPreferences and SharedPreferences.OnSharedPreferenceChangeListener