Android RatingBar example

In this Android tip, I am going to show you how to RatingBar widget. RatingBar shows a rating in stars. You can specify the number of stars to be displayed on the rating bar. You can touch or drag the starts to make the rating. You should note that you can touch or drag the stars when using the default rating bar style. The smaller rating bar style ( ratingBarStyleSmall ) and the rating bar indicator style ( ratingBarStyleIndicator ) do not allow user interactions. To use the smaller rating bar or rating bar indicator style, you add the style attribute to the RatingBar widget in xml file.

style="?android:attr/ratingBarStyleSmall"
style="?android:attr/ratingBarStyleIndicator"

To get the rating value from the rating bar, you need to implement the OnRatingBarChangeListener interface and override the onRatingChanged() method. In the example application, the rating value is displayed in a TextView when the user touched or dragged the starts.
Now to have an application on RatingBar example, you create a new Android project. Then modify the activity_main.xml file to add a RatingBar and a TextView.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.checkfileexists.MainActivity"
   android:orientation="vertical"
   >
  
   <RatingBar
     android:id="@+id/ratingBar"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:numStars="5"
     android:stepSize="1.0"
     android:rating="2.0"      
    
  
     />
  
   <TextView
     android:id="@+id/txtRatingValue"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
/>

</LinearLayout>


Then, in the MainActivity class that hosts the RatingBar, you need to implement the OnRatingBarChangeListener and override the onRatingChanged() method to get the rating value when the user touched or dragged the stars.

package com.example.androidexamples;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnRatingBarChangeListener{
private TextView txtValue;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     txtValue = (TextView) findViewById(R.id.txtRatingValue);
     RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
     ratingBar.setOnRatingBarChangeListener(this);
          
   }
  
   public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

txtValue.setText(String.valueOf(rating));

}
  
}

Android-RatingBar-example

Posted by: Dara | post date: 11-10-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.