Android Quick Tips #1 — TextView

Even if you're spending your entire day in your favorite IDE working in an environment you love, you'll discover small things which blow your mind. Since they provide so much value to us, we'll share our finds in this Android Quick Tips series. We hope to regularly publish a few tips regarding one topic for each blog post.

Android Quick Tips #1 Series Overview

Let's jump into our first quick tips post. This week it's all about the small tricks around Android's TextView.

Android TextView

Every Android developer has used TextViews in the past and might already know some of the following tips. But just in case, check them out:

String Empty && Null-Check

Are you sick of writing the following code a lot:

String someString = getStringFromObjectOrApi(): // this is some external dependency

if (someString != null && !someString.isEmpty()) {  
    // do something with the string, e.g. set on TextView    
}
else {  
    // do something in case the string is null/empty, e.g. hide the TextView
}

The if-clause can be simplified with various external libraries. But that's not necessary since Android ships with the TextUtils class, which contains an isEmpty()-method. Using that method makes the code a bit more readable and faster to write:

String someString = getStringFromObjectOrApi(): // this is some external dependency

if (!TextUtils.isEmpty(someString)) {  
    // do something with the string, e.g. set on TextView    
}
else {  
    // do something in case the string is null/empty, e.g. hide the TextView
}

Limit the Length of Android TextView to the Screen Size

If your app loads data dynamically from a server, you should always take a good look at edge data. What happens if null strings come back? Or on the other end, what happens if really long strings come back? If you display these directly, it might destroy your entire layout and make your app look bad! One simple solution is to limit the text to one line and just let an ellipsis (...) at the end indicate that it's not the complete text. The implementation is adding just two lines to your TextView tag in your layout file:

<TextView  
...
    android:singleLine="true"
    android:ellipsize="end"/>

If you have the case where a TextView can have more than one line, but should still be limited, use maxLines instead. For example, this cuts the text off after three lines:

<TextView  
    ...
    android:maxLines="3"
    android:ellipsize="end"/>

Let the Text Run across the Screen

If you like the previous tip, but are worried that some important content is being hidden by cutting of the text, you can think about using marquee to let the text run across the screen. The configuration is done in XML and very easy:

<TextView  
    ...
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"/>

Be careful with the usage of marquee. You don't want your users to get bad flashbacks to the era of 1990ies websites.

Hint: sometimes marquee is a little tricky to get going in some sublayouts, look at this stackoverflow thread for pointers.

Add Line Breaks to a TextView

Do you have a case where you want to use a line break, but not implement two TextViews to get the wished effect done? There are a lot of crazy solutions out there, ignoring the easiest:

Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.

Quick Tips

This was an experimental first post for the Quick Tips series. Please let us know how you liked it and suggest improvements, if you've any ideas in mind.

Explore the Library

Find interesting tutorials and solutions for your problems.