Rectangle 27 4

It's true, links with a custom URI scheme don't load automatically launch apps from a WebView.

webView.setWebViewClient(new CustomWebViewClient());

and then in the shouldOverrideUrlLoading(), have the following code:

public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {

    if (url.startsWith("my.special.scheme://")) {

        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        // The following flags launch the app outside the current app
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        activity.startActivity(intent);

        return true;
    }  

    return false;
}

Launch custom Android application from WebView - Stack Overflow

android android-webview
Rectangle 27 3

It's true, links with a custom URI scheme don't load automatically launch apps from a WebView.

webView.setWebViewClient(new CustomWebViewClient());

and then in the shouldOverrideUrlLoading(), have the following code:

public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {

    if (url.startsWith("my.special.scheme://")) {

        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        // The following flags launch the app outside the current app
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        activity.startActivity(intent);

        return true;
    }  

    return false;
}

Launch custom Android application from WebView - Stack Overflow

android android-webview
Rectangle 27 1

I'm not sure, but I believe that WebView simply doesn't handle custom URI schemes.

The workaround is to override WebViewClient.shouldOverrideUrlLoading() and manually test if the URL uses your URI scheme, launching your app and returning true if it matches, otherwise returning false.

Launch custom Android application from WebView - Stack Overflow

android android-webview
Rectangle 27 1

I'm not sure, but I believe that WebView simply doesn't handle custom URI schemes.

The workaround is to override WebViewClient.shouldOverrideUrlLoading() and manually test if the URL uses your URI scheme, launching your app and returning true if it matches, otherwise returning false.

Launch custom Android application from WebView - Stack Overflow

android android-webview
Rectangle 27 4

The problem was the way that the Android webview attempts to access the HTML data when you call loadData on it. I noticed that when I got the error the "web address" it was trying to access was basically my HTML code as a URL, replacing non standard characters with their http friendly versions (%20 for the space character for example). So I simply replaced the % sign in my "width='95%'" with the code for the % sign ( %25 ) and it works perfectly now. No need for CSS or anything like that

Android webview html compliance? - Stack Overflow

android html webview
Rectangle 27 5

Just adding a default custom WebViewClient will do. This makes the WebView handle any loaded urls itself.

mWebView.setWebViewClient(new WebViewClient());

Android WebView, how to handle redirects in app instead of opening a b...

android android-webview
Rectangle 27 3

You will have to set your custom WebviewClient overriding shouldOverrideUrlLoading method for your webview before loading the url.

mWebView.setWebViewClient(new WebViewClient()
        {
            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url)
            {
                return shouldOverrideUrlLoading(url);
            }

            @TargetApi(Build.VERSION_CODES.N)
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
            {
                Uri uri = request.getUrl();
                return shouldOverrideUrlLoading(uri.toString());
            }

            private boolean shouldOverrideUrlLoading(final String url)
            {
                Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);

                // Here put your code

                return true; // Returning True means that application wants to leave the current WebView and handle the url itself, otherwise return false.
            }
        });

Note that the above link for an example code doesn't exist.

i have uploaded the link.

Android WebView, how to handle redirects in app instead of opening a b...

android android-webview
Rectangle 27 7

As of October 2013, facebook no longer allows apps to login via custom webviews.

android - Facebook Login inside webpage on Webview not working - Stack...

android facebook webview android-webview facebook-login
Rectangle 27 17

Yes you have the syntax error. If you want to get your Javascript errors and printing statements in your logcat you must implement the onConsoleMessage(ConsoleMessage cm) method in your WebChromeClient. It gives the complete stack traces like Web console(Inspect element). Here is the method.

public boolean onConsoleMessage(ConsoleMessage cm) 
    {
        Log.d("ShowMote", cm.message() + " -- From line "
                             + cm.lineNumber() + " of "
                             + cm.sourceId() );
        return true;
    }
console.log

I believe the is on the WebChromeClient and not the WebViewClient.

Thanks, your answer was a just what I needed. I'd also suggest people override onJsAlert() in the WebChromeClient. The combination of javascript logging and alerts can be really helpful when debugging Javascript hosted in the webview.

Android Calling JavaScript functions in WebView - Stack Overflow

javascript android webview
Rectangle 27 17

Yes you have the syntax error. If you want to get your Javascript errors and printing statements in your logcat you must implement the onConsoleMessage(ConsoleMessage cm) method in your WebChromeClient. It gives the complete stack traces like Web console(Inspect element). Here is the method.

public boolean onConsoleMessage(ConsoleMessage cm) 
    {
        Log.d("ShowMote", cm.message() + " -- From line "
                             + cm.lineNumber() + " of "
                             + cm.sourceId() );
        return true;
    }
console.log

I believe the is on the WebChromeClient and not the WebViewClient.

Thanks, your answer was a just what I needed. I'd also suggest people override onJsAlert() in the WebChromeClient. The combination of javascript logging and alerts can be really helpful when debugging Javascript hosted in the webview.

