0

I have two EditTexts side by side in my application.

This is what they look like with 1 line of input

enter image description here

Currently when I type multiple lines of input in one EditText grows but the other stays the same height - as seen here

enter image description here

I want the EditTexts to match in height. When one grows the other matches it's height - as seen here

enter image description here

Not sure how to achieve this effect. I don't want to fill in the other EditText with new lines. I want the actual View to grow.

This is my current layout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="10dp"
    android:weightSum="6"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:inputType="textMultiLine"
        android:gravity="top|start"/>
    <EditText
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:inputType="textMultiLine"
        android:gravity="top|start"/>
</LinearLayout>
Gary Holiday
  • 3,297
  • 3
  • 31
  • 72

3 Answers3

0

Try to change the height of the other edittext in the onTextChange method... example

 yourEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //Change your other layout height here 

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • Leave that simply what i am saying is change the other Edittext width from the onTExtChanged method.. – Amsheer Aug 01 '16 at 04:19
0

Please try the following,

<LinearLayout
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="2">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:orientation="vertical"
    android:padding="5dp">

    <EditText
        android:gravity="top|start"
        android:id="@+id/left"
        android:inputType="textMultiLine"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_width="match_parent" />
</LinearLayout>

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:orientation="vertical"
    android:padding="5dp">

    <EditText
        android:gravity="top|start"
        android:id="@+id/right"
        android:inputType="textMultiLine"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_width="match_parent" />
</LinearLayout>

</LinearLayout>
LvN
  • 701
  • 1
  • 6
  • 22
0

I did some research and came across this answer. Based on the answer linked, I created this solution:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:weightSum="6">

    <View
        android:id="@+id/strut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@id/strut"
        android:gravity="top|start"
        android:layout_alignBottom="@+id/right"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/strut"
        android:layout_alignParentRight="true"
        android:gravity="top|start"
        android:inputType="textMultiLine" />
</RelativeLayout>

In your java code do this:

        editTextLeft = (EditText) findViewById(R.id.left);
        editTextRight = (EditText) findViewById(R.id.right);
        editTextLeft.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    RelativeLayout.LayoutParams leftLayoutParams = (RelativeLayout.LayoutParams) editTextLeft.getLayoutParams();
                    RelativeLayout.LayoutParams rightLayoutParams = (RelativeLayout.LayoutParams) editTextRight.getLayoutParams();
                    leftLayoutParams.removeRule(RelativeLayout.ALIGN_BOTTOM);
                    rightLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM,editTextLeft.getId());
                    int lines = editTextRight.getLineCount() > editTextLeft.getLineCount() ? editTextRight.getLineCount() : editTextLeft.getLineCount();
                    editTextLeft.setMinLines(lines);
                    editTextRight.setMinLines(lines);
                    editTextLeft.setLayoutParams(leftLayoutParams);
                    editTextRight.setLayoutParams(rightLayoutParams);

                }
            }
        });
        editTextRight.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b) {
                    RelativeLayout.LayoutParams leftLayoutParams = (RelativeLayout.LayoutParams) editTextLeft.getLayoutParams();
                    RelativeLayout.LayoutParams rightLayoutParams = (RelativeLayout.LayoutParams) editTextRight.getLayoutParams();
                    rightLayoutParams.removeRule(RelativeLayout.ALIGN_BOTTOM);
                    leftLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, editTextRight.getId());
                    int lines = editTextRight.getLineCount() > editTextLeft.getLineCount() ? editTextRight.getLineCount() : editTextLeft.getLineCount();
                    editTextLeft.setMinLines(lines);
                    editTextRight.setMinLines(lines);
                    editTextLeft.setLayoutParams(leftLayoutParams);
                    editTextRight.setLayoutParams(rightLayoutParams);
                }
            }
        });
Community
  • 1
  • 1
Eric B.
  • 4,622
  • 2
  • 18
  • 33