open a url on click of ok button in android

AndroidButtonClick

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

	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);
		}
	});

Solution 3 - Android

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);
}

Solution 4 - Android

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.

Solution 5 - Android

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);

Solution 6 - Android

Add this code to your OK button click listener.

startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")))

Solution 7 - Android

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"/>

Solution 8 - Android

 private fun goToUrl(url: String) {
        val uriUrl = Uri.parse(url)
        val launchBrowser = Intent(Intent.ACTION_VIEW, uriUrl)
        startActivity(launchBrowser)
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUserView Question on Stackoverflow
Solution 1 - AndroidParag ChauhanView Answer on Stackoverflow
Solution 2 - AndroidDharmendra MishraView Answer on Stackoverflow
Solution 3 - AndroidDogu Deniz UgurView Answer on Stackoverflow
Solution 4 - AndroidMayur SojitraView Answer on Stackoverflow
Solution 5 - AndroidpcodexView Answer on Stackoverflow
Solution 6 - AndroidGhayasView Answer on Stackoverflow
Solution 7 - AndroidAbdul MateenView Answer on Stackoverflow
Solution 8 - AndroidRaihan MahamudView Answer on Stackoverflow