Skip to content
[Android Library] A SharedPreferences helper library to save and fetch the values easily.
Kotlin
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
gradle/wrapper init Mar 26, 2019
preferencehelper replaced callback interface with kotlin's typealias Apr 5, 2019
sample replaced callback interface with kotlin's typealias Apr 5, 2019
.gitignore init Mar 26, 2019
LICENSE.txt Create LICENSE.txt Mar 28, 2019
README.md updated Readme Mar 31, 2019
build.gradle Bintray setup Mar 29, 2019
gradle.properties init Mar 26, 2019
gradlew init Mar 26, 2019
gradlew.bat init Mar 26, 2019
settings.gradle init Mar 26, 2019

README.md

Preference Helper

A SharedPreferences helper library to save and fetch the values easily.

Featured in

AndroidDev Digest

Use in your project

Add this to your module's build.gradle

repositories {
    jcenter()
}

dependencies {
    implementation 'com.naveentp:preferencehelper:0.0.1'
}

Usage Example

  • Initialise the SharedPreferences

    • Default SharedPreferences

      PreferenceHelper.defaultPref(this)

      or Lazy loading

      private val sharedPref: SharedPreferences by lazy {
          PreferenceHelper.customPref(this, "Sample")
      }
    • Custom SharedPreferences

      PreferenceHelper.customPref(this, "YOUR_SHARED_PREFERENCES_NAME")
  • Save / Fetch values from SharedPreferences

    • Using operator overloading method

      • Save

        // To save the value to SharedPreferences
        sharedPref["name"] = "Naveen" 
        
      • Fetch

        // To fetch the value from SharedPreferences
        val userName = sharedPref["name", DEFAULT_VALUE] 
        
    • Using Delegation properties method

      • Initialisation

        // Create userName variable that corresponds to sharedPreference "name" key
        var name by PreferenceDelegate(sharedPref, "name", DEFAULT_VALUE)
        
      • Save

        //Saves value "Naveen" to sharedPreference with key "name"
        name = "Naveen"
      • Fetch

        // To fetch the value "Naveen" from SharedPreferences
        val userName = name
        
    • Adding Listeners

      • Individual

        var userAge by PreferenceDelegate(sharedPref, "name", 0, object : PreferenceObserver {
            override fun <T> onChangePreferenceValue(key: String, value: T) {
                print("$key: $value")
            }
        })
      • Global (Activity level)

        class MainActivity : AppCompatActivity(), PreferenceObserver {
            
            //Lazy initialisation of SharedPreferences
            private val sharedPref: SharedPreferences by lazy {
                PreferenceHelper.customPref(this, "Sample")
            }
            
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_main)
                
                //Pass activity reference handle callbacks all at one place.
                var userAge by PreferenceDelegate(sharedPref, "name", 0, this)
            }
            
            
            // Global listener which listens to all the PreferenceDelegates
            override fun <T> onChangePreferenceValue(key: String, value: T) {
                print("$key: $value")
            }
        }

    ** Refer MainActivity in Sample project for more samples

License

Licensed under Apache Licence, Version 2.0. Check the full licence here

You can’t perform that action at this time.