Download Code

Xamarin Preferences Example Android

In this tutorial you will learn how to create xamarin preferences example android application.Let me give you some introduction about preferences.

Xamarin Preferences

Mostly android applications have setting option which includes push notification enable or disable,sound enable or disable.So for that purpose you can use Preferenc API which is most common feature in android application to save user settings.

Now create new android application project.File > New > Solution > Android App.

After creating,go to your Resources directory right click and create new folder,named(xml).

Now Right click on XML Folder and add new xml file(codec.xml).

Some preferences setting using preference Screen.

Codec.XML layout have three preference elements like edit text,list and a check box.Then your xml will look like this.

xamarin preferences,xamarin preferences example,xamarin preferences example android,preferences example android,preference manager,

 

Three attribute you have just used.

  • key( refering id ).
  • title( Title of Element ).
  • summary( Description what Element have).

Now next you will list items that you haven’t added yet.

So for that go ahead inside Resources > values.Right click and create new xml file named “array.xml” with Resource type like this.

xamarin preferences,xamarin preferences example,xamarin preferences example android,preferences example android,preference manager,

Next you will attach these things into preference layout so for that go to your codec.xml layout and within the list preference add these two lines for entries and entry values.

Remember both should be present if any one of them is absent your application will crashed.

xamarin preferences,xamarin preferences example,xamarin preferences example android,preferences example android,preference manager,

Next go to your MainActivity.cs class and set layout.

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Preferences;
using Android.Content;
using System;

namespace XamarinPreference
{
	[Activity (Label = "XamarinPreference", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : PreferenceActivity
	{
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			AddPreferencesFromResource (Resource.Xml.codec); // preferenceActivity method where we refer preference layout
		}
	}
}

Next you will see how to read data from Preference using ISharedPreference and Preferences Manager.

Shared Preference

Shared preference basically used to access and modify preference for user setting or according to the user setting.

Preferences Manager

Preferences Manager to handle access or anything thing else.It handle how to create preference hierarchies from activities.

So you will use shared preferences variable to access preference by using Preference Manager and then display in a Toast.Then your final Code will look like this.

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Preferences;
using Android.Content;
using System;

namespace XamarinPreference
{
	[Activity (Label = "XamarinPreference", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : PreferenceActivity
	{
		

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			AddPreferencesFromResource (Resource.Xml.codec); // preferenceActivity method where we refer preference layout
			ISharedPreferences d = PreferenceManager.GetDefaultSharedPreferences (this); // getting access from Preference manager
			String data = d.GetString("name","Android Codec");
			Toast.MakeText (this, data, ToastLength.Short).Show (); 


		}
	}
}

Now run your Xamarin Preferences example android application project.

xamarin preferences,xamarin preferences example,xamarin preferences example android,preferences example android,preference manager,

that’s it for Xamarin Preferences example android tutorial.I hope you got the idea.

Thanks.