Ruben Meiring
Ruben Meiring

Reputation: 343

How can I save the Values in edittext boxes

I have an app where you upload images to a server. My problem is the user enters their email password and client ID in edittext boxes these are then used for URL building. I want to save the information entered into the edittext boxes so the user doesn't have to retype it every time.
Oly answered that I can find on SO to save the values to a textView ETC. I want the values to be saved in the edittext boxes

code where the text boxes are called and passed to another activity

public class LoginActivity extends AppCompatActivity {

    private EditText email, password, id;

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

        email=findViewById(R.id.emailtext);
        password=findViewById(R.id.pwdtext);
        id=findViewById(R.id.clientid);
        Button loginBtn=findViewById(R.id.button);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String emailAddress=email.getText().toString().trim();
                String userPassword=password.getText().toString().trim();
                String clientId=id.getText().toString().trim();
                Intent intent=new Intent(LoginActivity.this, 
    CameraActivity.class);
                intent.putExtra("clientId", clientId);
                intent.putExtra("email", emailAddress);
                intent.putExtra("password", userPassword);
                startActivity(intent);
            }
        });
    }
}

Upvotes: 1

Views: 82

Answers (2)

Matthew Strukov
Matthew Strukov

Reputation: 351

You can use SharedPreferences:

public class LoginActivity extends AppCompatActivity {
    private static final String ID = "id";
    private static final String EMAIL = "email";
    private static final String PASS = "pass";
    private SharedPreferences mPreference;
    private EditText email, password, id;

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

        email=findViewById(R.id.emailtext);
        password=findViewById(R.id.pwdtext);
        id=findViewById(R.id.clientid);
        Button loginBtn=findViewById(R.id.button);

        mPreference = PreferenceManager.getDefaultSharedPreferences(this);

        id.setText(mPreference.getString(ID, ""));
        email.setText(mPreference.getString(EMAIL, ""));
        password.setText(mPreference.getString(PASS, ""));

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String emailAddress=email.getText().toString().trim();
                String userPassword=password.getText().toString().trim();
                String clientId=id.getText().toString().trim();

                mPreference.edit().putString(ID, clientId).apply();
                mPreference.edit().putString(EMAIL, emailAddress).apply();
                mPreference.edit().putString(PASS, userPassword).apply();

                Intent intent=new Intent(LoginActivity.this, 
    CameraActivity.class);
                intent.putExtra("clientId", clientId);
                intent.putExtra("email", emailAddress);
                intent.putExtra("password", userPassword);
                startActivity(intent);
            }
        });
    }
}

Upvotes: 3

Jaypal Sodha
Jaypal Sodha

Reputation: 2688

At what time you want these values to be populated??

When user get back to the same activity or when user kills the activity and reopen your app??

Please tell

Upvotes: 2

Related Questions