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();       
7
  • 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. Commented Mar 4, 2013 at 5:20
  • I tried, but it didn't work. My original text is kept. Commented Mar 4, 2013 at 5:24
  • 1
    Post your code. You probably did something incorrectly, and I'd be happy to take a look at it. Commented Mar 4, 2013 at 5: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<a href=\"$2\">$2</a>$4");" Commented Mar 4, 2013 at 5:40
  • From this example, I don't find the HtmlUtils class. Commented Mar 4, 2013 at 6:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.