Android | Creating a RatingBar

RatingBar is used to allow the users to rate some products. In the below code getRating() function is used to calculate the rating of the products. The getRating() function returns double type value.

Below steps are involved to create a RatingBar in Android:

  1. Create a new android project.
  2. Add RatingBar in your activity_main.xml.
  3. Add Button to invoke action.
  4. Use TextView to display the ratings.
  • To use the rating bar in the app, we will use the in-built RatingBar widget, hence the first step is to import it into the project.
  • In the MainActivity, make the RatingBar object denoted by the variable ‘rt’ and find its corresponding view in the XML file. This is done by the findViewById() method. After the java object has successfully bind to its view, create the ‘stars’ layout, which the user will interact with, to set the rating.
  • To get the drawable stars, the method rt.getProcessDrawable() is used. Then to modify the colours of the stars, the method setColorFilter() is used and the argument Color.YELLOW is passed. Finally, the Call method is written to extract the value of the rating that the user has selected, by the method rt.getMethod().

Program to create MainActivity:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is the code for MainActivity.java
package com.example.hp.rating;
  
// importing required libraries
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
  
public class MainActivity extends AppCompatActivity {
RatingBar rt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        //binding MainActivity.java with activity_main.xml file
        rt = (RatingBar) findViewById(R.id.ratingBar);
  
        //finding the specific RatingBar with its unique ID
        LayerDrawable stars=(LayerDrawable)rt.getProgressDrawable();
  
       //Use for changing the color of RatingBar
        stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
    }
  
    public void Call(View v)  
    {
        // This function is called when button is clicked.
        // Display ratings, which is required to be converted into string first.
        TextView t = (TextView)findViewById(R.id.textView2);
        t.setText("You Rated :"+String.valueOf(rt.getRating()));
    }
}

chevron_right


Note: For the layout, ConstraintLayout is good to use if you are a beginner because it can adjust the views as per the screens.
This XML file defines the view of the application.

Program to create layout for MainActivity:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
  
    <!-- Cover the entire width of the screen -->
    android:layout_width="match_parent"
    <!-- Cover the entire height of the screen -->
    android:layout_height="match_parent"  
  
    tools:context="com.example.hp.rating.MainActivity"
    android:background="@color/colorPrimary">
  
    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="104dp"
        android:background="@color/colorPrimary"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rate Me!!!"
        android:textColor="@android:color/background_dark"
        android:textSize="30sp"
        android:textStyle="bold|italic"
        tools:layout_editor_absoluteX="127dp"
        tools:layout_editor_absoluteY="28dp" />
  
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="148dp"
        android:textColorHint="@color/colorAccent"
        android:textSize="24sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ratingBar"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintLeft_creator="1" />
  
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="50dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="Call"
        android:text="Submit"
        android:textColor="@android:color/background_light"
        android:textStyle="bold|italic"
        app:layout_constraintBottom_toTopOf="@+id/textView2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ratingBar"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>

chevron_right


Here we don’t need to change the manifest file, no permission is required for the ratingBar. By default, all the created new activities are mentioned in the manifest file.

Below is the code for AndroidManifest.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
    package="com.example.hp.rating" >
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  
</manifest>

chevron_right


Output:



My Personal Notes arrow_drop_up

Geeksforgeeks Intern

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.