Yahoo Yahoo - 3 months ago 21
Android Question

How to remove white screen while loading android application?

I am new to android. I have a web view which loads a URL. The problem is that after I open the application there is a white screen for 2-3 seconds after that the web view‘s URL is loaded.

I think this is the time the application Initiates. How can I remove the white screen and display my Logo for that time?
I heard of splash screen but in that the logo appears for 1 second then white screen appears for another 2-3 seconds and then finally the web view is loaded.

What am I doing wrong? Is it splash screen or some other way to display the logo while web view loads?

package com.exampe.dating;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

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

WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("http://www.example.com/mobile/index.php");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView mywebview = (WebView) findViewById(R.id.webview);
if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
mywebview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}


splashActivity.java

package com.exampe.dating;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class SplashActivity extends Activity {

private static String TAG = SplashActivity.class.getName();
private static long SLEEP_TIME = 10; // Sleep for some time

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

this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar

setContentView(R.layout.splash);

// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}

private class IntentLauncher extends Thread {
@Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}

// Start main activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
}
}


androidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampe.dating"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.exampe.dating.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</application>

</manifest>

Answer

It's very weird to have two launch activities declared in your manifest like that. You should eliminate the intent filter for SplashActivity and just start SplashActivity from within onCreate of your MainActivity:

EDIT Updated to replace sleeping with waiting. Splash screen will disappear when the page is loaded or when a time-out occurs, whichever comes first.

public class MainActivity extends Activity {
    public static Object SPLASH_LOCK = new Object();

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


        WebView mywebview = (WebView) findViewById(R.id.webview);
        mywebview.loadUrl("http://www.example.com/mobile/index.php");
        WebSettings webSettings = mywebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebview.setWebViewClient(new WebViewClient());

        startActivity(new Intent(this, SplashActivity.class));
    }
    . . .
}

// in your WebViewClient:
public void onPageFinished (WebView view, String url) {
    synchronized (SPLASH_LOCK) {
        SPLASH_LOCK.notifyAll();
    }
}    

Then in SplashActivity, wait the delay you want and just call finish(). You don't need to start MainActivity again, because it's sitting behind the SplashActivity and will appear when SplashActivity finishes.

public class SplashActivity extends Activity {

       private static String TAG = SplashActivity.class.getName();
       private static long MAX_SPLASH_TIME = 10000;

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

          . . .

          setContentView(R.layout.splash);

          new Thread() {
              @Override
              public void run() {
                  synchronized (MainActivity.SPLASH_LOCK) {
                      // wait for notify or time-out
                      try { MainActivity.SPLASH_LOCK.wait(MAX_SPLASH_TIME); }
                      catch (InterruptedException ignored) {}
                  }
                  finish();
              }
          }.start();
       }
       . . .
}

Note that you don't need the IntentLauncher class.

P.S. You didn't ask about this, but if you add this to the SplashActivity tag in your manifest:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

then you don't need to call requestWindowFeature or set window flags in onCreate.

Comments