Question

I m new in android. so i can not populate radio group from activity within alert builder dialog, Please help me any one/body.

Was it helpful?

Solution

Try this:

AlertDialog.Builder builderSingle = new AlertDialog.Builder(MyActivity.this);
builderSingle.setIcon(R.drawable.ic_launcher);
builderSingle.setTitle("MyDialog");

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
        MyActivity.this,
        android.R.layout.select_dialog_singlechoice);

for (int i = 0; i < 10; i++) {
    arrayAdapter.add("Item " + i);
}
// cancel button
builderSingle.setNegativeButton("cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

builderSingle.setAdapter(arrayAdapter,
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Log.i("Selected Item : ", arrayAdapter.getItem(which));
                dialog.dismiss();

            }
        });
builderSingle.show();

OTHER TIPS

some code of what you are trying to do would be helpful.

in general, when you create a dialog somewhere like this:

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_layout);

then you have to use that dialog to access layout items within it, for example:

RadioGroup dialogRadioGroup = (RadioGroup) dialog.findViewById(R.id.myRadioGroup);
CharSequence[] values = {"Search by Name", "Search by Email", "Search by Mobile"};


    AlertDialog.Builder builder = new AlertDialog.Builder(MainMenu.this);
    builder.setTitle("Select type of Search");

    builder.setSingleChoiceItems(values, -1, new DialogInterface.OnClickListener() {


                @Override
                public void onClick(DialogInterface dialogInterface, int item)
                {
                    switch (item)
                    {
                        case 0:
                            Toast.makeText(MainMenu.this, "First Item :  Search by Name Clicked" , Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(MainMenu.this, "Second Item :  Search by Email Clicked" , Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            Toast.makeText(MainMenu.this, "Third Item :  Search by Mobile Clicked" , Toast.LENGTH_SHORT).show();
                            break;
                    }

                    alertDialogWithRadioButtons.dismiss();

                }
            }
    );

    alertDialogWithRadioButtons = builder.create();
    alertDialogWithRadioButtons.show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top