Shared preferences "bypass default value"? - java

So I have a few different checkboxes in my app and I'm changing their texts with the click of a button. What I'm trying to do is to save those strings in Shared Preferences but I have one problem. When creating shared preferences I have to enter a default value, but I already did set a default value in XML file, and I have too many checkboxes to set a default value for every single one of them. So my question is: is there a way to "bypass" this default value? This is my code so far:
private String getItemQuantity(String key){
SharedPreferences itemQuantitySP = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE);
return itemQuantitySP.getItemQuantitySP(key, );
}
Thank you!

You can put your default values into constants to ie. const.java file, then instead of putting default values in XML files set initial value in Activity onCreate using shared preference value. Of course read your shared preference value using default values from const.java.

Related

How to save a state in Shared preferences on Android Java?

In my application after login I have to save state in shared preference to change the app view on a button click. can anyone help me to save state in shared Preferences and on a button click i need to replace the initial state with the new one.
You can't save a layout. You can save a layout id- but I wouldn't suggest it. Resource ids are not stable across builds, so any update would break it. Your best bet is to create an enum with whatever values you wish it to have, save the enum, and later on convert the string back into an enum when you read it in. Then hold a map<enum, resourceId> in code that maps to the correct resource id.

How to save Strings locally and then update them programmatically?

what is the best way to store some Strings(used in Textview as a text) locally and then update them programmatically? Because I think(researched) that data can not be changed during runtime from res/values/Strings.xml
for example, I have a Textview, EditText, and a button. I get TextValue from Strings.xml using SetText in JAVA. I want to change the single string value from Strings.xml the value that I get from EditText.
Tell me some alternative method to get and change strings value or to how to use custom strings.xml to change the value in runtime.
Values places in XML are immutable, so you cannot change them at runtime. If you want changed values then set them directly on TextView by adding TextWatcher to your Edittext or setValues from a button's click listener.
Despite the name, this is what SharedPreferences are there for.

How do I persist searched text in the SearchViews text field on search?

I have a SearchView in my action bar. When the user searches for a query and presses the "Go" button on keyboard, the search intent gets passed to my activity but the search field collapses and the query text is lost. How can I prevent this from happening? I would like the text to remain in the search field until explicitly cleared by the user.
Thanks
You can store the search request either in a String variable or in the shared Preferences when the "Go" button is clicked by using getQuery() when the search event happends (setOnSearchClickListener) and then write them back in either as the Query (setQuery) or as a hint (setQueryHint).
Edit (Based on the Question you can read below this answer)
How can you store the query string if the activity is recreated after every search? If you have some service that is persitent in the runtime of your aplication you could store it there, but that isn't that elegant. The easyest way to do it is to use the SharedPreferences. This is a persitant (key,value)-storage that you can use throughout your app to store and retreive values.
You initiate a SharedPreference with
SharedPreferences prefs = getSharedPreferences("mysharedprefenrences", 0);
The String can be anything you want. Whenever you use the same String to create a SharedPreferences-Object in your app, you will have acces to these values again.
To get a String, like your search query, use
String aString = prefs.getString("searchQuery", "")
with the second String being the default, if the query isn't set yet (in this case "").
When the setOnSearchClickListener is enacted you can store the Query by using an Editor.
Editor editor = prefs.edit();
editor.putString("searchQuery", queryString);
editor.commit();
Now the next time the activity is created, instead of the default String your stored String will be found.
My solution turned out to be dead simple. Apparently, I wasn't setting the android:launchMode properly for the Activity. Ideally an activity that provides search functionality should be a single top so the user doesn't have to navigate backwards through all the previous searches upon clicking the "Back" button. Mine wasn't. Therefore, every time I searched, a new instance of my search activity was initiated and added to the view stack.
I added this to my manifest file under the correct activity.
android:launchMode="singleTop"

Set/Get values for RadioGroupFieldEditor in SWT

I'm trying to add a RadioGroupFieldEditor in an Eclipse RCP application I'm developing, but seem unable to do two key things:
set the value for the radio button (i.e. when the dialog/window is opened, I'd like to for example set the default to "button1")
get the current value of the selected radio button (i.e. what has been selected by the user, or if nothing has been set, the default value set above).
The code I'm using is as follows:
String[][] radioButtonOptions = new String[][] { { "Button1" "button1" },
{ "Button2" "button2" } };
RadioGroupFieldEditor radioButtonGroup
= new RadioGroupFieldEditor("PrefValue", "Choose Button1 or Button2", 2,
radioButtonOptions, parent, true)
I have a fireValueChanged() method, which I could use to set another String variable with the value (when the user makes a choice), but this just seems messy. It also won't allow me to set the default value...
I suspect I'm missing something significant! Should there be get/set methods for the above?
Since this control is operating on preferences, you can set the default value in your preference initializer.
To get the value of the control, you could gt the actual radio control via the getRadioBoxControl(Composite) method and query that object. Not the cleanest way, but it does work adequately.

Saving and restoring sets of preferences

Are there any nice ways to save and then restore the values for a group of preferences (i.e. stored using SharedPreferences) in Android?
For my game app, I have about 10 preferences concerning how the game should be rendered. What I'd like to do is add various profiles that can be selected, where each profile would set the values of the 10 preferences. For example, I might have a "low battery usage" profile that would set all the rendering preferences to values that use the least amount of battery or a "high detail" profile to set all the rendering preferences to their highest settings.
I will also have several preference that are not set by these profiles. For example, I will have a preference that stores the name of the current active profile.
What options do I have about how I save/restore profiles? How would changing profiles be implemented?
IMHO basically there are three ways, how to achieve your goals:
Create set of SharedPreferences - separate for each profile, like:
settingsCommon=context.getSharedPreferences("MyCommon", MODE_PRIVATE);
settingsLowBattery=context.getSharedPreferences("MyLowBattery", MODE_PRIVATE);
Add to each preference key smth like
suffix which will point to profile:
int getIntPreference(SharedPreferences settings, String profile, String key, int defValue)
{
String fullKey=key+"."+profile;
return settings.getInt(fullkey, defValue);
}
Extend SharedPreferences class and
implement your own, including
creating/parsing of any kind XML

Categories

Resources