2

Android WebView: WebGL is not working on some devices

I use Webview in my android application. The task is to add WebGL interactive elements on the screen. Application have minSdk v21. Google announced that they support WebGL in WebView v36. I check WebGL status with html5test.com page and my test web page.

My test setup: Google Nexus 6P - Android 6.0.1, webView v48 - WebGL OK works

Sony Xperia Z2 - Android 5.1.1, webView v48 - WebGL OK works

Samsung Galaxy Note 4 - Android 5.1.1, webView v48 - WebGL OK works

Samsung Galaxy Tab S - Android 5.0.2, webView v48 - WebGL FAILED blank screen

RKM V5 Android TV - Android 5.1.1, webView v39 - WebGL FAILED blank screen

I do not see any info about in Google Developer documentation for android.webkit.WebView element Is there any way to make it possible to work on all devices?

My webView initialization:

    mElementView = new WebView(mContext);
    mElementView.setVerticalScrollBarEnabled(scrollEnabled);
    mElementView.setHorizontalScrollBarEnabled(scrollEnabled);

    mElementView.getSettings().setJavaScriptEnabled(true);
    mElementView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    mElementView.getSettings().setAllowFileAccessFromFileURLs(true);

Update: WebGL works in Chrome browser on all devices, but fails in Galaxy Tab and Android TV webViews

Slonorib
  • 79
  • 2
  • 11

3 Answers3

8

Late but I think this will be helpful to someone. In your activity using webView, set this in androidmanifests set: android:hardwareAccelerated="true". And my webView looks like this:

 webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setLoadsImagesAutomatically(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
    }
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    webView.setWebChromeClient(new WebChromeClient());
CaptainBli
  • 4,121
  • 4
  • 39
  • 58
TedVN
  • 536
  • 5
  • 10
  • 2
    For Xamarin, we had to add `[assembly: Application(HardwareAccelerated = true)]` to the top of our WebView renderer source file, and then our webview was able to render WebGL – A. Blodgett Mar 14 '19 at 23:18
1

this coding working well. try this

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

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    TextView title=(TextView)findViewById(R.id.toolbar_title);
    title.setText(R.string.about);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    webView=(WebView)findViewById(R.id.webView);
    webView.setWebViewClient(new MyBrowser());
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.loadUrl(url);
}
private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent report = new Intent(About.this, MainActivity.class);
            startActivity(report);
            finish();
            break;
        default:
            break;
    }
    return false;
}
@Override
public void onBackPressed() {
    Intent intent=new Intent(About.this,MainActivity.class);
    startActivity(intent);
    super.onBackPressed();
}
Ajin kumar
  • 311
  • 1
  • 3
  • 14
1

Check out this answer. It might be of some help. Otherwise, I would advise you to use the CrossWalk project.

CaptainBli
  • 4,121
  • 4
  • 39
  • 58
Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75