SharedPreferences in Android is used to store private primitive application specific data in the form of key-value pair.
SharedPreferences is application specific it means data will be lost when user either uninstall application or clear application data.
We can store Java object to SharedPreferences after convert it to JSON format using Gson library, since SharedPreferences allows you to store only primitive values or set of strings.
We can store JSON object to SharedPreferences after convert it to String using toString() method.
Here we will understand how to use SharedPreferences in Android using a sample application.
MainActivity.java
SharedPreferences is application specific it means data will be lost when user either uninstall application or clear application data.
We can store Java object to SharedPreferences after convert it to JSON format using Gson library, since SharedPreferences allows you to store only primitive values or set of strings.
We can store JSON object to SharedPreferences after convert it to String using toString() method.
Here we will understand how to use SharedPreferences in Android using a sample application.
Source Code:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/activity_horizontal_margin" tools:context=".MainActivity"> <EditText android:id="@+id/edtUserName" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="@string/user_name" android:inputType="text" android:layout_centerHorizontal="true" android:layout_margin="12dp"/> <EditText android:id="@+id/edtPassword" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="@string/password" android:inputType="textPassword" android:layout_centerHorizontal="true" android:layout_below="@+id/edtUserName" android:layout_margin="12dp"/> <Button android:id="@+id/btnSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/save" android:layout_below="@+id/edtPassword" android:layout_centerHorizontal="true" android:layout_margin="12dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"> <Button android:id="@+id/btnShowRecord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_record" /> <Button android:id="@+id/btnClear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clear_record" /> </LinearLayout> </RelativeLayout>
MainActivity.java
package com.sonevalley.sushil.sharedpreferencesexample; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText edtUserName; EditText edtPassword; Button btnSave; Button btnShowRecord; Button btnClearRecord; public static final String MyPREFERENCES = "UserCredentials"; SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); edtUserName = (EditText) findViewById(R.id.edtUserName); edtPassword = (EditText) findViewById(R.id.edtPassword); btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("userName", edtUserName.getText().toString()); editor.putString("password", edtPassword.getText().toString()); // To store boolean //editor.putBoolean("oldUser", true); // To store float //editor.putFloat("floatNum", 2.86543f); // To store int //editor.putInt("age", 25); // To store long //editor.putLong("longNum", 999999999999l); editor.commit(); Toast.makeText(MainActivity.this, "Record Saved to SharedPreferences Successfully", Toast.LENGTH_SHORT).show(); } }); btnShowRecord = (Button) findViewById(R.id.btnShowRecord); btnShowRecord.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userName = sharedPreferences.getString("userName", "Not Available"); String password = sharedPreferences.getString("password", "Not Available"); Toast.makeText(MainActivity.this, "User Name: " + userName + " Password: " + password, Toast.LENGTH_LONG).show(); // To get boolean // boolean oldUser = sharedPreferences.getBoolean("oldUser", false); // To get flat // float floatVal = sharedPreferences.getFloat("floatVal", 0.0f); // To get int // int intVal = sharedPreferences.getInt("intVal", 0); // To get long // long longVal = sharedPreferences.getLong("longVal", 0l); } }); btnClearRecord = (Button) findViewById(R.id.btnClear); btnClearRecord.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sharedPreferences.edit(); // It will remove all the value from SharedPreferences editor.clear(); // If you want to remove only specific key-value, userName in our case // editor.remove("userName"); editor.commit(); Toast.makeText(MainActivity.this, "SharedPreferences Record Cleared", Toast.LENGTH_SHORT).show(); } }); } }
Screen Preview
Saving Record to SharedPreferencs |
Displaying Record from SharedPreferences |
0 comments:
Post a Comment