open a url on click of ok button in android
Asked Answered
S

9

125

I have to open a URL on Click of OK Button in a view. Can someone tell how to do this?

Salsify answered 8/2, 2011 at 6:30 Comment(5)
Use HttpUrlConnection.Clove
public void openWebURL( String inURL ) { Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( inURL ) ); startActivity( browse ); }Salsify
This will work perfectly dude.. so 1 up...Sinclare
@tushar: have you tried it? i think it should work properly. do you get any error while running this code?Clove
try this way vogella.de/articles/AndroidIntent/article.htmlWillin
A
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.

Acquit answered 8/2, 2011 at 6:44 Comment(3)
OR startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"))Nodus
@Chris-Jr you have missed the last parenthesis i.e. )Scanlon
How is this different from opening a webview item?Rubbish
W
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);
        }
    });
Wiegand answered 1/6, 2017 at 12:36 Comment(0)
G
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);
}
Glorygloryofthesnow answered 26/6, 2017 at 20:4 Comment(0)
S
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.

Sidetrack answered 22/8, 2019 at 2:34 Comment(0)
K
2

Add this code to your OK button click listener.

startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")))
Kalikalian answered 26/3, 2021 at 6:33 Comment(0)
S
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);
Schlieren answered 4/4, 2018 at 13:39 Comment(0)
K
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"/>
Kira answered 6/1, 2020 at 19:5 Comment(1)
Interesting, but it doesn't allow longClicks to override the autoLink.Mcgowan
D
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")
Dollarfish answered 23/6, 2022 at 0:20 Comment(1)
Note, I combined @Mahamud and Sojitra to arrive at this solution.Dollarfish
Y
0
 private fun goToUrl(url: String) {
        val uriUrl = Uri.parse(url)
        val launchBrowser = Intent(Intent.ACTION_VIEW, uriUrl)
        startActivity(launchBrowser)
    }
Yeoman answered 15/12, 2021 at 10:44 Comment(1)
Can you please give some explanation?Yul

© 2022 - 2024 — McMap. All rights reserved.