2

I'm firstly create Android Test App.

I'm using webview form, and I'm insert some url, and when I'm running app its work fine.

But when I'm open app, and click some other page link, than I get chosen list, example, open it on "Google Chrome". But I want to open it in webview.

I think that clearly explained.

activity_mail.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp" tools:context=".MainActivity">

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        />
</RelativeLayout>

MainActivity.java

package com.example.webviewapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

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

        String url = "http://example.com";
        WebView  view=(WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(url);

    }
}

Thank you!

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
George B.
  • 172
  • 2
  • 2
  • 14
  • Possible duplicate of [Link should be open in same web view in Android](https://stackoverflow.com/questions/7308904/link-should-be-open-in-same-web-view-in-android) – Marcos Vasconcelos Apr 04 '18 at 22:08

2 Answers2

13

You need to Create a WebViewClient

and override the shouldOverrideUrlLoading() method

try this:

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

    WebView view = (WebView) findViewById(R.id.webView1);
     view.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    });
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl("http://example.com");
}

From the Documentation of shouldOverrideUrlLoading

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
1

Check the WebView tutorial here. Just implement the web client and set it before loadUrl. The simplest way is:

view.setWebViewClient(new WebViewClient());

For more advanced processing for the web content, consider the ChromeClient.

shmakova
  • 6,076
  • 3
  • 28
  • 44