Android Sharedpreferences with Radiobutton

Asked

Viewed 84 times

3

I have two Radiobutton, I want to select one of them, close the app and when open load the last selection.

Can you help me? I can’t find a clear tutorial explained.

Thank you.

  • 4

    You know how to use the Sharedpreferences?

1 answer

0

The example I’m going to set is with checkbox, but the method is no different, just change to your radiobutton.

Create a preference class. There you can store anything you want.
Follow my example:

public class Preferencia {
    public static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences("energy", Context.MODE_PRIVATE);
    }

  //// preferencia de checkbox
   public static boolean setcbremember(Context context, boolean a) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("Cbremember", a);
        editor.commit();
        return a;

    }
    public static boolean getcbremember(Context context) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        return sharedPref.getBoolean("Cbremember", true);
    }
 //// Deletar preferencia de login e senha
        public static void deletar(Context context, String login) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.clear();
        editor.commit();

    }

}

Where I save the checkbox, changes to the radiobutton and with an if and Else, assign to the shared what the user chooses.

Follow the spot where I get the checkbox to assign the preference:

public void btnentrarclick(View currentButton) {

        if(cbremember.isChecked()) {
            AcessoLogin ac = new AcessoLogin(Tela_de_Login.this,
                    edtlogin.getText().toString(), edtsenha.getText().toString());
            Preferencia.setlogin(Tela_de_Login.this, edtlogin.getText().toString());
            Preferencia.setsenha(Tela_de_Login.this, edtsenha.getText().toString());
            Preferencia.setcbremember(Tela_de_Login.this, cbremember.isChecked());

            ac.execute("");
        }
        else{

            AcessoLogin ac = new AcessoLogin(Tela_de_Login.this,
                    edtlogin.getText().toString(), edtsenha.getText().toString());
            Preferencia.deletar(Tela_de_Login.this, edtlogin.getText().toString());
            Preferencia.deletar(Tela_de_Login.this, edtsenha.getText().toString());

            ac.execute("");
        }

    }

In my case, as you can see, if the user chooses to checkbox triggered, it will save the user information, if it deselects the checkbox, it will not save and delete what is saved.
I hope it helped

  • the answer was good, but try to accentuate correctly the words and phrases, the answer was very confusing without proper accents. This way, it is easier to understand correctly. Do not misunderstand me, it is just a tip for your answers to be even better :)

  • thank you very much.. I will check yes ...

Browser other questions tagged

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