Android Star Rating View Tutorial

Udemy Generic 728x90

Objective

In this tutorial, we will learn how to implement Star Rating view in Android application through a simple example.

 



 
rating

 

Using RatingBar widget, we can display rating bar component with start icon. To set rating, we can touch and click on the start. In this tutorial, we will use RatingBar widget to make rating bar with start icon. When user set rating and click on the submit button, we will display rating through Toast.

 

Step 2

Create a new Project

Create a new Project in Android Studio, goto File ⇒ New ⇒ New Projects.

 

Step 3

Create Layout

To implement star Rating view and display rating on click button, we need to use RatingBar and Button widget in activity_main.xml file.

<TextView
    android:id="@+id/rate_me"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Rate Me"
    android:textSize="18dp"/>

<RatingBar
    android:layout_below="@+id/rate_me"
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0" />

<Button
    android:layout_below="@+id/ratingBar"
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:onClick="rateMe"/>

default rating

 

Step 4

Initialize RatingBar

To use RatingBar properties, we need to initialize RatingBar in onCreate() method.

public class MainActivity extends AppCompatActivity {

    public RatingBar ratingBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Initialize RatingBar
        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        
    }
}

 

Step 5

Display Rating

We will display rating on click submit button. So, we need to add a callback rateMe() method. Within this method, we will display rating by using getRating() method.

public class MainActivity extends AppCompatActivity {

    public RatingBar ratingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Initialize RatingBar
        ratingBar = (RatingBar) findViewById(R.id.ratingBar);

    }

    /**
     * Display rating by calling getRating() method.
     * @param view
     */
    public void rateMe(View view){

        Toast.makeText(getApplicationContext(), 
                 String.valueOf(ratingBar.getRating()), Toast.LENGTH_LONG).show();
    }
}

 

rating

 



 
 

Udemy Generic 728x90

Spread the word. Share this post!

  • anujagg

    good article

  • Soe Lwin

    This website always post good article . Thanks you , all.