0

I need some help to get my custom webView CONTENT height, please check below is my code

    webView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final int displayHeight = webView.getHeight();
            final int contentRange = webView.getContentHeight();
            final int y = v.getScrollY();


            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    Log.d("Get webView height ", "" + displayHeight); // Result : 528
                    Log.d("Get webView content height ", "" + contentRange); // Result : 2112
                    Log.d("Get webView content scroll top ", "" + y);

                    return false;

                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_MOVE:

            }

            return false;
        };

Above is my code to get my custom webView content height with webview.setOnTouchListener, that code is work.

But, now I want to get my custom WebView content height from button click, that does not work, what I got is always get a height of my webView not the content height, below my code for button click

Button btn = (Button) this.getActivity().findViewById(R.id.btnCLick);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int webViewContentHeight = webView.getContentHeight();

            Log.d("Get my custom webView content height", " " + webViewContentHeight); // Result : 528

        }
    });

Much appreciate if I can have a detailed procedure, thanks.

ilovebali
  • 513
  • 2
  • 7
  • 20
  • http://stackoverflow.com/questions/22878069/android-get-height-of-webview-content-once-rendered – nixn Aug 12 '14 at 06:53
  • @user3698577 thanks for the comment, the code still give me result of webView height not content webView height :( – ilovebali Aug 12 '14 at 07:02
  • Are you sure that you fill your WebView before .getContentHeight? If there is no content, ContentHeight = WebView.Height – nixn Aug 12 '14 at 07:18
  • Hi @user3698577 hmmm I put my both code in `public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ...`, is that make a problem??? thank a lot :) – ilovebali Aug 12 '14 at 07:31

3 Answers3

1

The best way I found to get contentHeight after rendering :

Create your own class extended WebView and this class in your layout :

public class BKWebView extends WebView {

private Activity yourActivity;

public BKWebView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public BKWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public BKWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

@Override
public void invalidate() {
    super.invalidate();

    if (getContentHeight() > 0) {
        // WebView has displayed some content and is scrollable.

        if (yourActivity != null)
            yourActivity.callBackWebView(getContentHeight());

    }
}

/**
 * @param yourActivity the yourActivity to set
 */
public void setYourActivity(Activity yourActivity) {
    this.yourActivity = yourActivity;
}   

}

in your activity implement the callback method and then in your listener :

private webViewContentHeight;
BKWebView wv;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    wv = (BKWebView) findViewById(R.id.yourwebview);
    wv.setYourActivity(this);
    ...
}

public void callBackWebView(int contentHeight) {
    webViewContentHeight = contentHeight;
}


...
Button btn = (Button) this.getActivity().findViewById(R.id.btnCLick);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("Get my custom webView content height", " " + webViewContentHeight); 
    }
});
...
gduh
  • 1,079
  • 12
  • 30
  • Hi thanks for the code, but I'm confuse what fjd is?? thanks a lot for your help – ilovebali Aug 12 '14 at 09:31
  • Hi sorry I just back to my desk, yesterday was very busy, again thanks for your code, still I'm not understand what fjd is?? I use your code and android said can not resolve your symbol fjd on `if (fjd != null) fjd.callBackWebView(getContentHeight()); }`, thanks :) – ilovebali Aug 13 '14 at 03:00
  • I'm sorry, in BkWebView class I rename private var yourActivity, but I forgot to rename it inside the code. Code is now updated. Hope this help you. – gduh Aug 13 '14 at 08:03
0

Sorry for misunderstanding your question before. You can set the View and the Button global in the activity and instantiate it at onCreate. Make a separate onClickEvent for the button, not in OnCreate, where you call the global WebView.getContentHeight() this might work.

And in first case you could get things working right now with sending a click-event to the WebView. More dirty solution.

WebView.performClick();
nixn
  • 1,337
  • 3
  • 16
  • 33
  • Hmm, if the content never set, why when I used `webView.setOnTouchListener` to get my webView content height, it's work fine, only when I use btn.setOnClickListener it doesn't work, any suggest ??thank for your help..:) – ilovebali Aug 12 '14 at 07:51
0

you can use javascript

String js = "javascript:function initialize() { "
            + "var d = document.getElementsByTagName('body')[0];"
            + "var visibleH = window.innerHeight; " 
            + "var fullH = d.offsetHeight; "
            + "jsInterface.jsGetContentHeight(visibleH ,fullH); " 
            + "}";  
wv.loadUrl(js);
wv.loadUrl("javascript:initialize()");  

and then define JavaScriptInterface for webview to retrieve values

wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(new JavaScriptInterface(context),
            "jsInterface"); 
public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void jsGetContentHeight(int visibleH,int fullH) {
        Toast.makeText(mContext, visibleH +"-"+ fullH, Toast.LENGTH_LONG).show();
    }
} 
MHP
  • 2,613
  • 1
  • 16
  • 26