RatingBar in Kotlin - GeeksforGeeks

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:
Click on File, then New => New Project.After that include the Kotlin support and click on next.Select the minimum SDK as per convenience and click next button.Then select the Empty activity => next => finish.
Click on File, then New => New Project.
After that include the Kotlin support and click on next.
Select the minimum SDK as per convenience and click next button.
Then select the Empty activity => next => finish.
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
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    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>
Name of the application can be put in strings.xml file
xml
<resources>    <string name="app_name">RatingkBarInKotlin</string></resources>
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()

Kotlin
package com.geeksforgeeks.myfirstkotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Buttonimport android.widget.RatingBarimport 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()            }        }    }}
xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"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>
 
ayushpandey3july
Android-Bars
Kotlin Android
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Broadcast Receiver in Android With Example
How to Create and Add Data to SQLite Database in Android?
Services in Android with Example
Content Providers in Android with Example
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Content Providers in Android with Example
Services in Android with Example
Android UI Layouts
Android RecyclerView in Kotlin