What we will do ?
-> We will create an application with five stars. As we select stars we will get the numbers associated with it.
What we will need ?
Step 1 : Create a new Application. Go to File->New->Android Application Project.-> We will create an application with five stars. As we select stars we will get the numbers associated with it.
What we will need ?
- LinearLayout and Rating Bar in activity_main.xml
- Additions in MainActivity.java
Step 2 : Give a Name(RatingDemo) and a package name(mia) to your Application. Click Next continuously then click Finish.
Step 3 : Drag RatingBar and Button in activity_main.xml layout.Or else copy the below code in activity_main.xml.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RatingBar android:id="@+id/ratingBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:numStars="5" /> <Button android:id="@+id/button1" android:layout_width="174dp" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="OK" /> </LinearLayout>
Step 4 : Open MainActivity.java from src->com.mia.ratingdemo. Copy the code below in it.
MainActivity.java
package com.mia.ratingdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity
{
RatingBar ratingBar;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
btn=(Button)findViewById(R.id.button1);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(getApplicationContext(),"Your Selected Ratings : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
}
});
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
float rating=ratingBar.getRating();
Toast.makeText(getApplicationContext(),"Your Selected Ratings : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
}
});
}
}
Step 5 : Run the Application.
Explanation of the code :
Explanation from activity_main.xml
- We just add a RatingBar with a Button in a LinearLayout. numStars:5 is used to limit the rating to 5 .
<RatingBar android:id="@+id/ratingBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:numStars="5" />
Explanation from MainActivity.java
- Create a variable of RatingBar as ratingBar and of Button type as btn;
RatingBar ratingBar; Button btn;
- We relate the rating bar and the button from the layout to the variables respectively using findViewById().
ratingBar=(RatingBar)findViewById(R.id.ratingBar1); btn=(Button)findViewById(R.id.button1);
- the onRatingBarChangeListener() method is used to perform action on clicking the RatingBar. Here we will display a Toast with the number of starts given by the user, when the action is performed.The float rating variable stores the rating value in float number.
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(getApplicationContext(),"Your Selected Ratings : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
- We will perform the same action when button is pressed using setOnClickListener() method.











Thanks for solving my confusion
ReplyDeleteThnk u for the post
ReplyDeleteI neede this code for my app
ReplyDelete