Saturday, 16 June 2012

Android Shared Preference Example

Android Shared Preference Example


SharedPrefActivity


package com.shared;





import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SharedprefActivity extends Activity {
private static final String TAG = SharedprefActivity.class.getSimpleName();
private EditText id,name;
private Button bt1;
private SharedPreferences shared;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample1);
        
        id = (EditText)findViewById(R.id.ET1);
        name = (EditText)findViewById(R.id.ET2);
        bt1 = (Button) findViewById(R.id.backbutton);
       shared = getSharedPreferences("shared", 0);
       
        id.setText(shared.getString("id", "novalue"));
        name.setText(shared.getString("name", "noname"));
        bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG,"button called" );
String f1,f2;
f1 = id.getText().toString();
f2 = name.getText().toString();
Log.i(TAG,"c log example" +f1+" "+f2);
SharedPreferences.Editor editor = shared.edit();
editor.putString("id", f1);
editor.putString("name",f2 );
editor.commit();
Intent  act = new Intent(v.getContext(),act.class);
startActivity(act);
}
});
    }
   
}

Act.java


package com.shared;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class act extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act);
SharedPreferences shared;
EditText name = (EditText) findViewById(R.id.ET1);
EditText id = (EditText) findViewById(R.id.ET2);
shared = getSharedPreferences("shared", 0);
String idshared = shared.getString("name", "null");
String nameshared = shared.getString("id", "null");
name.setText(nameshared);
id.setText(idshared);
}

}

Layout

Sample.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <Button 
    android:id="@+id/backbutton"
    android:text="Back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
   <TextView
    android:text="First Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
   <EditText
       android:id="@+id/ET1"
    android:width="100px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">    
   <TextView
    android:text="Last Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
   <EditText
       android:id="@+id/ET2"
    android:width="100px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </LinearLayout> 
</LinearLayout>

Act.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
   <TextView
    android:text="First Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
   <EditText
       android:id="@+id/ET1"
    android:width="100px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">    
   <TextView
    android:text="Last Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
   <EditText
       android:id="@+id/ET2"
    android:width="100px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </LinearLayout> 
</LinearLayout>


No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...