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);
}
}
Related
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);
}
}
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);
}
}
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);
}
I have used progress bar to indicate page is loading in android webview. But the Progress Bar is showing for ever even after the page has been loaded completely. I used logcat for debugging which shows onPageFinished function is called.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
_dialog =ProgressDialog.show(UmlScreen.this, "", "Please wait...");
super.onPageStarted(view, url, favicon);
_dialog.setCanceledOnTouchOutside(true);
Log.d("LOGCAT", "started");
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if (_dialog != null || _dialog.isShowing()) {
_dialog.dismiss();
Log.d("LOGCAT", "finished");
}
}
Try initialization of _dialog outside WebViewClient such as in OnCreateView method
Because the page is not finished. May be the page have a lot of images so it is taking time to download them.
you can check it using break point. debug you code and put break point at super.onPageFinished(view, url);
I can give you some dummy codes.
webview.load(url);
dialog.show();
public void onPageFinished(WebView view, String url) {
//....
dialog.dismiss();
}
#Override
public void onPageFinished(WebView view, String url) {
try{
if (prDialog.isShowing()|| prDialog!= null) {
prDialog.dismiss();
prDialog= null; /*** Add ***/
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
Or visit...
https://stackoverflow.com/a/31981161/3857542
I want to be able to scroll the webview until it has reached the end of the Webview. But the code below continues to scroll down automatically even when the webview page has reached to it's end. I want to make sure that each page scrolls down until the end of the webview.
webview.loadUrl("file:///android_asset/"+position+".html");
webview.getSettings().setBuiltInZoomControls(true);
webview.setPictureListener(new PictureListener() {
public void onNewPicture(WebView view, Picture picture) {
int webBot = webview.getBottom();
webview.scrollBy(0, 1);
}
});
Try this, (Basically using onPageFinished() instead of onNewPicture())
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Log.i(TAG,"onPageFinished invoked!!");
wv.scrollBy(0, wv.getBottom());
}
});