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
- 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;
- namespace SharedPreference
- {
- public class AppPreferences
- {
- private ISharedPreferences nameSharedPrefs;
- private ISharedPreferencesEditor namePrefsEditor;
- private Context mContext;
- private static String PREFERENCE_ACCESS_KEY = "PREFERENCE_ACCESS_KEY";
- public static String NAME = "NAME";
- public AppPreferences(Context context)
- {
- this.mContext = context;
- nameSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);
- namePrefsEditor = nameSharedPrefs.Edit();
- }
- public void saveAccessKey(string key)
- {
- namePrefsEditor.PutString(PREFERENCE_ACCESS_KEY, key);
- namePrefsEditor.Commit();
- }
- public string getAccessKey()
- {
- 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);
-
-
-
- SetContentView(Resource.Layout.Main);
-
-
-
-
-
- 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)
-
- {
-
-
-
- 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)
- {
-
-
-
- Context mContext = Android.App.Application.Context;
-
- AppPreferences ap = new AppPreferences(mContext);
-
- string key = ap.getAccessKey();
-
- txtview.Text = key.ToString();
-
- }
Step 9 - Next to create Reset Button Event, this event would work with remove stored value in specified ACCESS_KEY.
C# Code
- private void Btnreset_Click(object sender, EventArgs e)
- {
- ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences(this);
- ISharedPreferencesEditor editer = pref.Edit();
- editer.Remove("PREFERENCE_ACCESS_KEY").Commit();
- }
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.