Dynamic 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 programmatically or dynamically.
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.
Modify the activity_main.xml file
In this file, we use the LinearLayout and set its attributes like id, padding etc and it can be accessed in the Kotlin file using id.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:id = "@+id/container" android:layout_width = "wrap_content" android:layout_height = "wrap_content" tools:context = ".MainActivity" android:orientation = "vertical" android:paddingLeft = "35dp" android:paddingTop = "50dp" > </ LinearLayout > |
Add application name in strings.xml file
Here, we can put all the strings which can be used in the application in any file. So, we update the app_name which can be seen on the top of the activity.
XML
< resources > < string name = "app_name" >DynamicRatingBarInKotlin</ string > </ resources > |
Creating RatingBar in MainActivity.kt file
First of all, we declare variable rBar to create RatingBar and set its attributes using it.
val rBar = RatingBar(this) val layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) rBar.layoutParams = layoutParams rBar.stepSize = 1.0.toFloat() rBar.numStars = 5
then, we declare another variable to create a button like this
val button = Button(this) val layoutParams1 = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) button.text="Submit Rating"
After this, RatingBar and button added into the LinearLayout using the statements
linearLayout?.addView(rBar) linearLayout?.addView(button)
Kotlin
package com.geeksforgeeks.myfirstkotlinapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.ViewGroup import android.widget.Button import android.widget.LinearLayout import android.widget.RatingBar import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Create RatingBar val rBar = RatingBar( this ) val layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) rBar.layoutParams = layoutParams rBar.stepSize = 1.0 .toFloat() rBar.numStars = 5 //create button val button = Button( this ) val layoutParams1 = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) button.text= "Submit Rating" val linearLayout = findViewById<LinearLayout>(R.id.container) // Add RatingBar and button to LinearLayout linearLayout?.addView(rBar) linearLayout?.addView(button) button?.setOnClickListener { val msg = rBar.rating.toString() Toast.makeText( this @MainActivity , "Given Rating: " +msg, Toast.LENGTH_SHORT).show() } } } |
AndroidManifest.xml file
XML
<? 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 > |
Run as Emulator:


Please Login to comment...