Webview links do not work

Asked

Viewed 522 times

2

Something has changed in the implementation of Webview, Webviewclient for android 7?

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

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

Follows ocode:

@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);
    }
}
  • I tested your code and got no error. Enter which compileSdkVersion and targetSdkVersion.

  • @ramaral, The code does not even present error, as described in the post, I have a subfolder inside the Assets in which are the html files. webview loads the html file html list. normally, but if I click on a link contained in that file to call another file in the same folder (help) the file is not loaded in webview. On android 4 and 6 in which I did the tests works perfectly, but on android 7 simply does not load.

  • You don’t give that information on the question. Edit it and put it there.

  • @ramaral, I edited the question.

  • @ramaral, just to supplement the question and answer your questions, I am using compileSdkVersion 25 and targetSdkVersion 25

2 answers

6


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

true indicates that the application will handle the URL (for example, launching an Intent with the link to be viewed in the native browser) and not Webview. Thus, in order for Webview to present the page referring to the URL, the method must return false.

The Overload of the method shouldOverrideUrlLoading() with that signature, boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request), only added in API 24.
The other, boolean shouldOverrideUrlLoading (WebView view, String url), is now considered obsolete.

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

Anyway, the correct way to implement, since you want to support lower versions, will be to do the override of 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;
}
  • After amendment suggested by ramaral is working normally on android 7. Thanks for the help!

  • Ramaral, feel free to describe a little more about the method shouldOverrideUrlLoading so that you can understand exactly what is happening in the Henqsan code. From now on, you have my +1;

  • The true is when you want to deny the standard behavior, no? Then within the condition imposed on a supposed if you decide to customize the response or create a custom protocol, for all elses would be false to follow with the standard behavior. I believe, I’m not quite sure :)

  • @ramaral also do not know why true/false behaved differently, maybe it worked "contrary" or simply to abort a request you had to over-write the arguments, anyway it is an excellent response and I was the first to leave the +1, then already this guaranteed :D

  • @Guilhermenascimento It worked with true because the override that Overload of the method is not called in lower versions(it only exists from the 7). That is, be there or not, return true or false is the same. It is not called in the case of an inferior version. The strange is the Android Studio not giving any Warning/error.

  • 1

    @ramaral understood, really this is it, I will perform the tests and take advantage and implement a customized protocol that I created here. Thank you!

Show 1 more comment

1

This answer complements the question.

Let’s create an app with webview in the Android 7 (API 24) to open files HTML of local shape.

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

The files go this way:

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

html test.

<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 layout file XML 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 part of java (Mainactivity) for him to upload the webview to 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 doesn’t let webview links open in the default Android browser. So they will always open in webview. Now just test your application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.