0

I'm trying to make the javascript work in android for this page:

http://test.swiss-impulse.com/scratch_card_game/index.html

But i have issues regarding the browser.

This is the parameters set for the webview:

webView1.setWebChromeClient(mClient);

webView1.setDownloadListener(new DownloadListener() {

        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

webView1.addJavascriptInterface(new JavaScriptInterfaceForTitle(this, mPasswordStrength, webView1), "HtmlViewer");

webView1.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            String cookie = CookieManager.getInstance().getCookie(url);
            if (url.equals(getContentUrl() + "addcomment")) {
                webView1.stopLoading();
                Intent addcommentIntent = new Intent(mContext, AddCommentActivity.class);
                addcommentIntent.putExtra("url", previousUrl);
                startActivity(addcommentIntent);
            } else {
                previousUrl = url;
                ((BaseActivity) mContext).showLoadingDialog();
                if (!urlStackList.contains(url)) {
                    urlStackList.add(url);
                }
                super.onPageStarted(view, url, favicon);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            ((BaseActivity) mContext).hideLoadingDialog();
            if (url.contains("content/shows/") || url.contains("content/tips/")) {
                counter = 0;
                view.loadUrl("javascript:window.HtmlViewer.showHTML(document.getElementById('" + mContext.getString(R.string.title_name) + "').innerHTML);");
                view.loadUrl("javascript:window.HtmlViewer.showHTML(document.getElementById('name_en').innerHTML);");
                view.loadUrl("javascript:window.HtmlViewer.showHTML(document.getElementById('name_zh').innerHTML);");
            } else {
                mStateBar.setShowTitle(mContext.getString(R.string.tv_drama));
            }
            super.onPageFinished(view, url);
        }

    });
webview1.setWebChromeClient(mClient);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.getSettings().setPluginState(PluginState.ON);
webView1.getSettings().setPluginsEnabled(true);
webView1.getSettings().setAppCacheMaxSize(httpCacheSize);
webView1.getSettings().setSupportZoom(false);
webView1.getSettings().setBuiltInZoomControls(false);
webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Am i missing some parameters to make this work on android webview ? I checked chrome browser and there it works with no problems, so i'm sure i missed something and cannot find what.

EDIT:

class MyChromeClient extends WebChromeClient {

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        mCustomViewCallback = callback;
        mTargetView.addView(view);
        mCustomView = view;
        mContentView.setVisibility(View.GONE);
        mTargetView.setVisibility(View.VISIBLE);
        mTargetView.bringToFront();
    }

    @Override
    public void onHideCustomView() {
        if (mCustomView == null)
            return;

        mCustomView.setVisibility(View.GONE);
        mTargetView.removeView(mCustomView);
        mCustomView = null;
        mTargetView.setVisibility(View.GONE);
        mCustomViewCallback.onCustomViewHidden();
        mContentView.setVisibility(View.VISIBLE);
    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        TvDramaActivity.this.startActivityForResult(Intent.createChooser(i, "Chooser"), 100);
    }

    // For Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        openFileChooser(uploadMsg, "");
    }

    // For Android > 4.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        openFileChooser(uploadMsg, "");
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
    }
}

public boolean inCustomView() {
    return (mCustomView != null);
}

public void hideCustomView() {
    mClient.onHideCustomView();
}
8
  • 1
    are there any errors or warnings in LogCat Commented Mar 28, 2014 at 9:18
  • Add webview1.setWebChromeClient(new WebChromeClient()); Commented Mar 28, 2014 at 9:22
  • no errors or warnings Commented Mar 28, 2014 at 9:22
  • webView1.setWebChromeClient(mClient); i have that already. I will edit post to see my webchromeclient Commented Mar 28, 2014 at 9:23
  • Please paste your complete code for webview Commented Mar 28, 2014 at 9:24

2 Answers 2

1

This works for me :

In your WebView Activity class :

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

    webView1 = (WebView) findViewById(R.id.web);

    webView1.setWebChromeClient(new WebChromeClient());
    webView1.getSettings().setJavaScriptEnabled(true);
    webView1.getSettings().setPluginState(PluginState.ON);

    webView1.getSettings().setSupportZoom(false);
    webView1.getSettings().setBuiltInZoomControls(false);
    webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    webView1.loadUrl("http://test.swiss-impulse.com/scratch_card_game/index.html");
   }

Your layout file :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView 
    android:id="@+id/web"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

</LinearLayout>

Don't forget to add <uses-permission android:name="android.permission.INTERNET"/> in manifest file.

Hope this helps.Thanks.

Sign up to request clarification or add additional context in comments.

13 Comments

Does the page scratches? I mean is there a picture if u scratch the blue area ?
You can check this from your end too.
its wierd because for me it doesn't work. Can you tell me what Android version you where testing ? (Im testing on android 4.3)
I am testing on Android 4.4 HTC One.
Thanks for the link. I will report to the web team to check it.
|
0

you can try below code

mWebView.setWebChromeClient(new WebChromeClient());
//support JavaScript
mWebView.getSettings().setJavaScriptEnabled(true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.