CREATE SHARED PREFERENCES IN ANDROID
JAVA CODE OF SHARED PREFERENCES:-
package com.javacodegeeks.android.androidsharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
CheckBox checkBox;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox = (CheckBox) findViewById(R.id.checkBox1);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean cbValue = sp.getBoolean("CHECKBOX", false);
String name = sp.getString("NAME", "YourName");
if (cbValue) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
editText.setText(name);
}
private void savePreferences(String key, boolean value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePreferences(String key, String value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences("CHECKBOX", checkBox.isChecked());
if (checkBox.isChecked())
savePreferences("NAME", editText.getText().toString());
finish();
}
}
XML CODE OF SHARED PREFERENCES:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Box" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Your Name...">
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
MANIFEST FILE CODE:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javacodegeeks.android.androidsharedpreferences"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.javacodegeeks.android.androidsharedpreferences.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
STRING XML CODE:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidSharedPreferences</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
package com.javacodegeeks.android.androidsharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
CheckBox checkBox;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox = (CheckBox) findViewById(R.id.checkBox1);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean cbValue = sp.getBoolean("CHECKBOX", false);
String name = sp.getString("NAME", "YourName");
if (cbValue) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
editText.setText(name);
}
private void savePreferences(String key, boolean value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePreferences(String key, String value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences("CHECKBOX", checkBox.isChecked());
if (checkBox.isChecked())
savePreferences("NAME", editText.getText().toString());
finish();
}
}
XML CODE OF SHARED PREFERENCES:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Box" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Your Name...">
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
MANIFEST FILE CODE:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javacodegeeks.android.androidsharedpreferences"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.javacodegeeks.android.androidsharedpreferences.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
STRING XML CODE:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidSharedPreferences</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
No comments: