Reputation: 4187
I want to load the splash Screen while the Webview is loading . I am using the below code but after 10 secods I see a black webview. Please help.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setVerticalScrollBarEnabled(false);
myWebView.setHorizontalScrollBarEnabled(false);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("http://www.XXXXXXX.com);
setContentView(R.layout.splash);
}
@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;
}
private class MyWebViewClient extends WebViewClient {
public void onPageFinished (WebView myWebView, String url) {
setContentView(R.layout.activity_main);
}
}
I want that while the webview loads , the splash screen with the App's Logo is shown. How Do I do this ? Is my code Correct ? I get the webview after some time , but it appears to be blank.
Upvotes: 3
Views: 4598
Reputation: 2528
Calling setContentView()
multiple times is not recommended. Add an ImageView
to your main layout(set its src
as your splash screen image). Default make it hidden by using imageView.setVisibility(View.INVISIBLE);
. When you start loading a new page change the webView
visibility to INVISIBLE
and imageView
visibility to VISIBLE
in function onPageStarted()
.
When loading is finished , change the visibility of imageView
to INVISIBLE
and change the visibility of webView
to VISIBLE
in function onPageFinished()
.
Upvotes: 0
Reputation: 208
Please show a dialog or launch another activity at a point where you set splash screen.
setContentView(R.layout.splash);
Replace this line with your preferred solution either a costume dialog that looks like a splash or another full splash screen in new activity.
If you launch new activity, then keep instance of activity in your MainActivity and when your web page loading is finished then finish the splash screen activity.
I prefer you to make full screen dialog for it because it easier to maintain dialog as compare to activity in your case.
Upvotes: 1
Reputation: 1883
I will suggest you to make a custom dialog and place Image view in it and set background image you want on the webview's onPageStarted Method
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
ProgressDialog();
}
Upvotes: 0