WebView multiple pre-cache with progressBar Android - android

is it possible to load multiple WebPages(including html,images,JS,css ) to WebView cache with real ProgressBar ? I know it is possible to load one page in this way :
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
URL = "https://stackoverflow.com"
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setAppCachePath(appCachePath);
if (isNetworkAvailable()) {
appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
Log.e(TAG,"Load page from cache");
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
}
and it will make possible offline loading of that page, but is it any way to create custom WebView where I will have chance to load multiple URL in array or list, like
List<String> urls = new ArrayList<>();
urls.add("https://stackoverflow.com");
urls.add("https://google.com");
urls.add("https://facebook.com");
webView.loadURL(ursl);
And also I need a total progressBar for loading of those WebPages. I know I can set WebChromeClient for webView like:
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
Log.d(TAG,"onProgressChanged" + progress);
}
}
but it will show progress of loading only one WebPage, when I need total progress of few pages.
I do not want to write my own WebPage grabber, and store it in internal Storage, because it takes a lot of time and probably will cause a lot of different bugs and mistakes.
Please, can you give me some ideas ?

is it possible to load multiple WebPages(including html,images,JS,css ) to WebView cache with real ProgressBar ?
Not Possible .
Visit Android Doc for reference .

Related

loading only a specific element properly in webview

i am trying to load only a specific element in my webview webpage ('contentbody').
but i dont know how to modify my code to load that only. i can block elements in my webview by using this simple javascript.
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
// Removes element which id = 'mastHead'
view.loadUrl("javascript:(function() { "
+ "(elem = document.getElementById('smsform')).parentNode.removeChild(elem); "
+ "})()");
}
An option might be to use Jsoup, to load the HTML page from the website, extract the desired portion of the HTML, and then load just that into your WebView. A small example of how it could be done:
Document doc = Jsoup.connect("http://example.com/").get();
myWebView.loadDataWithBaseURL("http://example.com/", doc.select(".contentbody").first().outerHtml(), "text/html", "UTF-8", null);
With this, you use Jsoup to extract the first item with contentbody class from the HTML, and then directly load it into your WebView.

Scaling an Android WebView text html to fit?

I have a math equation as a text WebView that I need to fit the entire screen no matter its dimensions.. my first intuition of doing this is simply shrinking the text whenever it gets out of bounds so that it remains constrained, however, I can't find any functions or methods to actually MEASURE the content of that webview, I've tried:
webview.getMeasuredHeight, webview.getHeight
but the issue with them is that they are constantly affixed on the size of the webview widget, not on the content, so I moved to:
webview.getContentHeight
which seemed to work, the problem is it works only after the text is "loaded", so it doesn't get the right answer at first, even if it's called in onPageFinished.
My questions are:
1) Is there a way to know the content size of the TEXTUAL html webview?? I would even appreciate knowing the scroll bar length that would indicate size.
2) Is there a listener function that would enable me to run code from the moment the text actually loaded? The webview declaration looks something like this:
webView = (WebView) findViewById(R.id.webView);
js = "<html><head>"
+ "<link rel='stylesheet' href='file:///android_asset/mathscribe/jqmath-0.4.3.css'>"
+ "<script src='file:///android_asset/mathscribe/jquery-1.4.3.min.js'></script>"
+ "<script src='file:///android_asset/mathscribe/jqmath-etc-0.4.3.min.js'></script>"
+ "</head><body>"
+ "<script>var s = '$$" + functext + "$$';M.parseMath(s);document.write(s);</script> </body>";
webView.loadDataWithBaseURL("", js, "text/html", "UTF-8", "");
The text does not load instantly, so the code relating to it is usually flawed.
3) Is there a webview.getContentWidth or something of the like?
EDIT:
This code may help you in fitting your wepage to webview.
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
You have to calculate the scale so that content fits on the screen.
private int getScale()
{
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
Double val = new Double(width)/new Double(Your_webpage_width);
val = val * 100d;
return val.intValue();
}
Then use
WebView web = new WebView(this);
web.setPadding(0, 0, 0, 0);
web.setInitialScale(getScale());
2) To run something after webview has completely loaded,just implement WebViewClient and extend onPageFinished() as follows:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});

