Open or Launch URL in Browser from Android App
Here’s how to open or launch a URL in an external browser from an android app.
How to launch a URL in an external browser in the android app?
Here we discuss how to open URL on a button click, by clicking in an image, on a function call, or by clicking on a menu item click.
Open URL on Button Click
How to open an URL in the android app by clicking on a button.
Button buttonTutorials = (Button)findViewById(R.id.button_tutorials);
buttonTutorials.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.techantena.com/tutorial/";
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Open URL on ImageView Click
How to open an URL in the android app by clicking on an ImageView.
ImageView imageTutorials = (ImageView)findViewById(R.id.image_tutorials);
imageTutorials.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.techantena.com/tutorial/";
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Open URL on a function call
How to open an URL in the android app by calling a function.
Call below function and pass URL as a string.
public void openExternalURL(String url){
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Open URL on a menu item click
How to open an URL in the android app on a menu item click.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_tutorials:
String url = "https://www.techantena.com/tutorial/";
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}