A very common need for an android developer is the need to create a place for storing app information. For example you may need to store some settings about the user of the app or more data that user want to keep.
You can create an SQL database, but this can be very costly. You can also use a tool that Android provides called SharedPreferences. This is the basic way to save a small amount of information on user’s phone, but it still not so clear as is could be.
So, a guy called kcochibili created a library called TinyDB that uses the SharedPreferences to create a super simple to way for programmers to create local databases.
This class simplifies calls to SharedPreferences in a line of code. It can also do more like: saving a list of strings, integers and saving images. All in 1 line of code!
Here is a simple example:
We suppose that you want to store a String called “username” You want to store the String in phone’s hard drive of course to be able to use it whenever you want.
All you have to do is:
TinyDB tinydb = new TinyDB(context); tinydb.putString("userName", "john");
to save the value.
When you want to retrieve it is:
TinyDB tinydb = new TinyDB(context); String datastring = tinyDB.getString("userName");
That’s extremely simple and beautiful.. You can also store a whole ArrayList with putListString, putListInt etc methods.
Finally you don’t have to install it at all. All you have to do it to add the TinyDB.java file to your project as class, uncomment the commented methods and install Gson. You can download it the .jar from here, then put it in your “libs” folder (create one if there isn’t). Then find it on your android studio project structure, right click and the “Add as library”.
Thanks me later!