Monday, February 5, 2018

Android Rating Bar Example

What is Rating Bar

A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBa

In Android, you can use “android.widget.RatingBar” to display rating bar component in stars icon. The user is able to touch, drag or click on the stars to set the rating value easily.


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

Lets create a project and use Rating bar


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.   
  7.     <RatingBar  
  8.         android:id="@+id/ratingBar1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="44dp" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/button1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignLeft="@+id/ratingBar1"  
  20.         android:layout_below="@+id/ratingBar1"  
  21.         android:layout_marginLeft="92dp"  
  22.         android:layout_marginTop="66dp"  
  23.         android:text="submit" />  
  24.   
  25. </RelativeLayout>  


MainActivity.xml

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

Now run the Project, you will see how rating bar works.


No comments:

Post a Comment