Using Preference Fragment to Display XML File Containing Preferences

I'm having some trouble creating a PreferenceFragment. I have followed the tutorial to the letter and my app runs in the emulator, but the screen which is meant to hold the preference xml is blank. I have installed the v4 update and applied all of the required using statements. The screen works using PreferenceActivity but I need access to the shared preferences.. I understand that this isn't possible via the PreferenceActivity which is why I require the PreferenceFragment to work. Below is my code.. maybe someone can spot a mistake I have missed?

using System;
using Android.App;
using Android.Support.V4.App;
using Android.Content;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Widget;
using Android.Views;
using Android.Preferences;


namespace ManageMyMoney
{
    [Activity (MainLauncher = true)]
    public class MainActivity : TabActivity
    {
        protected override void OnCreate (Bundle bundle)
        {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Main);

        CreateTab (typeof(Home), "main", "Main", Resource.Drawable.ic_tab_home);
        CreateTab (typeof(Calendar), "calendar", "Calendar", Resource.Drawable.ic_tab_calendar);
        CreateTab (typeof(Input), "input", "Input", Resource.Drawable.ic_tab_input);
        CreateTab (typeof(Settings), "settings", "Settings", Resource.Drawable.ic_tab_settings);

        }

        private void CreateTab(Type activityType, string tag, string label, int drawableId )
        {
        var intent = new Intent(this, activityType);
        intent.AddFlags(ActivityFlags.NewTask);

        var spec = TabHost.NewTabSpec(tag);
        var drawableIcon = Resources.GetDrawable(drawableId);
        spec.SetIndicator(label, drawableIcon);
        spec.SetContent(intent);

        TabHost.AddTab(spec);
        }
    }

    [Activity (Label = "Home")]         
    public class Home : Activity
    {
    protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
        TextView textview = new TextView (this);
        textview.Text = "This is the Main tab";
        SetContentView (textview);
    }
    }

    [Activity (Label = "Calendar")]         
    public class Calendar : Activity
    {
    protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
        TextView textview = new TextView (this);
        textview.Text = "This is the Calendar tab";
        SetContentView (textview);
    }
    }

    [Activity (Label = "Input")]            
    public class Input : Activity
    {
    protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
        TextView textview = new TextView (this);
        textview.Text = "This is the Input tab";
        SetContentView (textview);
    }
    }

    [Activity (Label = "Settings")]
    [MetaData(PreferenceManager.MetadataKeyPreferences, Resource = "@drawable/preferences")]
    public class Settings : Activity
    {
    protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
    }
    }

    public class SettingsFragment : PreferenceFragment
    {
    public override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
        var intent = new Intent (this.Activity, typeof(Settings));
        AddPreferencesFromIntent (intent);
    }
    }

}

I am unable to add my XML code for some reason.. I've tried adding spaces in front of it?

Sign In or Register to comment.