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>
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.
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.
set the android:numStars="5"
and android:layout_width="wrap_content"
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