Android edittext dialog

Dialog is a window that floats over other windows. In this Android tip, I am going to show you how to create an EditText dialog that allows user to input text to an EditText on the main activity.
There are different ways to create an EditText dialog. The way that i am talking here is using the Dialog class. The Dialog class can accept a custom view or layout. In the custom view, you place widgets that need to be displayed on the dialog. In case of EditText dialog, you should have a TextView, an EditText, and Button. The TextView displays message to the user. The EditText allows data input. Android the Button is clicked to pass the input data to a TextView on the main activity and close the dialog.
Now in the res/layout directory, you create dialogview.xml file as shown below. It is the custom view of the dialog.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=  ""


        android:layout_alignParentRight="true"        />
   </RelativeLayout> </LinearLayout>
You open the activity_main.xml file and add an EditText. The EditText will receive the input text from the dialog.

<LinearLayout xmlns:android=""    xmlns:tools=""    android:layout_width="match_parent"
   android:layout_height="match_parent"    android:orientation="horizontal"
   android:padding="10dp"    tools:context=".MainActivity"   >    <EditText    android:id="@+id/txt"    android:layout_width="match_parent"    android:layout_height="wrap_content">    </EditText> </LinearLayout>

In the MainActivity class, you write code to display the EditText dialog as shown below.
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
   @Override    protected void onCreate(Bundle savedInstanceState) {
     EditText txt=(EditText)findViewById(;
     showEditTextDialog(this,"Please input phone number",txt);
   public void showEditTextDialog(final Context context, String message, final EditText txt) {
    // Create dialog instance
     final Dialog dialog = new Dialog(context);
     // Set dialog title
     dialog.setTitle("Input box");
     // Set dialog layout that contains radio buttons
     // Dialog message
     TextView tvMess = (TextView) dialog.findViewById(;
     //Done button      Button btDone = (Button) dialog.findViewById(;      // EditText to allow user input      final EditText txtInput = (EditText) dialog.findViewById(;      txtInput.setText(txt.getText().toString());      // Change the EditText input type to that of the EditText of the activity that receives the input text      txtInput.setInputType(txt.getInputType());      btDone.setOnClickListener(new View.OnClickListener() {         @Override
        public void onClick(View view) {            // pass the text to the EditText of the activity            txt.setText(txtInput.getText().toString());            // close the dialog            dialog.dismiss();         }      });
     // show dialog;

edittext dialog

Posted by: Dara | post date: 05-09-2015 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.