RatingBar Android App Using Android Studio

Introduction

Android RatingBar can be used to get a rating from the user. The Rating returns a floating-point number. It may be 2.0, 3.5, 4.0 etc. Android RatingBar displays the rating in stars. In this article, I will show you how to create a RatingBar Android App using Android Studio.

Requirements

Important Points to Remember

The getRating() method of Android RatingBar class returns the rating number.

Create New Project

Open Android Studio and create a New Android Studio Project.

RatingBar Android App

activity_main.xml

Drag the RatingBar and Button from the palette now the activity_main.xml file will look like this,

  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <RatingBar  
  9.         android:id="@+id/ratingBar1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_centerHorizontal="true"  
  14.         android:layout_marginTop="44dp" />  
  15.   
  16.     <Button  
  17.         android:id="@+id/button1"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignLeft="@+id/ratingBar1"  
  21.         android:layout_below="@+id/ratingBar1"  
  22.         android:layout_marginLeft="92dp"  
  23.         android:layout_marginTop="66dp"  
  24.         android:text="submit" />  
  25.   
  26. </RelativeLayout>  

RatingBar Android App

Activity class

Let's write the code to display the rating of the user.

MainActivity.java

The Header File

RatingBar Android App

  1. package abu.ratingbar;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.RatingBar;  
  10. import android.widget.Toast;   

Declare the Button with Rating Bar,

RatingBar Android App

  1.  public class MainActivity extends Activity {  
  2.     RatingBar ratingbar1;  
  3.     Button button;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);  
  8.         addListenerOnButtonClick();  
  9.     }  
  10.   
  11.     public void addListenerOnButtonClick(){  
  12.         ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);  
  13.         button=(Button)findViewById(R.id.button1);  
  14.          
  15.         button.setOnClickListener(new OnClickListener(){  
  16.   
  17.             @Override  
  18.             public void onClick(View arg0)                 String rating=String.valueOf(ratingbar1.getRating());  
  19.                 Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  
  20.             }  
  21.   
  22.         });  
  23.     }  
  24.     @Override  
  25.     public boolean onCreateOptionsMenu(Menu menu) {  
  26.   
  27.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  28.         return true;  
  29.     }  
  30.   
  31. }  

Debug The Error

Now, go to the menu bar and click make project or press ctrl+f9 to debug the error.

RatingBar Android App

Run the project

Then click Run button or press shift+f10 To run the project. And choose the virtual machine and click OK.

RatingBar Android App

Output

We have successfully created a RatingBar Android application using Android Studio.

RatingBar Android App

RatingBar Android App

Up Next
    Ebook Download
    View all
    Learn
    View all