ilovebali ilovebali - 1 month ago 7
Android Question

Need help get content view height - Android

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.

Answer

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); 
    }
});
...
Comments