Embed a web view in another view

I have 2 types in the application:

but. res / layout / main.xml - standard view with 1 button

b. res / layout / web_view.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/webview" android:layout_height="fill_parent" android:layout_width="fill_parent" /> </LinearLayout> 

When I click the button on the first view (a), it loads the webview (b) and loads the url:

 // click on the "Browser" button in view a public void goToWebView(View view) { setContentView(R.layout.web_view); WebView mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("http://www.google.com"); } 

Everything that works fine, loading the url is fine, but the browser is created in its own form (the third, not b itself), and my goal is to use Webview to display some HTML code in my application, and not outside it, in a separate browser .

Anyboyd any idea?

This is done using the level8 / Android 2.2 API.

Thank you for your help. Floor

+8
android android-webview
source share
1 answer

In fact, I finally understood. Even if you programmatically download the URL with

 mWebView.loadUrl("http://www.google.com"); 

you also need to change the default behavior (which opens the URL in a new browser instance).
The previous code requires 2 improvements.

 // override default behaviour of the browser private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } 

Then set a new behavior for the view that uses Webclient:

 public void goToWebView(View view) { setContentView(R.layout.web_view); WebView mWebView = (WebView) findViewById(R.id.webview); // add the following line ---------- mWebView.setWebViewClient(new MyWebViewClient()); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("http://www.google.com"); } 
+15
source share

Source: https://habr.com/ru/post/649843/


All Articles