Xamarin Android: Create Android ISharedPreferences App

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.  private ISharedPreferences nameSharedPrefs;  
  16.   private ISharedPreferencesEditor namePrefsEditor; //Declare Context,Prefrences name and Editor name  
  17.   private Context mContext;  
  18.   private static String PREFERENCE_ACCESS_KEY = "PREFERENCE_ACCESS_KEY"//Value Access Key Name  
  19.   public static String NAME = "NAME"//Value Variable Name  
  20.   public AppPreferences(Context context)  
  21.   {  
  22.   this.mContext = context;  
  23.   nameSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);  
  24.   namePrefsEditor = nameSharedPrefs.Edit();  
  25.   }  
  26.   public void saveAccessKey(string key) // Save data Values  
  27.   {  
  28.   namePrefsEditor.PutString(PREFERENCE_ACCESS_KEY, key);  
  29.   namePrefsEditor.Commit();  
  30.   }  
  31.   public string getAccessKey() // Return Get the Value  
  32.   {  
  33.   return nameSharedPrefs.GetString(PREFERENCE_ACCESS_KEY, "");  
  34.   }  
  35.   }  
  36.   }   

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.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5. android:orientation="vertical"  
  6.   
  7. android:layout_width="fill_parent"  
  8.   
  9. android:layout_height="fill_parent">  
  10.   
  11. <TextView  
  12.   
  13. android:text="Enter Name"  
  14.   
  15. android:layout_width="match_parent"  
  16.   
  17. android:layout_height="wrap_content"  
  18.   
  19. android:id="@+id/Text" />  
  20.   
  21. <EditText  
  22.   
  23. android:layout_width="match_parent"  
  24.   
  25. android:layout_height="wrap_content"  
  26.   
  27. android:id="@+id/txtsave" />  
  28.   
  29. <Button  
  30.   
  31. android:text="Save"  
  32.   
  33. android:layout_width="match_parent"  
  34.   
  35. android:layout_height="wrap_content"  
  36.   
  37. android:id="@+id/btnsave" />  
  38.   
  39. <TextView  
  40.   
  41. android:text="GET SAVED NAME"  
  42.   
  43. android:layout_width="match_parent"  
  44.   
  45. android:layout_height="wrap_content"  
  46.   
  47. android:id="@+id/textView2" />  
  48.   
  49. <TextView  
  50.   
  51. android:text=""  
  52.   
  53. android:textAppearance="?android:attr/textAppearanceLarge"  
  54.   
  55. android:layout_width="match_parent"  
  56.   
  57. android:layout_height="wrap_content"  
  58.   
  59. android:id="@+id/txtgetname" />  
  60.   
  61. <Button  
  62.   
  63. android:text=" GET STORED DATA"  
  64.   
  65. android:layout_width="match_parent"  
  66.   
  67. android:layout_height="wrap_content"  
  68.   
  69. android:id="@+id/btnget" />  
  70.   
  71. <Button  
  72.   
  73. android:text="Delete Preferences"  
  74.   
  75. android:layout_width="match_parent"  
  76.   
  77. android:layout_height="wrap_content"  
  78.   
  79. android:id="@+id/btnreset" />  
  80.   
  81. </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.   
  3. using Android.App;  
  4.   
  5. using Android.Content;  
  6.   
  7. using Android.Runtime;  
  8.   
  9. using Android.Views;  
  10.   
  11. using Android.Widget;  
  12.   
  13. using Android.OS;  
  14.   
  15. using Android.Preferences;  
  16.   
  17. namespace SharedPreference  
  18.   
  19. {  
  20.   
  21. [Activity(Label = "SharedPreference", MainLauncher = true, Icon = "@drawable/icon")]  
  22.   
  23. public class MainActivity : Activity  
  24.   
  25. {  
  26.   
  27. Button btnsave;  
  28.   
  29. Button btnget;  
  30.   
  31. Button btnreset;  
  32.   
  33. EditText txtname;  
  34.   
  35. TextView txtview;  
  36.   
  37. protected override void OnCreate(Bundle bundle)  
  38.   
  39. {  
  40.   
  41. base.OnCreate(bundle);  
  42.   
  43. // Set our view from the "main" layout resource  
  44.   
  45. SetContentView(Resource.Layout.Main);  
  46.   
  47. // Get our button from the layout resource,  
  48.   
  49. // and attach an event to it  
  50.   
  51. btnsave = FindViewById<Button>(Resource.Id.btnsave);  
  52.   
  53. btnget = FindViewById<Button>(Resource.Id.btnget);  
  54.   
  55. btnreset = FindViewById<Button>(Resource.Id.btnreset);  
  56.   
  57. txtname = FindViewById<EditText>(Resource.Id.txtsave);  
  58.   
  59. txtview = FindViewById<TextView>(Resource.Id.txtgetname);  
  60.   
  61. btnsave.Click += Btnsave_Click;  
  62.   
  63. btnget.Click += Btnget_Click;  
  64.   
  65. btnreset.Click += Btnreset_Click;  
  66.   
  67. }  
  68.   
  69. }   

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. {  
  4.   
  5. //Save Sessionvalue  
  6.   
  7. Context mContext = Android.App.Application.Context;  
  8.   
  9. AppPreferences ap = new AppPreferences(mContext);  
  10.   
  11. ap.saveAccessKey(txtname.Text);  
  12.   
  13. txtname.Text = "";  
  14.   
  15. }   

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.   
  4. //Get Sessionvalue  
  5.   
  6. Context mContext = Android.App.Application.Context;  
  7.   
  8. AppPreferences ap = new AppPreferences(mContext);  
  9.   
  10. string key = ap.getAccessKey();  
  11.   
  12. txtview.Text = key.ToString();  
  13.   
  14. }  
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.

Next Recommended Readings