Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashamTrivedi/d688e8f3ecf1b1671b80 to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/d688e8f3ecf1b1671b80 to your computer and use it in GitHub Desktop.
TextUtils class
/**
* This is to demo comma ellipsize
* Output will be: 2014 fifa worldcup quarterfinalists were Germany,Argentina,Netherlands,Brasil and 4 more
*/
private void commaEllipsizeDemo(TextView text) {
//Following is the original text, which will be ellipsied
final String string = "Germany,Argentina,Netherlands,Brasil,France,Belgium,Costa Rica,Colombia";
//Fetch the textpaint from above text
final TextPaint paint = text.getPaint();
//Following is the text which will be shown anyway.
String requiredText="Germany,Argentina,Netherlands,Brasil";
//Thus the measurement of above text is required
final float measureText = paint.measureText(requiredText);
//If only one item is going to be truncated, following text will be used
String oneMore=" and one more";
//For others following text will be used, pay attention to %d: no of truncated item is int, so %d is required
String manyMore=" and %d more";
text.setText("2014 fifa worldcup quarter finalists were: "+TextUtils.commaEllipsize(string, paint, measureText, oneMore,
manyMore));
}
/**
* This method Expands template to fill in the value.
*
* @param text
* : textview to fill value in.
* With this example output is
* You have been invited to watch The El Classico by Prasham Trivedi
*/
private void expandTemplateDemo(TextView text) {
String template = "You have been invited to watch ^1 by ^2";
text.setText(TextUtils.expandTemplate(template, "The El Classico", "Prasham Trivedi"));
}
/**
* This method checks if the string is empty or having null value.
*
* @param string
* : Charsequence string to check.
*
* @return <code>true</code> if string is null, blank or having "null" as value
*/
public static boolean isEmptyString(CharSequence string) {
return (TextUtils.isEmpty(string) || string.toString().equalsIgnoreCase("null"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment