Alert Dialog with EditText in Android Example with Source Code
Alert Dialog with EditText in Android Example with Source Code
Here we will learn how to user Alert Dialog with EditText in Android example with source code description. Also you can download complete source code from here. It’s very simple. We have already discussed how to create and use a simple Alert Dialog in Android.
Now we will learn how to use an EditText with an Alert Dialog in Android, so that you can get required text inputs (Example getting User Name and Password from user) along with OK/YES, CANCEL/NO button. If you want to learn how to use simple alert dialog in Android, then you might like my article ‘Alert Dialog in Android Example with Source Code and Description‘.
Only we will add 2 more lines of code with simple Alert Dialog source code. That’s it. However we will find the complete source code for Alert Dialog with EditText in Android here.
Code Snippet for EditText in Alert Dialog in Android
Here we will add the below 2 lines of code along with simple Alert Dialog source code. Put the below code snippet in your onClick listener code after creating the Alert Dialog. This code snippet will add an extra EditText Box in to the Alert Dialog. We will discuss complete code later in this post.
|
1 2 3 4 5 6 7 |
// Create an EditText view to get user input final EditText input = new EditText(this); //Set an EditText view to the Alert Dialog to get user inputs. //input is the variable form where you will get the input value from user. alert.setView(input); // Here the variable alert is the instance of //Alert Dialog Box. Don’t worry we will discuss with complete source code. |
Note: You can also add more than one EditText by repeating the above steps with different variables.
Steps:
- First, we will use the
AlertDialog.Builderdefaul android class to create the alert box interface for title, message to display, buttons, and button onClick function. - Then We will create a EditText Box dynamically and we will attach this EditText box with the simple Alert Dialog.
- Then We will attach above builder to
AlertDialog. - Then we will display it.
- Done.
Complete Alert Dialog with EditText Box Source Code and Description
We have used one button for the Alert Dialog project, so that we can show the Alert Dialog Box when we will click on that button.
- Create new android project [File >> New >> Android Project] with Project Name: AlertDialogExample
- Click next and select target android device version [I chose version 2.2]
- Click next and enter package name – ‘com.techblogon.alertdialog’
- Click finish
Layout creation:
Default Application layout – main.xml:
Open main.xml under /res/layout and replace it with the below XML.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click on Show Alert Dialog to show the Alert Dialog" /> <Button android:id="@+id/button_show_alert_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Show Alert Dialog" /> </LinearLayout> |
Now Coming to the Java File (AlertDialogExample.java)
Here we will find the complete java code for Android Alert Dialog.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.techblogon.alertdialog; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class AlertDialogExample extends Activity { /** Called when the activity is first created. */ // this context will use when we work with Alert Dialog final Context context = this; // just for test, when we click this button, we will see the alert dialog. private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button_show_alert_dialog); //Create onclick listener class button.setOnClickListener(new View.OnClickListener() { //When you click the button, Alert dialog will be showed public void onClick(View v) { /* Alert Dialog Code Start*/ AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here alert.setMessage("Enter Your Name Here"); //Message here // Set an EditText view to get user input final EditText input = new EditText(context); alert.setView(input); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //You will get as string input data in this variable. // here we convert the input to a string and show in a toast. String srt = input.getEditableText().toString(); Toast.makeText(context,srt,Toast.LENGTH_LONG).show(); } // End of onClick(DialogInterface dialog, int whichButton) }); //End of alert.setPositiveButton alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. dialog.cancel(); } }); //End of alert.setNegativeButton AlertDialog alertDialog = alert.create(); alertDialog.show(); /* Alert Dialog Code End*/ }// End of onClick(View v) }); //button.setOnClickListener }//End of onCreate }//class AlertDialogExample extends Activity |
You can also download complete code from here Alert Dialog Example With EditText in Android.
That’s It. Enjoy your Alert Dialog in Android. For other types of Alert Dialog, You can click Here or click on ”Previous Page” button below.
Post By SmrutiRanjan (46 Posts)
Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.
Website: → Techblogon








Previous Page
Android Tutorial Home Page









I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon". Techblogon is a technology blog. We regularly update this blog with really nice and helpful tips n tricks, latest updates on new technologies. I am also an active contributor for the blog-"Gadgets and Gizmos World".
Job is my necessity, but blogging is my passion.
Connect with me on
Adding this to twitter great info.
hi Ranjan ,
Can you please help me out got struck ed,I wanted to create registration form as pop-up or dialog ,trying from past one week ,unable to get it …please give me is you have source code ….thnks in advance
have a look
http://techblogon.com/android-login-registration-screen-with-sqlite-database-example/
I just like the valuable information you supply in your articles. I will bookmark your blog and take a look at again right here regularly. I am somewhat sure I will be informed many new stuff proper right here! Best of luck for the following!
i think its awwsm but tell me can we take 2 edit text and 2 button ,,,,,, and 2 text views and 2 swwkbars in dialog box ,,, ?????????? please
yes you can, what ever you will add in the xml file it will show on the dialog
Select all or required packages & proceed.
get rid of the tweet bird man it’s annoying. good article though
I will remove it asap.