0

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?

daedsidog
  • 1,732
  • 2
  • 17
  • 36

1 Answers1

0

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
    }
});
Vishavjeet Singh
  • 1,385
  • 11
  • 13
  • theres just ONE problem: I can't know the webpage's width as it expands and retracts depending on the mathematical equation, same goes for height... – daedsidog Nov 05 '15 at 18:29
  • You can try actual width of your webpage – Vishavjeet Singh Nov 05 '15 at 18:37
  • allow me to clarify: the width will constantly change based on the equation, and none of the functions I've listed above such as getMeasuredWidth will measure it, as it measures the actual webview widget. I don't KNOW my width and I would like to know how to measure it... also, I've saidthat onPageFinished does not seem to be called after my "webpage" loaded, as it's still blank when it gets called for some reason – daedsidog Nov 05 '15 at 18:41
  • i have updated my answer for fitting the webpage into webview please check – Vishavjeet Singh Nov 05 '15 at 18:47
  • did you passed the parameters correctly in onPageFinished? – Vishavjeet Singh Nov 05 '15 at 18:48
  • yes. webview.getContentHeight (under onPageFinished) is always zero when the page is first loaded since it isn't "actually" displaying the equation. – daedsidog Nov 05 '15 at 18:56
  • how much time it actually takes to load webview after onPageFinished call ? – Vishavjeet Singh Nov 05 '15 at 18:58
  • something like a second – daedsidog Nov 05 '15 at 19:00
  • Are pictures taking time to load because From the documentation : When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture). – Vishavjeet Singh Nov 05 '15 at 19:06
  • the page is simply nothing but text without any pictures. I have no idea why there is a delay, but if you would tell me how can I get the width of my constantly changing page it would probably solve my problem – daedsidog Nov 05 '15 at 19:09
  • have you tried this: `webview.getSettings().setLoadWithOverviewMode(true); webview.getSettings().setUseWideViewPort(true);` – Vishavjeet Singh Nov 05 '15 at 19:11
  • i don't know how to how to get the width..!! you can try a random value in my given method like 600 or 700 – Vishavjeet Singh Nov 05 '15 at 19:12