Android RatingBar Using Kotlin With Example


Hello Readers! In this post, we are going to learn about how to use android ratingBar using kotlin in any android application. We will also learn about different attributes of android ratingBar widget that can be used to customise this widget.


Tutorialwing Android RatingBar Output Android RatingBar Using Kotlin

Tutorialwing Android RatingBar Output

Getting Started

Android RatingBar can be defined as below –

Android RatingBar is a widget extended from SeekBar and ProgressBar that shows a rating in stars. The user can either touch or drag the arrow to set the rating.

Different Attributes of Android RatingBar Widget

Some of the popular attributes of android RatingBar are –

Sr. XML Attributes Description
1 android:isIndicator This attribute is used to specify if this rating bar is to be used as an indicator. An an indicator, it can not be changed by the user.
2 android:numStars It is used to set the number of rating items to be shown.
3 android:rating Sets the default rating.
4 android:stepSize It is used to set the step size of the rating.

Some of the popular attributes of android ratingBar inherited from AbsSeekBar are –

Sr. XML Attributes Description
1 android:thumbTint This attribute is used to set tint to apply to thumb drawable
2 android:thumbTintMode This attribute is used to set blending mode used to apply the thumb tint
3 android:tickMarkTint This attribute is used to set tint to apply to the tick mark drawable.
4 android:tickMarkTintMode This attribute is used to set blending mode used to apply the tick mark tint.

Some of the popular attributes of android ratingBar inherited from ProgressBar are –

Sr. XML Attributes Description
1 android:maxHeight It is used to set maximum height of the view.
2 android:minHeight It is used to set minimum height of the view.
3 android:maxWidth It is used to set maximum width of the view.
4 android:minWidth It is used to set minimum width of the view.

Some of the popular attributes of android ratingBar inherited from View are –

Sr. XML Attributes Description
1 android:background It is used to set background of the view
2 android:id It is used to set id of the view. Note – Id must always be unique in an xml file
3 android:padding It is used to set padding(left, right, top, bottom) to the view.
4 android:visibility It is used to set visibility(show/hide) of the view.

Example of Android RatingBar Using Kotlin

At first, we will create an android application. Then, we will use ratingBar widget in this application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as RatingBar. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other file to use ratingBar using kotlin file in the application.

2. Modify values folder

No values folders have been modified. So, we are not going to mention them here.

3. Use RatingBar Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>




In activity_main.xml file, we have defined ratingBar and button widget. Notice the attribute android:stepSize=”0.5″ in RatingBar. This attribute is responsible to set steps in which value will increment/decrement while selecting the rating in the RatingBar. Attribute android:theme=”@style/RatingBar” is used to set theme/style in the ratingBar. You can also set your custom theme in it.

Now, we will access button and ratingbar using kotlin file to perform some actions on them.

4. Access Android RatingBar Using Kotlin file

Open src/main/java/com.tutorialwing.ratingbar/MainActivity.kt file and add below code into it.

package com.tutorialwing.ratingbar

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 ratingBar = findViewById<RatingBar>(
        if (ratingBar != null) {
            val button = findViewById<Button>(
            button?.setOnClickListener {
                val msg = ratingBar.rating.toString()
                Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()

In MainActivity.kt file, we have accessed ratingBar and button widgets. Then, we have set click listener on button to show toast message, that displays selected rating in the ratingBar, whenever it is clicked.

Since AndroidManifest.xml file is very important in the application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.ratingbar"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


When we run the program, we will get output as shown above.

That’s end of our tutorial on Android RatingBar using Kotlin.