alvinalexander.com: Android 5 Preferences tutorial (PreferenceScreen, PreferenceActivity, and PreferenceFragment)

alvinalexander.com: Android 5 Preferences tutorial (PreferenceScreen, PreferenceActivity, and PreferenceFragment)

The code here has been updated from iRomani’s tutorial, and contains the necessary corrections to work with Android 5, specifically using a PreferenceFragment along with the PreferenceActivity:

package com.alvinalexander.preferencestestapp;

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

public class MyPreferencesActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s