How to get selected rating through rating bar in android on button click.
Rating bar is used to gives rating of product like game, application, website performance in start forms in android applications. There are five stars present on rating bar widget to provide ability of app user so they can put rating of developers product via stars selection. You can see the recent example of rating bar in Google play store. So here is the complete step by step tutorial for Android Star Rating Bar widget example tutorial.
Android Star Rating Bar widget example tutorial.
Code for MainActivity.java file.
package com.android_examples.com.ratingbarandroid; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RatingBar; import android.widget.Toast; import android.widget.RatingBar.OnRatingBarChangeListener; public class MainActivity extends Activity { RatingBar R1; Button RatingBarCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); R1 = (RatingBar)findViewById(R.id.ratingBar1); RatingBarCount = (Button)findViewById(R.id.button1); RatingBarCount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, String.valueOf(R1.getRating()), Toast.LENGTH_SHORT).show(); } }); } }
Code for activity_main.xml layout file.
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.android_examples.com.ratingbarandroid.MainActivity" > <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="41dp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Click Here to Show Selected rating bar stars count" /> </RelativeLayout>
Screenshot :