Reputation: 382
I have an AlertDialog with radio buttons on it. When I click on an option it inserts the value into a textview but I can not get the window to close after.
private void showRadioButtonDialog() {
LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vewInflater = inflater.inflate(R.layout.dialog_installments_radiogroup, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create();
builder.setView(vewInflater);
builder.setTitle("Dialog title");
RadioGroup currencySettingRadioGroup = vewInflater.findViewById(R.id.radio_group);
ArrayList<String> listArr = getInstallmentsList(amountToPay, 12);
for ( String items: listArr ) {
RadioButton rb = new RadioButton(this);
rb.setText(items);
currencySettingRadioGroup.addView(rb);
rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rb.setChecked(true);
TextView tvInstallments = findViewById(R.id.tvInstallments);
tvInstallments.setText(items);
dialog.dismiss(); // <== does not work
}
});
}
builder.show();
}
Upvotes: 0
Views: 27
Reputation: 17834
The show()
method calls create()
internally.
You get a reference to a built Dialog by calling builder.create()
, but Android does that too, when you call builder.show()
. That means Android has its own Dialog instance, which your dialog
variable isn't referencing.
Use dialog.show()
instead of builder.show()
.
You also need builder.create()
to be called after you finish setting all the properties of the Builder (move it to be below builder.setTitle()
).
Upvotes: 1