0

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
litaoshen
  • 1,762
  • 1
  • 20
  • 36

2 Answers2

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