- 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
03 | android:layout_width= "match_parent" |
04 | android:layout_height= "match_parent" |
05 | android:orientation= "vertical" |
06 | tools:context= ".MainActivity" > |
09 | android:id= "@+id/checkBox1" |
10 | android:layout_width= "wrap_content" |
11 | android:layout_height= "wrap_content" |
12 | android:text= "Check Box" /> |
15 | android:id= "@+id/editText1" |
16 | android:layout_width= "match_parent" |
17 | android:layout_height= "wrap_content" |
18 | android:text= "Enter Your Name..." > |
24 | android:id= "@+id/button1" |
25 | android:layout_width= "wrap_content" |
26 | android:layout_height= "wrap_content" |
27 | android:text= "Save" /> |
main activity
01 | package com.javacodegeeks.android.androidsharedpreferences; |
03 | import android.app.Activity; |
04 | import android.content.SharedPreferences; |
05 | import android.content.SharedPreferences.Editor; |
06 | import android.os.Bundle; |
07 | import android.preference.PreferenceManager; |
08 | import android.view.View; |
09 | import android.view.View.OnClickListener; |
10 | import android.widget.Button; |
11 | import android.widget.CheckBox; |
12 | import android.widget.EditText; |
14 | public class MainActivity extends Activity implements OnClickListener { |
21 | protected void onCreate(Bundle savedInstanceState) { |
23 | super .onCreate(savedInstanceState); |
24 | setContentView(R.layout.main); |
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(); |
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" ); |
39 | checkBox.setChecked( true ); |
41 | checkBox.setChecked( false ); |
44 | editText.setText(name); |
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); |
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); |
64 | public void onClick(View v) { |
66 | savePreferences( "CheckBox_Value" , checkBox.isChecked()); |
67 | if (checkBox.isChecked()) |
68 | savePreferences( "storedName" , editText.getText().toString()); |
Download Eclipse Project
This was an Android SavedPreferences Example. Download the Eclipse Project of this tutorial:
AndroidSharedPreferences.zip
nice blog very use friend
ReplyDeletevery nice blog ma very use full
ReplyDeleteVery informative post.I am really impressed by this incredible precious site.
ReplyDelete