14

enter image description here
I want to add a button centered below the two radio second radio button B and when i checked an option and click on validate, an action take place. Any help please

final CharSequence[] photo = {"A","B"};

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Select Gender");

alert.setSingleChoiceItems(photo,-1, new 

DialogInterface.OnClickListener()

{

    @Override
    public void onClick(DialogInterface dialog, int which) 
    {
        if(photo[which]=="A")

        {

            gen="B";
        }

        else if (photo[which]=="B")

        {

            gen="B";

        }
    }

});
alert.show();
0

4 Answers 4

17

Try to do this, you just have to select the default selection and add to the dialog an integer -> inputSelection

final CharSequence[] items = { " HDMI IN ", " AV IN" };

        // Creating and Building the Dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select Input Type");

        builder.setSingleChoiceItems(items,inputSelection,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        inputSelection = item;
                        levelDialog.dismiss();
                    }
                });
        levelDialog = builder.create();
        levelDialog.show();
13

My method making Custom dialog

http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application reference from here

  1. Create one xml custom dialog

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <RadioButton
            android:id="@+id/rd_!"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A" />
    
        <RadioButton
            android:id="@+id/rd_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rd_!"
            android:text="B" />
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rd_2"
            android:layout_centerInParent="true"
            android:text="OK" />
        </RelativeLayout>
    

and activity.java file

  Dialog dialog = new Dialog(Dialogeshow.this);
    dialog.setContentView(R.layout.custom_dialoge);
    dialog.setTitle("This is my custom dialog box");
    dialog.setCancelable(true);
    // there are a lot of settings, for dialog, check them all out!
    // set up radiobutton
    RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_);
    RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2);

    // now that the dialog is set up, it's time to show it
    dialog.show();
3
  • You should add your RadioButtons to RadioGroup in xml file. Feb 25, 2016 at 21:25
  • how to add onclicklistener Jun 29, 2016 at 9:16
  • 1
    @HarishReddy For the listeners: RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_1); rd1.setOnClickListener(new View.OnClickListener() { @ Override public void onClick(View v) { } }); Aug 17, 2016 at 15:13
5

You can use below method to show dialog

public void showDialog(Context context, String title, String[] btnText,
        DialogInterface.OnClickListener listener) {

    final CharSequence[] items = { "One", "Two" };

    if (listener == null)
        listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface,
                    int paramInt) {
                paramDialogInterface.dismiss();
            }
        };
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(title);

    builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {

                }
            });
    builder.setPositiveButton(btnText[0], listener);
    if (btnText.length != 1) {
        builder.setNegativeButton(btnText[1], listener);
    }
    builder.show();
}

And the calling part can be done as below:

showDialog(MainActivity.this, "Your Title", new String[] { "Ok" },
    new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            if(which==-1)
            Log.d("Neha", "On button click");
            //Do your functionality here
        }
    });
5
  • 1
    i wonder how can i checked if a radio button is checked with your solution
    – Dimitri
    Feb 21, 2013 at 8:03
  • 3
    when i called it both which value is -1
    – Dimitri
    Feb 21, 2013 at 8:09
  • Can you please explain how the circle shows green when we select the item in this code?? and can we directly call this alert dialog in onClick listener of some view without this showDialog method?? I tried this but the circle dose not show selected. @Neha Dhanwani
    – user3623979
    Dec 31, 2015 at 11:29
  • 1
    you can add this line: int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition(); Jun 6, 2016 at 7:13
  • The solution as it is not working and always which=-1. Have you tested this code and got the correct selected item ?
    – MBH
    Nov 10, 2016 at 7:05
1

You can add a single button to your dialog using Builder.setNeutralButton.

6
  • but how to select a radio option only on button click
    – Dimitri
    Feb 21, 2013 at 6:30
  • please refer this site wptrafficanalyzer.in/blog/… Feb 21, 2013 at 6:31
  • @peter your solution does not support froyo 2.2
    – Dimitri
    Feb 21, 2013 at 6:49
  • @sophia in androidmanifest file minsdk set as 8 and run your code:) Feb 21, 2013 at 6:58
  • <uses-sdk android:minSdkVersion="8" /> don't change anything
    – Dimitri
    Feb 21, 2013 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.