How to Store toogle button status in shared preference and load the status later in android?

408 views Asked by At

The thing is I want to store my toogle button status in shared preference, so that I when I return to the application my toogle button wil stay in previous state. This is kind of something Do you want to use this. If user make the button enable that means when he return it will show the button will enable. So far I did is

tb_vibrate = (ToggleButton)this.findViewById(R.id.tb_vibrate);
tb_vibrate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(tb_vibrate.isChecked())
            {
                Toast.makeText(ProfileActivity.this, "Toggle button is on", Toast.LENGTH_LONG).show();
                tb_vibrate.setChecked(true);
            }
            else {
                Toast.makeText(ProfileActivity.this, "Toggle button is Off", Toast.LENGTH_LONG).show();
                tb_vibrate.setChecked(false);
            }
        }
    });

And this is my SharePreference class

public class NotificationManager {

    private static final String PREFS_FILE_NAME = "AppNotificationManager";
    private static final String VIBRATE = "vibrate";
    private static final String NOTIFICATION_ALERT = "notification_alert";
    private static final String NOTIFICATION_SOUND = "notification_sound";
    private static final String CALL_RINGTONE = "call_ringtone";
    private static final String RINGTONE_VIBRATION = "ringtone_vibrate";

    public static void setVibrate(final Context ctx, final String vibrate) {
        final SharedPreferences prefs = ctx.getSharedPreferences(NotificationManager.PREFS_FILE_NAME, Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = prefs.edit();
        editor.putString(NotificationManager.VIBRATE, vibrate);
        editor.commit();
    }

    public static String getVibrate(final Context ctx) {
        return ctx.getSharedPreferences(NotificationManager.PREFS_FILE_NAME,
                Context.MODE_PRIVATE).getString(NotificationManager.VIBRATE, "");
    }


    }

So how to create my shared preference class for storing the data and use it later?

1

There are 1 answers

6
AskNilesh On BEST ANSWER

you can use SharedPreferences to store state of your ToggleButton

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

Sample code

SharedPreferences sp=getSharedPreferences("Login", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed=sp.edit()

tb_vibrate = (ToggleButton)this.findViewById(R.id.tb_vibrate);
tb_vibrate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(tb_vibrate.isChecked())
            {
                Toast.makeText(ProfileActivity.this, "Toggle button is on", Toast.LENGTH_LONG).show();
                tb_vibrate.setChecked(true);

                Ed.putBoolean("ISCHECKED",true);
                Ed.commit();

            }
            else {
                Toast.makeText(ProfileActivity.this, "Toggle button is Off", Toast.LENGTH_LONG).show();
                tb_vibrate.setChecked(false);
                Ed.putBoolean("ISCHECKED",false);
                Ed.commit();
            }
        }
    });

getting values use the below code

SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
 boolean flag = preferences.getBoolean("ISCHECKED", false);
 if(flag){
        tb_vibrate.setChecked(true);
    }else {
        tb_vibrate.setChecked(false);
    }