Is SharedPreferences' defaultValue always read as String when defined in .xml? - java

I'm trying to retrieve a default value:
Definition:
<integer name="keyOfDefaultValue">2</integer>
Referenced as:
android:defaultValue="#integer/keyOfDefaultValue"
Actual code:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int intVar = sharedPrefs.getInt(keyOfDefaultValue, fallbackIntValue);
I get ClassCastExceptions stating that I'm trying to cast String to an Integer. I've read the Android docs about default values:
http://developer.android.com/reference/android/preference/Preference.html#attr_android:defaultValue
so I expected to work with the correct type, but apparently I get a String.
Can someone confirm that default values defined in .xml are always to be retrieved as Strings? Or you could just point me to a page in the documentation where this is explained...
Thanks

if you check the return value on AOSP code for different types of preference
EditTextPreference, ListPreference return String values
SwitchPreference,CheckBoxPreference returns boolean
MultiSelectListPreference returns a set of strings
If you want to change it, I guess you can always override
onGetDefaultValue in your customPreference , if you have one

Related

android, String value isn't visible in debugger

I want to track the value of an edited text in android, I am using debug mode.
Here is a portion of code, I retrieve data from the user's input and I want to see it in the debugger.
EditText personName=findViewById(R.id.editTextTextPersonName2);
EditText number=findViewById(R.id.editTextNumber);
Intent sendBack=new Intent();
String sentName=personName.toString();
Integer a=Integer.parseInt(number.getText().toString());
sendBack.putExtra("name", sentName);
setResult(RESULT_OK, sendBack);
Activity2.this.finish();
But, I noticed that the value returned by an integer (Number ) is shown in the debugger while the string ( plainText ) value isn't.
I want to be able to see the string's value like what is happening for the integer as shown in the screenshot.
Actually, the String is visible in the debugger. As you have performed String sentName=personName.toString();, the EditText#toString function is returing the all information about personName (EditText) instead of it's value.
The information of this EditText contains the id of view, location, size, package name, value, etc.
You can observe in your screenshot that the string starts with the package name of the EditText being used (i.e. "androidx.appcompat.widget.AppCompatEditText") followed by some other information like ObjectId, etc.
In case you just need to access the value inside this EditText just modify your code from String sentName=personName.toString(); to String sentName=personName.getText().toString();
I guess you have done a typo in the first line you have written "personName" two times.
Check this line. EditText personName=personName=findViewById(R.id.editTextTextPersonName2); Also, more specifically if you are using API level < 26. It is better you cast it to EditText like this as findViewById() method returns an integer value.
EditText personName= (EditText)findViewById(R.id.editTextTextPersonName2);And use String sentName = personName.getText() to get the text data from EditText View.

How to change the resource background color of TextView?

I want to change drawable picture color of TextView(background) but I tried this code without success:
textView.getBackground().setColorFilter(Color.parseColor(listMap.get((int)position)
.get("color").toString()), PorterDuff.Mode.SRC_IN);
I'm getting this error:
The method parseColor(String) is undefined for the type String
Try this (Just checked it in android studio and it works fine):
val myBackgroundColor = Color.parseColor(listMap.get((int)position).get("color").toString())
textView.setBackgroundColor(myBackgroundColor)
If it doesn't work, I guess it means thatthe color value you are getting from your listMap is not a proper hex value like #00FF00.
Check and make sure it is in correct format.
what's the string received from this ?
listMap.get((int)position).get("color").toString()
I think this function just return the number of the color, you also need the "#"
Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are:
#RRGGBB
#AARRGGBB
acording to Android official documentation

checkbox to string conversion

I have a page that contains diff properties with checkboxes.and I have another page that contains the summary of the above page. That means If I check the in the first page its has to reflect the relavent field details in the summary page.But as the variable type in pojo is boolean its showing as true of false.I unable to get the fields name.Im using HTML front end and java. Please help me on how to do it.
In pojo the variable is boolean. Im fetching that variable in the next summary page. I need that to be name of the variable not true of false. hope im clear
You can add the string you want to properties, evaluate your boolean, get the message you want from properties according to the boolean value:
public String getLabel(){ if(isMarried) return messages.get(marriedPropertyMessage);}

How can I get the default value of a ListPreference as defined in the XML?

How I can programmatically get the default value of a ListPreference as it have defined in the XML?
Here is the snippet of my ListPreference:
<ListPreference
android:defaultValue="60"
android:entries="#array/interval_entries"
android:entryValues="#array/interval_values"
android:key="interval"
android:summary="#string/interval_summary"
android:title="#string/interval_title" />
I have been through the docs but I have not found a way to get this. Maybe I have overlooked it.
For PreferenceActivity (deprecated with Fragment), try:
ListPreference lp = (ListPreference) this.findPreference(this.getString(R.string.my_key));
lp.getValue();
Where my_key is the key value assigned to this ListPreference. Note: This value is defined in strings.xml. If you have hard-coded your key with a literal string, then substitute my_key with whichever string you've given for the android:key tag. So, in your case, the codes will be:
ListPreference lp = (ListPreference) this.findPreference("interval");
lp.getValue();
Are you trying to initially set the default value or reset it to default? Regardless have a look at:
PreferenceManager.setDefaultValues(this, R.xml.your_pref_xml, false);
and the documentation

How to get the default value from XML ( not working )

When starting service i need to check for particular setting ( boolean ). Please check following code:
XML Code:
<CheckBoxPreference android:title="Enable Sleep" android:defaultValue="false" android:key="checkbox_preference"/>
Java Code:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean sleepEnabled = prefs.getBoolean("checkbox_preference", true);
if(sleepEnabled) {
// Code
}
Even tho, default value in XML is "false" i got "true" on every service start. I know this is because I'm setting parameter in "getBoolean()" method to "true", but I need there the actual value of checkbox ..
Why Am I getting always "true" when starting service?
p.s If i go to Settings and change the value of checkbox it's fine. Then when starting service again it pulls the actual value. So how to get the actual DEFAULT value set in XML?
change this:
boolean sleepEnabled = prefs.getBoolean("checkbox_preference", true);
to this:
boolean sleepEnabled = prefs.getBoolean("checkbox_preference", false);
and take a look at the documentation:
http://developer.android.com/reference/android/preference/Preference.html#attr_android:defaultValue

Categories

Resources