0

I want to add underline and colour to textview, but it is a simple text, not link, not phone number, just simple "Hello world", that I want to have with underline and that blue link-like colour.

It failed to do so:

view.setMovementMethod(LinkMovementMethod.getInstance());

Linkify.addLinks(view, Linkify.ALL);

Thank you! I just underlined the text as Const suggested and changed color of textview. But I guess xoxol_89's answer is correct in my case and should be accepted.

Heisenberg
  • 3,153
  • 3
  • 27
  • 55
  • 1
    You don't need Linkify for that. See this topic on how to do it: http://stackoverflow.com/questions/10019001/how-do-you-underline-a-text-in-android-xml/10019093#10019093 – Const Apr 10 '14 at 12:48

3 Answers3

0

Do you try to use Spannable
For example

/**
       * Method allocates filtering substring in all contacts yellow color,
       * that satisfy the user's search 
       * @param inputText - DisplayName
       *        filtText - filtering Text
       * @return String with allocating substring (Spannable)
       */
    public static Spannable changeBackgroungFiltText(CharSequence inputText, String filtText, int color) {
        Spannable str = null;
      if(inputText != null)
      {
        String inputStr = inputText.toString();
        String inputLowerCaseStr = inputStr.toLowerCase();
        String filtLowerCaseStr = filtText.toLowerCase();
//      Spannable str = new SpannableStringBuilder(inputStr);
            str = new SpannableStringBuilder(inputStr); 
        if (filtText.length() != 0)
         {
             int indexStart = 0;
             while (true)
             {
                 int indexCur = inputLowerCaseStr.indexOf(filtLowerCaseStr, indexStart);
                 if (indexCur != -1) {
                     int start = indexCur;
                     int end = indexCur + filtText.length();
                     int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
                     str.setSpan(new ForegroundColorSpan(color),start, end, flag);
                     //str.setSpan(new BackgroundColorSpan(highlightColor), start, end, flag);
                     indexStart = indexCur + 1;
                 } else {
                     return str;
                 }
             }           
         } else {
             return str;
         }
      }
        return str;
    }
xoxol_89
  • 1,242
  • 11
  • 17
0

You can do that in 4 ways:

1.Automatically linkifies using android:autoLink=”all”

2.Link text by setMovementMethod

3.Link as html code using Html.fromHtml()

4.Link string by SpannableString

and you can find the examples here

kAnNaN
  • 3,669
  • 4
  • 28
  • 39
0

You can use SpannableString like this:

    final SpannableString text = new SpannableString("Hello World!");

    final int startAt = 0;
    final int endAt = text.length();
    final int sampleColor = Color.parseColor("#3333ff");

    text.setSpan(new UnderlineSpan(), startAt, endAt, 0);
    text.setSpan(new ForegroundColorSpan(sampleColor), startAt, endAt, 0);

    textView.setText(text);
LR89
  • 397
  • 1
  • 4
  • 14