Pages

About

Pages - Menu

Ads 468x60px

Social Icons

Main Menu

Featured Posts

Friday 27 June 2014

Android SharedPreferences Example

And paste the following code :

01<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02    xmlns:tools="http://schemas.android.com/tools"
03    android:layout_width="match_parent"
04    android:layout_height="match_parent"
05    android:orientation="vertical"
06    tools:context=".MainActivity" >
07 
08    <CheckBox
09        android:id="@+id/checkBox1"
10        android:layout_width="wrap_content"
11        android:layout_height="wrap_content"
12        android:text="Check Box" />
13 
14    <EditText
15        android:id="@+id/editText1"
16        android:layout_width="match_parent"
17        android:layout_height="wrap_content"
18        android:text="Enter Your Name...">
19 
20        <requestFocus />
21    </EditText>
22 
23    <Button
24        android:id="@+id/button1"
25        android:layout_width="wrap_content"
26        android:layout_height="wrap_content"
27        android:text="Save" />
28 
29</LinearLayout>

 

 

The code of this tutorial is very simple. Basically there are two main operations one can perform when using SharedPreferences  in Android. That is store data and load data. You should:
  • Create a new SharedPreferences object.
  • Then get an Editor instance from that object. The Editor object will help you manipulate the data that you’ve stored.
  • Use putString, putBoolean methods to store a pair key/value
  • Use getBoolean, getString to get the values you want.

01package com.javacodegeeks.android.androidsharedpreferences;
02 
03import android.app.Activity;
04import android.content.SharedPreferences;
05import android.content.SharedPreferences.Editor;
06import android.os.Bundle;
07import android.preference.PreferenceManager;
08import android.view.View;
09import android.view.View.OnClickListener;
10import android.widget.Button;
11import android.widget.CheckBox;
12import android.widget.EditText;
13 
14public class MainActivity extends Activity implements OnClickListener {
15 
16    CheckBox checkBox;
17    EditText editText;
18    Button button;
19 
20    @Override
21    protected void onCreate(Bundle savedInstanceState) {
22        // TODO Auto-generated method stub
23        super.onCreate(savedInstanceState);
24        setContentView(R.layout.main);
25 
26        checkBox = (CheckBox) findViewById(R.id.checkBox1);
27        editText = (EditText) findViewById(R.id.editText1);
28        button = (Button) findViewById(R.id.button1);
29        button.setOnClickListener(this);
30        loadSavedPreferences();
31    }
32 
33    private void loadSavedPreferences() {
34        SharedPreferences sharedPreferences = PreferenceManager
35                .getDefaultSharedPreferences(this);
36        boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);
37        String name = sharedPreferences.getString("storedName", "YourName");
38        if (checkBoxValue) {
39            checkBox.setChecked(true);
40        } else {
41            checkBox.setChecked(false);
42        }
43 
44        editText.setText(name);
45    }
46 
47    private void savePreferences(String key, boolean value) {
48        SharedPreferences sharedPreferences = PreferenceManager
49                .getDefaultSharedPreferences(this);
50        Editor editor = sharedPreferences.edit();
51        editor.putBoolean(key, value);
52        editor.commit();
53    }
54 
55    private void savePreferences(String key, String value) {
56        SharedPreferences sharedPreferences = PreferenceManager
57                .getDefaultSharedPreferences(this);
58        Editor editor = sharedPreferences.edit();
59        editor.putString(key, value);
60        editor.commit();
61    }
62 
63    @Override
64    public void onClick(View v) {
65        // TODO Auto-generated method stub
66        savePreferences("CheckBox_Value", checkBox.isChecked());
67        if (checkBox.isChecked())
68            savePreferences("storedName", editText.getText().toString());
69 
70        finish();
71    }
72 
73}

 

1 comments:

  1. Very informative post.I am really impressed by this incredible precious site.

    ReplyDelete

Followers