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= "http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="16sp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<EditText
android:id="@+id/txtinput"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btdone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_toRightOf="@id/txtinput"
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="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: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>
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText txt=(EditText)findViewById(R.id.txt);
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.setContentView(R.layout.dialogview);
// Dialog message
TextView tvMess = (TextView) dialog.findViewById(R.id.txtmessage);
tvMess.setText(message);
tvMess.setTextColor(Color.parseColor("#ff2222"));
//Done button
Button btDone = (Button) dialog.findViewById(R.id.btdone);
// EditText to allow user input
final EditText txtInput = (EditText) dialog.findViewById(R.id.txtinput);
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
dialog.show();
}
}
|
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.