I am working right now with Android's SharedPreferences.
I have set some preference binded to EditTextPreference - so content is String type and we access it with:
String defaultValue = "1337";
String value = preferences.getString("key", defaultValue);
But what about we want to work with int value ?
int defaultValue = 1337;
int value = Integer.parseInt(preferences.getString("key", String.valueOf(defaultValue)));
Is there a better way ? Can I somehow set EditTextPreference to be int typed ?
Can I somehow set EditTextPreference to be int typed ?
No, sorry. You can create your own custom subclass of DialogPreference, perhaps even a subclass of EditTextPreference (not sure how flexible that is), that saves values as an integer value in the SharedPreferences.
I wouldn't want to edit a numeric value as text, except as a secondary power-user method. There are so many ways you might want to constrain the value (e.g., min/max, or enumerated values) that it makes sense to use a widget that returns a result that is correct by construction. Both from the programming perspective and UX.
Related
How can I set textsize from setting preference fragment into textView? The font size is selected from a listView I will provide the code below the image.
Please see my image below:
I know I am doing something wrong. Please show me a proper way to do this.
This is my font size value:
This is my listpreference:
<ListPreference
android:title="Font Size"
android:summary="Select desirable font size"
android:key="FontSizeKey"
android:entries="#array/select_font_size"
android:entryValues="#array/select_font_size_value"
android:defaultValue="24"/>
and also if you guys have any website/tutorial/blogs link for me to refer, it would be good.
The value which TextView accept is float. One more thing is that to get the value from SharedPreference, use KEY.
String setFontSize = getSharedPreferences.getString("FontSizeKey");
setDuaText = setTextSize(Float.valueOf(setFontSize));
convert string to float using this method
secDuaText.setTextSize(Float.parseFloat(setFontSize));
From the hint its quite clear that you the method is expecting a float value. And you are passing it a string value. So either save it as a float using putDouble in your shared prefs and use getDouble and save it in a float variable. Like below
SharedPrefs.putDouble("FontSizeKey", 24.0);
double setFontSize = SharedPrefs.getDouble("FontSizeKey");
Else, just parse the String for a float value like below
secDuaText.setTextSize(Float.parseFloat(setFontSize));
I have a preferences screen in Android with multiple , like PreferenceCategory and EditTextPreference. But how do I access these fields? I searched on Google and came across findPreference, which is deprecated I guess. So how can I find these fields in my MainActivity.class so I can use their values or make onclick events for them.
Thanks in advance.
You can use the code below as an example
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(context);
String strUserName = SP.getString("username", "NA");
boolean bAppUpdates = SP.getBoolean("applicationUpdates",false);
String downloadType = SP.getString("downloadType","1");
Note that the first parameter is the key and the second parameter is the default value to return in case the value is not present.
use shared preferences instead
http://developer.android.com/reference/android/content/SharedPreferences.html
Even after setting EditText type to date or number etc., I need to convert the output of getText() to String first then to respective Date or Integer.
So, Editable does not seem any better than mere String, so why doesn't Android just return the String?
According to the documentation String is immutable. Editable however allows its markup and content to be changed.
The reason EditText returns an Editable is likely for your ease - so you can edit the text, and set it again.
I want to make my app settings with multi language support. Value of settings item will be different in each language. I have string array:
<string-array name="syncTemperature">
<item>#string/celcius</item>
<item>#string/fehrenheit</item>
</string-array>
Which is used in:
<ListPreference
android:key="prefTempUnit"
android:entries="#array/syncTemperature"
android:summary="#string/pref_temp_current"
android:entryValues="#array/syncTemperature"
android:title="#string/pref_temperature" />
and when I will call:
String celcius = sharedPrefs.getString("prefTempUnit", "Celcius")
I will get different value everytime.
My question is how to have one value for all strings under one item.
For example when I want to check what user choose and make some action after.
Like this:
if(prefTemUnit==celcius){
setTempUnitToCelc();
}
EDIT:
For now I figured out one option:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String unit=sharedPrefs.getString("prefTempUnit", "Celcius");
String[] stringArray = getResources().getStringArray(R.array.syncTemperature);
if(unit.equals(stringArray[0])){
//mymethod
}
but I dont know if its the proper one.
Okay I got your solution:
First of all:
String celcius = sharedPrefs.getString("prefTempUnit", "Celcius") will return "Celcius" as a default value. Maybe you should remove it?
Second:
If you would like to access the String value according to the actual language, you should use:
String retrievedValueFromStringXML = getResources().getString(R.string.celcius);
To add:
ListPreference has a method called getValue(), you should use it as well to retrieve the actual value.
I am trying to set String values for ids defined in xmls. I have defined the set of text which can be assigned to different ids in the layout xml files. Thereby I can also see the int values that are associated with different texts in R.java.
I have stored the variable names that I have given to the texts in my database along with the R.id prefix as they appear in R.java file.
For setting text,
TextView messageone = (TextView)findViewById(R.id.textfield1);
Normal Usage:
String message = "Hi, hello";
messageone.setText(Status);
What i want to implement:
public static final int messagestring is present in R.java
R.id.messagestring is stored in sqlite database in text format
messageone.setText(what_here);
what_here = a way to get the value from "R.id.messagestring" string as obtained from database.
I know public int getIdentifier (String name, String defType, String defPackage)
can be used here. The only change would be stored text in database will change from R.id.messagestring to messagestring. But there is a note discouraging this type of implementation.
It says: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Android Docs getIdentifier
I think although this method seems like a longer implementation, can be efficient when the objects dealt with are not text.
There's a getString(int Id) method you can use inside an Activity. It will return a String from a given R.string Id.
String something = getString(R.string.something);
I hope I helped out a bit here, because I'm not entirely sure if I understand your question.