Scotty Laughton
Scotty Laughton

Reputation: 53

How to create an app setting in an xamarin android app that shows in the Android Settings/Apps/App Setting for that app?

I have been over the documentation here: https://developer.android.com/guide/topics/ui/settings

The above page talks about creating a settings UI in your app, and shows using an XML file (that it doesn't tell you where to put by the way).

I do not want to add a settings UI in my app. I just want a setting to appear in android settings app under Apps when you pick my app from that list, it already has a section called "App settings". I want to add a setting for my app here, and be able to read it from my app. (Would be a bonus to be able to write to it from my app as well.)

Is this possible to do this and if so, can someone point me to an example.

Thanks for your time.

Upvotes: 2

Views: 1807

Answers (2)

Scotty Laughton
Scotty Laughton

Reputation: 53

I ended up going with Xamarin.Essentials: Preferences as this was the closest I could find to what I wanted and I didn't have to have a settings UI in the app.

https://learn.microsoft.com/en-us/xamarin/essentials/preferences?context=xamarin%2Fandroid&tabs=android

Upvotes: 1

Leon Lu
Leon Lu

Reputation: 9234

If you want to achieve that, you should add following nuget package firstly.

Xamarin.Android.Support.v7.Preference

Then create xml folder, Add the preferences.xml.

enter image description here

Here is code add preferences.xml for testing.

<PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">

  <SwitchPreferenceCompat
      app:key="notifications"
      app:title="Enable message notifications"/>

  <Preference
      app:key="feedback"
      app:title="Send feedback"
      app:summary="Report technical issues or suggest new features"/>

</PreferenceScreen> 

Then create a class called MySettingsFragment.cs, PreferenceFragmentCompat comes from Android.Support.V7.Preferences, populator the preferences.xml by SetPreferencesFromResource method.

using Android.OS;
using Android.Runtime;
using Android.Support.V7.Preferences;
using Android.Views;
using Android.Widget;

namespace App32
{
    public class MySettingsFragment : PreferenceFragmentCompat
    {
        public override void OnCreatePreferences(Bundle savedInstanceState, string rootKey)
        {
            SetPreferencesFromResource(Resource.Xml.preferences, rootKey);

        }
    }
}

In the end, we can add a FrameLayout in your layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/settings_container"/>
</RelativeLayout>

In the activity to use MySettingsFragment like Fragment's transaction.

    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            SupportFragmentManager.BeginTransaction().Replace(Resource.Id.settings_container,new MySettingsFragment()).Commit();

        }
}
}

Here is running sceenshot.

enter image description here

Upvotes: 3

Related Questions