Blog purpose for android basic example for android app developer. any query please my contact

Sunday 21 November 2021

android studio kotlin rating bar example

 android studio kotlin rating bar exmple


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.


activity_main.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>






2.MainActivity.java:


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

}

}

}

}



No comments:

Post a Comment