as the title above has said, is there any way to set default value of a preference from java code ?
if it is done from the xml side, it should be android:defaultValue.
But, how to do it from the java side ??
THX for help
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unfortunately, the default value specified in the preferences XML only applies when using a
PreferenceActivityand its UI. However, if you take a look at theSharedPreferencesobject, all of thegetmethods allow you to specify a default value to retrieve when the preference does not have a value yet. Now, you can combine both the preferences XML and Java default values using constants in the xml files.For example, declare a
<string>constant in a resource XML, then you can use it as the default value in the XML like sodefaultValue="@string/myDefaultValue". Then, in your java code you can do: