Search code examples
androidlayoutandroid-edittext

How to set max lenght message on edittext on android


waht I am trying to do is set a massage o max characters on an edittext. So far I have find out the TextInputLayout, but it is no quite what I need. The edittext is a big field, so I am trying to set a message at the bottom right corner of the edittex that says "max input 120" but inside the edittext, not on the outside. This is what I have so far

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_title"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        app:counterEnabled="true"
        app:counterMaxLength="120"
        >
    <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtComnent"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:hint="write your comments here"
            android:textSize="@dimen/font_size_2"
            android:gravity="top"
            android:padding="15dp"
            android:maxLength="120"/>
</com.google.android.material.textfield.TextInputLayout>

But like I said, it does no quite match my needs. Any help or suggestion would be great, thanks


Solution

  • Try this

    <RelativeLayout
            android:id="@+id/til_title"
            android:layout_width="264dp"
            android:layout_height="136dp"
            android:layout_marginTop="15dp"
            app:counterEnabled="true"
            app:layout_constraintBottom_toTopOf="@+id/btnCancelComment"
            app:layout_constraintTop_toBottomOf="@id/tvTitle"
            tools:ignore="MissingConstraints">
    
        <EditText
                android:id="@+id/edtComment"
                android:layout_width="match_parent"
                android:hint="Escriba sus comentarios aquí. "
                android:gravity="top"
                android:textSize="@dimen/font_size_2"
                android:layout_height="120dp"
                android:maxLength="121"
                android:background="@drawable/rounded_gray_background"/>
    
        <TextView
                android:id="@+id/tv"
                android:layout_below="@id/edtComment"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="end"
                android:paddingEnd="10dp"
                android:textStyle="normal|italic"
                android:textSize="@dimen/font_size_2"
                android:text="Máx 120 carácteres"
                android:background="@drawable/rounded_gray_background"/>
    
    
    </RelativeLayout>