Android Rating Bar Example

  • 0

Android Rating Bar Example

Category : Uncategorized

android rating bar

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. Android RatingBar is the subclass of AbsSeekBar class.

The getRating() method of android RatingBar class returns the rating number.

Android RatingBar Example

Let’s see the simple example of rating bar in android.

activity_main.xml

Drag the RatingBar and Button from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <RatingBar
  7.         android:id=“@+id/ratingBar1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentTop=“true”
  11.         android:layout_centerHorizontal=“true”
  12.         android:layout_marginTop=“44dp” />
  13.     <Button
  14.         android:id=“@+id/button1”
  15.         android:layout_width=“wrap_content”
  16.         android:layout_height=“wrap_content”
  17.         android:layout_alignLeft=“@+id/ratingBar1”
  18.         android:layout_below=“@+id/ratingBar1”
  19.         android:layout_marginLeft=“92dp”
  20.         android:layout_marginTop=“66dp”
  21.         android:text=“submit” />
  22. </RelativeLayout>

Activity class

Let’s write the code to display the rating of the user.

File: MainActivity.java
  1. package com.example.rating;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.RatingBar;
  9. import android.widget.Toast;
  10. public class MainActivity extends Activity {
  11.     RatingBar ratingbar1;
  12.     Button button;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         addListenerOnButtonClick();
  18.     }
  19.     public void addListenerOnButtonClick(){
  20.         ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);
  21.         button=(Button)findViewById(R.id.button1);
  22.         //Performing action on Button Click
  23.         button.setOnClickListener(new OnClickListener(){
  24.             @Override
  25.             public void onClick(View arg0) {
  26.                 //Getting the rating and displaying it on the toast
  27.                 String rating=String.valueOf(ratingbar1.getRating());
  28.                 Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
  29.             }
  30.         });
  31.     }
  32.     @Override
  33.     public boolean onCreateOptionsMenu(Menu menu) {
  34.         // Inflate the menu; this adds items to the action bar if it is present.
  35.         getMenuInflater().inflate(R.menu.activity_main, menu);
  36.         return true;
  37.     }
  38. }


Output:

android rating bar example output 1 android rating bar example output 2


Leave a Reply