
What is RatingBar ?
RatingBar is a shown of Rating starts.It can be used to get rating from the user.it is return of floating numbers.Like 2.0,2,3.5,4.5. Ratingbar supports user interaction placing widget to the left to right of the Ratingbar.
Smaller Ratingbar style to used of android : R.attr.ratingBarStyleSmall. And Larger indicator-only : R.attr.ratingBarStyleIndicator.
RatingBar is used to getRating() method to return rating number.
Attributes of RatingBar
android:numStars : Display Number of star show.
android:rating : Set Rating by default.
android:stepSize : The step size of the rating.
Methods of RatingBar
setNumStars : Sets The Number of stars to show in Ratingbar.
setOnRatingBarChangeListener : it is used change rating bar when user change click rating button pressed.
setRating : set the rating.
setStepSize : it is used to sets the step of size in ratingbar.
Implementation of RatingBar
Create activity_main.xml file and write and following this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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/activity_rating_bar" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.pstudy.bhaumik.programmingstudy.MainActivity"> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rating" android:layout_gravity="center_horizontal" android:stepSize="0.25" android:numStars="5"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Click Rate" android:id="@+id/btn_click" android:textAllCaps="false" android:textSize="16dp" android:layout_marginTop="20dp" /> </LinearLayout> |
Create MainActivity.java File write the following below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.pstudy.bhaumik.programmingstudy; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.RatingBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button click; RatingBar ratingBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); click = (Button) findViewById(R.id.btn_click); ratingBar = (RatingBar) findViewById(R.id.rating); click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { float ratings = ratingBar.getRating(); Toast.makeText(RatingBarActivity.this,"Rating is : " + ratings,Toast.LENGTH_SHORT).show(); } }); } } |
Now In this code to Use rating bar and button when user set rating on the ratingbar and click on button to get rating number is display toast.and display ratingbar to use getRating() method it is return of floating number.