Introduction
Let’s start
Step 1
Open Visual Studio, New Project, Templates, Visual C#, Android, Blank App, Select Blank App (Android). Then give Project Name and Project Location.

Step 2 - Next go to Solution Explorer-> Project Name then Right Click to Add->Class and open new Dialog box.

Step 3 - Dialog box to select Class and give the Name AppPreferences.cs,

Step 4 - Next, Open Solution Explorer, Project Name, Resources, AppPreferences.cs. Click to open the following code.

C# Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.Preferences; //Add New NameSpace
- namespace SharedPreference
- {
- public class AppPreferences
- {
- private ISharedPreferences nameSharedPrefs;
- private ISharedPreferencesEditor namePrefsEditor; //Declare Context,Prefrences name and Editor name
- private Context mContext;
- private static String PREFERENCE_ACCESS_KEY = "PREFERENCE_ACCESS_KEY"; //Value Access Key Name
- public static String NAME = "NAME"; //Value Variable Name
- public AppPreferences(Context context)
- {
- this.mContext = context;
- nameSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);
- namePrefsEditor = nameSharedPrefs.Edit();
- }
- public void saveAccessKey(string key) // Save data Values
- {
- namePrefsEditor.PutString(PREFERENCE_ACCESS_KEY, key);
- namePrefsEditor.Commit();
- }
- public string getAccessKey() // Return Get the Value
- {
- return nameSharedPrefs.GetString(PREFERENCE_ACCESS_KEY, "");
- }
- }
- }
Step 5
Then Open Solution Explorer, Project Name, Resources, layout, Main.axml, then click open Design View. Select Toolbar, then Drag and Drop design of Buttons, TextView, PlainText.

XML Code
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:text="Enter Name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/Text" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtsave" />
- <Button
- android:text="Save"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnsave" />
- <TextView
- android:text="GET SAVED NAME"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView2" />
- <TextView
- android:text=""
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtgetname" />
- <Button
- android:text=" GET STORED DATA"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnget" />
- <Button
- android:text="Delete Preferences"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnreset" />
- </LinearLayout>
Step 6
Then open Solution Explorer, Project Name, MainActivity.cs. Click to open C# Code page. Then go to section OnCreate() and declare create Button, TextView and EditText Events.

C# Code
- using System;
- using Android.App;
- using Android.Content;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using Android.Preferences;
- namespace SharedPreference
- {
- [Activity(Label = "SharedPreference", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- Button btnsave;
- Button btnget;
- Button btnreset;
- EditText txtname;
- TextView txtview;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- // Get our button from the layout resource,
- // and attach an event to it
- btnsave = FindViewById<Button>(Resource.Id.btnsave);
- btnget = FindViewById<Button>(Resource.Id.btnget);
- btnreset = FindViewById<Button>(Resource.Id.btnreset);
- txtname = FindViewById<EditText>(Resource.Id.txtsave);
- txtview = FindViewById<TextView>(Resource.Id.txtgetname);
- btnsave.Click += Btnsave_Click;
- btnget.Click += Btnget_Click;
- btnreset.Click += Btnreset_Click;
- }
- }
Step 7
Next to create Save Button Event, save the entered text in textbox values. It is the value to maintain Session of AppPreferences.

C# Code
- private void Btnsave_Click(object sender, EventArgs e)
- {
- //Save Sessionvalue
- Context mContext = Android.App.Application.Context;
- AppPreferences ap = new AppPreferences(mContext);
- ap.saveAccessKey(txtname.Text);
- txtname.Text = "";
- }
Step 8
Next to create Get Button Event, this event work to retrieve the AppPreferences stored value. After Retrieve Value it will display in TextView.

C# Code
- private void Btnget_Click(object sender, EventArgs e)
- {
- //Get Sessionvalue
- Context mContext = Android.App.Application.Context;
- AppPreferences ap = new AppPreferences(mContext);
- string key = ap.getAccessKey();
- txtview.Text = key.ToString();
- }

- private void Btnreset_Click(object sender, EventArgs e)
- {
- ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences(this);
- ISharedPreferencesEditor editer = pref.Edit();
- editer.Remove("PREFERENCE_ACCESS_KEY").Commit(); ////Remove Spec key values
- }
Step 10 - Press F5 or Build and Run the Application.


Finally, we successfully created Xamarin Android SharedPreferences(AppPreferences) Application.

Rupesh KahanePosted Nov 25, 2016, 5:20 AM
Anbu Mani Thanks for sharing great article... I have one question for you. Is it possible to give Session Timeout so after login session will automatically log out ?
Prasanna MuraliPosted May 20, 2016, 9:32 PM
Nice one...
Neeraj KumarPosted May 7, 2016, 11:41 AM
Nice Article...
Vignesh ManiPosted May 6, 2016, 5:01 PM
good one
Kuppurasu NagarajPosted May 6, 2016, 9:10 AM
Nice Sharing..
Sr KarthigaPosted May 6, 2016, 3:36 AM
good one