Is there any way to get Android WebView content width after the webpage loaded with vertical scroll using java?
In case you have the option to extend the used WebView you can also try the output of the protected Method computeHorizontalScrollRange().
Update: I forgot to mention that this only works properly if the content is bigger than the webview.
class YourCustomWebView extends WebView {
public YourCustomWebView(Context context) {
super(context);
}
public int getContentWidth()
{
return this.computeHorizontalScrollRange();
}
}
public void onPageFinished(WebView page, String url) {
Log.v(LOG_TAG,"ContentWidth: " + ((YourCustomWebView) page).getContentWidth());
}
You need the width and height of the WebView CONTENTS, after you loaded your HTML. YES you do, but there is no getContentWidth method (only a view port value), AND the getContentHeight() is inaccurate !
Answer: sub-class WebView:
/*
Jon Goodwin
*/
package com.example.html2pdf;//your package
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
class CustomWebView extends WebView
{
public int rawContentWidth = 0; //unneeded
public int rawContentHeight = 0; //unneeded
Context mContext = null; //unneeded
public CustomWebView(Context context) //unused constructor
{
super(context);
mContext = this.getContext();
}
public CustomWebView(Context context, AttributeSet attrs) //inflate constructor
{
super(context,attrs);
mContext = context;
}
public int getContentWidth()
{
int ret = super.computeHorizontalScrollRange();//working after load of page
rawContentWidth = ret;
return ret;
}
public int getContentHeight()
{
int ret = super.computeVerticalScrollRange(); //working after load of page
rawContentHeight = ret;
return ret;
}
//=========
}//class
//=========
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With