Alejandro Vargas
Alejandro Vargas

Reputation: 89

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

Upvotes: 1

Views: 319

Answers (3)

jvargas
jvargas

Reputation: 843

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>

Upvotes: 0

skafle
skafle

Reputation: 1025

I think, there is no built in system we have in android. One thing I can suggest is to create a TextView that stays wherever you want over the EditText. In the code, implement the TextWatcher to count the number of character typed and update the counter TextView accordingly.

You can do something like this: You can adjust your text size, margin, padding as per your need. This is just an idea.

<EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:paddingRight="15dp"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/editText"
        app:layout_constraintEnd_toEndOf="@id/editText"
        android:text="0"
        android:textSize="10sp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="5dp"/>

In the code: If you want to show number of characters typed then use this code:

editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)

var characterCount = 0

editText.addTextChangedListener(object : TextWatcher{
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        characterCount++
        textView.text = characterCount.toString()
    }

    override fun afterTextChanged(p0: Editable?) {

    }

})

Result:

Click to See ScreenShot

OR If you want to show text like 'max : 300':

editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)

var textToShow = "max : 300"

editText.addTextChangedListener(object : TextWatcher{
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        textView.text = textToShow
        //Here you might need to set up the padding for edittext accordingly
        //so as not to be typed over the text 'max : 300'
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        
    }

    override fun afterTextChanged(p0: Editable?) {

    }

})

OR If you want to display something like this: 53 characters remaining

editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)

var maxAllowed = 300
var characterCount = 0

editText.addTextChangedListener(object : TextWatcher{
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
       
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
         characterCount++
         var characterRemaining = maxAllowed - characterCount
         textView.text = "$characterRemaining characters remaining"
    }

    override fun afterTextChanged(p0: Editable?) {

    }

})

Upvotes: 2

Anwar Elsayed
Anwar Elsayed

Reputation: 493

you can use the below code

<androidx.constraintlayout.widget.ConstraintLayout
  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="4dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="4dp"
        android:ems="10"
        android:hint="EditText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText"
        android:layout_centerInParent="true"
        android:text="Count Display Here"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/editText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

java code

editText.addTextChangedListener(new TextWatcher() {
     @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        int length = editText.length();
        String convert = String.valueOf(length);
        textView.setText(convert);
     }
     @Override
     public void onTextChanged(CharSequence s, int start, int before, int count) {
     }
     @Override
     public void afterTextChanged(Editable s) { }
  });

Upvotes: 1

Related Questions