-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can i map to xml prefs #47
Comments
Hi, is it pretty simple! Firs you should define some constants keys in <!--PREFERENCES CONFIG - START -->
<string name="preference_file_name" translatable="false">app_preferences_file_name</string>
<string name="preference_property_key" translatable="false">property_key</string>
<string name="preference_property_default_value">Default value here</string>
<!--PREFERENCES CONFIG - END --> In your <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="@string/preference_file_name"
>
<EditTextPreference
android:dialogTitle="@string/title_location_preference"
android:key="@string/preference_property_key"
android:defaultValue="@string/preference_property_default_value"
android:title="@string/preference_property_title"
/>
</PreferenceScreen> Now, you your class MyAppPreferencesFragment : PreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
preferenceManager.sharedPreferencesName = getString(R.string.preference_file_name)
addPreferencesFromResource(R.xml.preferences)
}
} In your KotprefModel you should set the same filename, keys and values: object MyAppPreferences : KotprefModel() {
override val kotprefName: String = context.getString(R.string.preference_file_name)
var property by stringPref(context.getString(R.string.preference_property_default_value), R.string.preference_property_key)
} Now you can use Kotpref to read and store the pref value in your application. In this example I'm using a simple string preference property. But you can d adapt it to your needs, for instance, list preference. In this case, remember to set the values that you defined in <ListPreference
android:id="@+id/pref_music_repeat"
android:defaultValue="@string/preference_property_default_value"
android:entries="@array/Replay_arrays"
android:entryValues="@array/array_repeat_value"
android:icon="@drawable/ic_pref_time"
android:key="@string/preference_property_key"
android:summary="%s"
android:title="@string/preference_property_title" /> |
Thanks @dsdebastiani !! 😄 @saikiran91 object MyAppPreferences : KotprefModel() {
// set default shared preference file name
override val kotprefName: String = "${context.packageName}_preferences"
} |
I have an XML preference
How can i use this in Kotpref
The text was updated successfully, but these errors were encountered: