How to store username password in device memory. Even after the user closes the application and returns back , he should be able to authenticate his username and password. Right now I am testing in Eclipse... so please help me with some pointers/links which will allow me to test in Eclipse and eventually run on Mobile.
Asked
Active
Viewed 6,689 times
2 Answers
1
I've used SharedPreferences to store username/password, it is a much lighter weight storage solution and secure to your app.

mmaitlen
- 824
- 9
- 17
-
1SharedPreferences is not secure! Check this out: https://stackoverflow.com/questions/9244318/android-sharedpreference-security – Paul Spiesberger Apr 15 '16 at 21:18
-1
hey.. i made d simple demo for u to save username nd password..!!! it stored into device's internal memory, it creates a file and save/fetch the data by/from that file.. code is following..
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Login extends Activity {
LinearLayout lymain;
EditText user_edit;
EditText pass_edit;
TextView user_txt;
TextView pass_txt;
CheckBox savepass;
Button save;
public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lymain = new LinearLayout(getApplicationContext());
lymain.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
lymain.setPadding(15,15,15,15);
lymain.setOrientation(1);
lymain.setGravity(Gravity.CENTER);
user_edit = new EditText(getApplicationContext());
user_edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
pass_edit = new EditText(getApplicationContext());
pass_edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
user_txt = new TextView(getApplicationContext());
user_txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
user_txt.setText("Enter Username");
pass_txt = new TextView(getApplicationContext());
pass_txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
pass_txt.setText("Enter Password");
savepass = new CheckBox(getApplicationContext());
savepass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,40));
savepass.setText("Save Username/Password?");
save = new Button(getApplicationContext());
save.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
save.setText(" SAVE ");
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String loginName = user_edit.getText().toString();
String password = pass_edit.getText().toString();
if(savepass.isChecked()){
getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_USERNAME, loginName)
.putString(PREF_PASSWORD, password)
.commit();
Toast.makeText(getApplicationContext(),"Saved Successfully",Toast.LENGTH_LONG).show();
}
}
});
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
user_edit.setText(pref.getString(PREF_USERNAME, null));
pass_edit.setText(pref.getString(PREF_PASSWORD, null));
lymain.addView(user_txt);
lymain.addView(user_edit);
lymain.addView(pass_txt);
lymain.addView(pass_edit);
lymain.addView(savepass);
lymain.addView(save);
setContentView(lymain);
}
}

Nirav Dangi
- 3,607
- 4
- 49
- 60
-
1I don't recommend to store the password into the SharedPreferences because of security reasons, maybe rooted mobiles can read those options, It could be useful to use AccountManager to save password or something like that. – ziniestro Dec 16 '14 at 01:06