I have the following code in my xml resource for my settings menu:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:defaultValue="top_rated"
android:dialogTitle="Search for:"
android:entries="#array/SettingEntries"
android:entryValues="#array/SettingEntriesValue"
android:key="pref_sync"
android:summary="%s"
android:title="Show" />
</PreferenceScreen>
When my app loads the first time, i dont want it to load the stored value from the previous run, but select the first value on the list.
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sharedPrefs.edit().putString("pref_sync", "top_rated");
I tried to change the value in SharedPreferences when the app gets created but that didn't work. Any advice on how to solve the matter?
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>
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 have a preferences page which is defined by XML - including some default values. I use a PreferenceActivity to display and handle this page. Whenever I use this page to set the preferences the preference file on the file system is updated properly - I can see this via adb.
However, whenever I go back to the settings page after have changed some of the settings, it's the defaults that are shown. Worse than that, if I press back without changing any settings, it then sets the, all back to the default.
Any ideas on how I can get the prefs to actually show the current settings?
My PreferenceActivityis created thus:
#Override
protected void onCreate(Bundle savedInstanceState) {
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
setDefaults(this);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
The XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:id="#+id/numberOfYearsList"
android:key="numberOfYears"
android:title="Number of Years to Read the Bible"
android:summary="How many years would you like to take to read through the reading plan?"
android:entries="#array/numberOfYears"
android:entryValues="#array/numberOfYears"
android:dialogTitle="How Many Years?"
android:defaultValue="1"
/>
<CheckBoxPreference android:key="ignoreDates"
android:id="#+id/ignoreDatesCheckbox"
android:title="Ignore Dates"
android:summary="Would you like to use the dates in the plan?"
android:defaultValue="false"
/>
</PreferenceScreen>
Nevermind - I did a mistake. As you can see from above, when creating the Preference Activity I'm making a call to a method that sets all the prefs to defaults... Duh!