I tried to add ratingbar in dialog but I have some issue in stars number

Viewed 2450

I have tried to adding rating bar which have stars to do rating, I want make the rate have 4 stars, but in my code I get more than 7 stars and when I tried to click on a star then appear double.. (for example: I click star 1 then display 2 stars selected)..

my Example:

my Example:

My Code:

public class test extends AppCompatActivity {


   TextView textView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        textView =(TextView)findViewById(R.id.textView1);

        // Button1
        final Button btn1 = (Button) findViewById(R.id.btnAddHotelRate);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ShowDialog();
            }
        });

    }



    public void ShowDialog()
    {
        final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
        final RatingBar rating = new RatingBar(this);
        rating.setMax(4);

        popDialog.setIcon(android.R.drawable.btn_star_big_on);
        popDialog.setTitle("Add Rating: ");
        popDialog.setView(rating);

        // Button OK
        popDialog.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        textView.setText(String.valueOf(rating.getProgress()));
                        dialog.dismiss();
                    }

                })

                // Button Cancel
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        popDialog.create();
        popDialog.show();

    }

}

4 Answers

Issue is because RatingBar width matches the parent view and it creates stars irrespective of number of stars, RatingBar should have width as wrap_content respect to the parent.

Try placing RatingBar inside LinearLayout and add LinearLayout to your popup Dialog.

public void ShowDialog()
{
    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);

    LinearLayout linearLayout = new LinearLayout(this);
    final RatingBar rating = new RatingBar(this);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    rating.setLayoutParams(lp);
    rating.setNumStars(4);
    rating.setStepSize(1);

    //add ratingBar to linearLayout
    linearLayout.addView(rating);


    popDialog.setIcon(android.R.drawable.btn_star_big_on);
    popDialog.setTitle("Add Rating: ");

    //add linearLayout to dailog
    popDialog.setView(linearLayout);



    rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
            System.out.println("Rated val:"+v);
        }
    });



    // Button OK
    popDialog.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    textView.setText(String.valueOf(rating.getProgress()));
                    dialog.dismiss();
                }

            })

            // Button Cancel
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    popDialog.create();
    popDialog.show();

}

In your xml layout give

Height is wrap_content

Widhth is also wrap_content

And set numStars

You need use like this

final RatingBar rating = new RatingBar(this);
 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
 LinearLayout.LayoutParams.WRAP_CONTENT );
 rating.setLayoutParams(lp);
 rating.setNumStars(4);

The same issue solve through the xml, try like this

   <RatingBar 
 enter code hereandroid:id="@+id/ratingBar"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:numStars="4" android:stepSize="1.0"
 android:rating="2.0" />

Then it will work...

For more information

https://googleweblight.com/i?u=https://developer.android.com/reference/android/widget/RatingBar.html&grqid=wuehy0Mb&hl=en-IN

https://www.mkyong.com/android/android-rating-bar-example/

Hope it will help you :)

Use rating.setNumStars(4); instead of rating.setMax(4);

Related