Simple Android SharedPreferences wrapper.

Overview

ko-fi

Prefs

Android Arsenal

Simple Android SharedPreferences wrapper.

Repository

Add this in your root build.gradle file (not your module build.gradle file):

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

Dependency

Add this to your module's build.gradle file (make sure the version matches the JitPack badge above):

dependencies {
	...
	compile 'com.github.GrenderG:Prefs:1.3'
}

Usage

This is the basic usage, you can read values (specifying a default value or not) and write values like so:

  Prefs.with(yourContext).readInt(YOUR_KEY_VALUE);
  Prefs.with(yourContext).readInt(YOUR_KEY_VALUE, defaultValue);
  
  Prefs.with(yourContext).writeInt(YOUR_KEY_VALUE, valueToStore);

You can also specify a custom name for the preferences' name:

  Prefs.with(yourContext, "CUSTOM_NAME").readInt(YOUR_KEY_VALUE);

And force the re-instantiation of the Prefs instance:

  Prefs.with(yourContext, true).readInt(YOUR_KEY_VALUE);
  Prefs.with(yourContext, "CUSTOM_NAME", true).readInt(YOUR_KEY_VALUE);

You can also set and retrieve ordered String sets and unordered String sets.

You might also like...
Command-line tool to count per-package methods in Android .dex files

dex-method-counts Simple tool to output per-package method counts in an Android DEX executable grouped by package, to aid in getting under the 65,536

View Inspection Toolbar for Android Development
View Inspection Toolbar for Android Development

View Inspector Plugin View inspection toolbar for android development. Features Boundary show outlines show margins show paddings Layer Scalpel featur

Make Android screenshots of scrollable screen content
Make Android screenshots of scrollable screen content

scrollscreenshot Make Android screenshots of scrollable screen content - brought to you by PGS Software SA This tool makes a number of screenshots, sc

🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.
🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.

🇨🇳 中文 / 🇯🇵 日本語 / 🇬🇧 English 🍼 Debug Bottle An Android debug / develop tools written using Kotlin language. All the features in Debug bottle are

[] Dissect layout traversals on Android
[] Dissect layout traversals on Android

Probe Dissect layout traversals on Android. Features Intercept View methods. onMeasure(int, int) onLayout(boolean, int, int, int, int) draw(Canvas) an

Android Library Finder
Android Library Finder

alfi Android Library Finder Search through thousands of android libraries that can help you scale your projects elegantly Usage Search for something a

Combines tools for fast android app devlopment

Android - Rapid Test Driven Development Combine tools to generate most of the boilerplate code. Examples how to test different aspects of an android a

Make mosaic effect on android
Make mosaic effect on android

ProMosaic Make mosaic for image on android. Features Select Mode Follow finger Select rectangle Effect Mode Grid color based on original image Blur Im

A set of Android tools that facilitate apps development
A set of Android tools that facilitate apps development

A set of Android tools that facilitate apps development Well, this repo contains pretty much code used internally at Stanfy to develop Android apps. S

Comments
  • Customize preferences file name

    Customize preferences file name

    I notice by default this library handles preferences with the name 'preferences' but I think it would be cool to specify the name of the Shared Preference file the user wants to access.

    Basically creating a call like: Prefs.with(yourContext, "FILE NAME").readInt(YOUR_KEY_VALUE); having the second parameter optional, defaulting to the what the library does now.

    enhancement 
    opened by mastrgamr 2
  • Static methods versus class

    Static methods versus class

    Hi,

    I used below code for shared prefs. My friend suggested not use static methods. He advised me to use the class like you have implemented. Can you please suggest with which way we can improve performance.

        public static void saveStringPref(Context context, String key, String value) {
    
            SharedPreferences sharedPref = context.getSharedPreferences(
                    "Pref-Values", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString(key, value);
            editor.commit();
        }
    
        public static String getStringPref(Context context, String key,
                                           String defaultValue) {
            SharedPreferences sharedPref = context.getSharedPreferences(
                    "Pref-Values", Context.MODE_PRIVATE);
            return sharedPref.getString(key, defaultValue);
        }
    
        public static void saveIntPref(Context context, String key, int value) {
    
            SharedPreferences sharedPref = context.getSharedPreferences(
                    "Pref-Values", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putInt(key, value);
            editor.commit();
        }
    
        public static int getIntPerf(Context context, String key,
                                     int defaultValue) {
            SharedPreferences sharedPref = context.getSharedPreferences(
                    "Pref-Values", Context.MODE_PRIVATE);
            return sharedPref.getInt(key, defaultValue);
        }
    
        public static void saveBooleanPref(Context context, String key, boolean value) {
    
            SharedPreferences sharedPref = context.getSharedPreferences(
                    "Pref-Values", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putBoolean(key, value);
            editor.commit();
        }
    
        public static boolean getBooleanPerf(Context context, String key,
                                             boolean defaultValue) {
            SharedPreferences sharedPref = context.getSharedPreferences(
                    "Pref-Values", Context.MODE_PRIVATE);
            return sharedPref.getBoolean(key, defaultValue);
        }
    
        public static void deletePref(Context context, String key) {
            SharedPreferences sharedPref = context.getSharedPreferences(
                    "Pref-Values", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.remove(key);
            editor.commit();
        }
    
    
    invalid 
    opened by prashanthd 1
Owner
null
A simple utility to remove unused resources in your Android app to lower the size of the APK. It's based on the Android lint tool output.

android-resource-remover android-resource-remover is utility that removes unused resources reported by Android Lint from your project. The goal is to

Keepsafe 1.3k Dec 16, 2022
Annotation based simple API flavored with AOP to handle new Android runtime permission model

Let Annotation based simple API flavoured with AOP to handle new Android runtime permission model. If you check Google's Samples about the new permiss

Can Elmas 530 Nov 25, 2022
Simple library to generate and view PDF in Android

PDFCreatorAndroid Simple library to generate and view PDF in Android A simple library to create and view PDF with zero dependency Or native code. Add

Tej Pratap Singh 234 Dec 11, 2022
A tool to install components of the Android SDK into a Maven repository or repository manager to use with the Android Maven Plugin, Gradle and other tools.

Maven Android SDK Deployer Original author including numerous fixes and changes: Manfred Moser [email protected] at simpligility technologies i

simpligility 1.4k Dec 27, 2022
This is a Android Studio/ IntelliJ IDEA plugin to localize your Android app, translate your string resources automactically.

#Android Localizationer This is a Android Studio/ IntelliJ IDEA plugin to localize your Android app, translate your string resources automactically. T

Wesley Lin 822 Dec 8, 2022
A tool to install components of the Android SDK into a Maven repository or repository manager to use with the Android Maven Plugin, Gradle and other tools.

Maven Android SDK Deployer Original author including numerous fixes and changes: Manfred Moser [email protected] at simpligility technologies i

simpligility 1.4k Dec 27, 2022
Automated-build-android-app-with-github-action - CI/CD Automated Build Android App Bundle / APK / Signed With Github Action

Automated Build Android With Using Github Action Project Github Action Script Us

Faisal Amir 34 Dec 19, 2022
proguard resource for Android by wechat team

AndResGuard Read this in other languages: English, 简体中文. AndResGuard is a tooling for reducing your apk size, it works like the ProGuard for Java sour

shwenzhang 8.1k Jan 9, 2023
A super fast build tool for Android, an alternative to Instant Run

Freeline Freeline is a super fast build tool for Android and an alternative to Instant Run. Caching reusable class files and resource indices, it enab

Alibaba 5.5k Jan 2, 2023