Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RatingBar showing more than 5 stars

Tags:

android

I want to show a rating bar via an alert dialog in my android app. The problem I am facing is that depending upon the width of the screen, the ratingbar shows more than 5 stars(upto 10) in landscape mode and the function setNumStars() has no effect. There are posts already dealing with this issue but they deal with a ratingbar whose laout is defined statically while I am creating it dynamically. How to solve this problem?

public class MainActivity extends Activity {
private Button launchButton;
//Rating dialog
 private AlertDialog.Builder rater;
 private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    launchButton =(Button)findViewById(R.id.ratingButton);
    launchButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Here the alert dialog will be launched
            showRatingDialog();
        }
    });

      //Setting up rating dialog
        rater = new AlertDialog.Builder(this);

        rater.setTitle("This is the rating dialog");


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void showRatingDialog()
{
  ratingBar = (RatingBar)findViewById(R.id.ratingStars);
      rater.setNegativeButton("Abbrechen", null);
      rater.setView(ratingBar);
      rater.show();

}
}

I have tried using static layout for the ratingbar but when I do this and try to display the ratingbar via an alert dialog, no stars are shown. Here is the layout file for rating bar:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
        android:id="@+id/ratingStars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="1"
        />

</LinearLayout>
like image 459
user1107888 Avatar asked Mar 19 '13 16:03

user1107888


People also ask

What is Rating bar in Android user interface?

Android RatingBar is a user interface widget which is used to get the rating from the customers or users. It is an extension of SeekBar and ProgressBar that shows star ratings and it allow users to give the rating by clicking on the stars.

How can I give space between star and rating bar in Android?

You can use Custom SVG and Set Your Separation value By using this class, you can fix Android custom SVG RatingBar and set Drawable End by replacing value(I marked this value as There_You_Can_Set_Your_Value) inside the class.


1 Answers

set the android:numStars="5" and android:layout_width="wrap_content"

like image 145
zacharia Avatar answered Nov 15 '22 21:11

zacharia