| Free tutorials for Java, Eclipse and Web programming |
Preferences allow you to save data for your application. Preferences are stored as key values. Intents allow you to start Activities from other Activities.
We will continue using the example project "de.vogella.android.preferences" from the last chapter.
Preference values can also be stored as a XML resource. Create another Android XML File "preferences.xml" this time of type "Preference".

Press Add, add a category and add two preferences "EditTextPreferences" to this category : "User" and "Password".



To maintain preferences you can define a Activity with extends PreferenceActivity. This activity can load a perference definition resources via the method addPreferencesFromResource(). Create the class "Preferences" which will load the "preference.xml".
package de.vogella.android.preferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
To make this class available as an activity for Android you need to register it in your "AndroidManifest.xml" file. Select "AndroidManifest.xml" and the tab "Application". Add the activity "Preferences".

The first button will show the current maintained preferences via a Toast and the second button will revert the maintained user name to demonstrate how you could change the preferences via code.
package de.vogella.android.preferences;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class HelloPreferences extends Activity {
SharedPreferences preferences;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.Button01);
// Initialize preferences
preferences = PreferenceManager.getDefaultSharedPreferences(this);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String username = preferences.getString("username", "n/a");
String password = preferences.getString("password", "n/a");
Toast.makeText(
HelloPreferences.this,
"You entered user: " + username + " and password: "
+ password, Toast.LENGTH_LONG).show();
}
});
Button buttonChangePreferences = (Button) findViewById(R.id.Button02);
buttonChangePreferences.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Editor edit = preferences.edit();
String username = preferences.getString("username", "n/a");
// We will just revert the current user name and save again
StringBuffer buffer = new StringBuffer();
for (int i = username.length() - 1; i >= 0; i--) {
buffer.append(username.charAt(i));
}
edit.putString("username", buffer.toString());
edit.commit();
// A toast is a view containing a quick little message for the
// user. We give a little feedback
Toast.makeText(HelloPreferences.this,
"Reverted string sequence of user name.",
Toast.LENGTH_LONG).show();
}
});
}
}
We will update the method onOptionsItemSelected() to open the activity "Preferences" once you select the option menu. Even though we currently have only one option in our menu we use a switch to be ready for several new menu entries. To see the maintained preferences we also define a button and use the class "PreferenceManager" to get the sharedPreferences.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
// This method is called once the menu is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// We have only one menu option
case R.id.preferences:
// Launch Preference activity
Intent i = new Intent(HelloPreferences.this, Preferences.class);
startActivity(i);
// Some feedback to the user
Toast.makeText(HelloPreferences.this,
"Here you can maintain your user credentials.",
Toast.LENGTH_LONG).show();
break;
}
return true;
}
Run your application. Press the "menu" hardware button and then select your menu item "Preferences". You should be able to enter your user settings then press the back hardware button to return to your main activity. The saved values should be displayed in a small message windows (Toast) if you press your first button. If you press the second button the username should be reversed.
