RatingBar in Kotlin

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 Attributes Description
android:id Used to uniquely identify the control.
android:rating Used to set the default rating value for ratingbar.
android:numStars Used to set number of stars to display.
android:background Used to set the background color for Ratingbar.
android:padding Used to set the padding for left, right, top or bottom of Ratingbar.
android:stepSize Used 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity ="center">
  
    <RatingBar
        android:id="@+id/rBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stepSize="0.5"
        android:theme="@style/Widget.AppCompat.RatingBar"
        android:background="@color/colorPrimary"
        android:numStars="4"/>
  
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit Rating" />
  
</LinearLayout>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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>(R.id.rBar)

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

val button = findViewById<Button>(R.id.button)

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

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

edit
close

play_arrow

link
brightness_4
code

package com.geeksforgeeks.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.RatingBar
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        val rBar = findViewById<RatingBar>(R.id.rBar)
        if (rBar != null) {
            val button = findViewById<Button>(R.id.button)
            button?.setOnClickListener {
                val msg = rBar.rating.toString()
                Toast.makeText(this@MainActivity,
                    "Rating is: "+msg, Toast.LENGTH_SHORT).show()
            }
        }
    }
}

chevron_right


AndroidManifest.xml file

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
package="com.geeksforgeeks.myfirstkotlinapp">
  
<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


Run as Emulator:

 



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :

Be the First to upvote.


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