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. <TextView
  12. android:layout_height="wrap_content"
  13. android:layout_width="wrap_content"
  14. android:text="RATINGBAR"
  15. android:textColor="#000000"
  16. android:textStyle="bold"
  17. android:textSize="25dp"
  18. android:layout_centerHorizontal="true"/>
  19. <Button
  20. android:layout_width="150dp"
  21. android:layout_height="70dp"
  22. android:text="Button"
  23. android:id="@+id/button"
  24. android:textSize="30dp"
  25. android:layout_centerInParent="true"
  26. />
  27. <RatingBar
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_marginTop="50dp"
  31. android:id="@+id/ratingBar"
  32. android:layout_centerHorizontal="true">
  33. </RatingBar>
  34. </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. public class MainActivity extends Activity {
  10. Button button;
  11. RatingBar ratingBar;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. //Create id of button and RatingBar
  17. button=(Button)findViewById(R.id.button);
  18. ratingBar=(RatingBar)findViewById(R.id.ratingBar);
  19. //perform click event on button
  20. button.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. String rating= String.valueOf(ratingBar.getRating());
  24. Toast.makeText(getApplicationContext(),rating,Toast.LENGTH_LONG).show();
  25. }
  26. });
  27. }
  28. @Override
  29. public boolean onCreateOptionsMenu(Menu menu) {
  30. // Inflate the menu; this adds items to the action bar if it is present.
  31. getMenuInflater().inflate(R.menu.main, menu);
  32. return true;
  33. }
  34. }
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. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.ratingbarexample.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 5
Rating Bar with no star selected
imageentervalue
Rating bar with 2.5 stars selected
image which show data