00 Votes

Android Programming: Open URL/Set Link to Website

Tutorial by Stefan Trost | 2014-04-17 at 15:13

Today, I would like to show you a way of how to open a URL out of an Android App. For example to set a link to a website within an App.

In order to open our URL, we are using an implicit Intent. This means, that we do not explicitly specify the component which should be used for opening. Instead, the Android system should choose the right one to finish the action. 

String url = "https://www.askingbox.com/"; 

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));

startActivity(intent);

Specifically, this means that we are just creating a new Intent set to "Intent.ACTION_VIEW" and passing the URL. Android will then check the content of the passed parameter and will see that it is a link to a website because of the "http" at the beginning of the string. Accordingly, Android will open it in the default browser or it will show a list of all browsers available to complete the action.

Who does not want to write so many lines of code, can use the following two lines with the same result:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("URL"));
startActivity(intent); 

When passing geo-coordinates or a link to the marketplace instead of the URL, Android will also find a fitting application such as Maps or Google Play.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Android Splash Screen Tutorial

Tutorial | 0 Comments

HTACCESS: Simplify URL

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.