The method getFloat()
exists to pull a float value from android's SharedPreferences
API. Yet, in the xml, EditTextPreference
always stores a string value, even if numeric is defined.
One would expect getFloat()
to automatically return this but instead it throws a ClassCastException
and we have to use Float.parseFloat(SharedPreferences.getString())
to get this value.
Is there literally no use for getFloat()
or am I missing something here?
The method getFloat() exists to pull a float value from android's SharedPreferences API.
Yes.
Yet, in the xml, EditTextPreference always stores a string value, even if numeric is defined.
I think you are mixing up things here. You are setting SharedPreferences
equal to elements on a PreferenceScreen
.
PreferenceScreen's
save all their values into your SharedPreferences
and it's true that EditTextPreferences
can't save a float, but you can however save a float into your SharedPreferences
yourself:
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("aFloat", 1.2f);
editor.commit();
and then retrieve the float like this:
float someFloat = prefs.getFloat("aFloat", 0.0f);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With