Android Studio: Dialogs with User Input

Grace Kim
3 min readJan 11, 2019

Learn about AlertDialogs and EditTexts in Android Studio — what they are, when they are used, and how to implement them.

What Are Dialogs?

Dialogs are a feature on Android Studio that provides some information to the user. It typically prompts the user to make decisions or input information into the program.

It usually does not fit the screen but is rather a small window that allows for the user’s interaction with the app. Some real-life example of dialogs are alarms, timers, and password inputs.

How Can an AlertDialog Accept User Input?

AlertDialogs can accept user input by being paired with an EditText— a user interface element for entering and modifying text —within a custom layout. This can be applied for gathering all sorts of Strings from users — such as usernames, titles, and commentary —and in this case, can be used within a pop-up dialog.

GET READY. You’re about to have an AlertDialog as snazzy as the one below :D

How to Implement an EditText Within a Dialog

  1. Initialize variables for analyzing the user input
ArrayList<CharSequence> arrayListCollection = new ArrayList<>();
ArrayAdapter<CharSequence> adapter;
EditText txt; // user input bar

2. Construct your alert dialog and user input bar (EditText)

Note: a single alert dialog can have more than one input bar! simply duplicate the code below with new variables!

AlertDialog.Builder alertName = new AlertDialog.Builder(this);
final EditText editTextName1 = new EditText(ClassName.this);

3. Code the formatting for your dialog (example: linear layout)

alertName.setTitle(" Alert Dialog Title"); 
// titles can be used regardless of a custom layout or not
alertName.setView(editTextName1);
LinearLayout layoutName = new LinearLayout(this);
layoutName.setOrientation(LinearLayout.VERTICAL);
layoutName.addView(editTextName1); // displays the user input bar
alertName.setView(layoutName);

4. Add a positive and negative button for your dialog

Note: positive and negative buttons can be incorporated in all dialogs, regardless of whether or not it has a custom layout! by all means, not all dialogs need to require user input!

alertName.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
txt = editTextName1; // variable to collect user input
collectInput(); // analyze input (txt) in this method
}
});

alertName.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel(); // closes dialog
alertName.show() // display the dialog

5. Within another method, insert user input into an ArrayList in order to manipulate it within your other coding elements (such as putting the user input into lists, printing out the input, or searching the input for specific words)

public void collectInput(){    // convert edit text to string
String getInput = txt.getText().toString();

// ensure that user input bar is not empty
if (getInput ==null || getInput.trim().equals("")){
Toast.makeText(getBaseContext(), "Please add a group name", Toast.LENGTH_LONG).show();
}
// add input into an data collection arraylist
else {
arrayListCollection.add(getInput);
adapter.notifyDataSetChanged();
}
}

6. Optional: If you would like your alert dialog to look clean and minimal, feel free to add grayed-out text to the user input bar. This faded text will disappear only once your user types something into the bar.

// add line after initializing editTextName1
editTextName1.setHint(" Add Grayed-out Text Here");

Moving Forward?

Congrats! You have finished your very own EditText AlertDialog! From here, you can add aspects such as an additional input bar, input checkers (such as if-else statements that ensure users don’t leave the bar blank or type in duplicates), or even extra buttons! Have fun :)

--

--