alberzyzz
alberzyzz

Reputation: 277

Remember me button in Android

I am trying to do a remember me button, but my app is not remembering the username, either the password. There are no crashes. I am not very used to Android, so I got the code from another post and tried to adapt it. Here it is:

public class MainUserActivity extends AppCompatActivity implements OnClickListener {

    Button uButton;
    EditText uEdit;
    EditText uEdit2;
    String textValue;
    String textValue2;
    private CheckBox saveLoginCheckBox;
    private String user,pass;
    private SharedPreferences loginPreferences;
    private SharedPreferences.Editor loginPrefsEditor;
    private Boolean saveLogin;
    private Button ok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_user);

        TextView t2 = (TextView) findViewById(R.id.textViewRegister2);
        t2.setMovementMethod(LinkMovementMethod.getInstance());

        ok = (Button)findViewById(R.id.buttonUser);
        ok.setOnClickListener(this);
        uEdit   = (EditText)findViewById(R.id.user);
        uEdit2   = (EditText)findViewById(R.id.pass);

        saveLoginCheckBox = (CheckBox)findViewById(R.id.saveLoginCheckBox);
        loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
        loginPrefsEditor = loginPreferences.edit();

        saveLogin = loginPreferences.getBoolean("saveLogin", false);
        if (saveLogin == true) {
            uEdit.setText(loginPreferences.getString("user", ""));
            uEdit2.setText(loginPreferences.getString("pass", ""));
            saveLoginCheckBox.setChecked(true);
        }

        ok.setOnClickListener(
                new View.OnClickListener()
                {
                    public void onClick(View view)
                    {
                        user = uEdit.getText().toString();
                        pass = uEdit2.getText().toString();
                        changeActivity(view);
                    }
                });
    }

    @Override
    public void onBackPressed()
    {
     .
     .
     .
    }

    public void changeActivity(View view){
     .
     .
     .
    }

    public void onClick(View view) {
        if (view == ok) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(uEdit.getWindowToken(), 0);

            user = uEdit.getText().toString();
            pass = uEdit2.getText().toString();

            if (saveLoginCheckBox.isChecked()) {
                loginPrefsEditor.putBoolean("saveLogin", true);
                loginPrefsEditor.putString("user", user);
                loginPrefsEditor.putString("pass", pass);
                loginPrefsEditor.commit();
            } else {
                loginPrefsEditor.clear();
                loginPrefsEditor.commit();
            }
        }
    }
}

I believe it must be a stupid thing, as I have read the code several times and I understand how it works.

Upvotes: 0

Views: 97

Answers (2)

Vikas Tiwari
Vikas Tiwari

Reputation: 499

Remove ok.setOnClickListener(this);

and make below changes:

ok.setOnClickListener(
                new View.OnClickListener()
                {
                    public void onClick(View view)
                    {
                        user = uEdit.getText().toString();
                        pass = uEdit2.getText().toString();

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(uEdit.getWindowToken(), 0);



                if (saveLoginCheckBox.isChecked()) {
                    loginPrefsEditor.putBoolean("saveLogin", true);
                    loginPrefsEditor.putString("user", user);
                    loginPrefsEditor.putString("pass", pass);
                    loginPrefsEditor.commit();
                } else {
                    loginPrefsEditor.clear();
                    loginPrefsEditor.commit();
                }

                        changeActivity(view);
                    }
                });

Upvotes: 1

Adinia
Adinia

Reputation: 3731

As @ashishdhiman2007 pointed in the comments, you should set the OnClickListener only once, and combine the actions of the two actual ones (saving to Preferences and going to the next activity).

For example, leave only ok.setOnClickListener(this); and then add changeActivity(view); to the actual onClick function:

 public void onClick(View view) {
    if (view == ok) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(uEdit.getWindowToken(), 0);

        user = uEdit.getText().toString();
        pass = uEdit2.getText().toString();

        if (saveLoginCheckBox.isChecked()) {
            loginPrefsEditor.putBoolean("saveLogin", true);
            loginPrefsEditor.putString("user", user);
            loginPrefsEditor.putString("pass", pass);
            loginPrefsEditor.commit();
        } else {
            loginPrefsEditor.clear();
            loginPrefsEditor.commit();
        }
        changeActivity(view);
    }
}

Upvotes: 0

Related Questions