Android Webview wait until it loads fully - android

I´ve searched in all topics but i've not found anything that works for me.
I have an android application with a webview. I set up the page with a small javascript code in OnPageFinished and i see how it hides and shows elements, but i don´t want to see that. I would want that webview shows the current page while it is loading and when it's loaded fully, webview shows the new page.
Is there any way to get it?? Sorry for my english. Thanks.

Show progress bar or progress dialog ,initialize a progress bar on your xml page and use it to load untill the your webpage has fullyloaded
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setDomStorageEnabled(true);
webView.post(new Runnable() {
#Override
public void run() {
webView.loadUrl("file:///android_asset/Guage.html");
}
});
webView.setWebViewClient(new myWebClient());
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}

Related

News link in webview

I am new in android and I am displaying a news link in a webview. News link is displaying properly but video in news link is not playing.
Link is :
http://aajtak.intoday.in/livetv.html
My code is:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
web.getSettings().setPluginState(PluginState.ON);
web.loadUrl("http://aajtak.intoday.in/karyakram/video/so-sorry-episode-of-10th-august-2016-on-delhi-aam-admi-party-mla-and-rajnath-singh-najeeb-jung-1-882177.html");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
To support video in webview, you need to have hardware acceleration turned on, and set a WebChromeClient.
For full screen support, implementations of onShowCustomView(View, WebChromeClient.CustomViewCallback) and onHideCustomView() are required.
Used this, but now a days onShowCustomView() is deprecated.
from
YouTube Video not playing in WebView - Android
Try adding this code :
webView.setWebChromeClient(new WebChromeClient());

ProgressBar in android webview not stop spinning

i'm creating a android webview app. Im facing a problem with progress bar. The progress bar is completely and fully implemented and keeps spinning. but it doesn't stop spinning even after the page loads completely. what should i do?
main activity layout::::
<ProgressBar android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
mainActivity.java:::
public class MainActivity extends ActionBarActivity {
WebView web;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("http://google.com");
web.setWebViewClient(new WebViewClient()); }
public class webclient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
Where is the error here? please help me im also having problem with adview. here
You've created a custom WebViewClient class but you aren't using it.
Change web.setWebViewClient(new WebViewClient());
to web.setWebViewClient(new webclient());
Change these two methods.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
progressBar.dismiss();
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
Change web.setWebViewClient(new WebViewClient());
to web.setWebViewClient(new webclient());

How to hide page loading process inside webview while control is inside onpagestarted() in android?

i don't want to show page loading process inside webview while control is inside onpagestarted() android. what i want is, to show the the contents in web view when complete page is downloaded. the code i am using is`
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://iba.edu.pk/");
and `WebViewClient class is as follows
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
//web.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
Log.d("Main"," onPageStarted");
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.d("Main"," shouldOverrideUrlLoading");
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Log.d("Main"," on Page finsihed");
progressBar.setVisibility(View.GONE);
}
}

How To Display Custom "Web Page Not Available" Error Page in WebView

pls help me to display custom Web Page Not Available error page...in webview..i tried many code and when i run the app it shows app has been stopped
You can override onReceivedError callback of WebViewClient to achieve the desired functionality.
Either you can display some dialog to denot your custom message or load the webview with any custom html available locally in your app.
USE this it working fine on my side. May be it will help you
private WebView webviewSites;
String URL="http://www.google.com";
webviewSites = (WebView) findViewById(R.id.webview_sites);
WebSettings webSettings = webviewSites.getSettings();
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptEnabled(true);
webviewSites.setWebViewClient(new myWebClient());
webviewSites.loadUrl(URL);
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(final WebView view, final String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// mProgressBar.setVisibility(View.GONE);
Log.i("LOAD_URL", "" + url);
}

Scroll an HTML file right after it loaded

In my app I'm loading a local HTML file to a webview. If some button was clicked, I want to load a local HTML file and scroll it with specific value in y axis.
The problem is that the command:
webview.scrollTo(0, scrollY);
is executed too early, before the HTML file finish loaded, so the scrolling is not happen.
I tried to use a lot of answers from Stackoverflow (like this, and this) but the problem is always the same. I can see in debug mode that the scrollTo command executed before the file is visible.
Anyone have any idea how can I achieved this scrolling?
After trying all the answers I found that only the following really help:
private final Handler mHandler = new Handler();
// in onCreate
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (mWebView.getContentHeight() > 0) {
mWebView.scrollTo(0, mLastPosition);
Log.d("scrolling", "true");
mHandler.removeCallbacks(this);
} else {
mHandler.postDelayed(this, 100);
}
}
}, 100);
This solution was taken from hereenter link description here
WebViewClient will help you. Extend onPageFinished() as follows:
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webview.scrollTo(0, scrollY);
}
});
fisrt set custom webview client like this
webview.setWebViewClient(new CustomWebViewClient());
then add following piece of code in class
private class CustomWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
webview.scrollTo(0, scrollY);
}
}

Categories

Resources