-1

I want to convert the selected text from Edit Text to Hyperlink. Basically i know how to convert text in hyperlink, but not able to do in Edit Text.

I have tried Linkify, but not working.

Any Help would be appreciated..

moDev
  • 5,248
  • 4
  • 33
  • 63
  • Show us how you used Linkify. Post the code here. – iTurki Aug 21 '12 at 19:24
  • Linkify.add(text,Linkify.WEB_URLS); – moDev Aug 21 '12 at 19:26
  • And how did you use that in your EditText ?!! – iTurki Aug 21 '12 at 19:29
  • I am selecting the text using SelectionStart and SelectionEnd and then making substring of selected text , storing text back to edit text – moDev Aug 21 '12 at 19:32
  • From what I read, it seems that Linkify is designed to work with TextView but not EditText. So, Linkify is a dead-end. Sorry – iTurki Aug 21 '12 at 20:05
  • 1
    What about using [`esitText.setAutoLinkMask(Linkify.WEB_URLS)`](http://developer.android.com/reference/android/widget/TextView.html#setAutoLinkMask%28int%29)? This will convert any hyperlink in your EditText. – iTurki Aug 21 '12 at 20:11
  • Maybe explaining what you are trying to achieve in the end would be better. – Hesham Saeed Aug 21 '12 at 20:18
  • @iturki AutoLinkMask is found to be useful,but how to convert only selected text, your code will convert the whole text if links found. I need to make it with selection.. – moDev Aug 21 '12 at 20:31
  • @Agrawal I'm afraid that you can't achieve that. I suggest you try a different approach. Like copying the selected text to a TextView where it is clickable. – iTurki Aug 21 '12 at 20:43
  • @iturki I have achieved using SpannableStringBuilder and autolinkMask. Somehow your also code helped me . Thanks.. – moDev Aug 21 '12 at 20:47

1 Answers1

2

Here is the way how i solved the problem.

text = EditText

            int start = text.getSelectionStart();

            int end = text.getSelectionEnd();

            SpannableStringBuilder spb = new SpannableStringBuilder(text
                    .getText().toString());

            String hyper = text.getText().toString().substring(start, end);

            text.setText(hyper);

            text.setAutoLinkMask(Linkify.ALL); // Linkify.WEB_URLS

            text.setText(spb);
moDev
  • 5,248
  • 4
  • 33
  • 63