Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn Toast on / off from shared preferences?

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?

like image 653
jasonflaherty Avatar asked Mar 03 '13 16:03

jasonflaherty


People also ask

What does Shared preferences mean?

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.

What is Shared preferences in Android development?

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.

How data is stored in a Shared preference?

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.

Why use SharedPreferences in Android?

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.


1 Answers

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();
    }
}
like image 109
appsroxcom Avatar answered Nov 11 '22 22:11

appsroxcom