Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText.setMaxLines() programmatically not working

I am trying to set Maxline for EditText programmatic, It's not giving extra space on EditText after clearing Text. If I set maxLine in xml the extra space is visible even there is no text in EditText.

How to give these extra space by programmatic way instead of using xml. because I need to use same xml for many Views.

Programmatic code

public void showDescriptionDialog() {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_upload_description);
    final EditText edtDescription = (EditText) dialog.findViewById(R.id.edt_upload_description);
    edtDescription.setHint(context.getString(R.string.description));
    edtDescription.setSingleLine(false);
    edtDescription.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
    edtDescription.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    edtDescription.setMaxLines(5);
    edtDescription.setVerticalScrollBarEnabled(true);
    edtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());
    edtDescription.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
    if (description != null) {
        edtDescription.setText(description);
    }
    dialog.show();


    dialog.findViewById(R.id.img_upload_description_back).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });

    dialog.findViewById(R.id.img_upload_description_done).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            description = edtDescription.getText().toString();
            dialog.dismiss();
        }
    });

    dialog.findViewById(R.id.btn_upload_description_done).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            description = edtDescription.getText().toString();
            dialog.dismiss();
        }
    });
}

XML Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="30dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:padding="10dp">

        <ImageView
            android:id="@+id/img_upload_description_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="10dp"
            android:contentDescription="@string/upload"
            android:src="@drawable/ic_back_white" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:padding="10dp"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="@string/title_description"
            android:textColor="@android:color/white"
            android:textSize="@dimen/font_size_large" />

        <ImageView
            android:id="@+id/img_upload_description_done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="10dp"
            android:contentDescription="@string/done"
            android:padding="8dp"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/ic_tick_white" />

    </RelativeLayout>

    <EditText
        android:id="@+id/edt_upload_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:hint="@string/txt_upload_description_hint"
        android:gravity="top"
        android:textSize="@dimen/font_size_small"
        android:layout_marginTop="30dp"
        android:minLines="5" />

    <Button
        android:id="@+id/btn_upload_description_done"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="20dp"
        android:background="@color/btn_magenta"
        android:text="@string/txt_done"
        android:textColor="@android:color/white" />


</LinearLayout>

If I use android:minLines="5" in EditText then only extra space appears in EditText even there is no text

Problem Screenshot Problem Images

Expected Result Screenshot Expected Result

like image 365
Naveen Kumar M Avatar asked Dec 24 '22 23:12

Naveen Kumar M


2 Answers

You can make your editext responsive my using the following code in your

android:inputType="textMultiLine"
android:layout_height="wrap_content"

and remove

  android:minLines="5"

Regards.

like image 99
abbas piplodawala Avatar answered Dec 26 '22 15:12

abbas piplodawala


set android:inputType="text" in your xml and remove android:minLines = "5".

like image 39
Rajesh Gosemath Avatar answered Dec 26 '22 17:12

Rajesh Gosemath