How do they load the url inside the web browser instead of the androids internal browser?

I am trying to achieve this: MKyong - WebView . To get a hint about what exactly, take a look at the last picture before “Download Source Code”.

Code of my applications:

bookingView = (WebView) findViewById(R.id.fullscreen_content);
bookingView.getSettings().setJavaScriptEnabled(true);
bookingView.loadUrl("http://www.google.com");

But this code does open the URL (in this case Google) inside the default browser / internal browser in Android, and this does not mean in the Android application that I am doing.

Any ideas?

+5
source share
2 answers

Add this line before calling loadUrl()

bookingView.setWebViewClient(new WebViewClient());
+10
source

This is the answer:

Functions

  • Upload URL in WebView
  • - Webview dont .
  • , , .

MainActivity.java

public class MainActivity extends AppCompatActivity {

        WebView webview;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            webview = (WebView)findViewById(R.id.webview);
            webView();
        }

    //Metodo llamar el webview
    private void webView(){
        //Habilitar JavaScript (Videos youtube)
        webview.getSettings().setJavaScriptEnabled(true);

        //Handling Page Navigation
        webview.setWebViewClient(new MyWebViewClient());

        //Load a URL on WebView
        webview.loadUrl("http://stackoverflow.com/");
    }

    // Metodo Navigating web page history
    @Override public void onBackPressed() {
        if(webview.canGoBack()) {
            webview.goBack();
        } else {
            super.onBackPressed();
        }
    }

    // Subclase WebViewClient() para Handling Page Navigation
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("stackoverflow.com")) { //Force to open the url in WEBVIEW
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webview" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

start page

- WEBVIEW

Click on another page in the start page

+8

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


All Articles