UMAR-MOBITSOLUTIONS
UMAR-MOBITSOLUTIONS

Reputation: 77984

How to programmatically set maxLength in Android TextView?

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?

Upvotes: 194

Views: 106966

Answers (11)

Serhii Stakhiv
Serhii Stakhiv

Reputation: 1026

My solution for SWIFT 5

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

Upvotes: 1

Kyriakos Georgiopoulos
Kyriakos Georgiopoulos

Reputation: 567

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)

Upvotes: 3

Fatih Santalu
Fatih Santalu

Reputation: 4701

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

Upvotes: 8

CoolMind
CoolMind

Reputation: 28773

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

Upvotes: 15

hanswim
hanswim

Reputation: 1242

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

Upvotes: 0

saigopi.me
saigopi.me

Reputation: 14908

best solution i found

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

Upvotes: 0

Null Pointer Exception
Null Pointer Exception

Reputation: 1583

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

Upvotes: 0

Kevin
Kevin

Reputation: 1443

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)

Upvotes: 24

Farid Ahmed
Farid Ahmed

Reputation: 719

Easy way limit edit text character :

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

Upvotes: 22

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

Try this

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

Upvotes: 90

Sephy
Sephy

Reputation: 50392

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

Upvotes: 380

Related Questions