Multiline TextInputEditText with height wrap_content not restoring size

34 views Asked by At

If have a following layout in xml. It's a part of bigger layout

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layoutInputNewText"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:minHeight="52dp"
        android:layout_marginTop="5dp"
        android:background="@color/white"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/inputMessage"
            style="@style/TextInputLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginTop="0dp"
            android:layout_marginEnd="4dp"
            android:hint="@string/write_a_message"
            android:labelFor="@id/editMessage"
            android:textColorHint="@color/chip_text"
            app:layout_constraintEnd_toStartOf="@id/buttonSend"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:startIconTint="@color/default_green">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editMessage"
                android:layout_width="match_parent"
                android:maxHeight="150dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="4dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:inputType="textMultiLine"
                android:maxLines="3"
                android:paddingStart="20dp"
                android:paddingTop="15dp"
                android:paddingEnd="10dp"
                android:paddingBottom="15dp"
                android:textColor="@color/accent_color_light"
                android:textColorHint="@color/secondary_text_color"
                android:textSize="16sp"
                tools:ignore="Autofill,TextFields" />
        </com.google.android.material.textfield.TextInputLayout>

style of TextInputLayout it following:

    <style name="TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
        <item name="boxBackgroundColor">@android:color/transparent</item>
        <item name="background">@android:color/transparent</item>
        <item name="backgroundTint">@android:color/transparent</item>
        <item name="boxStrokeWidth">0dp</item>
        <item name="android:layout_marginStart">@dimen/main_side_margin</item>
        <item name="android:layout_marginEnd">@dimen/main_side_margin</item>
        <item name="android:layout_marginTop">@dimen/label_to_field_margin</item>
        <item name="colorAccent">@android:color/white</item>
        <item name="errorTextAppearance">@style/ErrorText</item>
        <item name="errorIconDrawable">@null</item>
        <item name="boxStrokeWidthFocused">0dp</item>
        <item name="colorControlNormal">@android:color/transparent</item>
        <item name="colorControlActivated">@android:color/transparent</item>
        <item name="colorControlHighlight">@android:color/transparent</item>
        <item name="android:theme">@style/ThemeOverlay.AppTheme.TextInputEditText.Outlined</item>
    </style>

    <style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined" parent="">
        <item name="colorControlActivated">@color/accent_color_light</item>
    </style>

I set text in code like this: editMessage.setText(message.message) If the text is long I have a multiline TextInputEditText as expected after complete editing and clicking a button I do the following:

fun enableNextMessage(errorMsg: String?) {
        with(binding) {
            buttonSend.isEnabled = true
            if (errorMsg == null) {
                layoutReply.isVisible = false
                inputMessage.startIconDrawable = null
                editText.text = null
            } else showWarning(false, errorMsg)
        }
    }

The problem is that the editText size stays as if it's multiline and has maxlines.

I tried to set editMessageheight in the following way:

editMessage.layoutParams.height = FrameLayout.LayoutParams.WRAP_CONTENT

It doens't help

What helps is do the following:

binding.editMessage.layoutParams.height = resources.getDimensionPixelSize(R.dimen.default_input_size)

But of course after that editMessage is not multiline anymore and doesn't change it's size. I tried to set fixed height and then WRAP_CONTENT. It's not working. As soon as I set WRAP_CONTENT it returns to bigger size.

1

There are 1 answers

0
Al Sh On

What helped is get rid of TextInputLayout and leave only TextInputEditText