Android: Linkify TextView

I have a textview that I need to link. That's what I'm doing.

TextView text = (TextView) view.findViewById(R.id.name); text.setText("Android...Update from Android"); Pattern pattern = Pattern.compile("Android"); String scheme = "www.android.com"; Linkify.addLinks(text, pattern, scheme); 

The text view displays correctly with the text "Android ... Update from Android", but I have two problems.

1) My text string contains two instances of the string "Android". Thus, both texts are related. I want only the first occurrence to be connected. How can I do it?

2) When I click the linkfy link, it opens a browser, but the URL is weird. The URL he is trying to open is www.comandroid. I do not know what is going on here. The text “android” in the URL is replaced. I am doing something wrong when linknifying text.

Any help would be greatly appreciated.

+27
source share
10 answers

An easy way to put autoLink in an XML layout.

  android:autoLink="web" 
+22
source

I created a static method that makes this simple:

 /** * Add a link to the TextView which is given. * * @param textView the field containing the text * @param patternToMatch a regex pattern to put a link around * @param link the link to add */ public static void addLink(TextView textView, String patternToMatch, final String link) { Linkify.TransformFilter filter = new Linkify.TransformFilter() { @Override public String transformUrl(Matcher match, String url) { return link; } }; Linkify.addLinks(textView, Pattern.compile(patternToMatch), null, null, filter); } 

And the OP could use it simply:

 addLink(text, "^Android", "http://www.android.com"); 
+19
source

I do not have much experience with Linkify. For now, all I wanted to do was make the URLs in the text link automatically processed by another version of addLinks .

However, the regular expression that you use to match on “Android” can be changed to match the one that runs the string, customizing your regular expression:

 Pattern pattern = Pattern.compile("^Android"); 

Note the addition of '^'. This tells the regex rule to match the Android pattern when it launches the line. If this works with your lines then fine. If you have other cases, the word you want to associate is not at the beginning of the line, you will need to study the regular expressions again. I recommend this site to learn more regular-expressions.info

+9
source

If you want to open all the URLs that are contained in the text, you can do this.

  TextView textview=(TextView)findViewById(R.id.text); textview.setText("Android....Update from android...www.android.com"); Linkify.addLinks(textview, Linkify.WEB_URLS); textview.setMovementMethod(LinkMovementMethod.getInstance()); 

It can work for all urls.

+8
source

Here, from Shawns answer and SpunkerBaba comment, I came out with this helper class. This stops the problem of double adding what you are changing by adding to your url.

 package com.your.package; public class Linkify { /** * @param textView * textView who text you want to change * @param linkThis * a regex of what text to turn into a link * @param toThis * the url you want to send them to */ public static void addLinks(TextView textView, String linkThis, String toThis) { Pattern pattern = Pattern.compile(linkThis); String scheme = toThis; android.text.util.Linkify.addLinks(textView, pattern, scheme, new MatchFilter() { @Override public boolean acceptMatch(CharSequence s, int start, int end) { return true; } }, new TransformFilter() { @Override public String transformUrl(Matcher match, String url) { return ""; } }); } } 

Useage:

  Linkify.addLinks(profileTextView, "BlundellApps.com", "http://www.BlundellApps.com"); 
+3
source

I think you really need something simple:

 final TextView textView = ((BodyViewHolder) holder).textView; textView.setText(text); Linkify.addLinks(textView, Linkify.WEB_URLS); // I would do this only during view creation, btw. 

You are tracking the url. Some people suggest using Linkigy.ALL, but you don't want to:

 public static final int ALL = WEB_URLS | EMAIL_ADDRESSES | PHONE_NUMBERS | MAP_ADDRESSES; 

In addition, other people suggest adding:

 textview.setMovementMethod(LinkMovementMethod.getInstance()); 

after adding link rules, but addLinks already does this in its addLinks ; -)

Hooray!

+1
source

For Koltin with a lambda expression, it would be like this:

 val pattern = Pattern.compile(link.title) Linkify.addLinks(view, pattern, null, null, { matcher, url -> link.href }) 
+1
source

The problem is resolved. The solution to the first problem is the answer provided by Ian Leslie. The solution to the second problem is as follows. The behavior of the Linkify.addLinks API is such that it adds the string that you want to associate with the end of the URL. For ex..if you want to associate the text "Android" with "www.android.com". The final URL is “www.android.comAndroid” which is not what I need. So i used

 public static final void addLinks (TextView text, Pattern p, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter) TransformFilter transformFilter = new TransformFilter() { public final String transformUrl(final Matcher match, String url) { return ""; } }; 

Api transformFilter returns an empty string. Therefore my final url is "www.android.com"

0
source
 TextView noteView = (TextView) findViewById(R.id.noteview); noteView.setText(someContent); Linkify.addLinks(noteView, Linkify.ALL); 
0
source
  Linkify.addLinks(yourTextView, yourPattern, linkToGoto, null, new Linkify.TransformFilter() { @Override public String transformUrl(Matcher match, String url) { return ""; } }); 
0
source

Source: https://habr.com/ru/post/1437602/


All Articles