Android SharedPreferences field binder with annotation processor
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
buildSrc
demo
gradle/wrapper
preferencer-annotations
preferencer-compiler
preferencer-gradle-plugin
preferencer
.gitignore
LICENSE
README.md
build.gradle.kts
gradle.properties
gradlew
gradlew.bat
settings.gradle.kts

README.md

Preferencer

bintray download build license

Android SharedPreferences field injector with annotation processor.

public class ExampleActivity extends Activity {
    @Preference("username") String username;
    @Preference("age") int age;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Preferencer.bind(this);
        // TODO Use fields...
    }
}

Download

repositories {
    jcenter()
}

dependencies {
    compile "com.hendraanggrian.preferencer:preferencer:$version"
    annotationProcessor "com.hendraanggrian.preferencer:preferencer-compiler:$version" // or kapt with kotlin
}

Preference binding

Value binding

Use @Preference to load a preference value to field. When a key is not found, current value is returned.

@Preference("key1") String username;
@Preference String password; // key will be 'password'

PreferenceScreen binding

Preferencer can also bindPreferences Preference from default android.app., support preference-v7, and support preference-v14.

res/xml/settings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.v7.preference.PreferenceCategory>
        <android.support.v7.preference.EditTextPreference
            android:key="key0"/>
        <android.support.v7.preference.SwitchPreferenceCompat
            android:key="key1"/>
    </android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>
java/SettingsFragment.java
public class SettingsFragment extends PreferenceFragmentCompat {
    @Preference("key0") EditTextPreference editTextPreference;
    @Preference("key1") SwitchPreferenceCompat switchPreference;

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.settings);
        Preferencer.bind(this);
    }
}

Modifying preferences

Preferencer.bindPreferences() returns Saver, it is an interface that saves values of binded fields to SharedPreferences.

Saver saver = Preferencer.bind(this);

Allocate new value to field and call saveAll() or save() to save individually. Saver will ignore the field it is an instance of android.app. support preference-v7, or support preference-v14.

username = "Hendra Anggrian";
saver.save()

License

Copyright 2017 Hendra Anggrian

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.