• Privacy Policy
  • Contact us
  • Register
  • Log In
  • Facebook
  • Pinterest
  • YouTube
  • WhatsUp
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme

Android Widgets

  • Android DatePicker Example And Tutorial
  • Android SeekBar Exmple and Tutorial
  • Android WebView Example and Tutorial
  • Android RatingBar Tutorial and Example
  • Android ListView Tutorial and Example
  • Android Spinner Tutorial and Examples
  • Android Alert Dialog Tutorial and Example
  • Android Radio Button Tutorials
  • Android CheckBox Tutorial
  • Android Toggle Buttons
  • Android Toast Example
  • Types of Android Button and Examples
  • About Android Studio Widgets

About Android

  • How to Setup Different Android Emulators?
  • What is Android Architecture?
  • History of Android Studio and Version
  • What is Android Studio and Installation
  • Home
  • Courses
  • Android Widgets
  • Android RatingBar Tutorial and Example

Android RatingBar Tutorial and Example

Android RatingBar Tutorial and Example - NSC - Nulled Source Code - Android Studio Free Course Step by step toturials

Android RatingBar is a type of SeekBar which is used to get rating from the users, Rating Bar give a floating result like 1.5, 2.0, 3.5, 4.5 etc. To make rating user have to touch or press the star shape.

Click here to read about RatiBar Alert Dialog

To get the value of RatingBar please use getRating() method. which returns a floating value as told above.

Android RatingBar Example:

Create a new Project and add a RatingBar and a Button with onClick method use below code to do it:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="240dp"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:text="Submit"
        android:onClick="getRating"
        android:textSize="20sp"/>

</LinearLayout>
Android RatinBar Example

Add the below code into your MainActivity.java file:

package com.nsc.ratingbar;

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;

// Download Android Projects From :https://nulledsourcecode.com/
// Contact us for making custom Android App
// and School or College Projects. WhatsApp: +91 8307878848
// Telegram: @AndroidStudioCode
public class MainActivity extends AppCompatActivity {

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

                RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
                Button button = (Button) findViewById(R.id.btnSubmit);

                button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                String numberOfRating = String.valueOf(ratingBar.getRating());
                                Toast.makeText(MainActivity.this,numberOfRating,Toast.LENGTH_SHORT).show();
                        }
                });
        }

        public void getRating(View view) {
        }
}

Final Example look:

Android Rating Bar Example - NSC - Android Course Step by step free
Android Rating Bar Example

Set threshold value to RatingBar:

You can also set a thresohld value to Android Rating Bar to which submit button will action means RatingBar will respond only when user gives you 4 or more stars, it is very useful method to avoide bad or less rating. You can achieve like that:

package com.nsc.ratingbar;

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;
// Download Android Projects From :https://nulledsourcecode.com/
// Contact us for making custom Android App
// and School or College Projects. WhatsApp: +91 8307878848
// Telegram: @AndroidStudioCode
public class MainActivity extends AppCompatActivity {

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

                RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
                Button button = (Button) findViewById(R.id.btnSubmit);

                button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                String numberOfRating = String.valueOf(ratingBar.getRating());
                                //Toast.makeText(MainActivity.this,numberOfRating,Toast.LENGTH_SHORT).show();

                                // below method will show action only when user gives rating 4 or high else it will show thankyou.
                                float totalRating = Float.parseFloat(numberOfRating);
                                if (totalRating >= 4){
                                        Toast.makeText(MainActivity.this,numberOfRating,Toast.LENGTH_SHORT).show();
                                } else {
                                        Toast.makeText(MainActivity.this,"ThankYou!",Toast.LENGTH_SHORT).show();
                                }
                        }
                });
        }

        public void getRating(View view) {
        }
}

Download Source Code

RatinBar
What are your Feelings
What are your Feelings
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Updated on April 26, 2021
Android ListView Tutorial and ExampleAndroid WebView Example and Tutorial

Leave a Comment X

You must be logged in to post a comment.

Copyright © 2022.Nuled Source Code.

  • Native Android
  • Paid Projects
  • Flutter
  • My Tickets
  • Register
  • Log In
  • Register
 

Loading Comments...
 

You must be logged in to post a comment.