Tutorial details

Android beginner tutorial: Part 79 - EditTextPreference, Reading Preferences | App Code for Sale | Preview

Android beginner tutorial: Part 79 - EditTextPreference, Reading Preferences | Android Tutorial

How to add an EditTextPreference setting to our settings, as well as read and display saved preferences data in the application

Overview PAGE TOP

In this tutorial we'll add an EditTextPreference setting to our settings, as well as read and display saved preferences data in the application.

First of all, go to the preferences.xml file and edit the contents to add a new EditTextPreference object. Just like the existing CheckBoxPreference item that we have, we'll give this one key, title, summary and defaultValues attributes. We'll also give it a dialogTitle attribute, which represents the text that's displayed in the dialog window that opens when the item is clicked by the user.

<?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" />
</PreferenceScreen>

Go to strings.xml file and add the new 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="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="menu_settings">Settings</string>

</resources>

Now go to MainActivity.java class. Here we will read the values of the preferences when the application is resumed - in the onResume() callback function. Doing it there will make sure that the data is updated from the saved settings file every time the application is launched or resumed if it was paused.

In the onResume() function, load preferences like this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

Then read the values and handle them by simply displaying them in a Toast:

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", ""), Toast.LENGTH_SHORT).show();

Full MainActivity.java class code:

package com.example.codeforfoodtest_two;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

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 void onResume(){
SharedPreferences 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", ""), 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;
}
}

That's all for today!

Thanks for reading!

Reference PAGE TOP

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