i have a simple ratingbar in my app, but when i touch the stars to rate do nothing it should be easy but it's not working...
I've tried several tutorials but also didn't work.
Here is my code:
Layout code:
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:isIndicator="false"
android:stepSize="1.0"
android:rating="2.0"
android:numStars="5"
android:paddingBottom="5dp" />
Activity:
public final class ViewActivity extends Activity implements OnRatingBarChangeListener {
RatingBar ratingbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
ratingbar.setOnRatingBarChangeListener(this);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();
}
}
I also tried to do:
Activity:
public final class ViewActivity extends Activity implements OnRatingBarChangeListener {
RatingBar ratingbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
OnRatingBarChangeListener rating = new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();
}
};
final RatingBar sBar = (RatingBar) findViewById(R.id.ratingBar1);
sBar.setOnRatingBarChangeListener(rating);
}
And Also:
public final class ViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
RatingBar rating = (RatingBar)findViewById(R.id.ratingBar1);// create RatingBar object
rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();
}
});
I have used the debugger and put a breakpoint to the Toast and when I touch the stars doesn't enter to the method and didn't show the Toast either.
Thank you all.
Try this :
main.xml
<?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"
android:background="#C2C2A3">
<RatingBar
android:id="@+id/ratingBar1"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="OK" />
MainActivity.java
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);
// Set ChangeListener to Rating Bar
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();
}
});
}
}
Also, check below links :
Hope this helps.
Property isIndicator must be false.
You can programatically do that with a setter:
RatingBar ratingBar = findViewById(R.id.YOURID);
ratingBar.setIsIndicator(false);
Or in xml property:
android:isIndicator="false"
Make sure you are getting the right RatingBar.
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