Created
April 8, 2019 13:10
-
-
Save AhmadAyyaz1993/0fa6df260ada7e9aedf6886fdd219319 to your computer and use it in GitHub Desktop.
Method to add dots at the end of long text in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utility { | |
public static void addShowMoreDots(String targetString, TextView tvStringHolder, int charactersLimit, @Nullable final View.OnClickListener onDotsClickListener) { | |
if (targetString.length() > charactersLimit) { | |
String dotsString = " [ ... ]"; | |
targetString = targetString.substring(0, charactersLimit).concat(dotsString); | |
SpannableString spannableDots = new SpannableString(targetString); | |
ClickableSpan clickableSpan = new ClickableSpan() { | |
@Override | |
public void onClick(View view) { | |
if (onDotsClickListener != null) { | |
onDotsClickListener.onClick(view); | |
} | |
} | |
@Override | |
public void updateDrawState(TextPaint ds) { | |
ds.setUnderlineText(false); | |
} | |
}; | |
int startIndex = targetString.length() - dotsString.length() + 1; //from where [ of [...] starts | |
int endIndex = targetString.length(); | |
spannableDots.setSpan(clickableSpan, startIndex, endIndex, 0); // | |
spannableDots.setSpan(new RelativeSizeSpan(1.5f), startIndex + 1, endIndex - 1, 0); | |
spannableDots.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getAppContext(), R.color.colorButtonGreen)), startIndex + 1, endIndex - 1, 0); | |
tvStringHolder.setMovementMethod(LinkMovementMethod.getInstance()); | |
tvStringHolder.setText(spannableDots, TextView.BufferType.SPANNABLE); | |
} else { | |
tvStringHolder.setText(targetString); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment