Android
Input Dialog
Text Input
Android Development
User Interface

Input text dialog Android

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

An input-text dialog in Android is usually built with an EditText inside an AlertDialog or Material dialog. The mechanics are simple, but a usable implementation also needs to think about validation, soft-keyboard behavior, and how the entered text gets back to the caller cleanly.

Build a Basic Text Input Dialog

For a straightforward prompt, create an EditText, place it in a dialog, and read the value in the positive button callback.

java
1import android.app.AlertDialog;
2import android.content.Context;
3import android.text.InputType;
4import android.widget.EditText;
5
6public class DialogUtil {
7    public static void showNameDialog(Context context) {
8        final EditText input = new EditText(context);
9        input.setHint("Enter your name");
10        input.setInputType(InputType.TYPE_CLASS_TEXT);
11
12        new AlertDialog.Builder(context)
13            .setTitle("Profile name")
14            .setView(input)
15            .setPositiveButton("Save", (dialog, which) -> {
16                String value = input.getText().toString().trim();
17                System.out.println("User typed: " + value);
18            })
19            .setNegativeButton("Cancel", null)
20            .show();
21    }
22}

This covers the basic pattern: input field, confirm button, cancel button.

Handle Validation Without Closing Too Early

The default positive-button listener dismisses the dialog immediately. If empty input or invalid values should keep the dialog open, attach a click listener after show().

java
1AlertDialog dialog = new AlertDialog.Builder(context)
2    .setTitle("Project name")
3    .setView(input)
4    .setPositiveButton("Create", null)
5    .setNegativeButton("Cancel", null)
6    .create();
7
8dialog.setOnShowListener(d -> {
9    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
10        String text = input.getText().toString().trim();
11        if (text.isEmpty()) {
12            input.setError("Name is required");
13            return;
14        }
15        System.out.println("Creating project: " + text);
16        dialog.dismiss();
17    });
18});
19
20dialog.show();

This is much better for real forms because invalid input does not force the user to reopen the dialog.

Make the Keyboard and Input Type Match the Use Case

The dialog is more usable when the input type is correct. Text, number, email, and password inputs should not all behave the same.

java
input.setInputType(
    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
);

Other examples:

  • 'TYPE_CLASS_NUMBER for numeric entry'
  • 'TYPE_TEXT_VARIATION_PASSWORD for passwords'
  • 'TYPE_TEXT_FLAG_CAP_SENTENCES for sentence-style text'

You can also request the soft keyboard once the dialog is visible so the user can start typing immediately.

Return the Result Through a Callback

Instead of hardwiring the dialog to one activity method, use a small callback interface. That makes the dialog reusable.

java
public interface TextResultListener {
    void onTextConfirmed(String text);
}
java
1public static void showInputDialog(Context context, TextResultListener listener) {
2    final EditText input = new EditText(context);
3
4    new AlertDialog.Builder(context)
5        .setTitle("Enter value")
6        .setView(input)
7        .setPositiveButton("OK", (dialog, which) -> {
8            if (listener != null) {
9                listener.onTextConfirmed(input.getText().toString());
10            }
11        })
12        .setNegativeButton("Cancel", null)
13        .show();
14}

This keeps dialog construction separate from the business logic that receives the result.

Use Material Components When the App Uses Material Styling

If the rest of the app uses Material Components, prefer MaterialAlertDialogBuilder for visual consistency. The dialog logic is the same, but the component better matches modern Android theming.

The structural choice stays unchanged:

  • create EditText
  • attach it as the dialog view
  • validate input
  • return the result

The styling layer should not change the data-flow pattern.

Common Pitfalls

  • Reading the input in the default positive-button callback when validation should keep the dialog open dismisses too early.
  • Using the wrong input type makes the keyboard and text behavior feel clumsy.
  • Hardcoding dialog behavior directly into one activity reduces reuse and testability.
  • Forgetting to trim input can create values that look non-empty but are just spaces.
  • Using a raw dialog for multi-field forms can make the UI awkward when a full screen or bottom sheet would be clearer.

Summary

  • The standard Android input dialog uses an EditText inside an AlertDialog.
  • Use post-show() button listeners when validation should prevent premature dismissal.
  • Set the right input type so keyboard behavior matches the expected text.
  • Return the entered value through a callback instead of coupling the dialog to one screen.
  • Use Material dialog builders when the app already follows Material styling.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.