A simple and easy to use database library to save user data. It is very lighweight and uses on 8kb or less on the app! Also it needs minimum api level of 25 only. According to Android Studio, it will work 0n 92% devices. Isn't it amazing?
You can now create multiple databases. To use the default Datatabse, just call this:
public class MainActivity extends AppCompatActivity implements ValueChangeListener{
TextView textView;
TextView textView2;
TinyDefaultDB defaultDB;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.sampleTV);
textView2 = findViewById(R.id.sampleTV2);
defaultDB = TinyDB.getInstance().getDefaultDatabase(this);
defaultDB.setValueChangeListener(this); // this will trigger when a value will be modified ot deleted
defaultDB.putInt("abc",new Random().nextInt(10000));
textView.setText(defaultDB.getInt("abc",1)+ "");
}
@Override
public <E> void onValueAdded(String key, E value, String dbName) {
}
@Override
public void onKeyRemoved(String key, String dbName) {
}
@Override
public void onAllKeysRemoved(String dbName) {
}
}
This will store the values in the default database, but, you want to store the different values in the same key right, so, for that, create another custom database like this:
public class MainActivity extends AppCompatActivity implements ValueChangeListener{
TextView textView;
TextView textView2;
TinyCustomDB customDB;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.sampleTV);
textView2 = findViewById(R.id.sampleTV2);
customDB = TinyDB.getInstance().getCustomDatabase(this, "MyCustomDBName");
customDB.setValueChangeListener(this); // this will trigger when a value will be modified ot deleted
customDB.putInt("abc",new Random().nextInt(67));
textView2.setText(customDB.getInt("abc",1)+ "");
}
@Override
public <E> void onValueAdded(String key, E value, String dbName) {
}
@Override
public void onKeyRemoved(String key, String dbName) {
}
@Override
public void onAllKeysRemoved(String dbName) {
}
}
You can also view the sample app on how it works
It is a very simple library and easy to use too. But, not only that, it is also very easy to implement.
- Add this line to your
settings.gradle
file:
maven { url 'https://jitpack.io'}
You can also view the sample settings.gradle
file from here.
- Add this line to your app level
build.gradle
add this line:
implementation 'com.github.sambhav2358:TinyDB:2.0.1'
I guess I need not answer it. But I will. Just compare the lines of code for you to write. But any which way, the llibraray does the very same thing in the background
//With the library
TinyDefaultDB db = TinyDB.getInstance().getDefaultDatabase(this);
db.putInt("abc",new Random().nextInt(10000));
//With Shred Preferences
SharedPreferences prefs = context.getSharedPreferences("MyAppData",Context.MODE_PRIVATE );
SharedPreferences.Editor editor = prefs.edit();
editor.putString( key, value );
editor.apply();
As mentioned above, it is very easy to use.
In your class you can create the object like this
TinyDefaultDB tinyDB;
Then you can initialize it anywhere and then use it.
Context context = this;// provide the context here.
tinyDB = TinyDB.getInstance().getDefaultDatabase(this);
You can just call one method provide the key and then the value. The rest happens by itself in the background.
This could be an example usage of saving data:
tinyDB = TinyDB.getInstance().getDefaultDatabase(this);
tinyDB.putInt("abc",new Random().nextInt(10000));
You can also view the activity file here.
It is very easy too! Just do this:
textView = findViewById(R.id.sampleTV);
tinyDB = TinyDB.getInstance().getDefaultDatabase(this);
textView.setText(tinyDB.getInt("abc",1) + "");
Easy peasy just call this:
tinyDB.clearKey(String yourKey);
Just do this
tinyDB.clearAll();
You have listener for data change, removal and etc...
See this:
public class MainActivity extends AppCompatActivity implements ValueChangeListener{
TextView textView;
TinyDefaultDB defaultDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
defaultDB = TinyDB.getInstance().getDefaultDatabase(this);
defaultDB.setValueChangeListener(this); // this will trigger when a value will be modified ot deleted
}
@Override
public <E> void onValueAdded(String key, E value, String dbName) {
}
@Override
public void onKeyRemoved(String key, String dbName) {
}
@Override
public void onAllKeysRemoved(String dbName) {
}
}
You can save these kind of formats to save
- Int
- Boolean
- String
- Float
- List
- And is none of them is what you want, just call
put
and save whatever you want andget
to get a custom value.
For everyone who wants me to add a feature, just create an issue with the feature you want.
Free to use and modify the code.
Build with ❤️ by Sambhav Khandelwal