Sunday, September 15, 2013

CUSTOM RATINGBAR

CUSTOM RATINGBAR

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                
                <TextView android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="@string/hello" />

                <RatingBar android:id="@+id/ratingbar"
android:layout_width="wrap_content"
                                style="@style/CustomRatingBar"
android:layout_height="50px" />

</LinearLayout>


SOURCE CODE [styles.xml] is

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomRatingBar" parent="@android:style/Widget.RatingBar.Small">
<item name="android:progressDrawable”>@drawable/custom_ratingbar</item>
                <item name="android:minHeight">16dip</item>
                <item name="android:maxHeight">16dip</item>
</style>
</resources>

SOURCE CODE [custom_ratingbar.xml] is

<?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/custom_ratingbar_empty" />
    <item android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/custom_ratingbar_empty" />
    <item android:id="@+android:id/progress"
android:drawable="@drawable/custom_ratingbar_filled" />
</layer-list>

SOURCE CODE [custom_ratingbar_filled.xml] is

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

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

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

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

</selector>

SOURCE CODE [custom_ratingbar_empty.xml] is

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
                
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/star_off" />

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

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

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

</selector>

SOURCE CODE [CustomRatingBarExample.java] is

package Com.CustomRatingBarExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;

public class CustomRatingBarExample extends Activity
{
                
                RatingBar rb;

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

rb = (RatingBar) findViewById(R.id.ratingbar);
rb.setRating(3.5f);

}
}

Note:

Save the file [styles.xml] in values folder. This folder already has a file [strings.xml].
Save the files [custom_ratingbar.xml], [custom_ratingbar_filled.xml], [custom_ratingbar_empty.xml] in drawable folder.
Save the images [custom ratings icon – star_on.png, star_off.png] in the drawable folder.







The OUTPUT will be

 https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNmLhGf1msGzOJc645ICaWf8MM6O6XXPS12hO0BF2PPh3VLngSctrh-dy8Q7uFR7-_Iuj7Qi2Bqs3VB__jkoGa8ly1kTfOsclkH1GexbidjfC-djQzgSdaHizBKnzCwOymdoMH1ymObLA/

POSTED BY JACK 02- MAY- 2011 2 COMMENTS

SIMPLE RATINGBAR

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
    
<RatingBar android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
    
                <TextView android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
    
</LinearLayout>

SOURCE CODE [RatingBarExample.java] is

package com.RatingBarExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class RatingBarExample extends Activity
{

                RatingBar ratingbar;
                TextView ratings;

                public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                ratings = (TextView) findViewById(R.id.rating);
                                ratingbar = (RatingBar) findViewById(R.id.ratingbar);

                                ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{
                                                public void onRatingChanged(RatingBar ratingBar, float rating,
                                                                                boolean fromUser)
{
                                                                // TODO Auto-generated method stub
                                                                ratings.setText("The Rating is " + rating);
                                                }
                                });
                }
}

The OUTPUT will be

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAYwLdY-PW3H7DlQR8e_Q8E8r77NoEbXUyq0_pCy0hWJqJ-AoVLzbEEnvY0pdbwQ4biENx-69ljBQ1_UiSelnxBFMdC8AHehMUCgN3EEtJGFV-0WQDPffIDiYCU1MtSioPCJ4H5gCkd3g/

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkwgp248WyrCo4L-1442xTaZ8yZHIwqkMZfJc0UrFXH8QpAJWwObN0pfcBrPBQnPEIm2qYS9I0zGrEWWLK1iSvuEMYDIPw-PeabvIl-iOFOMGPCcFSAoeOd5pSkgwYjSmb0fPdr6S8xRY/

No comments:

Post a Comment