Android Preferences - Using Preferences in Android Tutorial
By: Ashley Printer Friendly Format
This sample android program shows you how to use preferences in Android. In this program the concept of storing the user's preferences in a SharedPreferences object is demonstrated. With this sharedpreference you can store and retrieve preferences and use it in any of your activities.
The PreferenceDemo1.java file is as follows:
package com.javasamples.PreferenceDemo1;
import java.util.Date;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class PreferenceDemo1 extends Activity implements OnClickListener {
Button btnSimplePref;
Button btnFancyPref;
TextView txtCaption1;
Boolean fancyPrefChosen = false;
View myLayout1Vertical;
final int mode = Activity.MODE_PRIVATE;
final String MYPREFS = "MyPreferences_001";
// create a reference to the shared preferences object
SharedPreferences mySharedPreferences;
// obtain an editor to add data to my SharedPreferences object
SharedPreferences.Editor myEditor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLayout1Vertical = (View)findViewById(R.id.linLayout1Vertical);
txtCaption1 = (TextView) findViewById(R.id.txtCaption1);
txtCaption1.setText("This is a sample line \n"
+ "suggesting the way the UI looks \n"
+ "after you choose your preference");
// create a reference to the shared preferences object
mySharedPreferences = getSharedPreferences(MYPREFS, 0);
// obtain an editor to add data to (my)SharedPreferences object
myEditor = mySharedPreferences.edit();
// has a Preferences file been already created?
if (mySharedPreferences != null
&& mySharedPreferences.contains("backColor")) {
// object and key found, show all saved values
applySavedPreferences();
} else {
Toast.makeText(getApplicationContext(),
"No Preferences found", 1).show();
}
btnSimplePref = (Button) findViewById(R.id.btnPrefSimple);
btnSimplePref.setOnClickListener(this);
btnFancyPref = (Button) findViewById(R.id.btnPrefFancy);
btnFancyPref.setOnClickListener(this);
}// onCreate
public void onClick(View v) {
// clear all previous selections
myEditor.clear();
// what button has been clicked?
if (v.getId() == btnSimplePref.getId()) {
myEditor.putInt("backColor", Color.BLACK);// black background
myEditor.putInt("textSize", 12); // humble small font
} else { // case btnFancyPref
myEditor.putInt("backColor", Color.BLUE); // fancy blue
myEditor.putInt("textSize", 20); // fancy big
myEditor.putString("textStyle", "bold"); // fancy bold
myEditor.putInt("layoutColor", Color.GREEN);//fancy green
}
myEditor.commit();
applySavedPreferences();
}
@Override
protected void onPause() {
// warning: activity is on its last state of visibility!
// It's on the edge of been killed! Better save all current
// state data into Preference object (be quick!)
myEditor.putString("DateLastExecution", new Date().toLocaleString());
myEditor.commit();
super.onPause();
}
public void applySavedPreferences() {
// extract the pairs, use default param for missing data
int backColor = mySharedPreferences.getInt("backColor",Color.BLACK);
int textSize = mySharedPreferences.getInt("textSize", 12);
String textStyle = mySharedPreferences.getString("textStyle", "normal");
int layoutColor = mySharedPreferences.getInt("layoutColor", Color.DKGRAY);
String msg = "color " + backColor + "\n" + "size " + textSize
+ "\n" + "style " + textStyle;
Toast.makeText(getApplicationContext(), msg, 1).show();
txtCaption1.setBackgroundColor(backColor);
txtCaption1.setTextSize(textSize);
if (textStyle.compareTo("normal")==0){
txtCaption1.setTypeface(Typeface.SERIF,Typeface.NORMAL);
}
else {
txtCaption1.setTypeface(Typeface.SERIF,Typeface.BOLD);
}
myLayout1Vertical.setBackgroundColor(layoutColor);
}// applySavedPreferences
}
The Strings.xml file in your res/values/ folder will be as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PreferenceDemo0!</string>
<string name="app_name">PreferencesDemo1</string>
</resources>
The main.xml file in your res/layout/ folder will be as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/linLayout1Vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="@+id/linLayout2Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnPrefSimple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pref Simple UI">
</Button>
<Button
android:id="@+id/btnPrefFancy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pref Fancy UI">
</Button>
</LinearLayout>
<TextView
android:id="@+id/txtCaption1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff006666"
android:text="This is some sample text ">
</TextView>
</LinearLayout>
The output of this program will be as shown in the android emulator below.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Solution to error: unable to open connection to server due to security error
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Intent in Android to call one activity from another activity.
Animation sample program in Android
Another Animation sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
GridView sample program in Android
Gallery sample program in Android
Date and Time sample program in Android
Progress bar and downloading a file sample program in Android
Archived Comments
1. thank you for ur programs.........
View Tutorial By: NAGARAJRATHINARAJ at 2012-01-24 14:39:19
2. Many Thanks!
View Tutorial By: T.Silambarasan at 2012-02-14 17:35:08