RatingBar Example

RatingBar Example in Android

CLASS FILE

package com.example.malik.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {

private static Button button;
private static TextView textView;
private static RatingBar ratingBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listenerForRatingBar();
onButtonClickListener();
}
private void listenerForRatingBar() {
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
textView = (TextView) findViewById(R.id.tvResult);

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
textView.setText(String.valueOf(rating));
}
});

}

private void onButtonClickListener() {
button = (Button) findViewById(R.id.bRating);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), String.valueOf(ratingBar.getRating()), Toast.LENGTH_SHORT).show();
}
});
}
}

 

XML FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<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”
android:orientation=”vertical”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”Result here”
android:id=”@+id/tvResult”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”36dp” />

<RatingBar
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/ratingBar”
android:layout_below=”@+id/tvResult”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”59dp”
android:numStars=”5″
android:rating=”2″
android:stepSize=”1″ />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Get Rating”
android:id=”@+id/bRating”
android:layout_centerVertical=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>

 

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.