All Projects → viralypatel → Android-SharedPreferences-Helper

viralypatel / Android-SharedPreferences-Helper

Licence: other
This Shared Preferences Helper library (Library size = ~15kb only) Simplifies usage of the default Android SharedPreferences Class. The developer can do in a few lines of code which otherwise would have required several. Simple to understand as compared to the default Shared Preferences class and easy to use. Can be used by simply adding the dep…

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Android-SharedPreferences-Helper

Pokedex
An application that uses Retrofit to consume the Pokeapi API, in addition to loading images with Glide.
Stars: ✭ 21 (+40%)
Mutual labels:  android-studio
android-projects
Android benchmark projects for Bazel and Gradle
Stars: ✭ 29 (+93.33%)
Mutual labels:  android-studio
GoGPUtils
Enhance productivity and avoid to reinvent the wheel every time that you start a Go project
Stars: ✭ 29 (+93.33%)
Mutual labels:  helper
advancedPermissionHandler
This Android library is for handle running time permissions in simplest way!
Stars: ✭ 13 (-13.33%)
Mutual labels:  helper
Serialize
🍒 Android 简单高性能读写本地数据, 直接存储对象/基础类型
Stars: ✭ 181 (+1106.67%)
Mutual labels:  sharedpreferences
receipt-manager-app
Receipt parser application written in dart.
Stars: ✭ 140 (+833.33%)
Mutual labels:  android-studio
MVPHelper
Base classes for quick and easy implementation of MVP for Android applications.
Stars: ✭ 17 (+13.33%)
Mutual labels:  helper
BugKotlinDocument
Plugin for IntelliJ IDEA ┗😃┛ Android Studio ┗😃┛ CLion ┗😃┛ AppCode.
Stars: ✭ 29 (+93.33%)
Mutual labels:  android-studio
libimobiledevice-glue
A library with common code used by libraries and tools around the libimobiledevice project
Stars: ✭ 46 (+206.67%)
Mutual labels:  helper
helpers.js
Small JavaScript functions for common use cases.
Stars: ✭ 37 (+146.67%)
Mutual labels:  helper
Android-WebView-in-Kotlin
Native Android WebView Example in Kotlin. Website to android app github open source template.
Stars: ✭ 87 (+480%)
Mutual labels:  android-studio
AirPodsDesktop
☄️ AirPods desktop user experience enhancement program, for Windows and Linux (WIP)
Stars: ✭ 462 (+2980%)
Mutual labels:  helper
NFlags
Simple yet powerfull library to made parsing CLI arguments easy. Library also allow to print usage help "out of box".
Stars: ✭ 44 (+193.33%)
Mutual labels:  helper
FusedBulb
Location fetch library.
Stars: ✭ 22 (+46.67%)
Mutual labels:  android-studio
VideoPreLoading
Demo for video PreLoading/ PreCaching using ExoPlayer 2.13.3 in Android.
Stars: ✭ 61 (+306.67%)
Mutual labels:  android-studio
MacGyver
🤖 A simple application using Google's MLkit library and cameraX api.
Stars: ✭ 73 (+386.67%)
Mutual labels:  android-studio
varname-go-die
Android Studio plugin(根据中文在线查找翻译并生成指定格式的变量名称)
Stars: ✭ 45 (+200%)
Mutual labels:  android-studio
Awesome-Android-Persistence
A curated list of awesome android persistence libraries about SQLite, ORM, Mobile Database, SharedPreferences, etc.
Stars: ✭ 69 (+360%)
Mutual labels:  sharedpreferences
langx-java
Java tools, helper, common utilities. A replacement of guava, apache-commons, hutool
Stars: ✭ 50 (+233.33%)
Mutual labels:  helper
WiFiPS
WiFi Based Indoor Positioning System, A MVP android Application
Stars: ✭ 105 (+600%)
Mutual labels:  android-studio

Android-SharedPreferences-Helper

Simplifies usage of the default Android SharedPreferences Class.

Library size = ~15kb only. The developer can do in a few lines of code which otherwise would have required several. Simple to understand as compared to the default class and easy to use.

Salient Features

Library size: Just ~15 KB

  • One line initialization and setup
  • Easily selecting whether to use default preferences or a custom preference file
  • Predefined (data type defaults) and customizable (what you may choose) default values for each datatype
  • Ability to set different default value for single use with just an additional param
  • You can register and unregister OnSharedPreferenceChangeListener as you do for default class

Installation/Setup

Download

Gradle Dependency (recommended)

Add the following to your module's build.gradle:

dependencies {
    ...
    ...
    compile(group: 'com.viralypatel.sharedpreferenceshelper', name: 'library', version: '1.1.0', ext: 'aar')
}
Maven
<dependency>
        <groupId>com.viralypatel.sharedpreferenceshelper</groupId>
        <artifactId>library</artifactId>
        <version>1.1.0</version>
        <type>aar</type>
</dependency>
Add aar file locally

You can also download the library-<version>.aar file from this page if you want to add it locally inside the project.

Use the source code

In case you want to customize it more to your needs:

  • Clone this repository to your system
  • Import the library module to your project and use

If you are customizing it and feel that the customizations are generic and would add value for other users of this library: Pull Requests are most welcome! :-)

Usage

Declaration of SharedPreferencesHelper object: (recommended at class level)

SharedPreferencesHelper sph; 

Instantiation of the SharedPreferencesHelper object: (recommended in onCreate() method)

    // use one of the following ways to instantiate
    sph = new SharedPreferencesHelper(this); //this will use default shared preferences
    sph = new SharedPreferencesHelper(this, "myappprefs"); // this will create a named shared preference file
    sph = new SharedPreferencesHelper(this, "myappprefs", 0); // this will allow you to specify a mode

Putting values into shared preferences

Fairly simple! Unlike the default way (when using the SharedPreferences class) you'll NOT need to call .edit() and .commit() ever time.

    sph.putBoolean("boolKey", true);
    sph.putInt("intKey", 123);
    sph.putString("stringKey", "string value");
    sph.putLong("longKey", 456876451);
    sph.putFloat("floatKey", 1.51f);

    // putStringSet is supported only for android versions above HONEYCOMB
    Set name = new HashSet();
    name.add("Viral");
    name.add("Patel");
    sph.putStringSet("name", name);

That's it! Your values are stored in the shared preferences.

Getting values from shared preferences

Again, just one simple method call with the key name.

    sph.getBoolean("boolKey");
    sph.getInt("intKey");
    sph.getString("stringKey");
    sph.getLong("longKey");
    sph.getFloat("floatKey");

    // getStringSet is supported only for android versions above HONEYCOMB
    sph.getStringSet("name");

Advanced Usage

What if the value is not set for a given key already? : It returns the default value. Default values are as follows:

    private int intDefaultVal = 0;
    private long longDefaultVal = 0;
    private float floatDefaultVal = 0;
    private boolean boolDefaultVal = false;
    private String stringDefaultVal = "";
    private Set<String> stringSetDefaultVal = null;

What if i want a different value as default for each type? : Check the next section to see how to change default value.

What if i want a different default value only for a partylar key? : Yes, you can achieve that by passing it as an additional parameter in the get method as follows:

    sph.getBoolean("boolKey", true);
    sph.getInt("intKey", -1);
    sph.getString("stringKey", "my custom default string");
    sph.getLong("longKey", -222);
    sph.getFloat("floatKey", -13.76f);

    // getStringSet is supported only for android versions above HONEYCOMB
    sph.getStringSet("name", new HashSet<String>());

Setting default values for each data type when no values are set

Be careful with this as this will set the default value for the data type.

    sph.setBoolDefaultVal(true);
    sph.setFloatDefaultVal(-3.6f);
    sph.setIntDefaultVal(-3);
    sph.setLongDefaultVal(-999);
    sph.setStringDefaultVal("custom default string");
    sph.setStringSetDefaultVal(new HashSet<String>());

Registering an OnSharedPreferenceChangeListener for the shared preferences

Just like you do it for the normal SharedPreferences instance:

    SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            // do what you got to do here
        }
    };
    
    sph.registerListener(listener);

Unregistering the OnSharedPreferenceChangeListener (if registered)

Simply unregister the listener you registered above.

    sph.unregisterListener(listener);

License

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].