In this application, we will learn how to use shared preference in android. Shared preferences is used to save the value in cache where we can retrieve and save any value very fast but the cache memory is very small in mobile devices so we used it mostly to save only user name and password in session management, game level, score in game or any application, etc. Let's start with this application, create new project and drop edit text, button and text view and give id editText1, button1 and textView1 respectively. We will take value from edit text and save it to shared preferences and retrieve saved value and display on text view. The code of android XML file is given below:
Now open Java file and initialize all objects. Give name to your shared Preferences space and open it privately. Use key value to store in shared preferences and we can retrieve the saved value using key value. The code of android Java file is given below with explanation:
Now run you project and test this application. If you have any doubts please comment. Share and and help all developers.
Shared Preferences Android Exmaple |
<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:background="#abc" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="44dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:onClick="action" android:text="Button" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text=" " android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>
Now open Java file and initialize all objects. Give name to your shared Preferences space and open it privately. Use key value to store in shared preferences and we can retrieve the saved value using key value. The code of android Java file is given below with explanation:
package com.smr; //your package name import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { EditText tv1=null; SharedPreferences sp=null; Button bt=null; TextView text=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initialize all view objects tv1=(EditText)findViewById(R.id.editText1); bt=(Button)findViewById(R.id.button1); text=(TextView)findViewById(R.id.textView1); //give share memory name and mode sp=getSharedPreferences("First_share_memory", Activity.MODE_PRIVATE); } //This method will execute when we click on button public void action(View v) { //Get Text from Edit text box and save in a string String str1=tv1.getText().toString(); //save in cache (Shared Preference) sp.edit().putString("share_key", str1).commit(); //retrieve from cache (Shared Preference) String saved_value=sp.getString("share_key",null); //Display Shared Preference value on text view text.setText("Shared Preference value= "+saved_value); } }
Now run you project and test this application. If you have any doubts please comment. Share and and help all developers.
No comments:
Post a Comment