Android SharedPreferences Example


A primitive data type values only stored in SharedPreferences. If stored we can use SharedPreferences values across the application.

You can download source code here.

Following steps:-

This class used for get value from SharedPreferences and put value to SharedPreferences.

package com.etr.sharedpreferenceexample;

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

public class PreferenceActivity extends Activity {
private SharedPreferences mPref;
private SharedPreferences.Editor mPrefEdit;
private EditText mText;
private Button mGet, mPut;

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

mPref = getSharedPreferences("Example", MODE_PRIVATE);

mText = (EditText) findViewById(R.id.editText1);
mPut = (Button) findViewById(R.id.button1);
mPut.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String value = mText.getText().toString();
if (value.length() >= 1) {
mPrefEdit = mPref.edit();
mPrefEdit.putString("sValue", value);
mPrefEdit.commit();
mText.setText("");
Toast.makeText(PreferenceActivity.this,
"Succussfully stored", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(PreferenceActivity.this,
"Please Enter value", Toast.LENGTH_LONG).show();
}
}
});
mGet = (Button) findViewById(R.id.button2);
mGet.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

// TODO Auto-generated method stub
Toast.makeText(
PreferenceActivity.this,
"Stored value is : "
+ mPref.getString("sValue",
"noValue Stored"),
Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
Log.e("Exception : ", Log.getStackTraceString(e));
}
}

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

}

Paste below code to activity_preference.xml for layout design.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".PreferenceActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="168dp"
        android:text="Put value to SharedPreference" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="36dp"
        android:text="Get value from SharedPreference" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="96dp"
        android:ems="10"
        android:hint="Enter value"
        android:paddingLeft="15dp" >

        <requestFocus />
    </EditText>

</RelativeLayout>

Output Screen :


Comments

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService