Skip to content
Related Articles

Related Articles

RatingBar in Kotlin

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 02 Feb, 2022

Android RatingBar is a user interface widget which is used to get the rating from the customers or users. It is an extension of SeekBar and ProgressBar that shows star ratings and it allow users to give the rating by clicking on the stars.

In RatingBar, we can set the step size using android:stepSize and it will always return a rating value as floating point number such as 1.0, 2.0, 2.5 etc. By using, android:numStars attribute we can specify the number of stars in RatingBar. RatingBar is used to get ratings form users or customers about the product, movie or hotel experience etc.

RatingBar can be created manually or programmatically but we are going to discuss manually.

First we create a new project by following the below steps:

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click next button.
  4. Then select the Empty activity => next => finish.

Different Attributes of RatingBar Widget

XML AttributesDescription
android:idUsed to uniquely identify the control.
android:ratingUsed to set the default rating value for ratingbar.
android:numStarsUsed to set number of stars to display.
android:backgroundUsed to set the background color for Ratingbar.
android:paddingUsed to set the padding for left, right, top or bottom of Ratingbar.
android:stepSizeUsed to set the step size on RatingBar like 0.5 or 1.

Modify the activity_main.xml file

In this file, we add RatingBar and button in the LinearLayout. Also set attributes for both of the widgets like id, stepSize, background etc.


<?xml version="1.0" encoding="utf-8"?>
    android:gravity ="center">
        android:text="Submit Rating" />

Name of the application can be put in strings.xml file


    <string name="app_name">RatingkBarInKotlin</string>

Access the RatingBar in MainActivity.kt file

First, we will declare the variable rBar to access the Rating using the id like

val rBar = findViewById<RatingBar>(

then, declare another variable button and access the button using its id.

val button = findViewById<Button>(

In the end, to display toast msg while submitting the ratings we code like this

button?.setOnClickListener {
      val msg = rBar.rating.toString()
       "Rating is: "+msg, Toast.LENGTH_SHORT).show()


package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.widget.Button
import android.widget.RatingBar
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val rBar = findViewById<RatingBar>(
        if (rBar != null) {
            val button = findViewById<Button>(
            button?.setOnClickListener {
                val msg = rBar.rating.toString()
                    "Rating is: "+msg, Toast.LENGTH_SHORT).show()

AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Run as Emulator:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!