9

I am designing an App, where there is TextView, and based on some check condition, I want to make links / phone numbers in the TextView clickable in Java side. Can I do this, I don't want to make it clickable by default which is done by setting in xml file

 android:autoLink="all" 

What I already tried: Create regular TextView and don't set autoLink="all",

     TextView myView = 
                (TextView)view.findViewById(R.id.thisIstheTextView);
       myView .setLinksClickable(true);

also tried :

     myView .setMovementMethod(LinkMovementMethod.getInstance());

None of the above Java code works, I don't want a predesigned xml TextView with autoLink enabled, I want to change the TextView behavior in Java code based on if () conditions. There is no error in my code, but I am not able to achieve what I want. Can you please share your knowledge. Thank you.

David Prun
  • 8,203
  • 16
  • 60
  • 86

2 Answers2

14

Use setAutoLinkMask (int mask).

The possible values that can be combined for mask are those defined for the Linkify class.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Thank you. Although this is equivalent method of android:autoLink, it still doesn't work. – David Prun Mar 15 '12 at 23:35
  • 11
    Sorry I disagree - it does work. You need to call it before a call to `setText(...)` though, it won't change the links in a `TextView` after it already has text in it. Your question asked about how to change the behaviour of a `TextView` depending on what you wanted to show as clickable / linked items (phone numbers etc) "based on some checked condition". I assumed you would do the check, then set the auto link mask then set the text. – Squonk Mar 16 '12 at 00:09
  • 1
    Use Linkify constant like this textView.setAutoLinkMask(Linkify.WEB_URLS); – Rafael Jan 08 '15 at 07:26
7
 Linkify.addLinks(myView, Linkify.ALL);

Fixed the issue.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
David Prun
  • 8,203
  • 16
  • 60
  • 86