Pages

Monday, 18 May 2015

Android Shared preferences

Shared preferences in android is very effective to handle session management and also for storing value for long time even if you close the application.
Following are simple code for shared preferences.
Note that you must commit the preferences for save the value.
It can be done with commit() method.

MainActivity.java.
package com.im.sharedpreferences;

import com.im.sharedpreferences.*;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView name ;
TextView phone;
TextView email;

public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "NAME";
public static final String Phone = "PHONE";
public static final String Email = "EMAIL";


SharedPreferences sharedpreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

name = (TextView) findViewById(R.id.editTextName);
phone = (TextView) findViewById(R.id.editTextPhone);
email = (TextView) findViewById(R.id.editTextEmail);


sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

if (sharedpreferences.contains(Name))
{
name.setText(sharedpreferences.getString(Name, ""));

}
if (sharedpreferences.contains(Phone))
{
phone.setText(sharedpreferences.getString(Phone, ""));

}
if (sharedpreferences.contains(Email))
{
email.setText(sharedpreferences.getString(Email, ""));

}
if (sharedpreferences.contains(Street))
{
street.setText(sharedpreferences.getString(Street, ""));

}
if (sharedpreferences.contains(Place))
{
place.setText(sharedpreferences.getString(Place,""));

}

}

public void run(View view){
String n = name.getText().toString();
String ph = phone.getText().toString();
String e = email.getText().toString();

Editor editor = sharedpreferences.edit();
editor.putString(NAME, n);
editor.putString(PHONE, ph);
editor.putString(EMAIL, e);


editor.commit();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
activiy_main.xml.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".DisplayContact" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="370dp"

>

<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="82dp"
android:ems="10"
android:inputType="text" >

</EditText>

<EditText
android:id="@+id/editTextEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="textEmailAddress" />



<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="28dp"
android:onClick="run"
android:text="@string/save" />



<EditText
android:id="@+id/editTextPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone|text" />

</RelativeLayout>

</ScrollView>


 AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.im.sharedpreferences"
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.im.sharedpreferences.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>

No comments:

Post a Comment