Labels

‘Google Play again alcohol analytics Android Android Control Android Developer Android ebook Android N Android Studio Android Wear AndroidDevStory androidn AndroidWear anime Apps average bag beach benidorm beta testing betatesting GooglePlayforFamilies birthday BLT buildingforbillions camden camera cat ears charm christmas Christmas Countdown Clock chrome circle lenses Clock coat coffee collar Connectivity cosplay Countdown craft CultureAlley curl cute Develop Developer Preview developerstory developerstory developerconsole Distribute DIY drunk Education engagement exhibition families fashion Featured Featured’ felt fox freshers friends funny Games geek ghibli Google I/O Google Play Google Play Games Google Play games services GooglePlay gothic lolita gyaru hair halloween hand made hangover happy harry belafonte havea HelloEnglish herbie hancock hiragana Hitman Reborn holiday home hood hoodie hot chocolate house Hutch i'm back instagram iOS Control japanese japanese lolita javea kawaii kawaii it yourself kitty and the bulldog KIY knitting la mort learning life lightning lolita london look love love lunch machine madoka kaname maid maid cafe make-up make-up tutorial mariscos recio MaterialDesign me medieval mobile vision APIs monetization museum music Naruto Clock Naruto Flash Game NBU NDK needles neko nerd new New Year Countdown Clock oh One Piece Flash Clock Onee-chan outfit owl pan party peter peter pan photography pink player pool punk lolita purikura Reborn record red riding hood sea Security sewing shoes sorry spain store listing experiments story Storytoys student sunny sunshine sweet lolita table football tail tan to today totoro trying tutorial Udacity uni vam VandA victoria and albert vinyl wear well wifi wig winter wool xabia yarn zombie

How to create custom RatingBar in Android


A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar.

In this tutorial, we show you how to create custom RatingBar and use it in Android.  

Here is a result of this tutorial:

Android Custom RatingBar


This project is developed in Eclipse 4.2.0.

1. Make main layout with some components.


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">
        <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="Rate 5 star for RatingBar"  
            />
        <RatingBar
           android:layout_width="wrap_content"
           android:layout_height="50dp"
           android:layout_marginBottom="20dp"
           style="@style/StarRatingBar"
           android:id="@+id/ratingbar_default"
           />  
        <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="show state up checkbox"
        android:textColor="#CC00CC"
        android:textSize="20dp"
        /> 
</LinearLayout> 

By default the RatingBar have 5 stars icon to set the rating value and the step size default is 0.5. These default can be change by add some android attributes in xml layout like:

        android:numStars="4"
        android:stepSize="1.0"

We custom RatingBar by define a new style for it

<resources>
    <style name="AppTheme" parent="android:Theme.Light" />
        <style name="StarRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/star_rating_bar_full</item>
        <item name="android:minHeight">48dip</item>
        <item name="android:maxHeight">48dip</item>
    </style>
</resources>

Create star_rating_bar_full.xml in drawable folder

<?xmlversion="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+android:id/background"
          android:drawable="@drawable/star_ratingbar_full_empty"/>
    <item android:id="@+android:id/secondaryProgress"
          android:drawable="@drawable/star_ratingbar_full_empty"/>
    <item android:id="@+android:id/progress"
          android:drawable="@drawable/star_ratingbar_full_filled"/>
</layer-list>

res/drawable/star_ratingbar_empty.xml
<?xmlversion="1.0" encoding="utf-8"?>
<!-- This is the rating bar drawable that is used to
 show a filled star. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2" />
<itemandroid:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2"/>
<itemandroid:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2"/>
<itemandroid:drawable="@drawable/star2" />
</selector>

res/drawable/star_ratingbar_fillled.xml
<?xmlversion="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star3"/>
<itemandroid:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star1"/>
<itemandroid:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star1"/>
<itemandroid:drawable="@drawable/star1" />
</selector>

2. Code

2.1. When User click on RatingBar the selected rating value will be displayed in the textview

ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating,
           boolean fromUser) {
           // TODO Auto-generated method stub
           text.setText("Rating: "+String.valueOf(rating));
}});

2.2. And if user clicks on the button, the value of RatingBar will set by 5 stars.

final Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
       ratingBar_default.setRating(5);
       text.setText("Rating: "+String.valueOf(ratingBar_default.getRating())); 
     }
});

3.DEMO
3.1. Click on RatingBar

Android Custom RatingBar

3.2. Click on Button

Android Custom RatingBar
  
You can download all source code of this tutorial at here
Reference: http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/

0 Response to "How to create custom RatingBar in Android"

Post a Comment