Android: Setting Max Length Text Field

Posted on

I have just started getting into Android programming and it is becoming quite fun. I feel the power of the XML GUI editor, but it can be quite annoying. My first “wtf” moment was when I was creating text fields (known as EditText) in the XML. I wanted to limit the number of characters the user enters and easy enough it was simple to do.

Below is the XML for creating a text username login text field limited to 30 characters. The property that limits it is android:maxLength

<EditText
        android:id="@+id/login_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/login_username"
        android:gravity="center"
        android:maxLength="30" >
      <requestFocus />
</EditText>

So this is great! And whats even better is that because I’m coding in Eclipse I can utilize the auto-complete feature (CTRL+SPACE) to see all possible properties allowed. OK so now I need to create another text field but this time programatically. Not because I wanna make my life difficult but because the program I was doing needed it.

So I create the EditText object and all that and then I am able to apply all the other properties except for android:maxLength. Why? Don’t ask me really. I don’t know if it is bad design or there was some really good reason to do so. You have to create a filter, fair enough….. no not really. Sorry, instead of having a developer friendly method like addFilter() they give us this setFilters() which takes an array of filters. This is annoying.

LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 10);

//Get my text to display on it from the strings.xml file
String displayText = getApplicationContext().getResources().getString(R.string.login_displayText);

//Create the array of filters and add my length filter to it	
InputFilter[] myTextFilterArray = new InputFilter[1];
myTextFilterArray[0] = new InputFilter.LengthFilter(5);
			
EditText myEditText = new EditText(this);
myEditText.setText(displayText);
myEditText.setLayoutParams(lp);
myEditText.setGravity(Gravity.CENTER_HORIZONTAL);
myEditText.setEms(10);
myEditText.setFilters(otpFilterArray);

//Add it to the layout			
linearLayout.addView(myEditText);

When you just want to add 1 filter you are going to be forced to create an array and and set its length and add your filter. This is tedious at best and it causes you to waste some time. Not only that it is a bit counter intuitive. Being a Java programmer we are used to the conventions of having add(), remove() and set() methods. If you can add multiple filters to an object then you should implement add() and remove() at least.

Advertisements

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s