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:
- 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.
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.
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 > |
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()
Kotlin
package com.w3wiki.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() } } } } |
AndroidManifest.xml file
xml
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.w3wiki.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 > |
Run as Emulator:
Contact Us