How To Use RatingBar 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. RatingBar is used to rate the app performance and quality based on user reviews to provide ratings.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below, are required to be followed in order to Use the RatingBar in the 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).



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.



Step 3

Next, go to the Solution Explorer and select Resource-->Layout-->double click to open main.axml page. 



Step 4

After opening, the main.axml file will open the main page designer. In this page, select the type which you want to design this page.



Next, delete the default "hello world" button from  source panel coding. And, delete the C# button action code by going to the MainActivity.cs page.

Step 5

Now, go to the toolbox Window. In the toolbox Window, scroll down and you will see all the tools and controls.

You need to drag and drop the RatingBar.



Step 6

Now, go to the properties window. You need to edit the RatingBar Id Value(EX: android:id="@+id/ratingbar").



Step 7

And also, edit the Ratingbar numStars value and stepSize value(E: android:numStars="5" android:stepSize="1.0").



Step 8

In this step, go to the Main.axml page Source Panel. Note, the RatingBar Id value.

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    android:orientation="vertical"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:minWidth="25px"  
  6.    android:minHeight="25px">  
  7. <RatingBar  
  8.    android:id="@+id/ratingbar"  
  9.    android:layout_width="wrap_content"  
  10.    android:layout_height="wrap_content"  
  11.    android:numStars="5"  
  12.    android:stepSize="1.0" />  
  13. </LinearLayout>  


Step 9

Next, go to the MainActivity.cs page from Solution Explorer. Write the following code between OnCreate() Method.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource  
  4.     SetContentView(Resource.Layout.Main);  
  5.     RatingBar ratingbar = FindViewById < RatingBar > (Resource.Id.ratingbar);  
  6.     ratingbar.RatingBarChange += (o, e) => {  
  7.         Toast.MakeText(this"Your Rating: " + ratingbar.Rating.ToString(), ToastLength.Short).Show();  
  8.     };  
  9. }  


Step 10

If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.

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.



Output

After a few seconds, the app will start running on your phone.

You will see that the RatingBar is work successfully.



You will give a rating. It will work successfully.





Summary

So, this was the process of Use the RatingBar in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all