How To Use SeekBar In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS).

In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

SeekBar control is visually similar to the ProgressBar but it has a slider, which can be dragged and it will allow the user to change the value displayed by the control.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below are required to be followed in order to use SeekBar in Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).

Xamarin

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Xamarin

Step 3

Go to Solution Explorer. in Solution Explorer, get all the files and source in your project. Select Resource-->Layout-->double click to open main.axml page. To write XAML code, you need to select the source.

Choose the Designer Window, if you want design and you can design your app.

Xamarin

Step 4

After opening main.axml, file will open the main page designer. You can design this page, as per your desire.

Xamarin

Delete the Default hello world button, go to the source panel you and you can see the button coding. You need to delete it. After deleting XAML code, now delete C# button action code.Go to the MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls. You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls. You need to drag and drop the SeekBar.

Xamarin

Step 6

You need to drag and drop the TextView.

Xamarin

Step 7

Now, go to the properties Window. You need to edit the SeekBar Id Value(EX: android:id="@+id/seekbar" ).

Xamarin

Step 8

Also, edit the TextView's Id value(Ex: android:id="@+id/textview" ).

Xamarin

Step 9

In this step, go to the Main.axml page Source Panel. Note, the SeekBar's Id value.and also note TextView's Id value.

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.     <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekbar" />  
  3.     <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview" />  
  4. </LinearLayout>  
Xamarin

Step 10

In this step, go to MainActivity.cs page in Solution Explorer. Add two variables and write the code mentioned below from OnCreate() Method.

MainActivity.cs
  1. public class MainActivity: Activity {  
  2.         //Variables  
  3.         SeekBar _seekBar;  
  4.         TextView _textView;  
  5.         protected override void OnCreate(Bundle bundle) {  
  6.             base.OnCreate(bundle);  
  7.             // Set our view from the "main" layout resource  
  8.             SetContentView(Resource.Layout.Main);  
  9.             _seekBar = FindViewById < SeekBar > (Resource.Id.seekbar);  
  10.             _textView = FindViewById < TextView > (Resource.Id.textview);  
  11.             _seekBar.ProgressChanged += (object sender, SeekBar.ProgressChangedEventArgs e) => {  
  12.                 if (e.FromUser) {  
  13.                     _textView.Text = string.Format("SeekBar Value is {0}", e.Progress);  
  14.                 }  
  15.             };  
  16.         }  
  17.     }  
  18.     // This is just a sample script. Paste your real code (javascript or HTML) here.  
  19.   
  20. if ('this_is' == /an_example/) {  
  21.     of_beautifier();  
  22. else {  
  23.     var a = b ? (c % d) : e[f];  
  24. }  
Xamarin

Step 11

If you have an Android virtual device, run the app on it, else, connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu

(Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Xamarin

Output

After few seconds, the app will start running on your phone. You will see SeekBar and slide value is working successfully.

Xamarin

Xamarin

Summary

This was the process of how to use SeekBar in Xamarin Android app, using Visual Studio 2015.


Similar Articles