Skip to content

Instantly share code, notes, and snippets.

@mlc
Last active October 21, 2015 18:12
Show Gist options
  • Save mlc/964063224f585f0f6fbc to your computer and use it in GitHub Desktop.
Save mlc/964063224f585f0f6fbc to your computer and use it in GitHub Desktop.
/**
* Attempts to set copy into a TextView using the normal copy. If it ends up
* wrapping onto more than one line, uses the short copy alternative instead.
* @param textView The TextView (or Button or whatever) to setText on.
* @param normalCopyResId The preferred copy string resource id.
* @param shortCopyResId The alternate (shorter) copy string resource id.
*/
public static void maybeUseShorterCopy(final TextView textView,
@StringRes final int normalCopyResId,
@StringRes final int shortCopyResId) {
textView.post(new Runnable() {
@Override
public void run() {
// posting to the view ensures that it's been laid out and sized first
textView.setText(normalCopyResId);
if (textView.getLineCount() > 1) {
textView.setText(shortCopyResId);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment