PreferencesHelper

Additional

Language
Java
Version
N/A
Created
Nov 29, 2017
Updated
Jan 2, 2019 (Retired)
Owner
Khang Tran (tntkhang)
Contributors
Khang Tran (tntkhang)
albertocappellina
2
Activity
Badge
Generate
Download
Source code

preferences-helper

SharePreferences is very popular with any project and all most all project has SharePreferences for saving data. This library will help you faster in configuration and use SharePreferences in your project. Let's setup and enjoy!

Setup

  1. Add to build.gradle in app level
implementation 'com.github.tntkhang:preferences-helper:1.3.2'
  1. Init the PreferencesHelper in your CustomApplication.java extend from Application with 2 ways:

    • Will use app name as of Preferences file name PrefHelper.initHelper(this);

    • Will use CustomName as the name of SharePreferences file PrefHelper.initHelper(this, "CustomName");

  2. How to use

    SET VALUE:

        PrefHelper.setVal(KEY_BOOLEAN, true);
        PrefHelper.setVal(KEY_DOUBLE, 123.123);
        PrefHelper.setVal(KEY_FLOAT, 234.234f);
        PrefHelper.setVal(KEY_INT, 345);
        PrefHelper.setVal(KEY_LONG, Long.MAX_VALUE);
        PrefHelper.setVal(KEY_STRING, "Khang");

        UserModel userModel = new UserModel("KhangTran", 27);
        PrefHelper.setVal(KEY_OBJECT, userModel);

        List<String> stringList =  Arrays.asList("Tran", "Nguyen", "Thai", "Khang");
        PrefHelper.setVal(KEY_ARRAY_LIST, stringList);

        String[] arr = new String[4];
        arr[0] = "Tran 2";
        arr[1] = "Nguyen 2";
        arr[2] = "Thai 2";
        arr[3] = "Khang 2";
        PrefHelper.setVal(KEY_ARRAY, arr);
        
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("Tr", "Tran");
        stringMap.put("N", "Nguyen");
        stringMap.put("Th", "Thai");
        stringMap.put("K", "Khang");
        PrefHelper.setVal(KEY_MAP, stringMap);

GET VALUE:

boolean booleanValue = PrefHelper.getBooleanVal(KEY_BOOLEAN, false);
double doubleValue = PrefHelper.getDoubleVal(KEY_DOUBLE, Double.MIN_VALUE);
float floatValue = PrefHelper.getFloatVal(KEY_FLOAT, Float.MIN_VALUE);
int intValue = PrefHelper.getIntVal(KEY_INT, Integer.MIN_VALUE);
long longValue = PrefHelper.getLongVal(KEY_LONG, Long.MIN_VALUE);
String stringValue = PrefHelper.getStringVal(KEY_STRING, "Empty");
UserModel userModel = PrefHelper.getObjectVal(KEY_OBJECT, UserModel.class);
List<String> stringList = PrefHelper.getListVal(KEY_ARRAY_LIST);
String[] strings = PrefHelper.getArrayVal(KEY_ARRAY);
Map<String, String> stringMap = PrefHelper.getMapVal(KEY_MAP);

Map<String, ?> stringMap = PrefHelper.getAll();

DEMO

Download and run project for simple demo

That is, let's enjoy!