Limit text length in edit text

I want to limit the text length of the text of the text of the ui element through code:

EditText et = (EditText) parent.findViewById(R.id.smsBody);; int maxLength = 300; InputFilter[] FilterArray = new InputFilter[1]; FilterArray[0] = new InputFilter.LengthFilter(maxLength); et.setFilters(FilterArray); 

But this gives me a null pointer exception on the last line. Any ideas on something wrong? These codes are in the onPostExecute method of my async task class. Parent is the main activity.

+4
source share
1 answer

You can insert your .xml file under your editing text:

Android: MaxLength = "300"

You do not need any other material in your .java file. Your .xml file controls things like color, length, size, etc.

+4
source

Source: https://habr.com/ru/post/1388475/


All Articles