TheRedosan
TheRedosan

Reputation: 79

No default Radio button selected

I need a dialog like this

enter image description here

but I can't find the way to do a RadioButton dialog with no option selected by default. I try with a null where you select the default button, but it doesn't work. Any ideas?

Here is my code:

public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(titulo)
        .setSingleChoiceItems(items, 0,  new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dlgRadiobuttonsListener.seleccionadoRB(DlgRadiobuttons.this, getResources().getStringArray(items)[which]);
            }
        });

    return builder.create();        
}

Upvotes: 2

Views: 909

Answers (1)

Rahul
Rahul

Reputation: 5049

The second parameter of setSingleChoiceItems is the index of the item to be selected, passing -1 will make android to not select anyone.

public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(titulo)
        .setSingleChoiceItems(items, -1,  new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dlgRadiobuttonsListener.seleccionadoRB(DlgRadiobuttons.this, getResources().getStringArray(items)[which]);
            }
        });

    return builder.create();

}

Upvotes: 4

Related Questions