I want to determine the width and the height of the WebView
. I have already tried it using:
webView.getWidth();
webView.getHeight();
If you want to get the original height after loading content, you can use the below code. Though the code is deprecated, yet it works perfectly.
webview.setPictureListener(new WebView.PictureListener() {
@Override
public void onNewPicture(WebView webView, @Nullable Picture picture) {
int height = webview.getContentHeight();
int height1 = webview.getMeasuredHeight();
int height2 = webview.getHeight();
}
});
I had the same problem. I just put initWebView method in onWindowFocusChangedt.
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
initWebView();
}
private void initWebView(){
if(isOnline()){
venueWebView = (WebView) findViewById(R.id.venueWebView);
String url = "some url";
int viewWight = venueWebView.getWidth();
int viewHeight = venueWebView.getHeight();
venueWebView.loadUrl(url);
}
you are checking size of webview too early. you can try this in following method
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
webView.getWidth();
webView.getHeight();
}
I used a work-around , instead of fetching from unreliable webview get from DisplayMetrics, assuming you have webview width almost equal to phone screen size and height almost equal to height of phone screensize.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = (int) displayMetrics.heightPixels*0.9;
int width = displayMetrics.widthPixels;
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
//=========
I hope my answer helps somebody.
If you want to get height of webview
after content is loaded then this could be helpful.
ViewTreeObserver viewTreeObserver = mWebView.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
int height = mWebView.getMeasuredHeight();
if( height != 0 ){
Toast.makeText(getActivity(),"height:"+height,Toast.LENGTH_SHORT).show();
mWebView.getViewTreeObserver().removeOnPreDrawListener(this);
}
return false;
}
});