user3304659
user3304659

Reputation: 59

My android webview loads pages within the webview in "Nexus 5X API 25" but does not loads in "Galaxy Nexus API 22 (Android 5.1.1)"

If anyone could please try this simple example and run the app I would really appreciate it. When the url loads, scroll a little bit down and try to click an orange button which says "TAKE ME THERE" there are two different behaviours in two different devices. In Nexus 5X it loads the destination in the same Webview but in Galaxy Nexus API 22 (Android 5.1.1) it opens the destination in the default android browser. I hope i was able to convey my problem. I tried searching for the solution but couldn't find it. Please HELP!!!!!!!!!!!!!!!!

package in.ac.cus.webview;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

private WebView myWebView;

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

    //Webview initial setup procedure
    myWebView = (WebView) findViewById(R.id.webview);
    WebSettings websettings = myWebView.getSettings();
    websettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://library.cus.ac.in");
    myWebView.setWebViewClient(new WebViewClient());
}

private class WebViewClient extends android.webkit.WebViewClient {
    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
    }
    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return true;
    }
}

}

Upvotes: 1

Views: 329

Answers (1)

Jonathan Aste
Jonathan Aste

Reputation: 1774

The public booleanshouldOverrideUrlLoading(WebView view, WebResourceRequest request) method is targeted to android N, wich is 7.x.x In your other shouldOverrideUrlLoading method, you are asking to the system to handle the web, thas why it opens the phone browser. You should do the same in both methods. load the given url into de webView.

private class WebViewClient extends android.webkit.WebViewClient {
    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return true;
    }   
}

Upvotes: 1

Related Questions