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 the Android RatingBar class returns the rating number.
Create a 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. <RatingBar
  8. android:id="@+id/ratingBar1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentTop="true"
  12. android:layout_centerHorizontal="true"
  13. android:layout_marginTop="44dp" />
  14. <Button
  15. android:id="@+id/button1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_alignLeft="@+id/ratingBar1"
  19. android:layout_below="@+id/ratingBar1"
  20. android:layout_marginLeft="92dp"
  21. android:layout_marginTop="66dp"
  22. android:text="submit" />
  23. </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. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.RatingBar;
  9. 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. public void addListenerOnButtonClick(){
  11. ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);
  12. button=(Button)findViewById(R.id.button1);
  13. button.setOnClickListener(new OnClickListener(){
  14. @Override
  15. public void onClick(View arg0) String rating=String.valueOf(ratingbar1.getRating());
  16. Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
  17. }
  18. });
  19. }
  20. @Override
  21. public boolean onCreateOptionsMenu(Menu menu) {
  22. getMenuInflater().inflate(R.menu.activity_main, menu);
  23. return true;
  24. }
  25. }
Debug The Error
Now, go to the menu bar and click make a 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