Android Calling JavaScript functions in WebView - Stack Overflow

javascript android webview
Rectangle 27 14

I created a nice wrapper to call JavaScript methods; it also shows JavaScript errors in log:

private void callJavaScript(String methodName, Object...params){
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("javascript:try{");
    stringBuilder.append(methodName);
    stringBuilder.append("(");
    for (int i = 0; i < params.length; i++) {
        Object param = params[i];
        if(param instanceof String){
            stringBuilder.append("'");
            stringBuilder.append(param);
            stringBuilder.append("'");
        }
        if(i < params.length - 1){
            stringBuilder.append(",");
        }
    }
    stringBuilder.append(")}catch(error){Android.onError(error.message);}");
    webView.loadUrl(stringBuilder.toString());
}
private class WebViewInterface{

    @JavascriptInterface
    public void onError(String error){
        throw new Error(error);
    }
}
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebViewInterface(), "AndroidErrorReporter");
final Object a = new Object(); callJavaScript(mBrowser, "alert", 1, true, "abc", a);
SyntaxError
Unexpected token
javascript:try{alert(,,'abc',,)}catch(error){console.error(error.message);}

Even simple call callJavaScript(mBrowser, "alert", "abc", "def"); generates error as there is always rogue comma at the end. -1 from me.

@Lukasz'Severiaan'Grela tnx, I fixed that. P.S Next time don't be lazy you could fix it yourself... ;)

For future reference: Let's stay positive. Pointing out an error in the code is not lazy. It's still helpful feedback.

Android Calling JavaScript functions in WebView - Stack Overflow

javascript android webview
Rectangle 27 14

I created a nice wrapper to call JavaScript methods; it also shows JavaScript errors in log:

private void callJavaScript(String methodName, Object...params){
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("javascript:try{");
    stringBuilder.append(methodName);
    stringBuilder.append("(");
    for (int i = 0; i < params.length; i++) {
        Object param = params[i];
        if(param instanceof String){
            stringBuilder.append("'");
            stringBuilder.append(param);
            stringBuilder.append("'");
        }
        if(i < params.length - 1){
            stringBuilder.append(",");
        }
    }
    stringBuilder.append(")}catch(error){Android.onError(error.message);}");
    webView.loadUrl(stringBuilder.toString());
}
private class WebViewInterface{

    @JavascriptInterface
    public void onError(String error){
        throw new Error(error);
    }
}
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebViewInterface(), "AndroidErrorReporter");
final Object a = new Object(); callJavaScript(mBrowser, "alert", 1, true, "abc", a);
SyntaxError
Unexpected token
javascript:try{alert(,,'abc',,)}catch(error){console.error(error.message);}

Even simple call callJavaScript(mBrowser, "alert", "abc", "def"); generates error as there is always rogue comma at the end. -1 from me.

@Lukasz'Severiaan'Grela tnx, I fixed that. P.S Next time don't be lazy you could fix it yourself... ;)

For future reference: Let's stay positive. Pointing out an error in the code is not lazy. It's still helpful feedback.

Android Calling JavaScript functions in WebView - Stack Overflow

javascript android webview
Rectangle 27 4

After another day of googling i found this page which was the solution to my problem: Rendering HTML in a WebView with custom CSS I was using loadDataWithBaseUrl to pass data into the webview, but i was passing in the baseurl as null, because i didn't know what do with it. Im instead going to pass a reference to the assets directory of my project.

Android WebView: Using Script Tags to Load an external javascript file...

javascript android html android-webview external-js
Rectangle 27 11

private void callJavaScript(WebView view, String methodName, Object...params){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("javascript:try{");
        stringBuilder.append(methodName);
        stringBuilder.append("(");
        String separator = "";
        for (Object param : params) {               
            stringBuilder.append(separator);
            separator = ",";
            if(param instanceof String){
                stringBuilder.append("'");
            }
                stringBuilder.append(param);
            if(param instanceof String){
                stringBuilder.append("'");
            }

        }
        stringBuilder.append(")}catch(error){console.error(error.message);}");
        final String call = stringBuilder.toString();
        Log.i(TAG, "callJavaScript: call="+call);


        view.loadUrl(call);
    }
callJavaScript(mBrowser, "alert", "abc", "def");
//javascript:try{alert('abc','def')}catch(error){console.error(error.message);}
callJavaScript(mBrowser, "alert", 1, true, "abc");
//javascript:try{alert(1,true,'abc')}catch(error){console.error(error.message);}

Note that objects will not be passed correctly - but you can serialize them before passing as an argument.

Also I've changed where the error goes, I've diverted it to the console log which can be listened by:

webView.setWebChromeClient(new CustomWebChromeClient());

and client

class CustomWebChromeClient extends WebChromeClient {
    private static final String TAG = "CustomWebChromeClient";

    @Override
    public boolean onConsoleMessage(ConsoleMessage cm) {
        Log.d(TAG, String.format("%s @ %d: %s", cm.message(),
                cm.lineNumber(), cm.sourceId()));
        return true;
    }
}

Android Calling JavaScript functions in WebView - Stack Overflow

javascript android webview
Rectangle 27 11

private void callJavaScript(WebView view, String methodName, Object...params){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("javascript:try{");
        stringBuilder.append(methodName);
        stringBuilder.append("(");
        String separator = "";
        for (Object param : params) {               
            stringBuilder.append(separator);
            separator = ",";
            if(param instanceof String){
                stringBuilder.append("'");
            }
                stringBuilder.append(param);
            if(param instanceof String){
                stringBuilder.append("'");
            }

        }
        stringBuilder.append(")}catch(error){console.error(error.message);}");
        final String call = stringBuilder.toString();
        Log.i(TAG, "callJavaScript: call="+call);


        view.loadUrl(call);
    }
callJavaScript(mBrowser, "alert", "abc", "def");
//javascript:try{alert('abc','def')}catch(error){console.error(error.message);}
callJavaScript(mBrowser, "alert", 1, true, "abc");
//javascript:try{alert(1,true,'abc')}catch(error){console.error(error.message);}

Note that objects will not be passed correctly - but you can serialize them before passing as an argument.

Also I've changed where the error goes, I've diverted it to the console log which can be listened by:

webView.setWebChromeClient(new CustomWebChromeClient());

and client

class CustomWebChromeClient extends WebChromeClient {
    private static final String TAG = "CustomWebChromeClient";

    @Override
    public boolean onConsoleMessage(ConsoleMessage cm) {
        Log.d(TAG, String.format("%s @ %d: %s", cm.message(),
                cm.lineNumber(), cm.sourceId()));
        return true;
    }
}

Android Calling JavaScript functions in WebView - Stack Overflow

javascript android webview
Rectangle 27 6

Well, this was actually a lot easier than originally thought. I didn't notice a JavaScript error occurring on load of the page in WebView, which wasn't occurring when loading the page in Chrome.

Uncaught TypeError: Cannot read property 'getItem' of null

Upon tracing the JS, I found this was stemming from use of window.localStorage. This is not permitted in WebView unless the setting is enabled.

mWebView.getSettings().setDomStorageEnabled(true);

javascript - WebView fails to load Imgur albums - Stack Overflow

javascript android webview imgur
Rectangle 27 2

in xWALK section..

dependencies {
    compile 'org.xwalk:xwalk_core_library:23.53.589.4'
}

Error with building android app (cordova-plugin-crosswalk-webview) - S...

android cordova ionic-framework webview
Rectangle 27 2

compile xwalkSpec
VERSION
find . -name "*xwalk_core_library*"
./android/.idea/libraries/xwalk_core_library_20_50_533_12.xml
preview-xwalk.gradle
compile 'org.xwalk:xwalk_core_library:20.50.533.12'

Error with building android app (cordova-plugin-crosswalk-webview) - S...

android cordova ionic-framework webview
Rectangle 27 2

It looks like if you are clicking a link with a custom scheme from a WebView on Android, that you now need to implement a handler in the WebViewClient.

To get this working, you need to update your implementation of WebViewClient.shouldOverrideUrlLoading to something like the following:

@Override
public boolean shouldOverrideUrlLoading(WebView view,
                                        WebResourceRequest request) {
    Uri uri = request.getUrl();
    if (Objects.equals(uri.getScheme(), "whatsapp")) {
        try {
            Intent intent = Intent.parseUri(request.getUrl().toString(), Intent.URI_INTENT_SCHEME);
            if(intent.resolveActivity(getPackageManager()) != null)
                startActivity(intent);
            return true;
        } catch (URISyntaxException use) {
            Log.e(TAG, use.getMessage());
        }
    }
    return super.shouldOverrideUrlLoading(view, request);
}

android - sharing whatsapp on webview - Stack Overflow

android webview
Rectangle 27 2

It looks like if you are clicking a link with a custom scheme from a WebView on Android, that you now need to implement a handler in the WebViewClient.

To get this working, you need to update your implementation of WebViewClient.shouldOverrideUrlLoading to something like the following:

@Override
public boolean shouldOverrideUrlLoading(WebView view,
                                        WebResourceRequest request) {
    Uri uri = request.getUrl();
    if (Objects.equals(uri.getScheme(), "whatsapp")) {
        try {
            Intent intent = Intent.parseUri(request.getUrl().toString(), Intent.URI_INTENT_SCHEME);
            if(intent.resolveActivity(getPackageManager()) != null)
                startActivity(intent);
            return true;
        } catch (URISyntaxException use) {
            Log.e(TAG, use.getMessage());
        }
    }
    return super.shouldOverrideUrlLoading(view, request);
}

android - sharing whatsapp on webview - Stack Overflow

android webview