articles

Home / DeveloperSection / Articles / Android Rating bar

Android Rating bar

Nitish Kesarwani 2896 28-Dec-2017

Rating bar is a control which provides a user interface to give feedback about the application. It always returns value in floating point. I am using Rating Bar, Button and Text view controls to get the feedback when the user clicks on the button. ratingbar() and onClickListener are used to invoke the users' feedback. 

Attributes

o android:numStars: This attribute displays the number of stars for user feedback

o android:ratingBy this method, you can set rating point for the user

o android:isIndicator: It checks whether rating bar has indicator state

o android:stepSize: It is used to set the number of step size increment in a value of rating.

Methods

o getOnRatingBarChangeListener()It is a callback method, which is called when the users give rating to the application

o setOnRatingBarChangeListenter(): This method sets the listener for star bars ,when user selects the number of stars.

o getnumStars(): We get the number of stars shown in devices by using this method

o setnumStars(): we can set the number of stars displayed on screens.

o getisIndicator(): This method allows users to not change the rating bar value

o setisIndicator(): If its value is true, users cannot change the value of stars.

o setMax(): It sets the maximum limit of rating bar stars values

o getRating(): It returns the number of a rating given by users.

o setRating(): In this method, we assign the value of stars is filled

o setStepSize(): We can set the number of stars value incremented on users feedback

o getStepSize(): We get the response of each value of star on the basis of selection of stars


Android_Manifest.xml
    <applicat<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.ratingbar">

ion
        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>
Main_Activity.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"
    tools:context="com.mapsample.msclient009.ratingbar.MainActivity"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#199119"
        android:gravity="center"
        android:text="Ths is Rating Bar"
        android:textColor="#F9F9F9"
        android:textSize="20dp" />

    <RatingBar
        android:id="@+id/ratebar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_below="@+id/tv"
        android:layout_centerHorizontal="true"
        android:layout_weight="1" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/btn"
        android:layout_marginTop="0dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@+id/ratebar"
        android:text="Rate it Now"
        />
    </LinearLayout>
</LinearLayout>


Main_Activity.java
package com.mapsample.msclient009.ratingbar;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
RatingBar rb;
Button btn;
TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        ratingStar();
    }
    public void ratingStar()
    { rb =(RatingBar)findViewById ( R.id.ratebar );
      btn =(Button)findViewById ( R.id.btn );
      tv =(TextView)findViewById ( R.id.tv );
      btn.setOnClickListener ( new View.OnClickListener ( ) {
          @Override
          public void onClick(View v) {
            String rateNumber =String.valueOf (rb.getRating ());
              Toast.makeText ( getApplicationContext (),"You have rated"+rateNumber,Toast.LENGTH_LONG ).show ();
              double value = Double.valueOf ( Double.valueOf (rateNumber ) );
              if(value>3 && value<=5)
              {
                  Toast.makeText ( getApplicationContext (),"This app is a good",Toast.LENGTH_LONG ).show ();

              } else {Toast.makeText ( getApplicationContext (),"This app is not a good",Toast.LENGTH_LONG ).show ();}
          }
      } );

    }
}
Launch Your APP

Android Rating barAndroid Rating bar


Leave Comment

Comments

Liked By