Dynamic RatingBar in Kotlin - GeeksforGeeks

28 Mar, 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 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.
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 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    xmlns:android="http://schemas.android.com/apk/res/android"    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 fileHere, 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>
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.AppCompatActivityimport android.os.Bundleimport android.view.ViewGroupimport android.widget.Buttonimport android.widget.LinearLayoutimport android.widget.RatingBarimport 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()        }    }}
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