194

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?

span
  • 5,405
  • 9
  • 57
  • 115
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

11 Answers11

380

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);
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
Sephy
  • 50,022
  • 30
  • 123
  • 131
90

Try this

int maxLengthofEditText = 4;    
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLengthofEditText)});
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    This works for me, but in Android 5.1 you can still continue to type letters, the are yust "invisible" in the inputfield. But they are shown in the text proposal. And when you try to delete letters on the end. – Radon8472 Dec 03 '15 at 15:14
  • 13
    This is not "another way" this is the short version of the first answer, same way. – Ninja Coding Jan 11 '16 at 17:22
24

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)

Kevin
  • 1,405
  • 15
  • 16
  • 1
    why not use setMaxLength as function name ? you could apply this to textview also... thanks +1 :) – crgarridos Oct 11 '17 at 09:42
  • I have other methods that follow this pattern: limitDecimalPlaces, limitNumberOnly, limitAscii to go along with limitLength. – Kevin Oct 13 '17 at 18:37
  • 1
    filters = filters.plus(InputFilter.LengthFilter(max)) Don't overwrite existing ones – ersin-ertan Oct 30 '18 at 04:20
22

Easy way limit edit text character :

EditText ed=(EditText)findViewById(R.id.edittxt);
ed.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Farid Ahmed
  • 699
  • 6
  • 8
15

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.)

CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • 1
    Its create bug if you want to change length latter like in my case I change MaxLength from 10 to 20, but as in code we add filter its remain set MaxLength 10 bcus now in array we have 10,20 two max lengths. – Nikhil Jun 29 '20 at 14:35
  • @Nikhil, agree with you, thanks! Yes, in this case we should first remove one filter (`LengthFilter(10)`) and then add another (`LengthFilter(20)`). – CoolMind Jun 29 '20 at 15:49
8

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))
Fatih Santalu
  • 4,641
  • 2
  • 17
  • 34
3

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)
1

My solution for SWIFT 5

editText.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(123))
Serhii Stakhiv
  • 936
  • 7
  • 11
0
     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);
0

best solution i found

textView.setText(text.substring(0,10));
saigopi.me
  • 14,011
  • 2
  • 83
  • 54
0

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);
hanswim
  • 1,182
  • 8
  • 20