A Rating Bar is used to show rating in Stars.The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar.
In this example I will show you how to implement RatingBar in Android.
Let’s get start by creating a project in Eclipse IDE.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java).
2. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i had taken three RatingBar and some textViews.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:padding="5dp" > <RatingBar android:id="@+id/ratingbar_default" style="?android:attr/ratingBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/ratingbar_default_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" /> <RatingBar android:id="@+id/getRatings" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="1.0" /> <RatingBar android:id="@+id/setRatings" style="?android:attr/ratingBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:isIndicator="true" android:numStars="5" android:stepSize="0.1" /> <TextView android:id="@+id/setcountText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" /> </LinearLayout> |
3. Now, come to MainActivity.java and add the following code. In this class for the first RatingBar i am showing the number of stars selected in textView and the next RatingBar is used to set the rating to third ratingbar when selected and a toast also displayed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
package com.android_ratingbar_demo; import java.text.DecimalFormat; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.widget.RatingBar; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private static RatingBar ratingbar_default, getRatingBar, setRatingBar; private static TextView ratingValue_default, setcountText; int count; float curRate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); setListeners(); } // Initializing the views void init() { ratingbar_default = (RatingBar) findViewById(R.id.ratingbar_default); getRatingBar = (RatingBar) findViewById(R.id.getRatings); setRatingBar = (RatingBar) findViewById(R.id.setRatings); ratingValue_default = (TextView) findViewById(R.id.ratingbar_default_number); setcountText = (TextView) findViewById(R.id.setcountText); } // Setting listeners on ratinbars void setListeners() { // Ratingbar default ratingbar_default .setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // Get currnet rating String ratedValue = String.valueOf(rating); // Get Number of stars int numStars = ratingBar.getNumStars(); // Now set the rated Value and numstars on textView ratingValue_default.setText("Rating value is : " + ratedValue + "/" + numStars); } }); // GetRatingBar getRatingBar .setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // Decimal format for converting all floats into decimal DecimalFormat decimalFormat = new DecimalFormat("#.#"); // Get Current rating curRate = Float.valueOf(decimalFormat.format((curRate * count + rating) / ++count)); // Showing a toast of current rating Toast.makeText(MainActivity.this, "New Rating: " + curRate, Toast.LENGTH_SHORT) .show(); // Set the current rating to setRatingBar setRatingBar.setRating(curRate); // Show the current count value setcountText.setText(count + " Ratings"); } }); } } |
4. Now, run the app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.