Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Thursday, 13 October 2016

Shared Preference Example in Android

In Android, There are many way to store data of an application. But if you want to store some small value then shared preferences is best way to store data of an application. It is allow to save and retrieve data from key and value.

Shared Preference is worked Key and Value. We can store string,integer, long, number etc. value

Let's see the example of Shared Preference :

File Name : MainActivity.java



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.sharedpreference;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

 EditText e1,e2;
 Button b1;
 SharedPreferences shared;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  e1 = (EditText)findViewById(R.id.editText1);
  e2 = (EditText)findViewById(R.id.editText2);
  b1 = (Button)findViewById(R.id.button);
  shared = getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
  
  String user = shared.getString("UserKey", "");//In first arguement set key which you store in variable and second argue is default value
  String pwd  = shared.getString("PassKey", "");
  
  e1.setText(user);//Here, if value is available in shared preference then value is display in edit text.
  e2.setText(pwd);
  
  b1.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
   
    String uname = e1.getText().toString();
    String pass = e2.getText().toString();
    
    //it is used to data by shared preference, SharedPreference.Editor class is used 
    SharedPreferences.Editor editor = shared.edit();
    editor.putString("UserKey", uname); // it is store key and value... UserKey is key and uname is value
    editor.putString("PassKey", pass);
    editor.commit();
    Toast.makeText(getApplicationContext(), "Record Saved in SharedPreference", Toast.LENGTH_SHORT).show();
   }
  });
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

File Name : activity_main.xml



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<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"
    tools:context=".MainActivity" >

    <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText1"
      android:layout_marginTop="67dp"
      android:hint="Enter User Name"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
    
    
    <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_below="@+id/editText1"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Enter Password" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Save" />
    
</RelativeLayout>

  • After click on save button data saved in shared preference. To see the proper output, first close this application after save data in shared preference, then open application and you will see all the text you have written back in your application.
Output :

  • Before Data Save


  • After Data Save




0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.