Save Checkbox Values in Android Studio

Asked

Viewed 1,323 times

2

I’m in a project that has many checkboxes, and they have to be stored permanently, IE, when the user closes the app and reopens it has to be marked. Would you like to know how to save checkbox modes in the app created in Android Studio? or turn the set Checkboxes into text, as they will not be unchecked ever again.

I’ll wait for your help.

2 answers

2

You can save in the database, or use SharedPreferences, that I believe is the simplest way:

To make it easier, I recommend creating a class that contains the values I wanted to save:

class Valores
{
    boolean check1;
    boolean check2;
    boolean check3;
    boolean check4;
}

Method to save this class:

   /**
     * seta os valores no SharedPreferences
     * @param valores
     */
    public void setValores(Valores valores)
    {
        // passamos o nome do objeto que vamos criar e o modo de armazenamento
        final SharedPreferences prefs = getApplicationContext().getSharedPreferences("VALORES_TELA_UM", Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = prefs.edit();
        // setamos o nome da propriedade e o valor
        editor.putBoolean("CHECK1", valores.check1);
        editor.putBoolean("CHECK2", valores.check2);
        editor.putBoolean("CHECK3", valores.check3);
        editor.putBoolean("CHECK4", valores.check4);
        // commitando as alteracoes
        editor.commit();

    }

Method to redeem values:

  public Valores getValores()
    {
        Valores valores = new Valores();

        final SharedPreferences prefs = getApplicationContext().getSharedPreferences("VALORES_TELA_UM", Context.MODE_PRIVATE);

        valores.check1 = prefs.getBoolean("CHECK1", false);
        valores.check2 = prefs.getBoolean("CHECK2", false);
        valores.check3 = prefs.getBoolean("CHECK3", false);
        valores.check4 = prefs.getBoolean("CHECK4", false);

        return valores;
    }

1

The Android Api has ways for you to save your app preferences as key/value(key/value) sets. Check out the documentation Saving Key-Value Sets.

To create the preferences file (in which case checkbox checked):

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

Where R.string.preference_file_key is a string that you declare with the file name that will contain the key/value sets.

To save a value key set:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(getString(R.string.show_score), true);
editor.commit();

Where R.string.show_score is a string with your key name, which in your case could be chkRodaEmModoNavio.

To read your preferences:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
boolean defaultValue = getResources().getBoolean(R.string.show_score_default);
boolean showHighScore = sharedPref.getBoolean(getString(R.string.show_score), defaultValue);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.