WebView is not using Cookies for loading images

I have troubles when I want my WebView to load images that requires Cookies.
I have set my cookies on the 'CookieManager'
final android.webkit.CookieManager instance = android.webkit.CookieManager.getInstance();
instance.setAcceptCookie(true);
instance.setCookie(".example.fr", mCookies, new ValueCallback<Boolean>() {
#Override
public void onReceiveValue(final Boolean value) {
loadWebView();
}
});
The WebView is then loaded with a custom HTML string because the app is generating the proper HTML.
private void loadWebView() {
// this string is an example of a generated HTML
String htmlContent =
"<!DOCTYPE html>" +
"<html><head>" +
"<link rel=\"stylesheet\" href=\"css/style.css\" type=\"text/css\" media=\"screen, projection\"/></head>" +
"<body><img src=\"www.example.fr/img.jpg\"/></body></html>";
mWebView.loadDataWithBaseUrl("file:///android_asset/", htmlContent, "text/html", "UTF-8", null);
}
I tried to proxy the network calls with Charles Proxy and I noticed that the request to www.example.fr/img.jpg had no cookie set in the headers. But, when I inspect the WebView using Chrome debbuging, I can see that the Cookies are properly under the Resources tab.
It seems that they are not used for the image downloading.
Any hints or advices to make the WebView using Cookies for resource downloading ?
Thank you.
I've faced with same problem, it is related with following changes:
https://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView
to fix it you need set:
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView,true);

Webview InputText not supported in 2.1 and 2.2

In Android we have an url
"http://offers.bartizan.com/we-want-ileads-at-our-shows"
which contains lot of data.
We load this url in webview, In android 2.3 and onwards its working fine means diplay all content of url data but in android 2.1 and 2.2 not display Input Text form.
Here is my code which i used to load url in webview.
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
webView.enablePlatformNotifications();
webView.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_DEFAULT);
webView.loadUrl("http://offers.bartizan.com/we-want-ileads-at-our-shows");
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//if(url.contains("http://www.youtube.com/embed/")){
Log.d("WBC", "This is a YouTube link: " + url);
Log.e("url---Video->", url);
view.loadUrl(url);
return true;
} });
class MyJavaScriptInterface {
public void showHTML(String html) {
String htmlString = html;
Log.e("<---MyJavaScriptInterface---showHTML----->", htmlString);
}
}
Below form not display in android OS. 2.1 and 2.2
First name *
Last name
Email address *
List the names of your shows here. Please include the show manager name, email and phone. *
1) Showing blank area above image
2) Textbox we require

Jsoup select() return nothing in Android application

I'm making an Android app for my board community. The board provider gives me RSS feeds from general categories but don't generate feeds from topics. So I retreive topics URLs from these feeds and want to parse HTML with Jsoup and give it to a WebView.
It works nice except with the select() function which returns nothing.
The "HTML RETREIVED" log gives me : <html><head><title>The topic title</title></head><body></body></html>
h1 tags are in the code on test purpose : it displays well on WebView and the title of the parsed webpage too.
I also putted the log line right after the select() line. It returns nothing too.
I've tried in a pure Java project to parse with Jsoup only and it goes well.
So I assumed something's wrong with Android.
PS : Internet permission is active in the manifest.
Did I miss something ?
Here is the code :
String html;
Bundle param = this.getIntent().getExtras();
String url = param.getString("url");
try {
Document doc = Jsoup.connect(url).get();
doc.select(".topic .clear").remove();
String title = doc.title().toString();
html = doc.select(".username strong, .entry-content").toString();
html = "<html><head><title>"+title+"</title></head><body><h1>"+title+"</h1>"+html+"</body></html>";
WebView webview = new WebView(this);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
Log.d("LOADING",""+ progress);
}
});
webview.loadData(html, "text/html", "UTF-8");
//webview.loadUrl(url);
Log.i("HTML RETREIVED", ""+html);
} catch (IOException e) {
Log.e("ERROR", "Error while generate topic");
}
Ok I've found out something interesting.
The class I wanted to select was not here because I'm getting the mobile version of the webpage. It appears Android App use a mobile user-agent, which is quite normal but not said anywhere.
Anyway I know what thinking about now.

Categories

Resources