PreferenceScreen does not work

VINNUSAURUSVINNUSAURUS Member ✭✭
edited April 15 in Xamarin.Android

i want a PreferenceScreen in my app, i have added the following code but app loads without inflating PreferenceScreen.

<?xml version="1.0" encoding="utf-8" ?> 
<android.support.v7.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto">
  <android.support.v7.preference.SwitchPreferenceCompat
            android:key="key1"
            android:title="Switch Preference"
            android:summary="Switch Summary"
            android:defaultValue="true" />
    </android.support.v7.preference.PreferenceScreen>

Preference Fragment:

 public class Preference : PreferenceFragment
    {

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            AddPreferencesFromResource(Resource.Layout.preferences);
        }

    }

Preference Activity:

  public class SettingsActivity : PreferenceActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (savedInstanceState != null)
            {
                FragmentManager.BeginTransaction().Add(Resource.Id.fragmentLayout, new Preference()).Commit();
            }

        }
    }

App loads but PreferenceScreen is not visible, when using the PreferenceScreen in axml Layout it gives error class not found.

Answers

  • GrahamMcKechnieGrahamMcKechnie AUMember ✭✭

    Should probably be using PreferenceFragmentCompat with V7.Preferences.

  • VINNUSAURUSVINNUSAURUS Member ✭✭
    edited April 16

    @GrahamMcKechnie said:
    Should probably be using PreferenceFragmentCompat with V7.Preferences.

    Tried changing to PreferenceFragmentCompat, same blank screen

  • VINNUSAURUSVINNUSAURUS Member ✭✭

    @GrahamMcKechnie said:
    Should probably be using PreferenceFragmentCompat with V7.Preferences.

    cant find PreferenceScreen ?

  • GrahamMcKechnieGrahamMcKechnie AUMember ✭✭

    I don't know what happened to your other reply, but yes you will also need to use SupportFragmentManager

  • VINNUSAURUSVINNUSAURUS Member ✭✭

    @GrahamMcKechnie said:
    I don't know what happened to your other reply, but yes you will also need to use SupportFragmentManager

    tried to use it but it gives red lines and no suggestion for the namespace i should use for it.

  • GrahamMcKechnieGrahamMcKechnie AUMember ✭✭

    The red lines are telling you something!!!

    The namespace you are missing is Android.Support.V4.App

    Have you researched how to use V7.Preferences???

    It is totally different compared to the previous preference system. If you want to support Pie and Q you have no choice because the old stuff is deprecated.

    Try the following as a starting tutorial. https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-1-5959aa49337c. It's a 4 part tutorial on V7.Preferences, but there are also plenty of others out there discussing V7.Preferences. I'd suggest, after reading the first two tutorials, building the TimePreference he presents in tutorial 3 using Android Studio. Once you have it working on a device, then convert it to C# and build using Xamarin. It is a great excuse to install Android Studio if you haven't already. Once you get to use their window designer and ConstraintLayouts there is no going back to Xamarin's Android Designer other than to copy and paste from AS.

    Google's docs are also a good starting point for the simple stuff. https://developer.android.com/guide/topics/ui/settings.html. These docs refer to Android.JetPack which you can ignore for the moment because Xamarin hasn't got to that yet, which will probably be the next pita as we move towards Q.

  • VINNUSAURUSVINNUSAURUS Member ✭✭

    @GrahamMcKechnie said:
    The red lines are telling you something!!!

    The namespace you are missing is Android.Support.V4.App

    Have you researched how to use V7.Preferences???

    It is totally different compared to the previous preference system. If you want to support Pie and Q you have no choice because the old stuff is deprecated.

    Try the following as a starting tutorial. https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-1-5959aa49337c. It's a 4 part tutorial on V7.Preferences, but there are also plenty of others out there discussing V7.Preferences. I'd suggest, after reading the first two tutorials, building the TimePreference he presents in tutorial 3 using Android Studio. Once you have it working on a device, then convert it to C# and build using Xamarin. It is a great excuse to install Android Studio if you haven't already. Once you get to use their window designer and ConstraintLayouts there is no going back to Xamarin's Android Designer other than to copy and paste from AS.

    Google's docs are also a good starting point for the simple stuff. https://developer.android.com/guide/topics/ui/settings.html. These docs refer to Android.JetPack which you can ignore for the moment because Xamarin hasn't got to that yet, which will probably be the next pita as we move towards Q.

    Thank you i will check it and revert back :)

Sign In or Register to comment.