Pulkit Tyagi
Pulkit Tyagi

Reputation: 61

async splashscreen to show before webview app

i am making an android that runs a fullscreen webview but the problem is the index page appears after a 2 sec delay so i want to implement a splash screen to be displayed until the webview loads the website in the background .please if you could he me with complete code of this splash screen because i am completely new to it .

main activity is :

public class MainActivity extends Activity  {

WebView website;

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

    setContentView(R.layout.activity_main);
    website = (WebView) findViewById(R.id.website);
    website.setWebViewClient(new WebViewClient());
    WebSettings webSettings = website.getSettings();
    webSettings.setJavaScriptEnabled(true);
    website.loadUrl("http://www.fitanity.com/index2.jsp");

}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && website.canGoBack()) {
        website.goBack();
        return true;
    }
    else
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

}

Upvotes: 1

Views: 1354

Answers (3)

Nikhil Gaur
Nikhil Gaur

Reputation: 1272

Dhwanik's solution worked for me too but I wanted to show come text with backgrount and loading gif as a splash screen so I replaced image with a new layout and placed all my contents in it.

Here is the link which which could be useful for everyone who need the same like me http://www.codewithasp.net/2016/01/show-splash-screen-while-android.html

Upvotes: 0

Dhwanik Gandhi
Dhwanik Gandhi

Reputation: 685

you can initially show ImageView, once webview get loaded in public void onPageFinished() change their visibility.

public class MainActivity1 extends Activity {

WebView website;
ImageView imgLoading;

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

    setContentView(R.layout.activity_main);
    website = (WebView) findViewById(R.id.website);
    imgLoading = (ImageView)findViewById(R.id.imgloader);
    WebSettings webSettings = website.getSettings();
    webSettings.setJavaScriptEnabled(true);
    website.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            //hide loading image
            imgLoading.setVisibility(View.GONE);
            //show webview
            website.setVisibility(View.VISIBLE);
        }

    });     

    website.loadUrl("http://www.fitanity.com/index2.jsp");

}

your xml will looks like :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imgloader"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/loader"
    android:visibility="visible" />

<WebView
    android:id="@+id/website"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone" />

</LinearLayout>

Upvotes: 1

AmniX
AmniX

Reputation: 653

To do this, You have to make a class which extends WebViewClient class.

in this class, There are some method you can or you have to override,

1.onPageStarted -- This will call when webview will start loading Page. (SHOW YOU SPALSH) 2.shouldOverrideUrlLoading -- This method you have to override 3.onPageFinished -- This will call when webview will stop loading Page. (HIDE YOUR SPLASH)

Now into you webview, use you own class of Webview like this,

    web.setWebViewClient(new mywebclient());

Hope It Helps you,

Cheers

-Aman

Upvotes: 0

Related Questions