Home > Software engineering >  Android webview widget stuck on blank screen
Android webview widget stuck on blank screen

Time:11-05

I want to have my VUE app on an Android app by using a webview. For such, I have found this project on github to view a URL.

I have deployed my VUE app on HERE to have it accessible.

Following the documentation from the Android project, I have modified couple of thins:

  1. On MainActivity.java, line 24, I have added my URL: mWebView.loadUrl("https://albert.suments.com/estelares");

  2. On MyWebClient.java, line 15, I have modified the hostname variable to make prevent it to open URLs under that host on a browser:

hostname = "albert.suments.com";
Uri uri = Uri.parse(url);
if (url.startsWith("file:") || uri.getHost() != null && uri.getHost().endsWith(hostname)) {
    return false;
}

With those values, when I run the app from Android Studio on a device, it gets stuck on a blank screen.

But, If I set the value as:

mWebView.loadUrl("https://www.google.com");

hostname = "google.com";

It shows the website correctly embebed on a Widget.

  1. Is there anything obvious that I might be missing?

UPDATE----------------------------------------

Here is the code for MyWebViewClient.java:

class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String hostname;

        // YOUR HOSTNAME
        hostname = "albert.suments.com";
        url = "https://albert.suments.com/estelares";
        Uri uri = Uri.parse(url);


        if (url.startsWith("file:") || uri.getHost() != null && uri.getHost().endsWith(hostname)) {
        /**
         * It works correctly when:
         *     hostname = "google.com";
         *     url = "https://www.google.com";
         * It gets stuck on blank screen when:
         *     hostname = "albert.suments.com";
         *     url = "https://albert.suments.com/estelares";
         */
            return false;
        }

        /**
         *
         * When this code executes, it opens a new tab on the browser and display the web correctly
         */
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;

    }
}

Here is the code for MainActivity:

public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyWebViewClient());

        // REMOTE RESOURCE
        mWebView.loadUrl("https://albert.suments.com/estelares");
        // LOCAL RESOURCE
        //mWebView.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

CodePudding user response:

Here you go !!

Add this line

webSettings.setDomStorageEnabled(true); Letme know if it worked for you and mark this answer as corrected if it worked for you Thanks!

  • Related