You must be familiar with Rating Start Bar that we see on websites that rate Movies, Music, and Products or for Feedbacks. These are 5 hollow stars that get filled when users select them to set a rating from 1-5 or even odd rankings like 0.5, 1.5, 4.5, and so on. We have this implementation in Android Programming too.
RatingBar in Android is an extension of SeekBar and ProgressBar. User can touch/drag or use arrow keys to set the rating when using the default size RatingBar.
In the Graphical Layout section under the Format Widgets tab you would see the RatingBar: Five Starts with the 1st one darker (as selected), drag it to your layout.xml file.
You would see that tag is added to the XML.
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Now create an Object of RatingBar in Java class file and map it to this View using R file reference (as we do for any View like Button, TextView, EditText or ImageView e.t.c)
setOnRatingBarChangeListener is the lister that we have to use to get the value set by the user selecting the Stars on screen. We can get the value of the RatingBar by using getRating() method.
Example RatingBar
Layout XML 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"
tools:context="com.code2care.example.ratingbarexample.MainActivity" >
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rateMessage"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ratingBar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="97dp"
android:contentDescription="@string/rate"
android:src="@drawable/smartphone" />
<TextView
android:id="@+id/rateMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="176dp"
android:text="@string/rate"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
MainActivity.java File
package com.code2care.example.ratingbarexample;
//Button icon form icon8.com
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
RatingBar ratingBar;
TextView rateMessage;
String ratedValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
rateMessage = (TextView) findViewById(R.id.rateMessage);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
ratedValue = String.valueOf(ratingBar.getRating());
rateMessage.setText("You have rated the Product : "
+ ratedValue + "/5.");
}
});
}
}
You can download this Tutorial from GitHub: https://github.com/code2care/RatingBarExample
Youtube Video Demo: https://www.youtube.com/embed/UcAhtmOUOFU- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
- How to install SpaCy (NLP Library) on Mac - Python
- fix fatal: --local can only be used inside a git repository error - Git
- Check Bluetooth is turned on or off on Android device programmatically [Java Code] - Android
- Center align text in TextView Android Programming - Android
- [Android] RuntimeException: Unable to start activity Need BLUETOOTH permission - Android
- Fix - Error:Invalid Gradle JDK configuration found (Android Studio) - Gradle
- SDK Manager: failed to install : Android Studio on Windows - Android-Studio
- The Date Command and its usage [Linux - Unix - macOS] - Linux