About TextUtils.

Posted on May 9, 2015 , 4 minute read

Table Of Contents

Android SDK has an awesome class called TextUtils. It has many great methods that saves us a lot of time and a lot of embarrassments. I use many of these methods daily. However some of those methods are not ideal for many cases, but with a small workaround you can make them perfect. Let’s take a look at what we can do with TextUtils.

The String, The blank and the null.

They are the good, the bad and the ugly. The normal String is good, the blank is bit bad, but null is ugly. And one of the very basic method of TextUtils saves us not only from ugly, but also from the bad. Calling TextUtils.isEmpty() with a String will return true if String is null or empty (""). Just filter all your string related calls with TextUtils.isEmpty() is false or not and you will never face any crash related to Strings1

Have I told you that this method and many of the methods from TextUtils is not limited to only Strings? It originally accepts CharSequence, which is not only the super class of String but almost all the classes that are used to make or print a Text. Just take a look at the classes inherited by CharSequence (Just expand Known Indirect Subclass).

However there is something to be noted. While dealing with many webservices I have encountered some will send "null"(=null-as-a-value) instead of sending "" or null. This string will pass from TextUtils.isEmpty test because it’s neither null nor blank. It will be super embarrassing when this value is supposed to be shown in UI, and As a workaround I have written this simple method.

It will also filter out null-as-a-value strings and will return true in such cases.

Join the stringsand together they will rule the galaxy forever

When you are joining Strings (We do it often), We need to make sure that our delimiter-often the comma-should not be appended after last item. And our TextUtils.join() method does exactly the same. Look at the code and it’s all clear.2

These are some simple and daily things for using TextUtils. Apart from that there are more bells and whistles from TextUtils like following…3

Expandable templates

Suppose one of your statement in UI looks like this.

“You have been invited to {event} by {user} , at {place} on {time}. The event will be finished by {time}, in any queries please contact {me} on {this email id}” To fill the value, how many variables to be replaced in the above statement? 8, it’s ok if you have the string in strings.xml (remember Tip3.4?), but what if this string is generated on server? Or generating this string is -for some reason- not possible from strings.xml? TextUtils.expandTemplate() is there to help.

This methods will look for placeholders with carets (like ^1 ,^2 upto 9). And unlike placeholders in strings.xml, we can convert anything into a string and pass in expand template. Look at this example

And Many More…….

Now you have a facebook status that has been liked by Harry, Ron, Hermione, Lucius fox, Han solo and even the great Odin of Asgrad!! And you only care about people from your planet, the rest can just be numbers. How you would represent this in a proper way so that likes shoud read

“Liked by Harry,Ron,Hermione,Lucius and 2 others”

Just call TextUtils.commaEllipsize(). And you will have a result.

Here is another football world example. To understand the code, pay attention to the comments.

Note: If there is more width available for the text view than required, your text may not be ellipsized at all. The ellipsis operation is dependent on the width available and the text we want to ellipsize.

  1. One of the most embarrassing and equally funny thing I have noted is there are many crashes which are caused by printing blank or null in a Log. ↩︎

  2. TextUtils also has split method, but it does not cover null or null as a value, so to be failsafe you need to have your own code. ↩︎

  3. Originally written here ↩︎

See Also



- TextUtils