Set single choice AlertDialog default item in android

android, android-alertdialog

Solution

just change 2nd perameter of `setSingleChoiceItems` method like below.. change it to 0,1,2,3

.setSingleChoiceItems(array, 2,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                // TODO Auto-generated method stub
                                selected = array[arg1].toString();
                            }
                        })

as per above code your 2nd item is selected by default.

Problem

I am using the following code to create an AlertDialog ``` CharSequence[] array = {"Font1", "Font2", "Font3", "Font4"}; callback = (DialogClickListener) fragment; builder = new AlertDialog.Builder(context); builder.setTitle("Font Settings") .setSingleChoiceItems(array, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub selected = array[arg1].toString(); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { callback.onYesClick(selected); } }); ``` How can I set an element to be selected by default? Please help :)

Original source

Related problems