Is there a way to globally turn on and off toast notifications with a checkbox in shared preferences?
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
boolean showToast = myPrefs.getBoolean("showToast",
true);
I was thinking maybe making a class:
boolean showToast(){
//code
}
BUT thought, SO might have a global solution?
Should I use a different type of notification system?
Any thoughts?
Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.
A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.
Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.
Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.
You may consider extending Toast to create your custom class which is smart enough to read the user preferences before showing the toast.
Then refactor your code to replace Toast with SmartToast.
SmartToast.makeText(this, "message", Toast.LENGTH_SHORT).show();
So implement SmartToast.makeText() to return an instance of SmartToast and override show() as follows:
@Override
public void show() {
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("showToast", true)) {
super.show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With