Android Example of Rating bar

      No Comments on Android Example of Rating bar

Android RatingBar can be used to rate the app. The Rating can be done in floating-point number like it may be 2.5, 4.5, 5 etc.

Drag the button and ratingbar from the pallete.

activity_main.xml

File name:-

activity_main.xml

<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: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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="show" />

</RelativeLayout>

MAinActivity.java

Write the code to get output of rating bar.

package com.example.scanfcode;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener
{
	  Button b1;
	  RatingBar r1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		b1=(Button)findViewById(R.id.button1);
		r1=(RatingBar)findViewById(R.id.ratingBar1);
		b1.setOnClickListener(this);
		
		
	}
	
	@Override
	public void onClick(View v)
	{
		if(v.getId()==R.id.button1)
		{
			String s1=String.valueOf(r1.getRating());
			Toast.makeText(getApplicationContext(),s1,Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

OUTPUT:-

scanfcode

Leave a Reply

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