Jagath Bandara
Jagath Bandara

Reputation: 71

Having blank/white screen on android WebView

First of all I would like to say that I can see lots of questions similar to my question in Stack Overflow and also in so many other places. However I still wasn't able to find a solution to my issue from them and so I'm putting my question here.

I'm writing an app with a WebView and I'm having a white blank screen when it's loading the URL. It worked fine and loaded the web page previously. However now it only gives a white screen and does not seem to be loading the page. It does not even give the message defined in onReceivedError() method when I give a wrong URL. Currently I'm testing with a virtual device with android API 22. Below are my codes. Can someone suggest me what the error is and how to fix it.

Main Activity

   package com.test.testApp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.Window;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    public class MainActivity extends AppCompatActivity {
        private WebView webView;
        private static final String URL = "https://www.google.com/";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getSupportActionBar().hide();
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            webView = (WebView) findViewById(R.id.webView);
            webView.setWebViewClient(new MyWebViewClient());
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
    
            webView.loadUrl(URL);
        }
    
        @Override
        public void onBackPressed() {
            if (webView.canGoBack()) {
                webView.goBack();
            } else {
                super.onBackPressed();
            }
        }
    
        public class MyWebViewClient extends WebViewClient {
    
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                String htmlData = "<html><body><div align=\"center\">Something wrong happened. Please check your internet connection and try again...!</div></body>";
                webView.loadUrl("about:blank");
                webView.loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null);
            }
        }
    }

activity_main.xml

     <?xml version="1.0" encoding="utf-8"?>
        <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
        
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent">
        
          <WebView
              android:id="@+id/webView"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layerType="hardware">
          </WebView>
         </LinearLayout>
        
        </androidx.constraintlayout.widget.ConstraintLayout>
    

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.testApp">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SettingsActivity">
            <intent-filter>
                <action android:name="com.test.testApp.SettingsActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

</manifest>

Upvotes: 5

Views: 14686

Answers (4)

svenyonson
svenyonson

Reputation: 1909

For anyone else experiencing this - I was having a problem when redirected to an SSO login page, and the page was just a blank page. It turns out that WebViews have javascript disabled by default, so if your login page uses javascript, you might have this symptom - or others.

What solved my problem was enabling javascript with WebSettings:

WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);

Upvotes: 0

Jagath Bandara
Jagath Bandara

Reputation: 71

I was able to get the issue fixed by simply updating the Android Studio and AVD. Don't know what the actual reason is. However I think it's better to try updating the Android studio and AVD for others too if they are facing this strange issue.

Upvotes: -2

Syed Rafaqat Hussain
Syed Rafaqat Hussain

Reputation: 1126

try this in your manifest may be your problem should be solved

android:usesCleartextTraffic="true"

Upvotes: -1

Ridvan Ozcan
Ridvan Ozcan

Reputation: 47

try

webView.getSettings().setDomStorageEnabled(true)

Upvotes: 3

Related Questions