create Link Text via SpannableString - Android android.text

Android examples for android.text:SpannableString

Description

create Link Text via SpannableString

Demo Code

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.URLSpan;

public class Main {

  public static SpannableString createLinkText(String text, String clickText, String url) {
    if (!text.contains(clickText)) {
      return null;
    }// w w w.j a  v a  2 s  . co  m
    SpannableString sp = new SpannableString(text);
    int index = text.indexOf(clickText);

    sp.setSpan(new URLSpan(url), index, index + clickText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sp;
  }

}

Related Tutorials