Я работаю над приложением, где пользователь может просматривать и оценивать продукты. Поэтому всякий раз, когда я нажимаю на ratingButton
, я получаю всплывающее окно или AlertDialog, где я могу оценить 1-5 звезд. Затем пользователь должен либо сохранить, либо отменить рейтинг. Однако, похоже, RatingBar не отображается внутри AlertDialog. Изображение ниже должно отображать звезды в диалоговом окне. Как я могу сделать RatingBar отображаться в AlertDialog?ОценкаBar не отображается внутри AlertDialog
Это то, что я получил до сих пор.
onClickListener для 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>
Я хочу ratingBar внутри alertdialog –