Friday, 20 April 2012

Shared Preferences

how data can be stored in SQLDB. However, many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs. 

Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity (which is not discussed here). 

The context object lets you retrieve SharedPreferences through the methodContext.getSharedPreferences(). 


------------------------------------------CODE------------------------------------------------------

package com.kiran;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SharedpreActivity extends Activity {



Button save, show;
EditText enter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button save = (Button) findViewById(R.id.button1);
Button show = (Button) findViewById(R.id.button2);


        final EditText enter = (EditText) findViewById(R.id.editText1);
       
  //-----------------------------SAVE---------------------------------------------------------//    
save.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(SharedpreActivity.this);
SharedPreferences.Editor editor = app_preferences.edit();
String text = enter.getText().toString();
editor.putString("key", text);
editor.commit();

}

});
//----------------------------------SHOW-------------------------------------------------//
show.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
TextView textView = (TextView) findViewById(R.id.textView1);
SharedPreferences app_Preferences = PreferenceManager
.getDefaultSharedPreferences(SharedpreActivity.this);
String text = app_Preferences.getString("key", "null");
            textView.setText(text);

}
});
}

}
---------------------------------------------XML------------------------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="TextView" android:textColor="@color/black"/>
   
   

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="14dp"
        android:text="SHOW" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="43dp" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/editText1"
        android:text="SAVE" />



    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/fish"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>