Open top menu



In this tutorial explain how to implement Rating Bar and how to used in xml class in android. Rating Bar implement very easy  show below.

The android Rating bar used to get the rating from end user, the rating bar return value in floating-point number. Android RatingBar is a subclass of  AbsSeekbar class.

The Rating Bar used in application get from  user feedback.

res/layout/activity_main.xml
................................................................................................................................................................


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     
        android:gravity="center"
        android:text="Android Beginner tutorials"
        android:textSize="12dp" />
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="20dp"
        android:gravity="center">

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rating Bar" />
 
    </LinearLayout>

</LinearLayout>

MainActivity.java
.................................................................................................................................................................



package com.androidbeginnerpoint.testapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
RatingBar RatingBar;
     Button button ;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("SeekBar tutorial");
setContentView(R.layout.activity_main);
RatingBar=(RatingBar)findViewById(R.id.ratingBar1);
       button=(Button)findViewById(R.id.button1);
       //Performing action on Button Click
       button.setOnClickListener(new OnClickListener(){

           @Override
           public void onClick(View arg0) {
               //Getting the rating and displaying it on the toast
               String rating=String.valueOf(RatingBar.getRating());
               Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
           }
       });
     

}
}

0 comments