Tutorial details

Android beginner tutorial: Part 80 - ListPreference and resetting settings | App Code for Sale | Preview

Android beginner tutorial: Part 80 - ListPreference and resetting settings | Android Tutorial

How to add a ListPreference item to our Settings, as well as the ability to reset preferences

Overview PAGE TOP

In this tutorial we are going to add a ListPreference item to our Settings, as well as the ability to reset preferences.

A ListPreference is a settings item which displays a list of choices when selected. The choices are all radio-buttons so there can be 0 or 1 choices selected at the same time.

We can supply choice labels and their values to a ListPreference by first declaring them in an array. Create a new xml file in res/values/ directory called arrays.xml.

Inside, we create a single-array named theme_colors with 4 possible choices:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="theme_colors">
        <item>Blue</item>
        <item>Red</item>
        <item>Green</item>
        <item>Yellow</item>
    </string-array>
</resources>

Now go to preferences.xml file and add a new ListPreference item:

<?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" />
    <EditTextPreference
        android:key="pref_textsize"
        android:title="@string/pref_textsize"
        android:dialogTitle="@string/pref_textsize_descr"
        android:summary="@string/pref_textsize_summ"
        android:defaultValue="14" />
    <ListPreference
        android:key="pref_theme"
        android:title="@string/pref_theme"
        android:dialogTitle="@string/pref_theme_descr"
        android:summary="@string/pref_theme_summ"
        android:entries="@array/theme_colors"
        android:entryValues="@array/theme_colors"
        android:defaultValue="Blue" />
</PreferenceScreen>

You can see 2 new attributes used in that item - entries and entryValues. They both have the same value of theme_colors array we've just defined in arrays.xml. Of course, they don't need to be the same. The difference between the two is that "entries" is an array of labels of the items in the list, while "entryValues" is an array of the values that are not visible to the user but can be used by the programmer to determine which item was selected.

Go to MainActivity.java class. You can read and display the selected item's value using a Toast like this:

@Override
public void onResume(){
prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("pref_mute", false)){
Toast.makeText(this, "Muted!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Not muted!", Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "Text size: " + prefs.getString("pref_textsize", "14"), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Theme color: " + prefs.getString("pref_theme", "Blue"), Toast.LENGTH_SHORT).show();

super.onResume();
}

Note how the "prefs" variable is not declared in this function. That is because I made it public in the beginning of the class:

public SharedPreferences prefs;

Now let's also add the ability to reset preferences. Go to activity_main.xml and add a button:

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <Button android:id="@+id/clearButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/clear_settings"
        />

</LinearLayout>

Back in MainActivity.java class, we add a click listener to that button and reset the settings by retreiving an Editor object from the preferences and calling its clear() and commit() methods:

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

Button btn_clear = (Button)findViewById(R.id.clearButton);
btn_clear.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Editor editor = prefs.edit();
editor.clear();
editor.commit();
onResume();
}
});
}

Full class code:

package com.example.codeforfoodtest_two;

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.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity{

static final int IDM_SETTINGS = 101;
public SharedPreferences prefs;

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

Button btn_clear = (Button)findViewById(R.id.clearButton);
btn_clear.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Editor editor = prefs.edit();
editor.clear();
editor.commit();
onResume();
}
});
}

@Override
public void onResume(){
prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("pref_mute", false)){
Toast.makeText(this, "Muted!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Not muted!", Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "Text size: " + prefs.getString("pref_textsize", "14"), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Theme color: " + prefs.getString("pref_theme", "Blue"), Toast.LENGTH_SHORT).show();

super.onResume();
}

@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;
}
}

You also need to declare all the string values that were used in the application in strings.xml:

<?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="pref_textsize">Text size</string>
    <string name="pref_textsize_descr">Enter text size</string>
    <string name="pref_textsize_summ">Set text size of the interface</string>
    <string name="pref_theme">Theme</string>
    <string name="pref_theme_descr">Pick a color</string>
    <string name="pref_theme_summ">Set the application\'s color theme</string>
    <string name="menu_settings">Settings</string>
    <string name="clear_settings">Reset settings</string>

</resources>

That's all for today.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/02/android-beginner-tutorial-part-80.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.