开发者

Setting Boolean Preference default value depending on device langage (Locale)

开发者 https://www.devze.com 2023-03-18 03:09 出处:网络
I currently have an application where the french users should have a preference set to false by default and the other one should have this preference set to true.

I currently have an application where the french users should have a preference set to false by default and the other one should have this preference set to true.

I don't understand a correct and clean way to handle that.

Currently , in my app, I call 2 times the preference.

*) first time in my preference xml l开发者_如何学运维ayout and in my preference activity, I have to set the preference at the first launch.

*) Second time, in my code:

boolean value = prefs.getBoolean("key"), true/false);

Thanbk a lot for all your ideas and explanations on how to make this in a clean way.


You can use resources in such a way to accomplish this.

Folder structure (you can probably pick whatever name you want for the actual XML file or even create the resource with other resources):

/res/values/bools.xml
/res/values-fr/bools.xml

In /res/values/bools.xml:

  • Make the boolean, you can rename this to whatever you need.
    • Name: pref_default
    • Type: boolean
    • Value: true

In /res/values-fr/bools.xml:

  • Make the boolean again, same name
    • Name: pref_default
    • Type: boolean
    • Value: false

In your preferences.xml:

  • Set the default value to @bool/pref_default

All set!

You should even be able to access that from code with R.bool.pref_default.

Disclaimer: I have never written code that involved multiple languages, but I have based this on my understanding of resource qualifiers.

If you mean the region France and not the French speakers, then you can probably find a -r qualifier for it (see my link).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号