android-preferences

How to set a default value to SharedPreferences programmatically?

情到浓时终转凉″ 提交于 2019-12-01 10:41:32
I am using SharedPreferences to keep the information about user's weight, which I need in my application. The problem is, how to set a default value (eg. 75 kg) automatically after installation? I know how to do it via .xml, but how to do this programmatically? My code: public class SettingsDialogFragment extends DialogFragment{ public static final String PREFS_NAME = "settings"; public Dialog onCreateDialog(Bundle savedInstanceState) { builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Data.weight = weight;

Save ArrayList in shared preferences

房东的猫 提交于 2019-12-01 06:08:38
i am searching for hours to find out why saving boolean list in shared preferences is not working. The methods are not saving or loading something... the loadmethod always returns the alternative values: so list_size as 0 and the booleans as false. First of all i create the list in my MainActivity and call the save Method: SaveLoadTraining sLoad = new SaveLoadTraining(); ArrayList<Boolean> listBoolTrain = new ArrayList<Boolean>(); listBoolTrain.add(true); listBoolTrain.add(true); sLoad.saveArray(listBoolTrain); The subclass SaveLoadTraining looks like this: package de.sebspr.app08.halle;

Save ArrayList in shared preferences

非 Y 不嫁゛ 提交于 2019-12-01 02:59:52
问题 i am searching for hours to find out why saving boolean list in shared preferences is not working. The methods are not saving or loading something... the loadmethod always returns the alternative values: so list_size as 0 and the booleans as false. First of all i create the list in my MainActivity and call the save Method: SaveLoadTraining sLoad = new SaveLoadTraining(); ArrayList<Boolean> listBoolTrain = new ArrayList<Boolean>(); listBoolTrain.add(true); listBoolTrain.add(true); sLoad

SharedPreferences.getInt() results in ClassCastException - Why?

…衆ロ難τιáo~ 提交于 2019-11-30 17:11:39
I have a simple (not user-editable) numerical setting defined in a preferences XML as follows: <EditTextPreference android:key="@string/numeric_val" android:defaultValue="0" /> And I read it using this simple statement: sharedPrefs.getInt(getString(R.string.numeric_val), 3) It works, but when I try to read it, for the first time after application install , it generates a ClassCastException . The documentation says that getInt() "Throws ClassCastException if there is a preference with this name that is not an int." - and I know that this preference is clearly defined as an <EditTextPreference>

Android preferences spinner

て烟熏妆下的殇ゞ 提交于 2019-11-30 15:13:07
How can I build something like this? I wanna open a dialog in the Preferences Activity, where the user can change the number with a spinner, when he clicks on the according preference item. I got the screenshot from Android Developer Tools , but I cannot find some example code. I have developed a Custom Preference, which can be found here . XML Code: <com.vanniktech.vntnumberpickerpreference.VNTNumberPickerPreference android:defaultValue="@integer/font_size_default_value" android:key="preference_font_size" android:title="@string/font_size" app:vnt_maxValue="@integer/font_size_max_value" app

Android Widget: Show configuration activity before widget is added to the screen

跟風遠走 提交于 2019-11-30 14:50:09
问题 I have an Android Widget that uses web services to retrieve and display the data on the widget. The widget has a configuration activity that extends PreferenceActivity . The configuration activity starts up as soon as the widget is installed, which is the desired behavior for this widget. The problem is, whenever a widget is added to the home screen, the widget attempts to update iteself before the configuration activity is started/completed which may potentially lead to a long delay (several

How to enable a preference in my android application when other preference is disabled?

99封情书 提交于 2019-11-30 14:35:12
问题 I have used PreferenceActivity to have preference in my android application. I want one preference say "pref 2" to be enabled when other preference say "pref 1" is NOT checked and "pref 2" to be disabled when "pref 1" is checked. i.e. exactly opposite of the android:dependancy attribute. How can I do that? 回答1: I don't think there's any out-of-the-box solution for it, i.e. an inverted dependancy attribute. But there's always the click-listener: preference1.setOnPreferenceClickListener(pref1

Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate

五迷三道 提交于 2019-11-30 14:32:34
I'm trying to add an ImageView to a preference fragment in order to show a preview of a color setting. I'm accessing the instance of the imageview via the onCreateView method toset the test color, and it will display. However it only works if I don't call addPreferencesFromResource in the onCreate method - which is a problem since the preferences must be added. Also if I leave the call to addPreferencesFromResource, but remove the entire onCreateView method the program will run (albiet without the updatable imageview). The error in both cases is "Content has view with id attribute 'android.R

Android: how to get list of all preference xml's for my app and read them?

元气小坏坏 提交于 2019-11-30 14:09:57
问题 how to get list of all application preferences for application, 1. I am saving shared preference in this manner 2. I know that they are in data/data/app_packagename/shared_prefs 3. THE PROBLEM: But how to get list of all preference xml files in a spinner and read each preference, i searched in SO, but i did not found any help regarding this, how to do read all preference xml files in my application directory and access the preferences? P.S: I am aware of SharedPreference.getAll(); , will be

android: How to change ListPreference title colour?

风流意气都作罢 提交于 2019-11-30 13:47:47
I would like to change the title and line color of my ListPreference from blue to pink, to match the line of my action bar. Any ideas? Thanks in advance! I've been looking through Android's themes.xml and styles.xml looking at things like dialogPreferenceStyle , but haven't figured it out yet. Had the same problem today. Old thread, but I found this: and it works perfectly. Simply modify your styles.xml <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item