Shared preferences is used to save
primitive data like booleans, floats, ints, longs, and strings.These
data will persist even when the application is killed.
Here we create an android project where
the user is asked to enter username and password in an activity and
we provide an option as 'save login'.The login credentials are stored
in shared preferences and the data is used again when the user
launched the application next time where he does not need to enter
the credentials again.If the 'save login' check box is cleared the
shared preferences data is removed.
1. Create a new project by going to
File -> New Android Project. Fill all the details and name your
activity as Login.
2.Once the project is created in
Login.java type following code
Login.java
public class Login extends Activity implements OnClickListener { private String username, password; private Button ok; private EditText editTextUsername, editTextPassword; private CheckBox saveLoginCheckBox; private SharedPreferences loginPreferences; private SharedPreferences.Editor loginPrefsEditor; private Boolean saveLogin; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); ok = (Button) findViewById(R.id.buttonLogin); ok.setOnClickListener(this); editTextUsername = (EditText) findViewById(R.id.editTextUsername); editTextPassword = (EditText) findViewById(R.id.editTextPassword); saveLoginCheckBox = (CheckBox) findViewById(R.id.saveLoginCheckBox); loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); loginPrefsEditor = loginPreferences.edit(); saveLogin = loginPreferences.getBoolean("saveLogin", false); if (saveLogin == true) { editTextUsername .setText(loginPreferences.getString("username", "")); editTextPassword .setText(loginPreferences.getString("password", "")); saveLoginCheckBox.setChecked(true); } } public void onClick(View view) { if (view == ok) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editTextUsername.getWindowToken(), 0); username = editTextUsername.getText().toString(); password = editTextPassword.getText().toString(); if (saveLoginCheckBox.isChecked()) { loginPrefsEditor.putBoolean("saveLogin", true); loginPrefsEditor.putString("username", username); loginPrefsEditor.putString("password", password); loginPrefsEditor.commit(); } else { loginPrefsEditor.clear(); loginPrefsEditor.commit(); } doSomethingElse(); } } public void doSomethingElse() { startActivity(new Intent(Login.this, Welcome.class)); Login.this.finish(); } }
3. Now we need to create a layout for
login screen. Under res/layouts create a new xml file and name it as
login.xml( Right Click on res/layout -> New -> Android XML
File)
4. In login.xml type following code
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000" android:padding="10dp" > <EditText android:id="@+id/editTextUsername" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/imageView1" android:hint="Username" android:imeOptions="actionNext" android:inputType="textNoSuggestions" /> <EditText android:id="@+id/editTextPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editTextUsername" android:hint="Password" android:imeOptions="actionDone" android:inputType="textPassword" /> <CheckBox android:id="@+id/saveLoginCheckBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editTextPassword" android:text="Save Login?" android:textColor="#FFF" /> <Button android:id="@+id/buttonLogin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/saveLoginCheckBox" android:layout_marginTop="40dp" android:text="Login" /> </RelativeLayout>
5.Once the login button is clicked the
new activity 'Welcome' is called.The corresponding java and xml files
are as follows.
Welcome.java
public class Welcome extends Activity { private SharedPreferences loginPreferences; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.welcome); loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); String un = loginPreferences.getString("username", ""); TextView tv = (TextView) findViewById(R.id.textView1); tv.setText("Welcome:" + un); } }
Welcome.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
If you are testing this project in an
emulator you can find the preference file in data/data/<your
package>/shared_prefs/loginPrefs.xml.Pull the file and you can see
the data in shared preference.