Links in Webview do not work

2

Has anything changed in the implementation of WebView, WebViewClient for Android 7?

I have some html files in a subfolder inside the Assets folder. The list.html file, which contains other links that call other html files that are in the same folder, is normally displayed but when I click the links to call the other html files are not appearing in WebView.

I tested on android 4 and 6 and it works perfectly, android 7 does not work.

Follow the code:

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

    myWebView = (WebView) findViewById(R.id.webViewHelp);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);

    myWebView.setWebViewClient(new ActivityHelp.myWebClient());
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setDisplayZoomControls(false);
    myWebView.getSettings().setSupportZoom(true);
    myWebView.loadUrl("file:///android_asset/help/lista.html");
}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean  shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        super.shouldOverrideUrlLoading(view, request);
        progressBar.setVisibility(View.VISIBLE);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.INVISIBLE);
    }
}
    
asked by anonymous 30.06.2017 / 04:46

2 answers

6

In method shouldOverrideUrlLoading() instead of return true return false .

true indicates that the application will treat the URL (for example, launch an Intent with the link so that it is seen in the native Browser) and not WebView. So, for WebView to display the URL page, the method must return false .

The overload method of shouldOverrideUrlLoading() with this signature, boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request) ", was only added in API 24.
The other, boolean shouldOverrideUrlLoading (WebView view, String url) , has become obsolete.

The reason it works in previous versions is that the override of that overload (which only exists from 7) will not be called, being called the implementation " base "of the other method, which returns false . That is, being there or not, returning true or false is the same, it is not called. The strange thing is that Android Studio does not give any warning / error on how the method can only be used with minSdkVersion 24 .

Anyway, the correct way for the implementation, since you want to support lower versions, will be to override the two methods.

@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    super.shouldOverrideUrlLoading(view, request);

    //fazer alguma coisa aqui

    return false;
}

@SuppressWarnings("deprecation")
@Override
public boolean  shouldOverrideUrlLoading(WebView view, String url) {
    super.shouldOverrideUrlLoading(view, url);

    //fazer alguma coisa aqui

    return false;
}
    
30.06.2017 / 17:48
1

This answer complements the subject of the question.

Let's create an webview application on Android 7 (API 24) to open HTML files from local form >.

1) First let's create the two HTML files as an example:

The files are in this path:

MyApplication\app\src\main\assets\teste.html
MyApplication\app\src\main\assets\teste2.html

test.html

<h3>Aaaa</h3>
<a href="teste2.html">Link1</a>

test2.html

<h3>Bbbb</h3>
<a href="teste.html">Link2</a>

2) Let's edit the XML layout file of the application ( MainActivity ):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.computador1.myapplication.MainActivity">

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

</android.support.constraint.ConstraintLayout>

3) Now let's set up the java ( MainActivity ) part to load the webview into ID defined in the layout.

package com.example.computador1.myapplication;

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

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;

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

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true); // permite o uso de javascript
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl("file:///android_asset/teste.html");

    }
}

Understand that the line:

    mWebView.setWebViewClient(new WebViewClient());

It's very important because it does not let the webview links open in the default Android browser. So they will always open in the webview. Now just test your application.

    
30.06.2017 / 19:57