My Life with Android
Introduction Android Componets UserInterface Advanced UI Data Storage Advanced Concepts Others New Studio

Android RatingBar

Android RatingBar can be used to get the rating from the user.
  • The Rating returns a floating-point number. It may be 2.0, 3.5, 4.0 etc.
  • Android RatingBar displays the rating in stars.
  • The getRating() method of android RatingBar class returns the rating number.
File: a_rating_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="16dp"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<RatingBar android:id="@+id/rb_rating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" />
<Button android:id="@+id/btn_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" /> </LinearLayout>
File: A_RatingBarDemo.java

package com.example.androidcollegeppt;

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

public class A_RatingBarDemo extends Activity{
	RatingBar rb_rating;
	Button btn_submit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_rating_bar);
		rb_rating=(RatingBar)findViewById(R.id.rb_rating);
		btn_submit=(Button)findViewById(R.id.btn_submit);
		btn_submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
			Toast.makeText(A_RatingBarDemo.this, "Your Selected Rating : "+rb_rating.getRating(), Toast.LENGTH_SHORT).show();
				
			}
		});
		
	}

}

OUTPUT