Search code examples
androidandroid-alertdialogandroid-dialogandroid-radiobutton

Alert Dialog doesn't show radio buttons


I have an alertDialog with the following code :

AlertDialog.Builder b = new AlertDialog.Builder(Activity.this);
String[] types = getResources().getStringArray(R.array.text_spinner_NewClass_dayList);
b.setSingleChoiceItems(types, 2, (dialog, which) -> {
            textView.setText(types[which]);
            dialog.dismiss();
        }
);
b.show();

I want to show all radio buttons of items but it only shows the selected item.

enter image description here


Solution

  • Create a xml layout as you want. And inflat that layout to alert dialog through View.

    AlertDialog.Builder b = new AlertDialog.Builder(Activity.this);
    
    //Your customized layout
    final View customLayout = getLayoutInflater().inflate(R.layout.custom, null);
    
    b.setView(customLayout);
    
    b.show();