Skip to content
Related Articles

Related Articles

Dynamic RatingBar in Kotlin
  • Last Updated : 23 Feb, 2021

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:

  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.

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 version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    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.




<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)




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 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:

 

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :