I have to open a URL on Click of OK
Button in a view. Can someone tell how to do this?
Asked
Active
Viewed 1.7e+01k times
124

Anatoliy Nikolaev
- 22,370
- 15
- 69
- 68

Tushar
- 5,907
- 15
- 49
- 81
-
1Use [HttpUrlConnection](http://developer.android.com/reference/java/net/HttpURLConnection.html). – Harry Joy Feb 08 '11 at 06:32
-
13public void openWebURL( String inURL ) { Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( inURL ) ); startActivity( browse ); } – Tushar Feb 08 '11 at 06:34
-
This will work perfectly dude.. so 1 up... – Ganapathy C Feb 08 '11 at 06:41
-
@tushar: have you tried it? i think it should work properly. do you get any error while running this code? – Harry Joy Feb 08 '11 at 06:44
-
try this way http://www.vogella.de/articles/AndroidIntent/article.html – Givantha Kalansuriya Aug 28 '11 at 16:21
9 Answers
293
On Button
click event write this:
Uri uri = Uri.parse("http://www.google.com"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
that open the your URL.

thanhbinh84
- 17,876
- 6
- 62
- 69

Parag Chauhan
- 35,760
- 13
- 86
- 95
-
12OR `startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"))` – Chris - Jr May 29 '17 at 00:28
-
-
9
Button imageLogo = (Button)findViewById(R.id.iv_logo);
imageLogo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url = "http://www.gobloggerslive.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});

Dharmendra Mishra
- 2,427
- 1
- 13
- 9
4
You can use the below method, which will take your target URL as the only input (Don't forget http://)
void GoToURL(String url){
Uri uri = Uri.parse(url);
Intent intent= new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}

Dogu Deniz Ugur
- 199
- 12
2
String url = "https://www.murait.com/";
if (url.startsWith("https://") || url.startsWith("http://")) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}else{
Toast.makeText(mContext, "Invalid Url", Toast.LENGTH_SHORT).show();
}
You have to check that the URL is valid or not. If URL is invalid application may crash so that you have to check URL is valid or not by this method.

Mayur Sojitra
- 690
- 7
- 19
2
Add this code to your OK button click listener.
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")))

Ghayas
- 1,266
- 10
- 17
1
create an intent and set an action for it while passing the url to the intent
yourbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String theurl = "http://google.com";
Uri urlstr = Uri.parse(theurl);
Intent urlintent = new Intent();
urlintent.setData(urlstr);
urlintent.setAction(Intent.ACTION_VIEW);
startActivity(urlintent);

pcodex
- 1,812
- 15
- 16
1
No need for any Java or Kotlin code to make it a clickable link, now you just need to follow given below code. And you can also link text color change by using textColorLink.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:textColorLink="@color/white"/>

Abdul Mateen
- 1,139
- 1
- 14
- 21
-
Interesting, but it doesn't allow longClicks to override the autoLink. – Michael Plischke Apr 30 '23 at 23:02
1
The following code worked perfectly for me.
fun Context.goToUrl(url: String) {
if (url.startsWith("https://") || url.startsWith("http://")) {
val uriUrl = Uri.parse(url)
val launchBrowser = Intent(Intent.ACTION_VIEW, uriUrl)
startActivity(launchBrowser)
} else {
Toast.makeText(this, "Invalid Url", Toast.LENGTH_SHORT).show()
}
}
call it in your actvity or fragment
requireContext().goToUrl("https://"+"something")

Kwesi Welbred
- 69
- 1
- 5
-
Note, I combined @Mahamud and Sojitra to arrive at this solution. – Kwesi Welbred Jun 23 '22 at 00:24
0
private fun goToUrl(url: String) {
val uriUrl = Uri.parse(url)
val launchBrowser = Intent(Intent.ACTION_VIEW, uriUrl)
startActivity(launchBrowser)
}

Raihan Mahamud
- 47
- 8