Monday, February 4, 2013

Android Custom Rating Bar

We Can custom a rating bar in android to show with our own drawable resources, please make a project with the Name ratingBarWidget and have a main activity as RatingbarActivity.java with following simple code.

This project is developed in Eclipse 4.2.0.


package com.example.ratingbarwidget;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

public class RatingbarActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ratingbar);
        

        final TextView text = (TextView) findViewById(R.id.textView);
        
        final RatingBar ratingBar_default = (RatingBar)findViewById(R.id.ratingbar_default);

        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()));  
            }
        });
        
        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)); 
       }}); 
    } 
}


the res/layout/activity_ratingbar.xml is as follows

<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/foodRatingBar"
  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>

/res/values/styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Light" />
<style name="foodRatingBar" 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>

res/drawable/star_rating_bar_full.xml

<?xml version="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_rating_bar_full_empty.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- This is the rating bar drawable that is used to
 show a filled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star2" />

<item android:drawable="@drawable/star2" />

</selector>

res/drawable/star_rating_bar_full_filled.xml

<?xml version="1.0" encoding="utf-8"?>

 <!-- This is the rating bar drawable that is used to
 show a unfilled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star3" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star1" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/star1" />

<item android:drawable="@drawable/star1" />

</selector>

you can download the source code from the link Android CustomRatingBar

14 comments:


  1. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.Android Training in chennai with placement | Android Training in velachery

    ReplyDelete
  2. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    Click here:
    angularjs training in bangalore
    Click here:
    angularjs training in pune

    ReplyDelete
  3. Appreciating the persistence you put into your blog and detailed information you provide
    Click here:
    Microsoft azure training in btm
    Click here:
    Microsoft azure training in rajajinagar

    ReplyDelete
  4. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    Devops training in sholinganallur

    ReplyDelete
  5. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    best rpa training in chennai |
    rpa training in chennai |
    rpa training in bangalore
    rpa training in pune | rpa online training

    ReplyDelete
  6. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Data Science course in rajaji nagar | Data Science with Python course in chenni
    Data Science course in electronic city | Data Science course in USA
    Data science course in pune | Data science course in kalyan nagar

    ReplyDelete
  7. Thanks For Sharing The Information Please Keep Updating us Information Shared Is Very Valuable Time Just Went On Reading The Article Python Online Training AWS Online Training Data Science Online Training Data Science Online Training Hadoop Online Training

    ReplyDelete
  8. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any python Related information please visit our website Python Training In Pune page!

    ReplyDelete
  9. Some may stag in Interviews!!! OOPS!! More than 50% of students do this in their career. Instead, do Hadoop Training in Chennai at Infycle. Those students can easily clear this Interview session because more than 5 times at INFYCLE practicing mock-interview sessions, Hence students are Getting out of their interview fear.

    ReplyDelete
  10. Great Post with valuable information. I am glad that I have visited this site. Share more updates.
    IELTS Classes in Chennai
    Best IELTS Coaching in Chennai


    ReplyDelete