Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 762
Victor Grazi Avatar asked Dec 28 '22 04:12

Victor Grazi


1 Answers

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>
like image 124
Jammy Lee Avatar answered Jan 08 '23 11:01

Jammy Lee