1

I need to perform exactly this, but on Android:

Java : replacing text URL with clickable HTML link

I've tried the examples provided in this solution (which is meant for Java), but it didn't work.

It's like the regex is not working.

Any solution for this?

thanks!


This is my code:

            final EditText postTextView = (EditText) findViewById(R.id.postText);
            Intent output = new Intent();
            String text = postTextView.getText().toString();

            text = text.replaceAll("(.*://[^<>[:space:]]+[[:alnum:]/])", "<a href=\"$1\">HereWasAnURL</a>");

System.out.println("* * * Converted = " + text.replaceAll("(.*://[^<>[:space:]]+[[:alnum:]/])", "HereWasAnURL"));

            output.putExtra(ZNMainActivity.RESULT_CODE_POST, text );
            setResult(RESULT_OK, output);
            finish();       
Community
  • 1
  • 1
shuot
  • 67
  • 9
  • Why can't you use the code shown in that link? It's Java, and android runs on Java. If you want to post your work, we can tell you where you are having problems. – TtT23 Mar 04 '13 at 05:20
  • I tried, but it didn't work. My original text is kept. – shuot Mar 04 '13 at 05:24
  • 1
    Post your code. You probably did something incorrectly, and I'd be happy to take a look at it. – TtT23 Mar 04 '13 at 05:26
  • 1
    Try the second answer for regex from your link. That one seemed to work for me "return escapedText.replaceAll("(\\A|\\s)((http|https|ftp|mailto):\\S+)(\\s|\\z)", "$1$2$4");" – TtT23 Mar 04 '13 at 05:40
  • From this example, I don't find the HtmlUtils class. – shuot Mar 04 '13 at 06:13
  • It worked, but only if I enter htttp:\\www.website.com. If I enter www.website.com instead, it doesn't work. When I read again the original post I refer, I understand that my needs are slightly different. What I need is to recognize any form of website address (they are actually typed or pasted by users) and convert it to a link form, as http://www.website.com/ – shuot Mar 04 '13 at 06:22
  • That's a very intricate feature you are asking and requires an in-depth regex knowledge to be able to parse any form of valid URL. I suggest you look at this [thread](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) to gain more insight. I will try to construct an example meanwhile. – TtT23 Mar 04 '13 at 07:06

1 Answers1

3

Do something like this in your EditText

EditText editText = (EditText) findViewById(R.id.edittext);
editText.setText(someContent);
Linkify.addLinks(editText, Linkify.ALL);
Log.d(TAG, "HTML: " + Html.toHtml(editText.getEditableText()));

See the details at http://developer.android.com/reference/android/text/util/Linkify.html

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Yes. I also previously tried this one. It transform the web addresses as links, for the Android interface easily, but it doesn't perform what I need. I need the described transformation in order to send it to a server. When I use Linkify and then use a toString() method in order to include the data to send to the server, it doesn't have any HTML link syntax into. – shuot Mar 04 '13 at 13:52