Create custom AlertDialog with EditText – button inside it android

How to set/show EditText inside alertdialog in android.

In this tutorial we are creating a EditText and button inside custom layout via another layout XML file and after that setting up that whole layout as alert dialog layout. So when user click on the button it will popup the whole layout just like alert dialog screen comes. So here is the complete step by step tutorial for Create custom AlertDialog with EditText – button inside it android.

android-project-download-code-button

How to Create custom AlertDialog with EditText – button inside it android.

Code for MainActivity.java file.

 package com.customalertdialogwithedittext_android_examples.com;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview;
 Button button,Submit;
 AlertDialog.Builder builder;
 LayoutInflater layoutinflater;
 EditText edittext;
 AlertDialog alertdialog;
 String EditTextValue;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 button = (Button)findViewById(R.id.buttonMain);

 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 
 builder = new AlertDialog.Builder(MainActivity.this);

 layoutinflater = getLayoutInflater();
 
 View Dview = layoutinflater.inflate(R.layout.edittext_with_button,null);

 builder.setCancelable(false);

 builder.setView(Dview);
 
 edittext = (EditText) Dview.findViewById(R.id.editText1);
 Submit = (Button) Dview.findViewById(R.id.button1);

 alertdialog = builder.create();

 Submit.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 
 alertdialog.cancel();
 
 EditTextValue = edittext.getText().toString();
 
 textview.setText(EditTextValue);
 }
 });
 
 alertdialog.show();
 }
 });
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.customalertdialogwithedittext_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/buttonMain"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click here to get value from USER" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/buttonMain"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="15dp"
 android:text="NO VALUE ENTERED"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Code for edittext_with_button.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="14dp"
 android:text="Click Here to Get EditText Value" />

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:ems="10"
 android:hint="Type Text Here" >

 <requestFocus />
 </EditText>

</RelativeLayout>

Screenshots:

alertdialog-1

Create custom AlertDialog with EditText - button inside it android

set-alertdialog

Click here to download Create custom AlertDialog with EditText-button inside it android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *