i have a google map app where users can rate on the restaurant they select via marker i stored that rating value in sq lite database and when they click on view rating button i display the name and rating value as string in the scrollview. Now i want to show that rating value as stars i.e the way the user enters.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="Rate me"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="@+id/rateme"
android:layout_weight="0.00" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Restaurent Name"
android:ems="10"
android:id="@+id/place"
android:layout_weight="0.00" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:rating="2.0"
android:layout_marginTop="64dp"
android:layout_below="@+id/place"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/rate"
android:layout_weight="0.00" />
<Button
android:text="Submit"
android:layout_width="395dp"
android:layout_height="wrap_content"
android:id="@+id/submit"
android:onClick="insert"/>
<Button
android:text="view Rating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewrate"
android:layout_weight="0.00"
android:onClick="display"/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textStyle="bold" />
<RatingBar
android:id="@+id/rat"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:isIndicator="false"
android:layout_weight="0.07" />
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Get Android RatingBar Value In android, by using RatingBar methods (getNumStars(), getRating()) we can get the number of stars and the rating value which was selected. Following is the code snippet to get the rating details from RatingBar in android applications. int noofstars = rBar. getNumStars();
To do this, you can use RatingBar
. See documentation.
In your layout XML
:
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:rating="2.0" />
In your Activity
:
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
Float rating = 3.0; // 3.0 is from your database
// To show rating on RatingBar
ratingBar.setRating(rating);
UPDATE:
To get rating
from RatingBar
:
Float rating = ratingBar.getRating();
// Show rating on TextView
textView.setText("Rating: " + String.valueOf(rating));
To get output as like your attached image:
# If the number
of Restaurant
fixed. (For example: 10
):
RatingBar
and 10 TextView
in your layout XML
. id
for RatingBar(rb1, rb2, rb3.....)
and TextView(tv1, tv2, tv3.....)
.Get rating
values from database
:
Float rating1 = 1.0;
Float rating2 = 1.0;
Float rating3 = 3.0;
.............
.......................
Set rating
values to RatingBar
and TextView
rb1.setRating(rating1);
rb2.setRating(rating2);
rb3.setRating(rating3);
................
......................
tv1.setText("Restaurant One" + String.valueOf(rating1));
tv2.setText("Restaurant Two" + String.valueOf(rating2));
tv3.setText("Restaurant Three" + String.valueOf(rating3));
................
.......................
# If the number
of Restaurant
variable:
ListView
or RecyclerView
. ArrayList
to store each restaurant data (name
and rating
) that you get from Google Map
.Adapter
to populate each restaurant data
on row item view
on ListView
/RecyclerView
. Here row item view is a XML containing a RatingBar
and TextView
.ArrayList
to Adapter
getView()
or onBindViewHolder()
get the restaurant name
and rating
from ArrayList
for each position
and show on TextView
and RatingBar
.Here are some good Tutorials:
Hope this will help~
Try This:
Float rating = Float.parseFloat(stringFromDatabase);
mRateBar.setRating(rating);
textView.setText("Rating: " + stringFromDatabase);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With