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));
Related
I want to divide two numbers fetched from edit text and save it in a TextView but I am unable to save a float type Number into String
tv.setData(div.toString()); //not working
In this code, where tv is TextView type variable and div is float type variable.
Use String.valueOf(div) instead of div.toString() like so:
tv.setText(String.valueOf(div));
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 have a dimension resource setup in my res/dimens.xml like so:
<dimen name="cornerRadius">40.0dp</dimen>
However, when I grab it into a float object like this:
float cornerRadius = R.dimen.cornerRadius;
And output the value, it gives me 2.1309686E9 every time. I'll change the value from 40.0dp to 15dp and it still outputs 2.1309686E9.
What am I doing wrong?
R.dimen.cornerRadius is just a reference but not a value. You should use getResources().getDimension(R.dimen.cornerRadius) instead
You are getting the ID not the value so try this :
getResources().getDimension(R.dimen.cornerRadius)
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.
Can you change the values of a R.string programmatically in an android program? I need to pull some API information (for example battery state, battery percentage, android os version) and would like to save it a R.string value. I know how to read it:
String helloValue= getResources().getString(R.string.hello);
I've also looked at: Change value of R.string programically? but it seems that only involves changing language and he ended up doing it a different way. Can anyone lend a hand please? I've also looked here: http://developer.android.com/guide/topics/resources/string-resource.html and found nothing to help out either :(
You can't change strings.xml dynamically since it's a compiled resource. There are other mechanisms for saving data in Android, here's a nice post that covers this topic: Data Storage. Hope this helps.
If you need to save small amounts of String information you should be using SharedPreferences that's exactly what it's for :)
There are many ways to finish your ideas. But, you cannot change your String resource, (as well as R.java resource), or layout resource,... and any other resource. You can do this by change resource to assets. this can read and write :)
You can get value from res/string value by this way.
String str = getResources().getString(R.string.<name_of_value>);
Now you can further use this value of this(str) String variable.
But according to me there is no way to set value of res/string