Advertisement

header ads

Android Studio Tutorial – Invoke Default Browser


So this Example project demonstrates how to invoke inbuilt browser with the name of valid website. It is uses intent to open another app directly from the app.

So then let’s start coding the app, first of all create a new project in android studio. Go to File > New > New Project.

After that right away you can start coding.

All these codes should go inside the protected void onCreate method.

Codes goes inside the MainActivity.java class



@Override
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView(R.layout.
activity_main);

    LinearLayout layout =
new LinearLayout(this);
   
    Button dBrowser =
new Button(this);
    dBrowser.setText(
"Click here to Open Browser");
    dBrowser.setOnClickListener(
new View.OnClickListener() {
       
@Override
       
public void onClick(View v) {
            Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
            startActivity(browserIntent);
        }
    });
   
    layout.addView(dBrowser);
    setContentView(layout);
}




This is all for the code. Now run this code on a virtual android device or your android device. You’ll get an output like the below. 



If you have any questions regarding this tutorial please feel free to drop a comment in the comment section. And if you think this tutorial is useful share this in social media. 

Happy Coding!!!

Post a Comment

2 Comments