˜´ ŠKçðó¡C¬ù (Shared Preferance)
݇†ó£Œ´ ÜŠO«èêQ™ ðò¡ð´ˆ¶‹
꣡P¬ù 𣘊«ð£‹. å¼ ÜŠO«èêQ™
Cô Ü®Šð¬ì îèõ™è¬÷ «êIˆ¶ H¡ù˜
܉î ÜŠO«èê¡ e‡´‹ îèõ¬ô ªðÁ‹
º¬øJ¬ù ܬñˆFì «û˜´ ŠKçðó¡v
ðò¡ð´Aø¶.
«û˜´ ŠKçðó¡v Shared Preferance Key
Value Pair â¡ø º¬øJ¬ù ðò¡ð´ˆ¶Aø¶.
꣡ø£è "Name" â¡ð¶ "Ramkumar" â¡ø£™
݇†ó£Œ´ ðò¡ð£†®™ PrefEditor put
string ("Name", "Ramkumar") â¡Á ðò¡ð´ˆî
«õ‡´‹. Þƒ° "Name" â¡ð¶ W ݰ‹.
'''Ramkumar'' â¡ð¶ «õ™Î ݰ‹. «û˜´
ŠK çðó¡C™ «ì†ì£ õ¬èJ¬ù (Data
Types) ªè£‡´ îèõL¬ù «êI‚è º®»‹.
«û˜´ ŠKçðó¡C™
A÷£v Íô‹ Boolean,
Float, Integer, Long
ñŸÁ‹ v®Kƒ õ¬è
îèõ™è¬÷ «êI‚è
º®»‹.
«û˜´ ŠKçðó¡CŸ
è£ù A÷£vèœ Android Content â¡ø «ð‚
«èT™ àœ÷ù. å¼ ÜŠO«èêQ™ ðô ŠK
çðó¡vè¬÷ ðò¡ð´ˆîô£‹. ꣡ø£è,
M¬÷ò£†®Ÿè£ù ÜŠO«èê¡ à¼õ£‚èŠ
𴋫𣶠ðòù£÷K¡ îèõ™è¬÷ å¼
ŠKçðó¡C½‹ (User name, E-mail, High Score)
«ð£¡ø¬õ ñŸÁ‹ ðòù£÷K¡ °PŠH†ì
G¬ô (current level, current score) «ð£¡ø
õŸP½‹ «êI‚èô£‹.
«û˜´ ŠKçðó¡C¬ù Ý‚®M†® G¬ôJ
½‹ Ü™ô¶ ÜŠO«èê¡ G¬ôJ½‹ ðò¡
ð´ˆî º®»‹.
«û˜´ŠKçðó¡C™«êI‚èŠð´‹îèõ™èœ
â‚vâ‹â™(XML) ç¬ðô£è «êI‚èŠ ð´‹.
«û˜´ ŠKçðó¡C™ «êI‚èŠð†ì îèõ
L¬ù e‡´‹ ÜŠO«èê¡ ªêò™ð´‹ «ð£¶
F¼‹ð ªðø º®»‹.
꣡ø£è å¼ F¬óJ™ àœ÷ ªì‚v†
ð£‚C¡ ⿈¶¼M¡ Ü÷M¬ù «û˜´ ŠK
çðó¡v Íô‹ «êIŠð¶ ðŸP 𣘊«ð£‹.
¹Fò Šó£ªü‚†®¬ù â‚OŠC™ File>
New>Android Project â¡Á ªî£ìƒè «õ‡
´‹. Þƒ° Project name, Build Target, Package
Name -Ÿè£ù îèõ™è¬÷ ªè£´‚è «õ‡´‹.
Þ‰î ÜŠO«èê‚è£ù Build Target -An
droid 2.2 â¡Á ªè£´‚è «õ‡´‹. «û˜´
ŠKçðó¡v â™ô£ õ¬èò£ù ݇†ó£Œ´
âv®«è-‚èÀ‹ ÝîK‚A¡øù. H¡ù˜ «ô
ܾ† ç¬ðô£è main.xml -™ F¬ó‚° «î¬õ
ò£ù îèõ™è¬÷ ªè£´‚è «õ‡´‹. hQò˜
«ôܾ†, Y‚ ð£˜, ªì‚v† MÎ, ⮆
ªì‚v†, ð†ì¡ (Linear Layout, Seek Bar,
Text View, Edit Text, Button) ðò¡ð´ˆîŠ
ð´Aø¶.
Ü´ˆî¶, Ý‚®M†®‚è£ù Gó™ ðŸP
𣘊«ð£‹. Þ‰î GóL™ Seek Bar Ü÷M¬ù
ñ£ŸÁ‹ «ð£¶ ⿈¶¼M™ Ü÷¾ ñ£Á
ñ£Á ܬñˆ¶ ܉î îèõL¬ù «û˜´ ŠK
çðó¡C™ «êI‚èŠ
«ð£A«ø£‹.
Þ‰î Ý‚®M†®‚
è£ù Gó™ ªè£´‚èŠð†
´œ÷¶.
Private Shared Prefe
rance Prefs; â¡ð¶
«û˜´ ŠKçðó¡C™ ðò¡ð´ˆîŠð†´œ÷¬î
°P‚Aø¶. ܉î ÜŠO«èê¡ ñ†´«ñ «û˜´
ŠKçðó¡C¬ù ðò¡ð´ˆî «õ‡´‹ â¡ðîŸ
è£è MODE-PRIVATE â¡ð¶ ðò¡ð´ˆîŠð†
´œ÷¶.
ŠKçðó¡C™ Key/value º¬øJ™ îèõ™
è¬÷ «êIŠð â®†ì˜ A÷£v ðò¡ð´
Aø¶. îèõ™è¬÷ «êIŠð Putstring(),
PutBoolean(),PutLong(),PutInt() ñŸÁ‹ PutFloat
() ªñˆî´èœ (Method) ðò¡ð´ˆîŠð†
´œ÷ù. ñFйè¬÷ ªè£´ˆî H¡ îèõL¬ù
«êIŠð commit () ªñˆî´ ðò¡ð´Aø¶.
⮆ ªì‚v†®™ àœ÷ ñFйè¬÷
«êIŠð W›è‡ì Gó™èœ ðò¡ð´Aø¶.
editor putFloatat(FONT_SIZE_KEY,
«û˜´ ŠKçðó¡v - îèõ™ «êIй!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SeekBar
android:id="@+id/SeekBar01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSave"
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
«û
ðì‹-1
ðì‹-2
Gó™-1
editText.get TextSize());
editor.putstring(TEXT_VALUE_KEY,
editText.getText().tostring());
editor commit ();
ÜŠO«èê¡ ªî£ìƒAò¾ì¡, Ý‚®M†®
ªêò™ðìªî£ìƒAò¾ì¡«û˜´ŠKçðó¡v
àœ÷ ñFйè¬÷ ºîL™ â´ˆ¶ ªè£œÀ‹.
SharedPreferances prefs=
Ý¡†ó£Œ´ -2
Þ󣋰ñ£˜ ªô†²Iï£ó£òí¡
09
package com.ram.sharedex;
import android.app.Activity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
public class SharedexActivity extends Activity {
private SharedPreferences prefs;
private String prefName = "MyPref";
private EditText editText;
private SeekBar seekBar;
private Button btn;
private static final String FONT_SIZE_KEY = "fontsize";
private static final String TEXT_VALUE_KEY =
"textvalue";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.EditText01);
seekBar = (SeekBar) findViewById(R.id.SeekBar01);
btn = (Button) findViewById(R.id.btnSave);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//---get the SharedPreferences object---
prefs = getSharedPreferences(prefName,
MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to
preferences---
editor.putFloat(FONT_SIZE_KEY,
editText.getTextSize());
editor.putString(TEXT_VALUE_KEY,
editText.getText().toString());
//---saves the values---
editor.commit();
//---display file saved message---
Toast.makeText(getBaseContext(),
"Font size saved successfully!",
Toast.LENGTH_SHORT).show();
}
});
//--load the SharedPreferences object---
SharedPreferences prefs =
getSharedPreferences(prefName, MODE_PRIVATE);
//---set the TextView font size to the previously saved
values---
float fontSize = prefs.getFloat(FONT_SIZE_KEY, 12);
//---init the SeekBar and EditText---
seekBar.setProgress((int) fontSize);
editText.setText(prefs.getString(TEXT_VALUE_KEY, ""));
editText.setTextSize(seekBar.getProgress());
seekBar.setOnSeekBarChangeListener(new
OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int
progress,
boolean fromUser) {
//---change the font size of the EditText---
editText.setTextSize(progress);
}
});
}
}
get shared preference (prefsName,
MODE_PRIVATE);
Ü´ˆîî£è ªì‚v† MÎM¡ ⿈¶¼
ñFŠH¬ù 㟪èù«õ «êIˆî ñFŠHŸ°
ãŸð 裇H‚°‹.
floatfontsize=prefs.getFloat(FONT_SIZE_KEY,12)
Seek Bar.setProgress((int font size);
editText.setText(prefs.getstring(TEXT_VALUE.
KEY,""));
editText SetTextSize(SeekBar.get Progress());
Shared Preferances ñFйè¬÷
/data/data/your-package-name/shared-prefs/
YOUR_PACKAGE_NAME_preferences.xml
â¡ø ¬ìó‚ìKJ™ «êI‚èŠð´‹. ÜŠO
«èê¡ ºîL™ ªêò™ð´‹ «ð£¶ ðì‹-1™
àœ÷¶ «ð£¡Á «î£¡Á‹. ⿈¶¼M¡
ñFŠH¬ù ñ£ŸP Save Button AO‚ ªêŒî£™
«û˜´ ŠKðó¡C™ ñFŠ¹èœ «êI‚èŠð´‹.
ÜŠO«èê¡ º®¾‚° ªè£‡´ õ‰¶ e‡´‹
ªêò™ð´ˆFù£™ ðì‹-2™ àœ÷¶ «ð£¡Á
«î£¡Á‹.
- ªî£ì¼‹
Gó™-2

Shared Preference in Android App

  • 1.
    ˜´ ŠKçðó¡C¬ù (SharedPreferance) ݇†ó£Œ´ ÜŠO«èêQ™ ðò¡ð´ˆ¶‹ ꣡P¬ù 𣘊«ð£‹. å¼ ÜŠO«èêQ™ Cô Ü®Šð¬ì îèõ™è¬÷ «êIˆ¶ H¡ù˜ ܉î ÜŠO«èê¡ e‡´‹ îèõ¬ô ªðÁ‹ º¬øJ¬ù ܬñˆFì «û˜´ ŠKçðó¡v ðò¡ð´Aø¶. «û˜´ ŠKçðó¡v Shared Preferance Key Value Pair â¡ø º¬øJ¬ù ðò¡ð´ˆ¶Aø¶. ꣡ø£è "Name" â¡ð¶ "Ramkumar" â¡ø£™ ݇†ó£Œ´ ðò¡ð£†®™ PrefEditor put string ("Name", "Ramkumar") â¡Á ðò¡ð´ˆî «õ‡´‹. Þƒ° "Name" â¡ð¶ W ݰ‹. '''Ramkumar'' â¡ð¶ «õ™Î ݰ‹. «û˜´ ŠK çðó¡C™ «ì†ì£ õ¬èJ¬ù (Data Types) ªè£‡´ îèõL¬ù «êI‚è º®»‹. «û˜´ ŠKçðó¡C™ A÷£v Íô‹ Boolean, Float, Integer, Long ñŸÁ‹ v®Kƒ õ¬è îèõ™è¬÷ «êI‚è º®»‹. «û˜´ ŠKçðó¡CŸ è£ù A÷£vèœ Android Content â¡ø «ð‚ «èT™ àœ÷ù. å¼ ÜŠO«èêQ™ ðô ŠK çðó¡vè¬÷ ðò¡ð´ˆîô£‹. ꣡ø£è, M¬÷ò£†®Ÿè£ù ÜŠO«èê¡ à¼õ£‚èŠ ð´‹«ð£¶ ðòù£÷K¡ îèõ™è¬÷ å¼ ŠKçðó¡C½‹ (User name, E-mail, High Score) «ð£¡ø¬õ ñŸÁ‹ ðòù£÷K¡ °PŠH†ì G¬ô (current level, current score) «ð£¡ø õŸP½‹ «êI‚èô£‹. «û˜´ ŠKçðó¡C¬ù Ý‚®M†® G¬ôJ ½‹ Ü™ô¶ ÜŠO«èê¡ G¬ôJ½‹ ðò¡ ð´ˆî º®»‹. «û˜´ŠKçðó¡C™«êI‚èŠð´‹îèõ™èœ â‚vâ‹â™(XML) ç¬ðô£è «êI‚èŠ ð´‹. «û˜´ ŠKçðó¡C™ «êI‚èŠð†ì îèõ L¬ù e‡´‹ ÜŠO«èê¡ ªêò™ð´‹ «ð£¶ F¼‹ð ªðø º®»‹. ꣡ø£è å¼ F¬óJ™ àœ÷ ªì‚v† ð£‚C¡ ⿈¶¼M¡ Ü÷M¬ù «û˜´ ŠK çðó¡v Íô‹ «êIŠð¶ ðŸP 𣘊«ð£‹. ¹Fò Šó£ªü‚†®¬ù â‚OŠC™ File> New>Android Project â¡Á ªî£ìƒè «õ‡ ´‹. Þƒ° Project name, Build Target, Package Name -Ÿè£ù îèõ™è¬÷ ªè£´‚è «õ‡´‹. Þ‰î ÜŠO«èê‚è£ù Build Target -An droid 2.2 â¡Á ªè£´‚è «õ‡´‹. «û˜´ ŠKçðó¡v â™ô£ õ¬èò£ù ݇†ó£Œ´ âv®«è-‚èÀ‹ ÝîK‚A¡øù. H¡ù˜ «ô ܾ† ç¬ðô£è main.xml -™ F¬ó‚° «î¬õ ò£ù îèõ™è¬÷ ªè£´‚è «õ‡´‹. hQò˜ «ôܾ†, Y‚ ð£˜, ªì‚v† MÎ, ⮆ ªì‚v†, ð†ì¡ (Linear Layout, Seek Bar, Text View, Edit Text, Button) ðò¡ð´ˆîŠ ð´Aø¶. Ü´ˆî¶, Ý‚®M†®‚è£ù Gó™ ðŸP 𣘊«ð£‹. Þ‰î GóL™ Seek Bar Ü÷M¬ù ñ£ŸÁ‹ «ð£¶ ⿈¶¼M™ Ü÷¾ ñ£Á ñ£Á ܬñˆ¶ ܉î îèõL¬ù «û˜´ ŠK çðó¡C™ «êI‚èŠ «ð£A«ø£‹. Þ‰î Ý‚®M†®‚ è£ù Gó™ ªè£´‚èŠð† ´œ÷¶. Private Shared Prefe rance Prefs; â¡ð¶ «û˜´ ŠKçðó¡C™ ðò¡ð´ˆîŠð†´œ÷¬î °P‚Aø¶. ܉î ÜŠO«èê¡ ñ†´«ñ «û˜´ ŠKçðó¡C¬ù ðò¡ð´ˆî «õ‡´‹ â¡ðîŸ è£è MODE-PRIVATE â¡ð¶ ðò¡ð´ˆîŠð† ´œ÷¶. ŠKçðó¡C™ Key/value º¬øJ™ îèõ™ è¬÷ «êIŠð â®†ì˜ A÷£v ðò¡ð´ Aø¶. îèõ™è¬÷ «êIŠð Putstring(), PutBoolean(),PutLong(),PutInt() ñŸÁ‹ PutFloat () ªñˆî´èœ (Method) ðò¡ð´ˆîŠð† ´œ÷ù. ñFйè¬÷ ªè£´ˆî H¡ îèõL¬ù «êIŠð commit () ªñˆî´ ðò¡ð´Aø¶. ⮆ ªì‚v†®™ àœ÷ ñFйè¬÷ «êIŠð W›è‡ì Gó™èœ ðò¡ð´Aø¶. editor putFloatat(FONT_SIZE_KEY, «û˜´ ŠKçðó¡v - îèõ™ «êIй! <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SeekBar android:id="@+id/SeekBar01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnSave" android:text="Save" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> «û ðì‹-1 ðì‹-2 Gó™-1
  • 2.
    editText.get TextSize()); editor.putstring(TEXT_VALUE_KEY, editText.getText().tostring()); editor commit(); ÜŠO«èê¡ ªî£ìƒAò¾ì¡, Ý‚®M†® ªêò™ðìªî£ìƒAò¾ì¡«û˜´ŠKçðó¡v àœ÷ ñFйè¬÷ ºîL™ â´ˆ¶ ªè£œÀ‹. SharedPreferances prefs= Ý¡†ó£Œ´ -2 Þ󣋰ñ£˜ ªô†²Iï£ó£òí¡ 09 package com.ram.sharedex; import android.app.Activity; import android.os.Bundle; import android.content.SharedPreferences; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Toast; public class SharedexActivity extends Activity { private SharedPreferences prefs; private String prefName = "MyPref"; private EditText editText; private SeekBar seekBar; private Button btn; private static final String FONT_SIZE_KEY = "fontsize"; private static final String TEXT_VALUE_KEY = "textvalue"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.EditText01); seekBar = (SeekBar) findViewById(R.id.SeekBar01); btn = (Button) findViewById(R.id.btnSave); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //---get the SharedPreferences object--- prefs = getSharedPreferences(prefName, MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); //---save the values in the EditText view to preferences--- editor.putFloat(FONT_SIZE_KEY, editText.getTextSize()); editor.putString(TEXT_VALUE_KEY, editText.getText().toString()); //---saves the values--- editor.commit(); //---display file saved message--- Toast.makeText(getBaseContext(), "Font size saved successfully!", Toast.LENGTH_SHORT).show(); } }); //--load the SharedPreferences object--- SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE); //---set the TextView font size to the previously saved values--- float fontSize = prefs.getFloat(FONT_SIZE_KEY, 12); //---init the SeekBar and EditText--- seekBar.setProgress((int) fontSize); editText.setText(prefs.getString(TEXT_VALUE_KEY, "")); editText.setTextSize(seekBar.getProgress()); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //---change the font size of the EditText--- editText.setTextSize(progress); } }); } } get shared preference (prefsName, MODE_PRIVATE); Ü´ˆîî£è ªì‚v† MÎM¡ ⿈¶¼ ñFŠH¬ù 㟪èù«õ «êIˆî ñFŠHŸ° ãŸð 裇H‚°‹. floatfontsize=prefs.getFloat(FONT_SIZE_KEY,12) Seek Bar.setProgress((int font size); editText.setText(prefs.getstring(TEXT_VALUE. KEY,"")); editText SetTextSize(SeekBar.get Progress()); Shared Preferances ñFйè¬÷ /data/data/your-package-name/shared-prefs/ YOUR_PACKAGE_NAME_preferences.xml â¡ø ¬ìó‚ìKJ™ «êI‚èŠð´‹. ÜŠO «èê¡ ºîL™ ªêò™ð´‹ «ð£¶ ðì‹-1™ àœ÷¶ «ð£¡Á «î£¡Á‹. ⿈¶¼M¡ ñFŠH¬ù ñ£ŸP Save Button AO‚ ªêŒî£™ «û˜´ ŠKðó¡C™ ñFŠ¹èœ «êI‚èŠð´‹. ÜŠO«èê¡ º®¾‚° ªè£‡´ õ‰¶ e‡´‹ ªêò™ð´ˆFù£™ ðì‹-2™ àœ÷¶ «ð£¡Á «î£¡Á‹. - ªî£ì¼‹ Gó™-2