I am using rating bar to mark a field as favorite. The user should be able to unmark it some time in future. But once i set it, the on-click listener is not working on that item.
XML code
<RatingBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numStars="1"
android:stepSize="1"
android:id="@+id/fav"/>
JAVA code
fav.setOnClickListener(new View.OnClickListener(){ //fav is a ratingbar
public void onClick(View view){
favRest = !favRest; //A boolean variable which is set/reset each time it is clicked
if(favRest)
{ fav.setRating(1.0f);
addFav(uname,hotelName);
}
else{
fav.setRating(0.0f);
removeFav(uname,hotelName);
}
}
});
Once you rate
, its not possible to clear
rate using touch on RatingBar
. You can only change rate value from 1.0 to 5.0
.
To clear/reset
rate value, you have to use other action like adding a Clear/Reset
Button
.
In Button
click listener, you can reset
rating value by using setRating(0.0)
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Reset
ratingBar.setRating(0.0);
}
});
UPDATED:
In your RatingBar
, you are using android:numStars="1"
and android:stepSize="1"
. So once you rate 1
its not possible to rate 0
by using touch on RatingBar
From my point of view:
ImageView
instead of RatingBar
favorite
and unfavorite
in res/drawable
folderImageView
as per checking favRest
value.Try This:
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/fav" />
imageview = (ImageView) findViewById(R.id.fav);
fav.setOnClickListener(new View.OnClickListener(){ //fav is a ratingbar
public void onClick(View view){
favRest = !favRest; //A boolean variable which is set/reset each time it is clicked
if(favRest)
{
imageview.setImageResource(R.drawable.icon_favorite);
addFav(uname,hotelName);
}
else
{
imageview.setImageResource(R.drawable.icon_unfavorite);
removeFav(uname,hotelName);
}
}
});
Hope this will help~