Tuesday, March 16, 2021

Anroid RatingBar

Android RatingBar Example

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. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.ratingbar.MainActivity">  
  8.   
  9.     <Button  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="submit"  
  13.         android:id="@+id/button"  
  14.         app:layout_constraintBottom_toBottomOf="parent"  
  15.         app:layout_constraintLeft_toLeftOf="parent"  
  16.         app:layout_constraintRight_toRightOf="parent"  
  17.         app:layout_constraintTop_toTopOf="parent"  
  18.         app:layout_constraintVertical_bias="0.615" />  
  19.   
  20.     <RatingBar  
  21.         android:id="@+id/ratingBar"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_marginLeft="72dp"  
  25.         android:layout_marginTop="60dp"  
  26.         app:layout_constraintStart_toStartOf="parent"  
  27.         app:layout_constraintTop_toTopOf="parent" />  
  28.   
  29. </android.support.constraint.ConstraintLayout>  


Activity class

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

File: MainActivity.java
  1. package example.javatpoint.com.ratingbar;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.RatingBar;  
  8. import android.widget.Toast;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.     RatingBar ratingbar;  
  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.         ratingbar=(RatingBar)findViewById(R.id.ratingBar);  
  21.         button=(Button)findViewById(R.id.button);  
  22.         //Performing action on Button Click  
  23.         button.setOnClickListener(new View.OnClickListener(){  
  24.   
  25.             @Override  
  26.             public void onClick(View arg0) {  
  27.                 //Getting the rating and displaying it on the toast  
  28.                 String rating=String.valueOf(ratingbar.getRating());  
  29.                 Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  
  30.             }  
  31.   
  32.         });  
  33.     }  
  34. }  

Output:

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

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile