Rating Bar in Android

Introduction

 
This article explains the RatingBar in Android. Android Studio is used to create the sample.
 
A RatingBar shows a rating. This application will show you the rating as a toast notification depending on the number of stars clicked on the RatingBar.
To get a rating you will use "getRating()".
 
The "getRating()" method returns the rating as a float.
 
Step 1
 
Create a project like this:
 
firstactivity
 
Step 2
 
Create an XML file with the following.
 
In this XML file, you will use a Button, RatingBar and TextView. The rating will show the rating as a toast Notification.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#524ff0">  
  11.    
  12.     <TextView  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_width="wrap_content"  
  15.         android:text="RATINGBAR"  
  16.         android:textColor="#000000"  
  17.         android:textStyle="bold"  
  18.         android:textSize="25dp"  
  19.         android:layout_centerHorizontal="true"/>  
  20.     <Button  
  21.         android:layout_width="150dp"  
  22.         android:layout_height="70dp"  
  23.         android:text="Button"  
  24.         android:id="@+id/button"  
  25.         android:textSize="30dp"  
  26.         android:layout_centerInParent="true"  
  27.         />  
  28.    
  29.     <RatingBar  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_marginTop="50dp"  
  33.         android:id="@+id/ratingBar"  
  34.         android:layout_centerHorizontal="true">  
  35.    
  36.         </RatingBar>  
  37.    
  38. </RelativeLayout> 
Step 3
 
Create a Java file with the following.
 
In this, you will create the id of the RatingBar and button. Call the getRating() method that returns the rating that is of float type. Set the button on its click listener and write the code for the toast notification that will show you the rating as a PopUp message.
  1. package com.ratingbarexample;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.RatingBar;  
  8. import android.widget.Toast;  
  9.    
  10. public class MainActivity extends Activity {  
  11.    
  12.   Button button;  
  13.     RatingBar ratingBar;  
  14.    
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.    
  20.         //Create id of button and RatingBar  
  21.         button=(Button)findViewById(R.id.button);  
  22.         ratingBar=(RatingBar)findViewById(R.id.ratingBar);  
  23.    
  24.         //perform click event on button  
  25.    
  26.         button.setOnClickListener(new View.OnClickListener() {  
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                String rating= String.valueOf(ratingBar.getRating());  
  30.                 Toast.makeText(getApplicationContext(),rating,Toast.LENGTH_LONG).show();  
  31.             }  
  32.         });  
  33.     }  
  34.     @Override  
  35.     public boolean onCreateOptionsMenu(Menu menu) {  
  36.         // Inflate the menu; this adds items to the action bar if it is present.  
  37.         getMenuInflater().inflate(R.menu.main, menu);  
  38.         return true;  
  39.     }     
Step 4
 
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.ratingbarexample"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.ratingbarexample.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.  </manifest> 
Step 5
 
Rating Bar with no star selected
 
imageentervalue
 
Rating bar with 2.5 stars selected
 
image which show data


Similar Articles