I have a TextView to show recipients of emails, the original string will be something like "FirstName LastName <xx@yy.com>, FirstName LastName <xx@yy.com>, FirstName LastName <xx@yy.com>"
. I want to show a string like "FirstName LastName, FirstName LastName, FirstName LastName"
while FirstName LastName
is clickable. I know how to get FirstName LastName
and I know how to make xx@yy.com
clickable by using URLSpan
, but it only makes the xx@yy.com
clickable, what I want to do is to only show FirstName LastName
and make it clickable, any idea?
Asked
Active
Viewed 76 times
0

Phantômaxx
- 37,901
- 21
- 84
- 115

litaoshen
- 1,762
- 1
- 20
- 36
-
2Use `ClickableSpan`. – ADM Mar 23 '18 at 19:07
-
https://stackoverflow.com/questions/49382532/how-does-instagram-embed-clickable-text-in-stories/49382706#49382706 – Santanu Sur Mar 23 '18 at 19:40
-
The problem is that I need to hide part of the text, I know how to set the clickable but how to hide the other part of the text? – litaoshen Mar 26 '18 at 19:20
2 Answers
0
I found the solution by extending the ClickableSpan and pass in the hidden information, like the code below.
public class RecipientsSpan extends ClickableSpan {
public String name;
private String emailAddress;
private int textColor;
private RecipientsClickHandler recipientsClickHandler;
public RecipientsSpan(String name, String emailAddress, int textColor) {
super();
this.name = name;
this.emailAddress = emailAddress;
this.textColor = textColor;
}
/**
* Set the interface that will be used to handle the click event.
* @param recipientsClickHandler interface to pass in.
*/
public void setRecipientsClickHandler(@NonNull RecipientsClickHandler recipientsClickHandler) {
this.recipientsClickHandler = recipientsClickHandler;
}
/**
* Interface to handle click event for different spans inside the same TextView.
*/
public interface RecipientsClickHandler {
void onRecipientsClicked(@NonNull String name, @NonNull String emailAddress);
}
@Override
public void onClick(View view) {
if (recipientsClickHandler != null) {
recipientsClickHandler.onRecipientsClicked(name, emailAddress);
}
}
/**
* Make text show the color specified and also avoid the default underline.
* @param ds TextPaint that will be applied to the text.
*/
@Override
public void updateDrawState(@NonNull TextPaint ds) {
ds.setColor(textColor);
ds.setUnderlineText(false);
}
}

litaoshen
- 1,762
- 1
- 20
- 36
-1
For each TextView you can set an onClick listener inside your Java code. So with that you can easily link the TextView with the mailadress

Microgamer
- 21
- 7
-
-
Then do it like proposed here https://stackoverflow.com/q/10696986/4417786 – Microgamer Mar 24 '18 at 19:39
-
The biggest problem is that I need to hide some text while making other text clickable. – litaoshen Mar 26 '18 at 19:21
-
There are enough methods and ways to alter strings! So you don't hide the text, you delete it – Microgamer Mar 27 '18 at 19:46
-
How do I know which email I clicked then? Do you have any reference that I can follow? Thanks! – litaoshen Mar 27 '18 at 19:50
-
You make it like proposed in the link above. For each name and mail combination you remove the mail from the string and set it as the listener for the clickableSpan. So you got a span for each name! – Microgamer Mar 29 '18 at 09:54
-
It ended that I need to create a new class that extends ClickableSpan and handle the click event there. – litaoshen Mar 29 '18 at 13:27