NoWar
NoWar

Reputation: 37632

Finish splash screen on website loading

I need to display splash screen until web page is loaded in webview.

I use following code. Is it possible to do?

public class SplashScreen extends Activity {

    protected Intent intent;

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_splash_screen);

        intent = new Intent(getApplicationContext(), MainActivity.class);

        // MainActivity.class  contains WebView

        Thread myThread = new Thread() {
            @Override
            public void run() {

                try {
                    sleep(5000);
                    startActivity(intent);
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        };

        myThread.start();
    }
}

And

public class MainActivity extends Activity {

    private WebView view;

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        String url = "http://google.com";
        view = (WebView)this.findViewById(R.id.webView1);

        view.clearCache(true);
        WebSettings s = view.getSettings();
        s.setJavaScriptEnabled(true);

        s.setCacheMode(WebSettings.LOAD_DEFAULT);
        s.setDomStorageEnabled(true);

        view.loadUrl(url);
    }

Upvotes: 0

Views: 791

Answers (2)

NoWar
NoWar

Reputation: 37632

My solution is here

private WebView view;
private ImageView splashScreen;

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

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

    splashScreen =   (ImageView) this.findViewById(R.id.spscreen);

    String url = "http://google.com";
    view = (WebView)this.findViewById(R.id.webView1);

    view.setWebViewClient(new WebViewClient() {            

       public void onPageFinished(WebView view, String url) {
            // do your stuff here
            splashScreen.setVisibility(View.INVISIBLE);
            view.setVisibility(View.VISIBLE);
        }
    });

      WebSettings s = view.getSettings();
    s.setJavaScriptEnabled(true);

    s.setCacheMode(WebSettings.LOAD_DEFAULT);
    s.setDomStorageEnabled(true);

    view.loadUrl(url);
}

And layout

<ImageView
    android:id="@+id/spscreen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/splashscreen"
     android:visibility="visible"
    android:scaleType="fitXY"/>

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView1"
    android:visibility="invisible" />

Upvotes: 0

Abilash
Abilash

Reputation: 218

Don't make a separate activity for splash screen, in MainActivity.java itself create a splash screen layout and webview layout and set visibility of webview to GONE.

On open of MainActivity initialize webView and set custom WebViewClient. Override onPageFinished() in your custom webViewClient and in this method make webview visible and splash screen layout to Gone.

Same here : Loading a WebView URL before splashscreen finishes

Upvotes: 1

Related Questions