And paste the following 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" /> |
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.
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()); |
Very informative post.I am really impressed by this incredible precious site.
ReplyDelete