How to Create Android Widgets: RatingBar in Android

Recommended by 17 users

Jan 8, 2013
How to Create Android Widgets: RatingBar in Android
Add to Bookmark Email this Post 2.2K    15

You have already created a few basic Android apps; it is now time for you to learn how to create Android widgets. If you are not clear with the basic concepts of Android, please attend this Android Course.

What are Widgets in Android?

For sure, all you folks are aware of the concept of Widgets in Android. For the benefit of the curious newbies, let me simplify it a little. The nice little weather bar and clock on your mobile screen, the music player controller that allows you to play a song without going to the music player application; they are all widgets.

Widgets are, in essence, miniature application views that can interact with the user. They are capable of performing a lot more functions than just providing shortcuts to applications.

Categories of Widgets

Before learning how to create Android widgets, it is important for you to understand the different categories of widgets. The Official Android website has defined the following categories:

1) Information Widgets:

These widgets display a piece of information, for instance the weather widget.

How to create Android widgets: Information widget

2) Collection Widgets:

These widgets help the user put together multiple elements of the same kind, for instance, the widgets that help organize all your email messages together.

Message widget in Android

3) Control Widgets:

Control Widgets display the often-used functions, so that the users can perform the tasks without actually going to the application screen. For instance, the WiFi toggle widget, that can help you turn the WiFi on your device ‘on’ or ‘off’.

WiFi toggle widget

4) Hybrid Widgets:

Many widgets combine the functionality of more than one type; for instance the Music Player. Not only does it allow the users to change the track (Control Widget), but also updates the user about the track that is playing (Information Widget).

How to create Android widgets: Music Player widget

How to create Android Widgets: RatingBar

It is now time to learn how to create Android widgets. In this tutorial, we will help you create a RatingBar Widget.

How to create Android widgets: RatingBar widget

It is a very useful widget especially when you want a feedback from your users. The user can touch/drag or use arrow keys to set the rating when using the RatingBar. You must remember that placing widgets to the left or right of the RatingBar is discouraged.

How to write an application for RatingBar?

You can create a nice little RatingBar Widget by following these four steps. This is how the Rating bar would finally look:

How to create Android widgets: RatingBar widget final overviewStep 1: Create a new Android Application Project.

Step 2: In the XML file, drag a RatingBar from “Form Widgets” or write the following XML code.

XML Code:


Step 4: Working with RatingBar is very simple; you only have to use setOnRatingBarChangeListener with RatingBar.

Step 5: Inside the onCreate() method copy the following code:

Java Code:

RatingBar rbar = (RatingBar)findViewById(R.id.ratingBar1);

rbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

@Override

public void onRatingChanged(RatingBar ratingBar, float rating,

boolean fromUser) {

Toast.makeText(MainActivity.this, "Rating : " + rating,

Toast.LENGTH_SHORT).show();

}

});

Glossary

float rating: This gives you current rating value on the RatingBar.

boolean fromUser : This gives you ‘true’ if the rating is changed by user.

Got a question for us? Mention them in the comments section and we will get back to you.

Related Posts:

Beginner’s Guide to Android Architecture

Android Development Course

 

Share on
Comments
15 Comments
  • Seth Holder

    I thought of creating rating bar for my application, but had a lack of knowledge about process. After reading this post I am feeling that I can start with it now.

  • Nurul

    How to make the rating bar then stored in the database?
    because I will make the rating bar and used for all the people who opened my application?

  • Jospeh Rotruck

    Good day. Very nice web site!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also…I am glad to find a lot of useful info right here within the post. Thank you for sharing..

  • Vincenza Varon

    As a “newbie” to blogging, I have been struggling with how to incorporate it into my biology classroom. I finally realized that the struggle came out of not knowing the answer to “Why blog at all?” This site has given me great food for thought and has enabled me to begin to plot a direction for my classroom.

  • Damian Naragon

    Wonderful web-site. I hope you write extra informative articles, as i will examine back frequently to read your posts.

  • Nice Tutorial.. i always follow edureka.in :) keep sharing also visit my site…
    The Android Mania

    • Julie

      Cheers pal. I do appcreiate the writing.

  • Muralidhara

    My academic project has a simple interface to get the details of the book.

    1.The user enter the name of the book
    2.When the submit is given, the database in the server
    should reply the query.
    3.How to access the remote database ?

    please could send me the code related to this.

24 X 7 Customer Support X

  • us flag 1-800-275-9730 (Toll Free)
  • india flag +91 88808 62004