I want the user to enter a link in an EditView and when the user clicks enter button, the entered text should show up as a clickable link TextView. Is there any possible way to achieve this?
Asked
Active
Viewed 86 times
-1
-
1Does this answer your question? [How to make links in a TextView clickable](https://stackoverflow.com/questions/2734270/how-to-make-links-in-a-textview-clickable) – primo Sep 29 '22 at 06:14
-
No... As stated earlier... I can't just simple use the tag in String resources... Since the user will add text and it's not predefined – Nobil Gautam Oct 01 '22 at 04:33
1 Answers
0
You can use Intent to open that url when the TextView is clicked. It will open your link in browser but the url text will not become hyperlink.
Here is the code: "Upvote if found helpful."
TextView tv = findViewById(R.id.text_view);
tv.setOnClickListener(view -> {
String url = tv.getText().toString();
if (!url.isEmpty()){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Download a browser to view", Toast.LENGTH_SHORT).show();
}
}
});
For api level 30+ add queries in your manifest file to launch browser from your app outside
<application>
tag
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
</queries>

Pawan Singh Harariya
- 479
- 5
- 8