How to programmatically set maxLength in Android TextView?

AndroidTextviewMaxlength

Android Problem Overview


I would like to programmatically set maxLength property of TextView as I don't want to hard code it in the layout. I can't see any set method related to maxLength.

Can anyone guide me how to achieve this?

Android Solutions


Solution 1 - Android

Should be something like that. but never used it for textview, only edittext :

TextView tv = new TextView(this);
int maxLength = 10;
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(maxLength);
tv.setFilters(fArray);

Solution 2 - Android

Try this

int maxLengthofEditText = 4;    
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLengthofEditText)});

Solution 3 - Android

Easy way limit edit text character :

EditText ed=(EditText)findViewById(R.id.edittxt);
ed.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});

Solution 4 - Android

For those of you using Kotlin

fun EditText.limitLength(maxLength: Int) {
    filters = arrayOf(InputFilter.LengthFilter(maxLength))
}

Then you can just use a simple editText.limitLength(10)

Solution 5 - Android

As João Carlos said, in Kotlin use:

editText.filters += InputFilter.LengthFilter(10)

See also https://stackoverflow.com/a/58372842/2914140 about some devices strange behaviour.

(Add android:inputType="textNoSuggestions" to your EditText.)

Solution 6 - Android

For Kotlin and without resetting previous filters:

fun TextView.addFilter(filter: InputFilter) {
  filters = if (filters.isNullOrEmpty()) {
    arrayOf(filter)
  } else {
    filters.toMutableList()
      .apply {
        removeAll { it.javaClass == filter.javaClass }
        add(filter)
      }
      .toTypedArray()
  }
}

textView.addFilter(InputFilter.LengthFilter(10))

Solution 7 - Android

I made a simple extension function for this one

/**
 * maxLength extension function makes a filter that 
 * will constrain edits not to make the length of the text
 * greater than the specified length.
 * 
 * @param max
 */
fun EditText.maxLength(max: Int){
    this.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(max))
}

editText?.maxLength(10)

Solution 8 - Android

My solution for SWIFT 5

editText.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(123))

Solution 9 - Android

     AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Title");
   
  
                    final EditText input = new EditText(this);
                    input.setInputType(InputType.TYPE_CLASS_NUMBER);
//for Limit...                    
input.setFilters(new InputFilter[] {new InputFilter.LengthFilter(3)});
                    builder.setView(input);

Solution 10 - Android

best solution i found

textView.setText(text.substring(0,10));

Solution 11 - Android

To keep the original input filter, you can do it this way:

InputFilter.LengthFilter maxLengthFilter = new InputFilter.LengthFilter(100);
        InputFilter[] origin = contentEt.getFilters();
        InputFilter[] newFilters;
        if (origin != null && origin.length > 0) {
            newFilters = new InputFilter[origin.length + 1];
            System.arraycopy(origin, 0, newFilters, 0, origin.length);
            newFilters[origin.length] = maxLengthFilter;
        } else {
            newFilters = new InputFilter[]{maxLengthFilter};
        }
        contentEt.setFilters(newFilters);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUMAR-MOBITSOLUTIONSView Question on Stackoverflow
Solution 1 - AndroidSephyView Answer on Stackoverflow
Solution 2 - AndroidIntelliJ AmiyaView Answer on Stackoverflow
Solution 3 - AndroidFarid AhmedView Answer on Stackoverflow
Solution 4 - AndroidKevinView Answer on Stackoverflow
Solution 5 - AndroidCoolMindView Answer on Stackoverflow
Solution 6 - AndroidFatih SantaluView Answer on Stackoverflow
Solution 7 - AndroidKyriakos GeorgiopoulosView Answer on Stackoverflow
Solution 8 - AndroidSerhii StakhivView Answer on Stackoverflow
Solution 9 - AndroidNull Pointer ExceptionView Answer on Stackoverflow
Solution 10 - Androidsaigopi.meView Answer on Stackoverflow
Solution 11 - AndroidhanswimView Answer on Stackoverflow