Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Alert Dialog with SingleItemSelection in Android

To create an AlertDialog with a single item selection (often referred to as a radio-button list) in Android using the traditional View system, you can use the setSingleChoiceItems method of AlertDialog.Builder.

Here's how to create such an AlertDialog:

1. Define the Activity Layout

For simplicity, we will create a Button in activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <Button
        android:id="@+id/showDialogButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Dialog"
        android:layout_centerInParent="true" />

</RelativeLayout>

2. Implement the AlertDialog in your Activity:

In your MainActivity.java:

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button showDialogButton;
    private int checkedItem = -1;  // This will store the position of the selected item

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

        final CharSequence[] items = {"Item 1", "Item 2", "Item 3", "Item 4"};

        showDialogButton = findViewById(R.id.showDialogButton);
        showDialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Choose an item");
                builder.setSingleChoiceItems(items, checkedItem, 
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            checkedItem = which;
                        }
                    });
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (checkedItem != -1) {
                            Toast.makeText(MainActivity.this, "Selected item: " + items[checkedItem], Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.setNegativeButton("Cancel", null);
                builder.show();
            }
        });
    }
}

In this example, clicking the button will show an AlertDialog with single selectable items (like radio buttons). After making a selection and pressing "OK", a toast message will display the selected item. The variable checkedItem is used to keep track of the currently selected item.

You can modify the items and actions as needed to suit your requirements.

  1. Implementing single-choice item in AlertDialog Android:

    To implement a single-choice item in an AlertDialog, you can use the setSingleChoiceItems method. Here's an example:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose an item")
           .setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   // Handle item click and update checkedItem
                   checkedItem = which;
               }
           })
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   // Handle positive button click
                   // The selected item is in the 'checkedItem' variable
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   // Handle negative button click
               }
           })
           .show();
    
  2. How to get selected item from AlertDialog in Android:

    To get the selected item from an AlertDialog with a single-choice item, use the variable that tracks the selected item. For example:

    int checkedItem = // your variable
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // The selected item is in the 'checkedItem' variable
        }
    });