Wiktor
Wiktor

Reputation: 925

TextInputLayout counter and margin problem

When using TextInputLayout with height set to anything else that wrap_content the counter disappears, and i'd also like to use it with ConstraintLayout, with it's height set to 0dp, with that the counter disappears and there's this margin at the top. Can the height of TextInputLayout be set to 0dp with visible counter and without this margin at the top?

enter image description here

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="50dp"
        app:counterEnabled="true"
        app:counterMaxLength="180"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#2342"
            android:gravity="top"
            android:inputType="textMultiLine|textNoSuggestions" />


    </android.support.design.widget.TextInputLayout>

</android.support.constraint.ConstraintLayout>

Upvotes: 0

Views: 1902

Answers (2)

Samuel
Samuel

Reputation: 231

I found that TextInputLayout inherits from LinearLayout. So I tried some combinations with layout_height and layout_weight. Finally, I found a solution to make TextInputLayout can have any kind of height and counter still visible.

In TextInputEditText, you have to set android:layout_height="match_parent" and android:layout_weight="1"

Yes, it's weird, but it works on API 30 and API 27 😏

screenshot

sample

sample code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="12dp"
    android:orientation="vertical">

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:hint="Label"
        app:counterEnabled="true"
        app:counterMaxLength="10000">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="start"
            android:maxLength="10000" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.checkbox.MaterialCheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Checkbox" />

</LinearLayout>

Upvotes: 1

Ebi Igweze
Ebi Igweze

Reputation: 460

I see what the issue is, you might have to rethink the layout design. The issue is not the TextInputLayout, it is the TextInputEditText. When you set TextInputEditText to match parent, it is matching the height TextInputLayout, hence there is no space left in the TextInputLayout to draw the counter. so if you change that height value of the TextInputEditText to any other value, you will see the counter.

My suggestion is, you either need to rethink your designed layout, or you would have to programmatically set the height of the InputEditText, depending on the activity's window visible height.

Upvotes: 3

Related Questions