Create A RatingBar In Android App Using Android Studio

Introduction

 
In this article I will show you how to develop a RatingBar app in an Android application, using Android Studio.
 
Requirements
  • Android Studio 2.1.3
If you want to create a RatingBar app, you should follow the steps given below.
 
Step 1
 
Now, open Android Studio and you can choose the file and subsequently New. Afterward,  choose New Project.
 
android
 
Step 2
 
Here, you can create your application name and choose where your project is stored on the location and click the NEXT button.
 
android
 
Now, we can select the version of Android; it is Target Android Devices.
 
android
 
Step 3
 
Here, we can add the activity and click the Next button.
 
android
 
Now, we can write the activity name and click the Finish button.
 
android
 
Step 4
 
Now, open your project and you will go to activity_main.xml and afterward,  you will build the design. You should choose toolbox and if you want some options (RatingBar, button), and use the drag and drop method.
 
android
 
Now, we can see the Graphical User Interface design.
 
android
 
Step 5
 
Here, you need to build on the design and write the.XML code.
 
activity_mai.xml code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.ratingbarapp.MainActivity">  
  3.     <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ratingBarbttn" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" />  
  4.     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SUBMIT" android:id="@+id/subbttn" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />  
  5. </RelativeLayout>  
Step 6
 
Now, you will go to the MainActivity.java page and build Java code.
 
First of all, you will declare a file, which is an extension file.
 
android
 
Now, we can see the MainActivity.java code.
  1. package xyz.rvconstructions.www.ratingbarapp;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.view.Menu;  
  5. import android.view.MenuItem;  
  6. import android.view.View;  
  7.   
  8. import android.widget.RatingBar;  
  9. import android.widget.Button;  
  10. import android.widget.Toast;  
  11.   
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         final RatingBar oneRatingBar = (RatingBar) findViewById(R.id.ratingBarbttn);  
  19.         Button submitButton = (Button) findViewById(R.id.subbttn);  
  20.         submitButton.setOnClickListener(new View.OnClickListener() {  
  21.             @Override  
  22.             public void onClick(View v) {  
  23.                 String totalStars = "Total Stars:: " + oneRatingBar.getNumStars();  
  24.                 String rating = "Rating :: " + oneRatingBar.getRating();  
  25.                 Toast.makeText(getApplicationContext(), totalStars + "\n" + rating, Toast.LENGTH_LONG).show();  
  26.             }  
  27.         });  
  28.     }  
  29. }  
Step 7
 
Here, you will go to run it and select Run-> Run app option.
 
android
 
Here, you will choose the Emulator or the devices; it is a Nokia Nokia _X.
 
android
 
Step 8
 
Here, you can see the output.
 
android
 
You will click the two stars and click submit. Afterward, you will get the rating.
 
android
 
You will click 3.5 stars and click submit. Afterwards, you will get the rating.
 
android


Similar Articles