-3

I want to load a website (in WebView), but it loads too slow and show a white screen. I want to show a splash screen and in the background load the WebView. After few seconds I want to close the splash screen and show the site that should be ready by then. How can I do it? Thanks!

1
  • In general here, it is a good idea to show what code you presently have, if only to save your question from downvotes!
    – halfer
    Apr 3, 2013 at 7:29

4 Answers 4

1

I use this code for a splash screen

public class SplashActivity extends Activity {

    private boolean isBackButtonPressed;
    private static final int SPLASH_DURATION = 2000; // 2 seconds

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTheme(R.style.SplashTheme);
        getWindow().setWindowAnimations(0);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_splash);

        Handler handler = new Handler();

        // run a thread after 2 seconds to start the home screen
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {

                // make sure we close the splash screen so the user won't come
                // back when it presses back key

                finish();

                if (!isBackButtonPressed) {
                    // start the home screen if the back button wasn't pressed
                    Intent intent = new Intent(SplashActivity.this, FragmentActivity.class);
                    startActivity(intent);
                    finish();   
                }

            }

        }, SPLASH_DURATION); 
    }

    @Override
   public void onBackPressed() {
        super.onBackPressed();
        isBackButtonPressed = true;
    }   

}
0
1

You can use the following code for it.

public class Splash extends Activity {
/** Called when the activity is first created. */

private boolean mSplashActive = true, mPaused;
private long mSplashTime = 1000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    new Thread(){
        public void run(){
            try {
                long ms = 0;
                while(mSplashActive && ms < mSplashTime) {
                    sleep(100);
                    if(!mPaused) {
                        ms += 100;
                    }
                }
                if(Resources.getResources().isUserRegistered(Splash.this)) {
                    //user is registered so launch main screen
                } else {
                    //user is not registered launch welcome wizard.
                    Intent intent = new Intent(Splash.this, WelcomeScreen.class);
                    Splash.this.startActivity(intent);
                }
                finish();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }.start();

}

@Override
public void onPause(){
    super.onPause();
    mPaused = true;
}

@Override
public void onResume(){
    super.onResume();
    mPaused = false;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);

    if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        mSplashActive = false;
    }
    return true;
}
}
0
0

in the webview class you can set something like this.

i setup this for you:

public class WebViewActivity extends Activity {

WebView webView;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);
    progressDialog = new ProgressDialog(WebViewActivity.this);
    progressDialog.setTitle("here your title");
    progressDialog.setMessage("message here");

    webView = (WebView) findViewById(R.id.webView1);

    webView.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            // here you can display an imageview fullscreen
            // or show and progressdialog
            progressDialog.show();

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            // here you can remove the imageview and the user can continue using the webview
            // or hide the progressdialog
            progressDialog.dismiss();

        }

    });

}

}

if you have any questions let me know

3
  • Thank You very much! your answere is what i needed most! but i'm using Chrome - ( mWebView.setWebChromeClient(new GeoWebChromeClient(){ ) , and there is no option like "onPageFinished" there... :-( Apr 3, 2013 at 10:55
  • You can set both Webchromeclient and webviewclient so it doesnt matter wheter you set webchrome or webview or both.
    – user2157571
    Apr 3, 2013 at 13:27
  • Also for the progressdialog i suggest setting: progressDialog.setCancelable(false); and progressDialog.setCanceledOnTouchOutside(false); this is so the dialog cannot be canceled (dismissed) by the user.
    – user2157571
    Apr 3, 2013 at 13:45
0

public class SplashScreen extends Activity { private Thread mSplashThread;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash);

    final SplashScreen sPlashScreen = this;

    // The thread to wait for splash screen events
    mSplashThread = new Thread() {
        @SuppressWarnings("deprecation")
        @Override
        public void run() {
            try {
                synchronized (this) {
                    // Wait given period of time or exit on touch
                    wait(5000);
                }
            } catch (InterruptedException ex) {
            }

            finish();

            // Run next activity
            Intent intent = new Intent();
            intent.setClass(sPlashScreen, MainActivity.class);
            startActivity(intent);

        }
    };

    mSplashThread.start();
}

/**
 * Processes splash screen touch events
 */
@Override
public boolean onTouchEvent(MotionEvent evt) {
    if (evt.getAction() == MotionEvent.ACTION_DOWN) {
        synchronized (mSplashThread) {
            mSplashThread.notifyAll();
        }
    }
    return true;
}

}

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.