Where to store Android preference keys?

I've found that it's possible to store keys in strings.xml and refer to them from preferences.xml just like all other values android:key="@string/preference_enable".

In code you can refer to key by typing getString(R.string.preference_enable)

You can mark the string to not be translated using a <xliff:g> tag. See Localization Checklist

<string name="preference_enable"><xliff:g id="preference_key">enable</xliff:g></string>

You could use a "keys.xml" files in "res/values", but should put something like this, this way you should dont have problem when you are using multiple languages:

    <resources
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="MissingTranslation">

    <string name="key1">key1</string>
    <string name="key2">key2</string>
...
</resources>

Then you could reference it like a normal string in xml:

....
android:key="@string/key1"
....

or in your java code for example:

SwitchPreference Pref1= (SwitchPreference) getPreferenceScreen().findPreference(getString(R.string.key1));

In strings.xml mark the key as untranslatable:

<string name="screw_spacing_key" translatable="false">Screw spacing</string>
<string name="screw_spacing_title">Screw spacing</string>
<string name="screw_spacing_summary">Set screw spacing</string>

Usage:

<EditTextPreference
    android:key="@string/screw_spacing_key"
    android:title="@string/screw_spacing_title"
    android:summary="@string/screw_spacing_summary"/>

See: Configure untranslatable rows


Try getString(R.string.key_defined_in_xml).