Some cool android text and time hacks for developer

In recent development I have come to know with some really cool utilities or “programming hacks”, which may go unnoticed for you but chances are that you are going to use them a lot once you are familiar with it.

Note: I don’t know how much they are performance friendly or how good they are compared to any of the equivalent solutions exist. Expert advice are always welcome.

So lets start with them.

1. Time since last……

Ever wondered how android social networking apps show “X minutes(seconds) ago” text (somewhere) near their status updates? do they fetch them from servers? No, there is a utility for that,

DateUtils.getRelativeTimeSpanString method gives you opportunity to show how much time is spent till now since “last status was updated” or “last run was scored” or even “you crossed that location”. Here is the demo for you are looking for

// Here we will go back to each minute and see how this tool will work
private void relativeTimeSpanStringDemo(TextView text) {
long now = System.currentTimeMillis();
long time = System.currentTimeMillis();
String textToShow = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0; i < 10; i++) {
time -= 60 * 1000; // subtract 1 minute
final String formatText = sdf.format(new Date(time));
textToShow += "\n"
+ formatText
+ " - "
+ DateUtils.getRelativeTimeSpanString(time, now, DateUtils.SECOND_IN_MILLIS,

2. Expandable templates

Suppose in your application you have to write this statement gazillion of times

“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}”

How many concatenations you have to make to give sense to the above string? atleast 8, and what if this string is being generated at too many places and you have neither spirit nor opportunity to have a common logic to be applied? You may be doomed, you are going to handover your republic to an evil sith empire. You will think a common solution can not exist. And you start to have a bad feeling about this…..

But wait, there is A New Hope, just use expandTemplate method. As shown below.

private void expandTemplateDemo(TextView text) {
String template = "You have been invited to ^1 by ^2";
text.setText(TextUtils.expandTemplate(template, "Barca VS PSG match", "Prasham Trivedi"));

This method except at most 9 variables.

And Many More………..

Now you have a status that has been liked by harry, ron, hermione, lucius fox, han solo and even the great odin of asgrad!! And you are Bruce Wayne or Commissioner Gordon, only care about people from your universe the rest are numbers. How you would represent this in a proper way?

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

Note: It will require two additional variables 1. paint and 2. width. The paint variable can be created by calling new Paint(), if you have access to any textView, you can get the same by calling textView.getPaint…

Width can be measured by calling paint.measureText(), argument passed here should be the text you want to retain…

Here is another football world example…

private void commaEllipsizeDemo(TextView text) {
 final String string = "Barca,Madrid,Gala,PSG,Dortmund,Munich,Juventus,Malaga";
 final TextPaint paint = text.getPaint();
 final float measureText = paint.measureText("Barca,Madrid,Gala,PSG,Dortmund");
 text.setText(TextUtils.commaEllipsize(string, paint, measureText, " and one more",
 " and %d more"));

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s