Introduction

This application is to create and maintain Session Values in Xamarin Android.

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Android.App;
  6. using Android.Content;
  7. using Android.OS;
  8. using Android.Runtime;
  9. using Android.Views;
  10. using Android.Widget;
  11. using Android.Preferences; //Add New NameSpace
  12. namespace SharedPreference
  13. {
  14. public class AppPreferences
  15. {
  16. private ISharedPreferences nameSharedPrefs;
  17. private ISharedPreferencesEditor namePrefsEditor; //Declare Context,Prefrences name and Editor name
  18. private Context mContext;
  19. private static String PREFERENCE_ACCESS_KEY = "PREFERENCE_ACCESS_KEY"; //Value Access Key Name
  20. public static String NAME = "NAME"; //Value Variable Name
  21. public AppPreferences(Context context)
  22. {
  23. this.mContext = context;
  24. nameSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);
  25. namePrefsEditor = nameSharedPrefs.Edit();
  26. }
  27. public void saveAccessKey(string key) // Save data Values
  28. {
  29. namePrefsEditor.PutString(PREFERENCE_ACCESS_KEY, key);
  30. namePrefsEditor.Commit();
  31. }
  32. public string getAccessKey() // Return Get the Value
  33. {
  34. return nameSharedPrefs.GetString(PREFERENCE_ACCESS_KEY, "");
  35. }
  36. }
  37. }

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:text="Enter Name"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/Text" />
  11. <EditText
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/txtsave" />
  15. <Button
  16. android:text="Save"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:id="@+id/btnsave" />
  20. <TextView
  21. android:text="GET SAVED NAME"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:id="@+id/textView2" />
  25. <TextView
  26. android:text=""
  27. android:textAppearance="?android:attr/textAppearanceLarge"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:id="@+id/txtgetname" />
  31. <Button
  32. android:text=" GET STORED DATA"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:id="@+id/btnget" />
  36. <Button
  37. android:text="Delete Preferences"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:id="@+id/btnreset" />
  41. </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

  1. using System;
  2. using Android.App;
  3. using Android.Content;
  4. using Android.Runtime;
  5. using Android.Views;
  6. using Android.Widget;
  7. using Android.OS;
  8. using Android.Preferences;
  9. namespace SharedPreference
  10. {
  11. [Activity(Label = "SharedPreference", MainLauncher = true, Icon = "@drawable/icon")]
  12. public class MainActivity : Activity
  13. {
  14. Button btnsave;
  15. Button btnget;
  16. Button btnreset;
  17. EditText txtname;
  18. TextView txtview;
  19. protected override void OnCreate(Bundle bundle)
  20. {
  21. base.OnCreate(bundle);
  22. // Set our view from the "main" layout resource
  23. SetContentView(Resource.Layout.Main);
  24. // Get our button from the layout resource,
  25. // and attach an event to it
  26. btnsave = FindViewById<Button>(Resource.Id.btnsave);
  27. btnget = FindViewById<Button>(Resource.Id.btnget);
  28. btnreset = FindViewById<Button>(Resource.Id.btnreset);
  29. txtname = FindViewById<EditText>(Resource.Id.txtsave);
  30. txtview = FindViewById<TextView>(Resource.Id.txtgetname);
  31. btnsave.Click += Btnsave_Click;
  32. btnget.Click += Btnget_Click;
  33. btnreset.Click += Btnreset_Click;
  34. }
  35. }

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

  1. private void Btnsave_Click(object sender, EventArgs e)
  2. {
  3. //Save Sessionvalue
  4. Context mContext = Android.App.Application.Context;
  5. AppPreferences ap = new AppPreferences(mContext);
  6. ap.saveAccessKey(txtname.Text);
  7. txtname.Text = "";
  8. }

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

  1. private void Btnget_Click(object sender, EventArgs e)
  2. {
  3. //Get Sessionvalue
  4. Context mContext = Android.App.Application.Context;
  5. AppPreferences ap = new AppPreferences(mContext);
  6. string key = ap.getAccessKey();
  7. txtview.Text = key.ToString();
  8. }
Step 9 - Next to create Reset Button Event, this event would work with remove stored value in specified ACCESS_KEY.

C# Code
  1. private void Btnreset_Click(object sender, EventArgs e)
  2. {
  3. ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences(this);
  4. ISharedPreferencesEditor editer = pref.Edit();
  5. editer.Remove("PREFERENCE_ACCESS_KEY").Commit(); ////Remove Spec key values
  6. }

Step 10 - Press F5 or Build and Run the Application.

Click Delete Preferences button to remove Session values.

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