Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch button with preference activity

Tags:

java

android

xml

I want to use switch button in my preference/settings activity to disable some code in my app when the switch is off. Please can anyone give me a little tutorial using shared preferences that uses switch/toggle button. I have this code but can't figure out where to put my on click listener and how to use it so it disables a certain part of my code when the button is set to off

preference.xml:

 <SwitchPreference
android:key="test"
android:title="Test" />

PreferenceActivity:

public class TestPrefActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.sample);
}}

In main activity

SharedPreferences sharedPrefs = PreferenceManager
        .getDefaultSharedPreferences(MainActivity.this);
sharedPrefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        boolean test = sharedPreferences.getBoolean("test", false);
        Log.e(TAG, "Value:" + test);
    }
});
like image 293
Blacklotis Avatar asked Aug 19 '15 21:08

Blacklotis


2 Answers

Use these methods to save preferences and load preferences:

    //save prefs
public void savePrefs(String key, Boolean value){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

//get prefs
private Boolean loadPrefs(String key, Boolean value){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Boolean data = sharedPreferences.getBoolean(key, value);
    return data;
}

}

When you are saving values with this method you can do so like this:

    boolean toggleButton = true;
    savePrefs("toggle", toggleButton);

When you are retrieving values follow this example:

boolean toggleButton = loadPrefs("toggle", toggleButton);
like image 41
joshgoldeneagle Avatar answered Sep 18 '22 12:09

joshgoldeneagle


Here is a simple example:

<SwitchPreference
    android:key="test"
    android:title="Test"
    android:defaultValue="false" />

In your code:

public class TestPrefActivity extends PreferenceActivity implements onSharedPreferenceChangeListener {

    public SwitchPreference testPref;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.sample);

        testPref = (SwitchPreference) findPreference("test"); //Preference Key
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("test")) {
        boolean test = sharedPreferences.getBoolean("test", false);
        //Do whatever you want here. This is an example.
        if (test) {
            testPref.setSummary("Enabled");
        } else {
            testPref.setSummary("Disabled");
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(TestPrefActivity.this);
        boolean test = preferences.getBoolean("test", false);

        if (test) {
            testPref.setSummary("Enabled");
        } else {
            testPref.setSummary("Disabled");
        }
    }
}

The SwitchPreference will save your key value automatically. You don't have to write code for it. It will be saved as a boolean.

You can then retrieve it from any activity you want like:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean test = sharedPreferences.getBoolean("test", false);
like image 97
Hussein El Feky Avatar answered Sep 18 '22 12:09

Hussein El Feky