Skip to content
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

Closed
saikiran91 opened this issue Apr 26, 2017 · 2 comments
Closed

How can i map to xml prefs #47

saikiran91 opened this issue Apr 26, 2017 · 2 comments
Labels

Comments

@saikiran91
Copy link

I have an XML preference

 <ListPreference
            android:id="@+id/pref_music_repeat"
            android:defaultValue="0"
            android:entries="@array/Replay_arrays"
            android:entryValues="@array/array_repeat_value"
            android:icon="@drawable/ic_pref_time"
            android:key="key_music_repeat"
            android:summary="%s"
            android:title="Repeat Count" />

How can i use this in Kotpref

@dsdebastiani
Copy link

Hi, is it pretty simple!

Firs you should define some constants keys in strings.xml

    <!--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 preference.xml you should set the key like this:

<?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 MyAppPreferencesFragment you should set the file name:

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 strings.xml.

 <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" />

@chibatching
Copy link
Owner

chibatching commented Apr 26, 2017

Thanks @dsdebastiani !! 😄

@saikiran91
@dsdebastiani way will be work fine. But if you use default shared preferences in production already, you can/should set default shared preferences filename in your KotprefModel.

object MyAppPreferences : KotprefModel() {
    // set default shared preference file name
    override val kotprefName: String = "${context.packageName}_preferences"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants