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">
    <CheckBoxPreference
        android:key="pref_mute"
        android:title="@string/pref_mute"
        android:summary="@string/pref_mute_summ"
        android:defaultValue="false" />

</PreferenceScreen>

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"?>
<resources>

    <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>

</resources>

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 {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.layout.preferences);
}

}

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;

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

@Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(Menu.NONE, IDM_SETTINGS, Menu.NONE, R.string.menu_settings);
return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==IDM_SETTINGS){
Intent intent = new Intent();
intent.setClass(this, TestPreferences.class);
startActivity(intent);
}
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

http://kirill-poletaev.blogspot.it/2013/02/android-beginner-tutorial-part-78.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in 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.