Android RatingBar Example | How To Use Rating Bar

Android RatingBar – Introduction

Android RatingBar –

Create activity_rating_bar File in XML

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_marginTop=”20dp”
android:orientation=”vertical”
android:gravity=”center”
tools:context=”.ratingBarActivity”>

<RatingBar
android:id=”@+id/ratingBar”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:rating=”2.5″
android:numStars=”5″
android:stepSize=”0.25″ />

<Button
android:id=”@+id/ratingBarButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Submit”
android:textSize=”20sp”
android:textColor=”@color/teal_200″
android:backgroundTint=”@color/design_default_color_primary_dark”
android:layout_marginTop=”15dp”/>

</LinearLayout>

Create ratingBarActivity File in Java

package com.eiheducation.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

public class ratingBarActivity extends AppCompatActivity {

RatingBar ratingBar; // declare the RatingBar object
Button ratingBarButton; // declare the Button object

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_bar);

// We need to find our elements by IDs

ratingBar = findViewById(R.id.ratingBar);
ratingBarButton = findViewById(R.id.ratingBarButton);

// Button Code

ratingBarButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float ratvalue = ratingBar.getRating();
Toast.makeText(ratingBarActivity.this, “Rating” + ratvalue, Toast.LENGTH_SHORT).show();
}
});

}
}

Android Manifest File

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>

<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/Theme.EIHeducationDemo”>

<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.ratingBarActivity” />
</application>

</manifest>

Android RatingBar – Output

Check Out the Headphones – 50% to 70% Off
Check Out the Earbuds – 50% to 70% Off
Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Check Out The LAB Configuration eBook – Class A, Class B, Class C, MiN worth $78 Get at $18

Leave a Reply

Your email address will not be published. Required fields are marked *