Custom Single Choice Dialog in Android

In Android application development, dialogs serve as common UI components for displaying information or collecting user input. Among various dialog elemants, single-choice selection controls allow users to pick one option from multiple choices.

This article demonstrates how to create a customized dialog containing a single choice selector in Android applications. We'll utilize the AlertDialog class to build the dialog and incorporate single choice options. The following sections detail the implementation steps.

Implementation Steps

Step 1: Project Setup

Create a new Android project in Android Studio and add the required imports in your target Activity:

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

Step 2: Dialog Construction

Implement a method within your Activity to display the single choice dialog:

private void launchChoiceDialog() {
    final String[] options = {"Option One", "Option Two", "Option Three"};
    int defaultSelection = 0;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select an Option")
            .setSingleChoiceItems(options, defaultSelection, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int index) {
                    Toast.makeText(MainActivity.this, "Selected: " + options[index], Toast.LENGTH_SHORT).show();
                }
            })
            .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Handle confirm button click
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Handle cancel button click
                }
            });
    builder.create().show();
}

This implementation defines a string array containing three choices with the first option pre-selected. Using AlertDialog.Builder, we construct the dialog and apply setSingleChoiceItems to populate the single choice options. Callbacks manage user interactions with the selections and buttons.

Step 3: Dialog Invocation

Call the launchChoiceDialog method from the Activity's onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    launchChoiceDialog();
}

Step 4: Application Execution

Compile and run the application. Upon startup, a dialog with single choice options will appear, allowing users to make a selection and confirm or cancel their choice through the respective buttons.

Conclusion

This guide illustrates how to implement a custom single choice dialog in Android using AlertDialog.Builder. This approach enables developers to easily create interactive dialogs with callback mechanisms for handling user selections. Dialogs enhance user interaction by providing structured interfaces for input and feedback in Android appplications.

Tags: Android Dialog singlechoice AlertDialog ui-components

Posted on Tue, 22 Sep 2026 16:35:38 +0000 by SoccerGloves