This tutorial will guide you to implement Rating Bar View in android in an application with a simple example. Follow some below steps to get RatingBar functionality in android.
Step 1: Introduction
Sometimes we need to give RatingBar View to rate our application, any particular content in the app and any list item by the user. We use RatingBar widget to display rating bar component and start icon. When user set rating and click on the submit button, then show rating through toast.
Step 2 : Create a layout
Crate an XML file activity_main.xml and need to use one RatingBar widget and one click button
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate Me"
android:textSize="18dp"/>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="rateMe"/>
Step 3: Initialize RatingBar
We need to initialize RatingBar in onCreate() method.
public class MainActivity extends AppCompatActivity {
private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize RatingBar
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
}
}
Step 4: Display Rating
We will display rating by using getRating() method click on button .
/*
* Display rating by calling getRating() method.
*/
public void rateMe(View view){
Toast.makeText(getApplicationContext(),
String.valueOf(ratingBar.getRating()), Toast.LENGTH_LONG).show();
}
0 Comment(s)