RatingBar can be defined as below –
"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."
Some of the popular attributes of android ratingbar are –
S. No. | XML Attributes | Description |
1 | android:id | Sets id of the view. Note – Id must always be unique in an xml file. |
2 | android:padding | Sets padding(left, right, top, bottom) to the view. |
3 | android:visibility | Sets visibility(show/hide) of the view. |
4 | android:background | Sets background of the view |
5 | android:isIndicator | Specifies whether this rating bar is an indicator. Note – It can’t be changed by the user if it is an indicator. |
6 | android:numStars | Specifies the number of stars(or rating items) to show. |
7 | android:rating | Specifies the rating to show by default. |
8 | android:stepSize | Specifies the step size of the rating. |
Some of the popular attributes of android ratingbar inherited from AbsSeekBar are –
S. No. | XML Attributes | Description |
1 | android:thumbTint | Tint to apply to thumb drawable. |
2 | android:thumbTintMode | Blending mode used to apply the thumb tint. |
3 | android:tickMarkTint | Tint to apply to the tick mark drawable. |
4 | android:tickMarkTintMode | Blending mode used to apply the tick mark tint. |
Some of the popular attributes of android RatingBar inherited from ProgressBar are –
S. No. | XML Attributes | Description |
1 | android:maxHeight | Sets maximum height of the view. |
2 | android:minHeight | Sets minimum height of the view. |
3 | android:maxWidth | Sets maximum width of the view. |
4 | android:minWidth | Sets minimum width of the view. |
At first, we'll create android application. Then, we will use ratingbar in this application.
Follow the steps below to create new application. Please ignore the steps if you've already created the application.
S. No. | Steps |
1 | Open Android Studio. |
2 | Click on Start a new Android Studio Project Write application name as RatingBar. Then, click next button. |
3 | Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button |
4 | Then, select Empty Activity => click next => click finish. |
5 | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail. |
Now, we'll modify the xml and java file to use ratingbar in this application.
Open res/values/strings.xml file. Then, add below code into it.
<resources>
<string name="app_name">RatingBar</string>
</resources>
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:stepSize="0.5" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/ratingbar" />
</android.support.constraint.ConstraintLayout>
In activity_main.xml file, we have used ratingbar and button. In RatingBar, android:stepSize=”0.5″ attribute is used to set incremental steps. Now, we will access these ratingbar and button in java file.
RatingBar in java file
Open src/main/java/com.ukacademe.ratingbar/MainActivity.java file. Then, add below code into it.
package com.ukacademe.ratingbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RatingBar ratingBar = findViewById(R.id.ratingbar);
if (ratingBar != null) {
Button button = findViewById(R.id.btnSubmit);
if (button != null) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = String.valueOf(ratingBar.getRating());
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
In MainActivity.java, we have accessed ratingbar. We have also set click listener to show toast message when button is clicked. Toast message displays the currently selected rating in ratingbar. Since the file of AndroidManifest.xml is very important in any android application. We are also going to mention it here.
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mohitnegi.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When you run the application, we will get output as shown below.