Pages

About

Pages - Menu

Ads 468x60px

Social Icons

Main Menu

Featured Posts

Monday 30 June 2014

Android SharedPreferences Example

  • Shared Preferences: Prrimitive data storage (boolean Strings, ints etc..).
  • Internal Storage: Device memory storage.
  • External Storage: Store public data on storage media, like SD cards.
  • SQLite Database.
With SharedPreferences we talk about permanent storage about the application. That means that if you uninstall the application, all your data will be lost, so this is application specific. In contrast , SQLite stores structured data in a database, so even when you delete the application the data won’t be lost.
For this tutorial, we will use the following tools in a Windows 64-bit platform:




And paste the following code :

xml_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>


main activity


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}




Download Eclipse Project

This was an Android SavedPreferences Example. Download the Eclipse Project of this tutorial: AndroidSharedPreferences.zip

3 comments:

  1. nice blog very use friend

    ReplyDelete
  2. very nice blog ma very use full

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

    ReplyDelete

Followers