Multimedia EditText for Android AlertDialog

Is there a way to create a multi-line EditText in an AlertDialog in Android . I set setLines and it displays a larger EditText for multiple lines. but when I type, it does not go to the next line and still prints on the same line . Here is my code.

Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Comment");

        final EditText input = new EditText(this);

        final String item_value = ItemList.get(position).get("comment");

        input.setText(item_value);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        input.setLines(5);
        input.setMaxLines(5);
        input.setGravity(Gravity.LEFT | Gravity.TOP);
        builder.setView(input);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


            }
        });

        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

and my dialog box looks like this: enter image description here

since i can fix it. Thank you and welcome.

+4
source share
5 answers

try this code for your EditText:

input.setSingleLine(false);  //add this
input.setLines(4);
input.setMaxLines(5);
input.setGravity(Gravity.LEFT | Gravity.TOP);
input.setHorizontalScrollBarEnabled(false); //this
+6
source

input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE) input.setSingleLine(false). , -

Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Comment");

        final EditText input = new EditText(this);

        final String item_value = ItemList.get(position).get("comment");

        input.setText(item_value);
        input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        input.setSingleLine(false)
        input.setLines(5);
        input.setMaxLines(5);
        input.setGravity(Gravity.LEFT | Gravity.TOP);
        builder.setView(input);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


            }
        });

        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
+3

This works for me. You can use this property for your EditText work time.

<EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:inputType="textMultiLine"
        android:lines="8"
        android:maxLines="10"
        android:minLines="6"
        android:scrollbars="vertical" />
+2
source
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Comment");

final EditText input = new EditText(this);

final String item_value = ItemList.get(position).get("comment");

input.setText(item_value);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setSingleLine(false);
input.setMaxLines(5);
input.setGravity(Gravity.LEFT | Gravity.TOP);
builder.setView(input);

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog, int whichButton) {

    }
});

builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
    }
});
AlertDialog alert = builder.create();
alert.show();
+1
source

Set the attribute android:inputType="textMultiLine"to your XML.

If this does not work, you need to implement TextWatcherand manually split the lines.

-1
source

Source: https://habr.com/ru/post/1670266/


All Articles