Android open url external browser and internal WebView

Launch an url in an external browser app from your app.

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

Launch an url internally within the app.
1. Make sure the app has the INTERNET permission enabled in the manifest file.


2. Create a layout file for the WebView, activity_webview



3. Create the WebViewActivity.java

public class WebViewActivity extends AppCompatActivity {
    public static final String WEBSITE_ADDRESS = "website_address";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String url  = getIntent().getStringExtra(WEBSITE_ADDRESS);
        if (url == null || url.isEmpty()) finish();

        setContentView(R.layout.activity_webview);
        WebView webView = (WebView) findViewById(R.id.nyc_poi_webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);
    }
}

4. Launch the url within the app.

Intent i = new Intent(InfoActivity.this, WebViewActivity.class);
i.putExtra(WebViewActivity.WEBSITE_ADDRESS, ""http://www.google.com"");
startActivity(i);

Search within Codexpedia

Custom Search

Search the entire web

Custom Search