Arnab Maiti
Arnab Maiti

Reputation: 85

How to know if an entered edittext text is a link or not?

I have an app to share YouTube videos. Edittext text must should be a link in this purpose. But the problem is that I don't know how to detect if it is a link or simple text. Let say I have an edittext with id edittext1. On button1 click i shall check if edittext consists of a link or not. Thanks in advance.

Upvotes: 3

Views: 1134

Answers (2)

sbmalik
sbmalik

Reputation: 534

You can create your own implementation I think there are two methods you can use

  1. Usually every link starts with http or https you can check for them x.startWith(str) Method. Is your string is starting with http or https then you can treat is as link else treat it as text.
  2. Second Method is that every link in my opinion contains .com so after getting the text from edittext you can check for .com using x.contains(str)

NOTE:

x = edittext1.getText().toString();

Upvotes: 2

Chrisvin Jem
Chrisvin Jem

Reputation: 4070

You can use URLUtil.isValidUrl(url) to check if a string is a valid URL.

P.S - Do note that there are many ways that a URL can be well-formed but not retrievable. It's always best to ensure that you're catching any potential exceptions that might be thrown even after you check if the URL is valid.

Upvotes: 4

Related Questions