Search code examples
androidregexstringsplitspannablestring

how to get special character with word and its click event


i have a 3 String like this:

"@Username: Deliverd your order",
"YOU got trophy: KING OF COINS",
"There is a package waiting for you to pick up from #surat to #mumbai",

what i wanted to do is get username and city name in different color with its click event.

what i m able to achive is get username by splitting to ":" character. but i dont know how to get city name and click event of both.

In city name only last city color is changing, how to change both city name color and get its click event.

this is what i tried:

if (notifications.getTitle().contains(":")) 
{
    String[] username = notifications.getTitle().split(":");
    String uname = getColoredSpanned(username[0] + ":", "#ff7505");
    String txt = getColoredSpanned(username[1], "#000000");
    holder.txtTitle.append(Html.fromHtml(uname +" " + txt));
    holder.txtTitle.setMovementMethod(LinkMovementMethod.getInstance());
} 
else if (notifications.getTitle().contains("#"))
{
     Matcher matcher = 
            Pattern.compile("#\\s(\\w+)").matcher(notifications.getTitle());
     i=0;
     while (matcher.find())
     {
           place.add(i, matcher.group(1));
           i++;
     }
     String place1 = getColoredSpanned("#" + place.get(0), "#237BCD");
     String place2 = getColoredSpanned("#" + place.get(1), "#237BCD");
     places1 = notifications.getTitle().replace("#" + place.get(0), place1);
     places1 = notifications.getTitle().replace("#" + place.get(1), place2);
     holder.txtTitle.setText(Html.fromHtml(places1));
}
else
{
    holder.txtTitle.setText(notifications.getTitle());
}

private String getColoredSpanned(String text, String color) {
    String input = "<font color=" + color + ">" + text + "</font>";
    return input;
}

and this is what i get as output:

enter image description here

and this is what i really expected:

enter image description here


Solution

  • I think you have done with username and facing problem with click on city so I have given answer with click on city name.

    Thanks to @Vinay for given some hint.

    Please check below code.

    public void setSpan() {
            String test = "There is a package waiting for you to pick up from #surat to #mumbai";
            SpannableString spannable = new SpannableString(test);
            final Matcher matcher = Pattern.compile("#\\s*(\\w+)").matcher(test);
            while (matcher.find()) {
                final String city = matcher.group(1);
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View textView) {
                        Toast.makeText(mActivity, city, Toast.LENGTH_SHORT).show();
                    }
    
                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setUnderlineText(false);
                        ds.setColor(Color.RED);
                    }
                };
                int cityIndex = test.indexOf(city) - 1;
                spannable.setSpan(clickableSpan, cityIndex, cityIndex + city.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            mTextViewNotification.setText(spannable);
            mTextViewNotification.setMovementMethod(LinkMovementMethod.getInstance());
        }
    

    Output Screenshot:

    enter image description here