Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dialog box with radio button and validate button

Tags:

android

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();
like image 522
Dimitri Avatar asked Feb 21 '13 05:02

Dimitri


People also ask

What are the three buttons that can be added to a dialog?

Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button. Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message.

How do you inflate dialog?

To inflate the layout in your DialogFragment , get a LayoutInflater with getLayoutInflater() and call inflate() , where the first parameter is the layout resource ID and the second parameter is a parent view for the layout. You can then call setView() to place the layout in the dialog.

What is a radio select button?

Radio buttons allow a user to select a single option among multiple options. You can set the Choice Value of each option, for each button, as well as group these buttons by giving them the same Group Name.


2 Answers

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();
like image 102
Victor Ruiz. Avatar answered Sep 28 '22 03:09

Victor Ruiz.


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();
like image 27
QuokMoon Avatar answered Sep 28 '22 04:09

QuokMoon