03 Aug Android RatingBar Control
If you want need to rate a movie or website i.e. rating in stars, then use the Android RatingBar control. It is the subclass of android.widget.AbsSeekBar,
Usage
Use the Android RatingBar control to show movie reviews, website rating, and product reviews etc.
Attributes
Here are the attributes of RatingBar Android Control,
RatingBar Attribute | Description | Value |
---|---|---|
android:isIndicator | Whether this rating bar is an indicator. | true or false |
android:numStars | Number of stars, visible to users. | Integer Value |
android:rating | Set the default rating with this attribute. | Floating Point Value |
android:stepSize | Set the step size of the rating. | Floating Point Value |
Example
MainActivity.java
The src\main\java\com\studyopedia\studyopediaapplication\MainActivity.java is a java class which sets the views. Here we will set the View to be displayed with the setContentView.
On button click, a Toast will appear with a message showing what rating you have given to the movie using the RatingBar. Toast in Android is a notification message visible in the form of popup.
Weāre using getRating() method to retrieve the value of rating. Ratings under RatingBar have floating point value, for example, 2.0, 3.0, 5.0, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.studyopediaapplication; import android.media.Rating; 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 { private RatingBar rating; private Button btResult; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Buttons rating =(RatingBar)findViewById(R.id.ratingBar); btResult=(Button)findViewById(R.id.buttonResult); // Listener btResult.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // getRating() method is used to retrieve rating String res =String.valueOf(rating.getRating()); Toast.makeText(getApplicationContext(), "Thank you for giving "+res+" rating to the movie Avengers", Toast.LENGTH_LONG).show(); } }); } } |
activity_main.xml
For src\main\res\layout\activity_main.xml,
Weāll add our UI content here, 2 TextView, 1 Button and 1 RatingBar. On button click, the rating you entered using RatingBar will get displayed. For displaying the rating, weāve used Toast,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<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=".MainActivity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/title" android:textColor="#000000" android:textSize="20sp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="12dp" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/review" android:textColor="#000000" android:textSize="25sp" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="21dp" /> <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="132dp" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonResult" android:text="@string/result" android:background="#0000A0" android:textColor="#ffffff" android:textSize="22sp" android:layout_marginTop="61dp" android:layout_below="@+id/ratingBar" android:layout_centerHorizontal="true" /> </RelativeLayout> |
AndroidManifest.xml
The content of src\main\AndroidManifest.xml, provides key information of your app to the system. The system needs this to run your code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.studyopediaapplication"> <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> |
strings.xml
Hereās the code for res/values/strings.xml. The app name and any hardcoded strings will be visible. We hardcoded string āStudyopedia RatingBar Applicationā and used @string/title in activity_main.xml. Here, it gets mentioned under <resources>.
We also hardcoded the review string and used @string/review and result string with @string/result,
1 2 3 4 5 6 7 8 |
<resources> <string name="app_name">StudyopediaApplication</string> <string name="title">Studyopedia RatingBar Application</string> <string name="result">Result</string> <string name="review">Movie Review: Avengers</string> </resources> |
Output
We created an AVD, so now we will run our app using the same AVD. Go to Run > Run app or press the run button or Shift + f10. This will install your app on the AVD and the following emulator with your app opens up.
Hereās the output, first screen visible,
Give a rating to the movie and click the RESULT button.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments