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
- <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">
- <RatingBar
- android:id="@+id/ratingbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:numStars="5"
- android:stepSize="1.0" />
- </LinearLayout>
Step 9
Next, go to the MainActivity.cs page from Solution Explorer. Write the following code between OnCreate() Method.
MainActivity.cs
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- RatingBar ratingbar = FindViewById < RatingBar > (Resource.Id.ratingbar);
- ratingbar.RatingBarChange += (o, e) => {
- Toast.MakeText(this, "Your Rating: " + ratingbar.Rating.ToString(), ToastLength.Short).Show();
- };
- }
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.