Tutorial details

Android beginner tutorial: Part 78 - Creating Preferences Activity | App Code for Sale | Preview

Android beginner tutorial: Part 78 - Creating Preferences Activity | Android Tutorial

How to create a preferences activity that can be invoked using the application menu

Overview PAGE TOP

In this tutorial we will create a preferences activity that can be invoked using the application menu.

First of all we'll need to create the xml that contains all the preferences in the settings menu. Go to the layout directory of your application's project and add a new file preferences.xml.

Add this code:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        android:defaultValue="false" />


The key property is used as an identificator for this setting. It has to be a unique string. The title value is the text representation of the setting, and summary is the description of the setting item.

Go to strings.xml file to set the missing string values:

<?xml version="1.0" encoding="utf-8"?>

    <string name="app_name">CodeForFood Test Two</string>
    <string name="pref_mute">Mute</string>
    <string name="pref_mute_summ">Disable all the sound in the application</string>
    <string name="menu_settings">Settings</string>


Create a new Activity in the same directory as the MainActivity.java class, call it TestPreferences.java.

Here's the code that applies the xml layout of the preferences page:

package com.example.codeforfoodtest_two;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class TestPreferences extends PreferenceActivity {

public void onCreate(Bundle savedInstanceState) {


Now go to AndroidManifest.file of the application and add this line to the application node:

<activity android:name="TestPreferences"></activity>

Doing so will tell Android that there's an Activity with that name in our application.

Now go to MainActivity.java class, add some code that creates an options menu and handles item selection in the menu. When the Settings item is selected, the preferences screen pops up:

package com.example.codeforfoodtest_two;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity{

static final int IDM_SETTINGS = 101;

protected void onCreate(Bundle savedInstanceState) {

public boolean onCreateOptionsMenu(Menu menu){
menu.add(Menu.NONE, IDM_SETTINGS, Menu.NONE, R.string.menu_settings);

public boolean onOptionsItemSelected(MenuItem item){
Intent intent = new Intent();
intent.setClass(this, TestPreferences.class);
return true;

And that's all for today! Changing the preference doesn't really do anything yet, but we'll work on that next time.

Thanks for reading!

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.