Android RatingBar in Example

In the previous Android 3 rating bar example here, we created a simple rating bar and accepted user rating and displayed the rating as a Toast message. In this Android RatingBar tutorial, we will create a rating bar with indicator using android.widget.RatingBar defined in XML layout file and implement OnRatingBarChangeListener to accept user rating, calculate the total ratings and display it in indicator rating bar.

1. StaticRatingBar.java

package com.tutorialsee.ratingbar;
import android.os.Bundle;
import android.app.Activity;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class StaticRatingBar extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static_rating_bar);
       
        RatingBar rr=(RatingBar) findViewById(R.id.ratingBar1);
        final TextView t1=(TextView) findViewById(R.id.textView1);
       
        rr.setRating((float) 2);
        rr.setStepSize((float) 0.3);
       
        rr.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub

t1.setText(String.valueOf(rating));
}
});
    }


}

2. DynamicRatingBar.java

package com.tutorialsee.ratingbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class DynamicRatingBar extends Activity {

RelativeLayout rl;
RatingBar rating;
TextView t1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        rl = (RelativeLayout) findViewById(R.id.rl);
        t1 = (TextView) findViewById(R.id.textView2);
       
        rating = new RatingBar (DynamicRatingBar.this);
       
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
        ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
        params.topMargin = 180;
        params.leftMargin = 20;
       
        rating.setLayoutParams(params);
       
        rl.addView(rating);
       
        rating.setNumStars(6);
        rating.setRating(3);
        rating.setStepSize((float) 0.5);
       
        rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub

t1.setText(String.valueOf(rating));
}
});
    }

}

3. CreateYourOwnRatingBar.java

package com.tutorialsee.ratingbar;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class CreateYourOwnRatingBar extends Activity {

RelativeLayout rl;
EditText star, step;
Button create;
RatingBar rating;

int numStar;
float stepSize;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_your_own_rating_bar);

rl = (RelativeLayout) findViewById(R.id.rl);
star = (EditText) findViewById(R.id.editText1);
step = (EditText) findViewById(R.id.editText2);

create = (Button) findViewById(R.id.button1);

create.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

numStar = Integer.parseInt(star.getText().toString());
stepSize = Float.parseFloat(step.getText().toString());

Create_RatingBar();
}
});
}

private void Create_RatingBar() {

rl.removeView(rating);
rating = new RatingBar(CreateYourOwnRatingBar.this);

rating.setStepSize(stepSize);
rating.setNumStars(numStar);

RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

param.addRule(RelativeLayout.CENTER_HORIZONTAL);
param.topMargin = 500;

rating.setLayoutParams(param);
rl.addView(rating);
}

}

4. activity_create_your_own_rating_bar.xml

<RelativeLayout 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"
    android:id="@+id/rl"
    android:background="#80DEEA"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:textSize="20sp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Create your Own RaringBar" />

    <EditText
        android:id="@+id/editText1"
        android:hint="Number of Stars"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:padding="10dp"
        android:background="@drawable/customlinechangeedittext"
        android:layout_centerHorizontal="true"
        android:inputType="number" />
   
    <EditText
        android:id="@+id/editText2"
        android:hint="Step Size"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="30dp"
           android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
                android:padding="10dp"
         android:background="@drawable/customlinechangeedittext"
        android:layout_centerHorizontal="true"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:textColor="#ffffff"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:text="Create" />

</RelativeLayout>

5. activity_static_rating_bar.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#B2EBF2"
    android:layout_height="match_parent" >

      <TextView
        android:id="@+id/textVi"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Dynamic RatingBar" />
    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp"
        android:numStars="5" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="20dp" />

</RelativeLayout>