Create Alert Dialog Box with Radio buttons group in android

How to show multiple radio buttons inside AlertDialog box with DialogInterface OnClickListener method.

In this tutorial we are going to make Alert Dialog Box on button click event and inside that alert dialog we are showing multiple choice into radio buttons format so when application user clicks on any of them it will close the Alert Dialog Box and show us a toast message regarding our select value. So here is the complete step by step tutorial for Create Alert Dialog Box with Radio buttons group in android .

android-project-download-code-button

How to Create Alert Dialog Box with Radio buttons group in android.

Code for MainActivity.java file.

package com.android_examples.alertdialogboxradiobuttons_android_examplescom;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button;
    AlertDialog alertDialog1;
    CharSequence[] values = {" First Item "," Second Item "," Third Item "};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CreateAlertDialogWithRadioButtonGroup() ;

            }
        });
    }

    public void CreateAlertDialogWithRadioButtonGroup(){


        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setTitle("Select Your Choice");

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

            public void onClick(DialogInterface dialog, int item) {

                switch(item)
                {
                    case 0:

                        Toast.makeText(MainActivity.this, "First Item Clicked", Toast.LENGTH_LONG).show();
                        break;
                    case 1:

                        Toast.makeText(MainActivity.this, "Second Item Clicked", Toast.LENGTH_LONG).show();
                        break;
                    case 2:

                        Toast.makeText(MainActivity.this, "Third Item Clicked", Toast.LENGTH_LONG).show();
                        break;
                }
                alertDialog1.dismiss();
            }
        });
        alertDialog1 = builder.create();
        alertDialog1.show();

    }

}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.alertdialogboxradiobuttons_android_examplescom.MainActivity"
    android:background="#FFF3E0">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to open alert dialog box with radio buttons in android"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Screenshots :

alert-radio-1

Create Alert Dialog Box with Radio buttons group in android

alert-radio-3

Click here to download Create Alert Dialog Box with Radio buttons group in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *