I am trying to retrieve a default value (which is the last value saved)
I am making some changes in the background color of the layout
so before the setContentView
I put this code in.
SharedPreferences preferences = getSharedPreferences("UID",MODE_PRIVATE);
setTheme(preferences.getInt("color",R.style.AppTheme));
But it won't work, the R.style.Apptheme's background color (which I set in the style.xml) works but I have changed the bg's color with this code
SharedPreferences preferences = getSharedPreferences("UID",MODE_PRIVATE);
preferences.edit().putInt("color",R.style.red_theme).commit();
finish();
startActivity(getIntent());
what is the proper way of retrieving the last saved style?
Related
I'm trying to gain access to a (custom) list preference and change its selected value (i.e. if index 3 is selected, change the selection to index 1). However, findPreference() can only be used inside a PreferenceActivity. I need to access the preference and change its selection inside a regular Activity. Is there a way this can be done? I don't see anything in SharedPreferences that I can use to change the selection, only a list preferences value.
This is how you do it,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference(this);
prefs.getString(PREF_KEY, "default");
I have a preference, to be used as settings of my application. I want to change the background color of a particular item in listpref (listview). I used the code below:
Drawable background = getResources().getDrawable(R.drawable.bg_barcode_des);
addPreferencesFromResource(R.xml.admin_preferences);
Preference credentials = findPreference("demo");
View credentialsView = credentials.getView(null, null);
credentialsView.setBackgroundDrawable(background);
//**OR** credentialsView.setBackground(background);
//**OR** credentialsView.setBackgroundColor(Color.RED);
//Non of these or working...
But nothing change in my preference activity. Any correction that I need to do???
I have a settings.xml file that contains Preferences for my app. All the values by default are set to "true" and its used by PreferenceActivity in my app.
In my main activity I read the values through
SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(appContext);
Boolean key = sp.getBoolean("M", false);
it gets me whatever I need at any run except the first time. Only when I open my app for the first time and doesn't open my settings menu I get "false". I mean I have to open menu and only after that the app run correctly.
Any suggestions?
Because yor are getting the default value for first time.
that is false -->
Boolean key = sp.getBoolean("M", false);
use this
Boolean key = sp.getBoolean("M", true);
Use sp.getBoolean("M", true); instead. Default values in preferences file is what it will be when preferences activity is first started. SharedPreferences know nothing about your settings.xml.
Alternatively you can have a separate defaults.xml file (or any other name) containing all the default values for all preferences. Then you use these values in both settings.xml (#bool/default_M_value) and your application (sp.getBoolean("M", getResources().getBoolean(R.bool.default_M_value))).
I personally would stick to first approach though.
I have an android application that displays, by default, a series of ImageButtons using xml. The user changes the image based on their input. I am trying to display the changed ImageButtons the next time the user loads the app.
Example:
ImageButton starts as Android.png (loaded from default xml page)
User enters text
ImageButton is changed to Correct.png
The next time that the app loads I want Correct.png to display instead of Android.png. Is there a way to iterate through the ImageButtons before the app starts (the buttons are NOT created programatically) to set the source value for each one before the application loads?
You can use findViewById(R.id.button_1) for each button and set it as needed. You'll then need to store which values it should have so that you can load these values and set the ImageButton source for each one. You can store it with either SharedPreferences or a SQLite database, depending on how much you have to change.
So, for example:
ImageButton b = (ImageButton)findViewById(R.id.button_1);
b.setImageDrawable(drawable);
You can load a drawable up for the checkbox so that you do it only once, which is more efficient, or you could setImageResource to R.drawable.correct, which would be a bit slower since I assume that the checkbox would probably be set for multiple images.
A better way to perform this by using SharedPreferences.
SharedPreferences sharedPref = getSharedPreferences("userpref", Activity.MODE_PRIVATE);
String image = pref.getString("userchoice","no-image");
if(image.equeals("no-image")
{
imageView.setBackgroundResource(R.drawable.android);
}
else
{
//compare here your image
imageView.setBackgroundResource(R.drawable.Correct);
}
And whenever user selects any other image, just edit your preference file by using following code.
SharedPreferences.Editor editor = pref.edit();
editor.putString("userchoice","Correct.png");
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