Android: Simple Rating Bar

Operating System

In this tutorial we will try to create a Simple Rating Bar using Android. This simple app can used on some application that needed a review system to their quality. Android is a mobile operating system developed by Google. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provide an adaptive framework that allow the developer to develop an apps in a simpler way. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first to do is to create a resource that we will be using for the design. To create the resource, go to drawable folder then create a new xml file named button_style. Then write these certain block of codes inside the newly created file.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item>
  4. <shape android:shape="rectangle">
  5. <corners android:radius="15dp" />
  6. <solid android:color="#337AB7"/>
  7. </shape>
  8. </item>
  9. </selector>
Now that we're done with the graphic resource we will create then layout of the apps. First locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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="com.razormist.simpleratingbar.MainActivity">
  8.  
  9.  
  10. <RatingBar
  11. android:id="@+id/rb_rate"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerHorizontal="true"
  16. android:layout_marginTop="192dp"
  17. android:isIndicator="false"
  18. android:numStars="5"
  19. android:rating="0"
  20. android:stepSize="1" />
  21.  
  22. <TextView
  23. android:id="@+id/tv_result"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_marginTop="42dp"
  27. android:layout_alignParentTop="true"
  28. android:textSize="40sp"
  29. android:layout_centerHorizontal="true" />
  30.  
  31. android:id="@+id/btn_submit"
  32. android:layout_height="wrap_content"
  33. android:layout_width="wrap_content"
  34. android:layout_below="@id/rb_rate"
  35. android:layout_centerInParent="true"
  36. android:layout_marginTop="50dp"
  37. android:text="Submit"
  38. android:background="@drawable/button_style"
  39. android:textColor="#fff"/>
  40.  
  41. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simpleratingbar">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity"
  13. android:configChanges="orientation"
  14. android:screenOrientation="portrait">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17.  
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22.  
  23. </manifest>

The Main Function

This code contains the main function of the application. This code will allow you to select your desire star and prompt some confirmation when the button is clicked. To start with first locate your MainActivity java file and open it, then write these some important variables inside the MainActivity class.
  1. private Button btn_submit;
  2. private TextView tv_result;
  3. private RatingBar rb_rate;
Then write these several method inside the activity class to make the apps work properly.
  1. void InitializeVariables(){
  2. rb_rate = (RatingBar)findViewById(R.id.rb_rate);
  3. btn_submit = (Button)findViewById(R.id.btn_submit);
  4. tv_result = (TextView)findViewById(R.id.tv_result);
  5. }
  6.  
  7. void RatingUpdate(){
  8. rb_rate.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
  9. @Override
  10. public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
  11. tv_result.setText("Rating: " + String.valueOf(Math.round(rating)));
  12.  
  13. }
  14. });
  15. }
  16.  
  17. void SubmitRating(){
  18. btn_submit.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. Toast.makeText(MainActivity.this, "Thanks for the rate", Toast.LENGTH_SHORT).show();
  22. }
  23. });
  24. }
Finally, initialize all the methods inside the onCreate method to run the application.
  1. InitializeVariables();
  2. RatingUpdate();
  3. SubmitRating();
Try to run the app and see if it worked. There you have it we create a Simple Rating Bar using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment