Search code examples
androidtextviewhighlightlinkify

linkify not work when update text


I using linkify to highlight URL and Emails in my text view, it work correctly in first render but when i try to change text not work.
in my text view i use spannable to append see more and see less when text length more than specific number of string. the following my span function that append text

  private static SpannableStringBuilder getTextToShown(Activity activity, String text, boolean showMore) {

    SpannableStringBuilder sb;

    final ForegroundColorSpan fcs = new ForegroundColorSpan(activity.getResources().getColor(R.color.cyan_mostly_black_light));
    String textToShow;
    if (!showMore) {
      textToShow = text + " " + activity.getResources().getString(R.string.see_less);
      sb = new SpannableStringBuilder(textToShow);
      sb.setSpan(fcs, text.length(), textToShow.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
      sb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), text.length(), textToShow.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
      textToShow = text.substring(0, NUMBER_OF_CHAR_TO_SHOWN - 1) + " " + activity.getResources().getString(R.string.see_more);
      sb = new SpannableStringBuilder(textToShow);
      sb.setSpan(fcs, NUMBER_OF_CHAR_TO_SHOWN, textToShow.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
      sb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), NUMBER_OF_CHAR_TO_SHOWN, textToShow.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return sb;
  } 

and i use it by the following

 if (postModel.getObject().getContent().length() > 140) {
          post_data.setText(getTextToShown(activity, postModel.getObject().getContent(), true));
          showMore = true;
          post_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              if (showMore) {
                post_data.setText(getTextToShown(activity, postModel.getObject().getContent(), false));
                showMore = false;
              } else {
                post_data.setText(getTextToShown(activity, postModel.getObject().getContent(), true));
                showMore = true;
              }
            }
          });
        } else {
          post_data.setText(postModel.getObject().getContent());
        }

and I add linkify by add this

post_data.setMovementMethod(LinkMovementMethod.getInstance());
post_data.setLinkTextColor(Color.BLUE);
Linkify.addLinks(post_data, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS);

till now it's work fine without any problem but when try to click in see more in textview it render the new text in textview but not highlight links or emails, I try to run it in UI thread but also i get the same result. Any help ?


Solution

  • I solve it by remove linkify and add only the following in xml

    android:textColorLink="@color/blue.pure"
    android:autoLink="all"
    

    and use.