Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input text dialog Android

When a user clicks a Button in my App (which is printed in a SurfaceView), I'd like a text Dialog to appear and I would like to store the result in a String. I'd like the text Dialog to overlay the current screen. How can I do this?

like image 709
Luke Taylor Avatar asked Jun 05 '12 19:06

Luke Taylor


People also ask

How to get input from dialog box in Android?

You simply need to create an EditText for the user to input data, and set it as the view of the AlertDialog. You can customize the type of input allowed using setInputType, if you need.

What is AlertDialog builder in Android?

Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.

How do I know if my android has dialog?

Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog(). You just have to keep a reference to the Dialogs you create in onCreateDialog().


2 Answers

Sounds like a good opportunity to use an AlertDialog.

As basic as it seems, Android does not have a built-in dialog to do this (as far as I know). Fortunately, it's just a little extra work on top of creating a standard AlertDialog. You simply need to create an EditText for the user to input data, and set it as the view of the AlertDialog. You can customize the type of input allowed using setInputType, if you need.

If you're able to use a member variable, you can simply set the variable to the value of the EditText, and it will persist after the dialog has dismissed. If you can't use a member variable, you may need to use a listener to send the string value to the right place. (I can edit and elaborate more if this is what you need).

Within your class:

private String m_Text = ""; 

Within the OnClickListener of your button (or in a function called from there):

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Title");  // Set up the input final EditText input = new EditText(this); // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); builder.setView(input);  // Set up the buttons builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {      @Override     public void onClick(DialogInterface dialog, int which) {         m_Text = input.getText().toString();     } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {     @Override     public void onClick(DialogInterface dialog, int which) {         dialog.cancel();     } });  builder.show(); 
like image 54
Aaron Avatar answered Sep 23 '22 02:09

Aaron


I will add to @Aaron's answer with an approach that gives you the opportunity to style the dialog box in a better way. Here is an adjusted example:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle("Title"); // I'm using fragment here so I'm using getView() to provide ViewGroup // but you can provide here any other instance of ViewGroup from your Fragment / Activity View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.text_inpu_password, (ViewGroup) getView(), false); // Set up the input final EditText input = (EditText) viewInflated.findViewById(R.id.input); // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text builder.setView(viewInflated);  // Set up the buttons builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {     @Override     public void onClick(DialogInterface dialog, int which) {         dialog.dismiss();         m_Text = input.getText().toString();     }    });  builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {     @Override     public void onClick(DialogInterface dialog, int which) {         dialog.cancel();     }    });   builder.show(); 

Here is the example layout used to create the EditText dialog:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:padding="@dimen/content_padding_normal">      <android.support.design.widget.TextInputLayout         android:layout_width="match_parent"         android:layout_height="wrap_content">          <AutoCompleteTextView             android:id="@+id/input"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:hint="@string/hint_password"             android:imeOptions="actionDone"             android:inputType="textPassword" />      </android.support.design.widget.TextInputLayout> </FrameLayout> 

The final result:

EditText Dialog example

like image 37
Michal Avatar answered Sep 22 '22 02:09

Michal