RatingBar not displaying inside AlertDialog

602 views Asked by At

I'm working on app where user can view and rate products. So whenever I click on the ratingButton I get a popup or AlertDialog where I can rate 1-5 star(s). Then user should either save or cancel the rating. However the RatingBar seems to be not displaying inside the AlertDialog. Image below should be displaying stars on the dialog. How can I make the RatingBar be displayed in the AlertDialog?

RatingBar not displayed

This is what I've got so far.

onClickListener for the ratingBtn

ratingBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
           AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.AlertDialogStyle);
           builder .setTitle("Rate!");

            View rootView = inflater.inflate(rate_layout, container, false);

            final RatingBar ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);

            ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
               @Override
               public void onRatingChanged(RatingBar ratingBar, float rating,
                                                    boolean fromUser) {
                   String value = String.valueOf(ratingBar.getRating()*2);
                   Toast.makeText(activity, " " + value, Toast.LENGTH_SHORT).show();
                        }
                    });

            builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   Toast.makeText(activity, "Saved", Toast.LENGTH_SHORT).show();
               }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

               @Override
               public void onClick(DialogInterface dialog, int which) {
                   dialog.dismiss();
               }
            });

            AlertDialog alert = builder.create();
            alert.getWindow().setLayout(600, 400);
            alert.show();
            }
          });

rate_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.test.Rate">

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/rateMessage"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="52dp"
        android:theme="@style/RatingBar"/>

</RelativeLayout>

styles.xml

 <style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#626262</item>
        <item name="android:textColorPrimary">#616161</item>
        <item name="android:background">@color/white</item>
    </style>
1

There are 1 answers

0
Jayanth On BEST ANSWER

You have missed builder.setView(rootView);

add this line after this line

View rootView = inflater.inflate(rate_layout, container, false);