This Android utility is about making clickable colored spans within a TextView
as painless and simple as possible! One common use case for this is building a Twitter or Facebook type app where different words in a message have different meanings and can be clicked to trigger an action. For example:
- Tweet items where "@foo" can be clicked to view a user's profile.
- Facebook posts where "Billy Jean" can be clicked to view a user.
- Slack messges where "#general" can be clicked to go to a different room.
This utility assumes you have one or more TextView
that are filled with text that you'd like to "spannify" based on different patterns. Suppose we have a TextView
that contains the following totally unstyled text:
Linkify all sub-strings within TextView
that match a regular expression:
new PatternEditableBuilder().
addPattern(Pattern.compile("\\@(\\w+)")).
into(textView);
This results in:
Linkify all sub-strings within TextView
that match a regular expression and then set color:
new PatternEditableBuilder().
addPattern(Pattern.compile("\\@(\\w+)"), Color.CYAN).
into(textView);
This results in:
Linkify all sub-strings within TextView
that match a pattern and then set color, and click handler:
new PatternEditableBuilder().
addPattern(Pattern.compile("\\@(\\w+)"), Color.BLUE,
new PatternEditableBuilder.SpannableClickedListener() {
@Override
public void onSpanClicked(String text) {
Toast.makeText(MainActivity.this, "Clicked username: " + text,
Toast.LENGTH_SHORT).show();
}
}).into(textView);
This results in:
Linkify all sub-strings within TextView
that match a pattern and then set custom styles:
new PatternEditableBuilder().
addPattern(Pattern.compile("\\@(\\w+)"),
new PatternEditableBuilder.SpannableStyleListener() {
@Override
void onSpanStyled(TextPaint ds) {
// ds contains everything you need to style the span
ds.bgColor = Color.GRAY;
ds.linkColor = Color.MAGENTA;
}
}).into(textView);
and this results in:
This is a more complete example which matches both usernames and hashtags:
new PatternEditableBuilder().
addPattern(Pattern.compile("\\@(\\w+)"), Color.GREEN,
new PatternEditableBuilder.SpannableClickedListener() {
@Override
public void onSpanClicked(String text) {
Toast.makeText(MainActivity.this, "Clicked username: " + text,
Toast.LENGTH_SHORT).show();
}
}).
addPattern(Pattern.compile("\\#(\\w+)"), Color.CYAN,
new PatternEditableBuilder.SpannableClickedListener() {
@Override
public void onSpanClicked(String text) {
Toast.makeText(MainActivity.this, "Clicked hashtag: " + text,
Toast.LENGTH_SHORT).show();
}
}).into(textView);
and this results in:
Created by Nathan Esquenazi from CodePath in 2016.
- http://stackoverflow.com/questions/15851655/android-change-the-background-color-of-a-clickablespan-when-clicked
- http://stackoverflow.com/questions/20856105/change-the-text-color-of-a-single-clickablespan-when-pressed-without-affecting-o
- http://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android
- http://stackoverflow.com/questions/19750458/android-clickablespan-get-text-onclick
- http://stackoverflow.com/questions/5595785/highlight-on-clickablespan-click
- http://stackoverflow.com/questions/11903414/find-a-particular-regex-word-and-linkify-to-open-an-activity-in-android
- http://stackoverflow.com/questions/7570239/android-linkify-text-spannable-text-in-single-text-view-as-like-twitter-twee
Thank you Bro...