Victor Grazi
Victor Grazi

Reputation: 16480

How can I create a text entry dialog in Android?

What is the best way to create a modal text entry dialog for android?

I want to block until the user enters the text and hits the ok button, then extract the text from the dialog, just like a modal dialog in awt.

Thanks!

Upvotes: 4

Views: 5772

Answers (3)

Jammy Lee
Jammy Lee

Reputation: 1516

Try this:

final Dialog commentDialog = new Dialog(this);
commentDialog.setContentView(R.layout.reply);
Button okBtn = (Button) commentDialog.findViewById(R.id.ok);
okBtn.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                    //do anything you want here before close the dialog
                                    commentDialog.dismiss();
                            }
 });
Button cancelBtn = (Button) commentDialog.findViewById(R.id.cancel);
cancelBtn.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                    commentDialog.dismiss();
                            }
 });

reply.xml

<?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"       android:background="#ffffff">

    <EditText android:layout_width="fill_parent"
            android:layout_gravity="center" android:layout_height="wrap_content"
            android:hint="@string/your_comment" android:id="@+id/body" android:textColor="#000000"
            android:lines="3" />
    <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <Button android:id="@+id/ok" android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:text="@string/send"  android:layout_weight="1"
                     />
            <Button android:id="@+id/cancel" android:layout_height="wrap_content"
                    android:layout_width="wrap_content"  android:layout_weight="1"
                    android:text="@android:string/cancel" />
    </LinearLayout>
</LinearLayout>

Upvotes: 7

Paresh Mayani
Paresh Mayani

Reputation: 128428

You can create a custom dialog with XML layout. And as you want to block it until the user enters the text and hits the ok button, you can make it setCancelable(false).

Check Google Search links: Dialog With EditText

Upvotes: 2

Chris.Jenkins
Chris.Jenkins

Reputation: 13129

I think you mean a Modal dialog box.

I would say Look at AlertDialog.Builder As a start, the newer way of doing it is using an DialogFragment which gives you more control over the dialog life cycle.

The key method you are looking for is dialog.setCancelable(false);

Upvotes: 1

Related Questions