3

I have a url: https://<site name>/pallavi/[Songs.PK]%2002%20.mp3

I have a text view, with property: android:autoLink="all"

If I simply set the text to the text view, my text view simply highlights the portion preceding the [. It looks something like this:

https://< site name >/pallavi/[Songs.PK]%2002%20.mp3

What i want is, the whole link should be highlighted like:

https://< site name >/pallavi/[Songs.PK]%2002%20.mp3

What I have tried till now:

  1. Used the < pre > tag and Html.fromHtml, but it doesn't seem to work! (I don't even know if the < pre > is supported in android though.)

  2. Used Jsoup.parser. But that too doesn't seem to work for me.

UPDATE I have tried this answer too: https://stackoverflow.com/a/12376115/1320263


Please let me know if the issue is with android that the text view's linkAll property itself does not consider parenthesis as a valid character or not? If it is supported, how do i hyperlink that too?

Also NOTE: The text(or link) I have written in the question is just a sample text. In reality, I am getting a block of text, from where it would be very difficult to identify where exactly the hyper link starts and where it ends. Also, the number of links present in the block would be un-known. Hence I cannot use the < a href = "" > thing...

Community
  • 1
  • 1
PrincessLeiha
  • 3,144
  • 4
  • 32
  • 53
  • Have you used `Linkify` see this one http://stackoverflow.com/questions/4746293/android-linkify-textview –  Mar 20 '14 at 07:01

2 Answers2

3

If some one else happens to have the same issue, following is the solution which worked for me:

    Pattern pattern = Pattern.compile("(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))");


    SpannableString spannable = new SpannableString(html);
    Matcher matcher = pattern.matcher(spannable);

    // Create ActivitySpans for each match
    while (matcher.find())
        spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Create a new TextView with these spans and enable the clickable links
    mTxtEventDescription.setText(spannable);
PrincessLeiha
  • 3,144
  • 4
  • 32
  • 53
0

You can try this:

((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));`
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
  • As I have already mentioned, the text view contains a paragraph. The link is just a content of the paragraph. The text view has a lot of text formatting, like new line characters, multiple spaces etc, I do not want to touch the formatting. With Html.fromHtml, the text formatting is disturbed. – PrincessLeiha Mar 20 '14 at 07:07
  • Try using regex refer this: http://examples.javacodegeeks.com/core-java/util/regex/matcher/extract-html-links-with-java-regular-expression-example/ and http://www.vogella.com/tutorials/JavaRegularExpressions/article.html – Himanshu Agarwal Mar 20 '14 at 07:20