• Stars
    star
    419
  • Rank 103,397 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 11 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small library containing a wrapper/helper for the Shared Preferences of android.

EasyPrefs

Deprecation notice: Since the arrival of Kotlin it has become easy to use the preferences in a typesafe manner without much boilerplate, for example, using this method. We advice to skip EasyPrefs for your next project.


A small library containing a wrapper/helper for the shared preferences of Android.

With this library you can initialize the shared preference inside the onCreate of the Application class of your app.

Java:

public class PrefsApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize the Prefs class
        new Prefs.Builder()
                .setContext(this)
                .setMode(ContextWrapper.MODE_PRIVATE)
                .setPrefsName(getPackageName())
                .setUseDefaultSharedPreference(true)
                .build();
    }
}

Kotlin:

class PrefsApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        // Initialize the Prefs class
        Prefs.Builder()
                .setContext(this)
                .setMode(ContextWrapper.MODE_PRIVATE)
                .setPrefsName(packageName)
                .setUseDefaultSharedPreference(true)
                .build()
    }

}

Usage

After initialization, you can use simple one-line methods to save values to the shared preferences anywhere in your app, such as:

  • Prefs.putString(key, string)
  • Prefs.putLong(key, long)
  • Prefs.putBoolean(key, boolean)

Retrieving data from the Shared Preferences can be as simple as:

String data = Prefs.getString(key, default value)

If the shared preferences contains the key, the string will be obtained, otherwise the method returns the default string provided. No need for those pesky contains() or data != null checks!

For some examples, see the sample App.

Bonus feature: ordered sets

The default implementation of getStringSet on Android does not preserve the order of the strings in the set. For this purpose, EasyPrefs adds the methods:

void Prefs.putOrderedStringSet(String key, Set<String> value);

and

Set<String> Prefs.getOrderedStringSet(String key, Set<String> defaultValue);

which internally use Java's LinkedHashSet to retain a predictable iteration order. These methods have the added benefit of adding the missing functionality of storing sets to pre-Honeycomb devices.

Download

Grab the latest dependency through Gradle:

dependencies {
    implementation 'com.pixplicity.easyprefs:EasyPrefs:1.10.0'
}

License

Copyright 2014 Pixplicity (http://pixplicity.com)

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.

More Repositories

1

MultiViewPager

The MultiViewPager is an extension of the support-v4 library's ViewPager that allows the pages to be wider or narrower than the ViewPager itself. It takes care of aligning the pages next to each other, and always keeping the selected page centered.
Java
906
star
2

HtmlCompat

Compatibility library for Android's Html class. [DEPRECATED]
Java
558
star
3

letterpress

Custom fonts without writing code.
Java
135
star
4

humpty-dumpty-android

Simple file dump utility for Android
Shell
104
star
5

gene-rate

Generate a better rating for your Android app
Java
67
star
6

android-custom-components

Custom Components for Android
Java
21
star
7

Proguard-rules

Proguard rules for commonly used libraries
19
star
8

sync-demo

Sync Demo
Java
16
star
9

android-bluetooth-demo

This project demonstrates communicating with a bluetooth device in an Android app.
Java
16
star
10

PoorIntentions

Illustrates invalid back stacks on Android and offers a resolution
Kotlin
11
star
11

adb-control

ADB Control for Root app
Java
6
star
12

HaldCLUT

C/C++ generator for Hald Color Lookup Tables
C
4
star
13

castdemo

Google Cast Demo
Java
4
star
14

ChoiceGridView

Variant of Android's GridView that has improved handling for multiselection.
Java
3
star
15

StandardSmashdown

The official Standard Rules for Smashdown Ping-Pong
3
star
16

gimbal-proximity

Fork and improvement of Gimbal Proximity SDK sample app
Java
3
star
17

flutter_animation_motion

Companion app for my "Animation and motion in Flutter" presentation, showcasing what the framework has to offer to developers for creating simple and yet beautiful animations.
Dart
3
star
18

multi-window-demo

Demo of Android N's Multi-Window mode
Java
2
star
19

weardemo

Java
2
star
20

overlay-android-bug

Demo of a bug in Android 7.1.2 (Nougat) on a Nexus 6P
Java
2
star
21

animation-demo

Java
1
star
22

nougat-photo

A very basic no-frills implementation of using the default camera to return a photo, compatible with Android Nougat.
Java
1
star
23

PriorityAlert

Java
1
star
24

HeadphoneData

Java
1
star
25

resource-converter

Java
1
star