15

I have an EditText on Android I'd for which I'd like any embedded urls to be clickable. I used the Linkify class, which has turned them blue and underlined them. However, I can't figure out how to actually make them clickable.

Thanks!

lowellk
  • 2,049
  • 4
  • 21
  • 26
  • I'd like to mention EditText links are already clickable by default - you just have to double-tap them to open a special menu. – Bip901 Apr 03 '21 at 17:18

4 Answers4

24

XML:

 android:linksClickable="true"
 android:autoLink="web|email"

JAVA:

TextView textView = (TextView) findViewById(R.id.textViewId);
textView.setText(Html.fromHtml(html));
textView.setMovementMethod(LinkMovementMethod.getInstance());
Kamil Lelonek
  • 14,592
  • 14
  • 66
  • 90
22

For edit text I managed to get links clickable on the following way. First i implemented a Custom MovementMethod as describe here

Java

(Create your edit text from xml or context)

editText.setLinksClickable(true);
editText.setAutoLinkMask(Linkify.WEB_URLS);
editText.setMovementMethod(CustomMovementMethod.getInstance());
//If the edit text contains previous text with potential links
Linkify.addLinks(editText, Linkify.WEB_URLS);

Then to manage that the urls look like links while the user types

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        @Override
        public void afterTextChanged(Editable s) {

                Linkify.addLinks(s, Linkify.WEB_URLS);

        }
    });
Community
  • 1
  • 1
pleonasmik
  • 779
  • 10
  • 16
  • 1
    This is perfect. Is there any way to implement this with the ability to detect phone numbers and highlight them as well? –  Aug 22 '14 at 22:31
  • 3
    Yes @Collinux the class [Linkify](http://developer.android.com/reference/android/text/util/Linkify.html) supports 4 types: Web links, Phone numbers, emails and map addresses – pleonasmik Aug 25 '14 at 13:57
  • Take note that, we should use `setLinksClickable(false);`, if we intent to have our own custom `CustomMovementMethod`. – Cheok Yan Cheng Sep 17 '18 at 12:36
0

XML - Write it in your EditText

android:autoLink="web"
android:inputType="textWebEditText"
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
  • You could dramatically improve your answer by (1) formatting code as `code`, i.e., enclosing code in backticks, (2) elaborating on the connection between your XML and Java/Kotlin code and steps to actually make this work, and, (3) explaining how your approach improves upon [Kamil's answer](https://stackoverflow.com/a/18219651/3410474) from 2013. – Valentin Kuhn Mar 03 '20 at 07:50
0

Hope this will help someone

val message: String = String.format(
        getString(R.string.message_content),
        firstNameEditText.text,
        lastNameEditText.text,
        dateTextView.text,
        timeTextView.text
    )

    val gMapURL = getString(R.string.google_map_location)

    // Setup my Spannable with clickable URLs
    val spannable: Spannable = SpannableString(gMapURL)
    Linkify.addLinks(spannable, Linkify.WEB_URLS)

    // Append a zero-width space to the Spannable
    val gText = TextUtils.concat(spannable, "\u200B")

    val finalText = TextUtils.concat(message, gText)
    messageContentEditText.setText(finalText)
vivek s
  • 91
  • 1
  • 3