Code2care : TechZone Apps & Tutorials

Android RatingBar Example







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 gets filled when users selects 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 a 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 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


Top











Posts related to android.


1. Share image and text Twitter using your Android Application Programatically

2. The Android Virtual Device 'myEmulator' is currently running an emulator and cannot be deleted.

3. Make Android View Scrollable both Horizontally and Vertically

4. Can we move apps like WhatsApp, Facebook to external MicroSD card

5. Parsing Data for android-21 failed unsupported major.minor version 51.0

6. Android : Prevent App for rotation landscape or portrait

7. ADT quit unexpectedly error on Mac OSX Android Eclipse SDK

8. Android-Failed to install apk on device EOF Timeout Error

9. How to change Android EditText Cursor Color

10. JavaDocs for Android SDK Eclipse IDE

11. Two Buttons next to each other in Android Layout

12. Change Title text for Android Activity using java code

13. How to add Newline to text in Android TextView

14. Android : Unable to load VM from snapshot : Mac OS X Error

15. INVALID FILE NAME: MUST CONTAIN ONLY [a-z0-9_.] Android Eclipse Error

16. Hide Tittle bar and Navigation bar from android activity

17. appcompat_v7 errors after updates to API level 21 Material Theme

18. How to Copy Text to Android Clipboard Programatically ClipboardManager (Both Old and New SDK)

19. [Solution] Running Android Lint has encountered a problem NullPointerException Error

20. Remove Trailing zeros BigDecimal Java

    more...






Popular tags
android
x 175
eclipse
x 29
notepadplusplus
x 20
macosx
x 14
sharepoint
x 14
html
x 14
mac
x 13
androidstudio
x 11




1000+ C Programs     PHP Tutorial     JSON Tutorial     Swift Tutorial     India Pinocdes     About Us     Privacy Policy


Code2care © 2012-17