0

I have a webview. My question is, when user clicks to a link in webview, how to continue with my app?

When user clicks to a link, this dialog box appearing:

continue with dialogbox

How can I avoid this dialogbox and continue with my app?

WebView Code:

WebView webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl(url);

Edit (MYActivity İn Test project)

package com.mycompany.myapp5;

import android.app.*;
import android.os.*;
import android.webkit.*;

public class MainActivity extends Activity 
{
WebView view;
String url="http://google.com";

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

    view.setWebViewClient(new WebViewClient()
{
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
    }
}

1 Answers1

0

This SO answer is exactly what you need.

The idea is to set a WebViewClient to your WebView and override shouldOverrideUrlLoading method like below:

webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

I hope this helps you!


Here is the complete code from a test project that is running.

The xml:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.myapplication.MainActivity">

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

Code in the Activity:

WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webview);
        String url="http://www.google.com";
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadUrl(url);
    }

This works fine in a 4.4 device. Please try this and let me know if you still have the issue.

Community
  • 1
  • 1
Swagata Acharyya
  • 647
  • 4
  • 10