Spinner, AlertDialog

A Spinner is a dropdown that shows one choice from a list you feed it through an adapter (read the pick with getSelectedItem), and an AlertDialog is a modal pop-up built with AlertDialog.Builder that asks a question with title, message, and OK/Cancel buttons.

9 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

A long list, then a final check

FestConnect needs two more things: a T-shirt size picker (S, M, L, XL, XXL) that does not eat the screen with five radio buttons, and a final 'Confirm registration?' box the student must answer before submitting.

Those are two widgets that often appear together: the Spinner (a compact dropdown) and the AlertDialog (a modal yes/no pop-up). One gathers a choice from a list; the other stops and asks a question. Learn them as a pair.

Theory

A drop-down menu and a security gate

A Spinner is a restaurant menu on a rail: it stays folded showing your current pick, and drops open to reveal the full list when tapped. An AlertDialog is a security gate: it blocks the corridor and will not let you pass until you answer 'proceed?' yes or no. One is a tidy chooser; the other is a deliberate stop-and-decide.

Theory

Spinner, then AlertDialog

Spinner (dropdown):

  • Fill it with an ArrayAdapter (from a string array).
  • React with setOnItemSelectedListener.
  • Read with getSelectedItem() (or getSelectedItemPosition()).

AlertDialog (modal question):

  • Build with new AlertDialog.Builder(this).
  • setTitle, setMessage, setPositiveButton("OK", ...), setNegativeButton("Cancel", ...).
  • Finish with create() then show() (or just .show()).

The dialog blocks until the user answers, unlike a Toast.

Practical

Size Spinner, then confirm

// Spinner: feed it a list with an adapter
Spinner size = findViewById(R.id.size);
String[] sizes = {"S", "M", "L", "XL", "XXL"};
size.setAdapter(new ArrayAdapter<>(this,
        android.R.layout.simple_spinner_item, sizes));

register.setOnClickListener(v -> {
    String picked = size.getSelectedItem().toString();

    // AlertDialog: modal confirm before submitting
    new AlertDialog.Builder(this)
        .setTitle("Confirm")
        .setMessage("Register with size " + picked + "?")
        .setPositiveButton("Yes", (dialog, which) ->
            Toast.makeText(this, "Registered!", Toast.LENGTH_SHORT).show())
        .setNegativeButton("Cancel", null)
        .show();
});

Think first

Toast or AlertDialog?

FestConnect could confirm the registration with a Toast ('Registered!') or an AlertDialog ('Register with size L? Yes/No'). Which fits a final confirmation the user must actively agree to, and why is the other wrong here?

Show the answer

The AlertDialog, because it is modal: it blocks and waits for the user to actively choose Yes or No, which is exactly what a final confirmation needs. A Toast is wrong for this step because it just flashes and fades with no buttons, the user cannot agree or decline, and might miss it entirely. Use a Toast for passive 'done' feedback (after they confirm), and an AlertDialog when you need a real decision.

Quiz

A Spinner shows nothing in its dropdown when tapped. What is the most likely missing step?

  1. No adapter was set to supply the list of items
  2. It needs a RadioGroup wrapper
  3. The AlertDialog was not created
  4. getSelectedItem() was never called
Show the answer

No adapter was set to supply the list of items

A Spinner is empty until you give it data through an adapter (e.g. an ArrayAdapter of the sizes); no adapter, no items (A). Spinners are not wrapped in RadioGroups (B). An AlertDialog is a separate widget and unrelated to populating the Spinner (C). getSelectedItem() reads the current pick, it does not fill the list (D). Always set an adapter to feed a Spinner its options.

Watch out

Spinner and dialog traps

1. Spinner with no adapter: it shows nothing. Populate it with an ArrayAdapter.

2. Building an AlertDialog but not showing it: like Toast, you must call show() (after create(), or directly). No show(), no dialog.

3. Using a Toast where a decision is required: a Toast cannot capture Yes/No. For a real choice, an AlertDialog with positive/negative buttons is correct.

Theory

Next: the last widget, smart text

One widget remains: the AutoCompleteTextView, a text field that suggests matches as you type (great for FestConnect's college field), paired with a TextWatcher that reacts to every keystroke. It combines the adapter idea from the Spinner with live text events, a fitting finale that ties the unit together.

Summary

Key takeaways

  • A Spinner is a dropdown; populate it with an ArrayAdapter and read the choice with getSelectedItem().
  • Use a Spinner for one choice from a longer list (RadioButtons suit a few options).
  • An AlertDialog is a modal question built with AlertDialog.Builder: setTitle, setMessage, setPositiveButton/setNegativeButton, then show().
  • An AlertDialog blocks until answered (a real decision); a Toast just flashes (passive feedback).
  • A Spinner needs an adapter, and an AlertDialog needs show(), or nothing appears.
  • Memory hook: a drop-down menu (Spinner) and a security gate (AlertDialog).

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Other Android Widgets (UI)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Spinner, AlertDialog · Mobile Application Development - 1 (option B) · Gri-Learn