1

I am creating an app that loads a website through the webview, and before it shows a splash screen. The problem is that after the splash screen a white screen appear and then the webview loads.
I don't want to use a timer in the splash scree, I want it to be gone once the webview is loaded. I saw that I need to move the splash activity to the main activity, but I am not sure how. I am a beginner with android studio.

AndroidManifest.xml:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />

        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.NoActionBar">
        </activity>
    </application>

</manifest>

MainActivity.java:

package app.atlasdatabase;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import static app.atlasdatabase.R.style.AppCompatAlertDialogStyle;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        WebView myWebView = (WebView) findViewById(R.id.atlasdatabase);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        myWebView.loadUrl("file:///android_asset/index.html");

    }

    /**
     * Exit the app if user select yes.
     */
    private void doExit() {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                MainActivity.this, AppCompatAlertDialogStyle);

         alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        alertDialog.setTitle(R.string.exit);
        alertDialog.setMessage(R.string.exitmsg);
        alertDialog.setNegativeButton(R.string.no, null);
        alertDialog.show();
    }

    @Override
    public void onBackPressed()
    {
        WebView webView = (WebView) findViewById(R.id.atlasdatabase);
        if(webView.canGoBack()){
            webView.goBack();
        }else{
            /* Close the app without the Dialog
            super.onBackPressed();
             */
            /* Use the dialog to Exit the App */
            doExit();
        }
    }
}

SplashActivity.java:

package app.atlasdatabase;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context="atlasdb.atlasdatabase.MainActivity">

    <WebView
        android:id="@+id/atlasdatabase"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_home_footer">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

background_splash.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Background color -->
    <item android:drawable="@color/colorPrimary"/>

    <!-- Image at the center of the screen -->
    <item>
        <bitmap android:gravity="center" android:src="@drawable/splash"/>
    </item>
</layer-list>
RogerHN
  • 584
  • 1
  • 11
  • 31

3 Answers3

3

I found the simplest answer in How to manage the Blank White Loading screen of an Android Webview?, which helped me to solve the problem I was facing without any Splash activity Screen:

In Android manifest:

    <activity android:name=".MainActivity"
        android:theme="@style/Splash">
...

and on Activity (from xml it's not working), after creating webView, set the background to be transparent.

myWebView = (WebView)findViewById(R.id.webView);
myWebView.setBackgroundColor(Color.TRANSPARENT);

(I have added progress bar popup and killed on pageFinished for users to know loading is in progress.)

Nokuap
  • 2,289
  • 2
  • 17
  • 17
Raghu Vallikkat
  • 365
  • 5
  • 16
2
  • Here in the answer you can see comments where you need to concern.
  • You don't need a SplashActivity for this.
  • You can keep a view which is match_parent to the root view (so it will go full screen) and add your image or whatever to that.
  • When your Activity loads make sure it is visible/or like in the code given @ onPageStarted if the view is not loaded yet, you can display your logo..
  • When the web view is ready as in the comment @ onPageFinished make your splashView invisible or view gone!

for these tasks you can use android:visibility="gone" in Xml, yourRootViewWithImage.setVisibility(View.VISIBLE); yourRootViewWithImage.setVisibility(View.GONE); use these lines in proper places!

Credit goes to this post, example :

   public class MainActivity  extends AppCompatActivity  {


    private boolean loadingFinished = true;
    private boolean redirect = false;
    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        myWebView = (WebView) findViewById(R.id.atlasdatabase);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        myWebView.loadUrl("file:///android_asset/index.html");


        myWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(
                    WebView view, WebResourceRequest request) {
                if (!loadingFinished) {
                    redirect = true;
                }

                loadingFinished = false;
                myWebView.loadUrl(request.getUrl().toString());
                return true;
            }

            @Override
            public void onPageStarted(
                    WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                loadingFinished = false;
                //SHOW LOADING IF IT ISNT ALREADY VISIBLE
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if(!redirect){
                    loadingFinished = true;
                }

                if(loadingFinished && !redirect){
                    //HIDE LOADING IT HAS FINISHED // HIDE YOUR SPLASH/LOADING VIEW
                } else{
                    redirect = false;
                }
            }
        });

    }
/// below code ..

}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Okay, but how do I implement this on my code without messing it? – RogerHN Feb 03 '17 at 02:42
  • @RogerHN very easy you have another layout as a root or first child.I think you can do that easily.Then add your image to that layout.Make it sure it will display as you want when you load the activity thats your first task – Charuක Feb 03 '17 at 02:46
  • @RogerHN then the easiest way is inside your onCreate add this code if you have a problem let me know – Charuක Feb 03 '17 at 02:50
  • Not sure if I understood how to do it. Can you please show me an examplein my code? I only need to add that code to my MainActivity.java onCreate part? And how I move my image to the layout that you mentioned? Sorry, but I am still learning. I did most of this with tutorials :) – RogerHN Feb 03 '17 at 02:53
  • @RogerHN updated the answer but you need to add some methods that you have but onCreate part is there my full answer wont help you to learn thats the thing – Charuක Feb 03 '17 at 02:57
  • @RogerHN you are welcome to ask if you missed something! – Charuක Feb 03 '17 at 02:58
  • @RogerHN concept is this.You have a web view but you dont know when it loads.so till it loads 100% you show a view(splash view).Once its load and ready to display that splash view is not needed.So you remove it. – Charuක Feb 03 '17 at 03:10
  • Okay, I will try here and see if it works, I tried to add the code in the onCreate part but it gave me some errors. I will spend some more time here and see if I get this thing working – RogerHN Feb 03 '17 at 03:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134725/discussion-between-charu-and-rogerhn). – Charuක Feb 03 '17 at 03:15
  • the link in your answer is broken – w0ns88 Oct 10 '17 at 07:34
0

I've experienced this problem before and this is just a hack , but i had the same problem and for some reason that i'm still reading why but changing the web view from opening from a fragment to an activity made the white screen at the start of loading the web view went away.

coding.cat3
  • 111
  • 4