Reputation: 543
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.
Upvotes: 0
Views: 449
Reputation: 1478
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();
Upvotes: 0
Reputation: 1172
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);
}
});
...
Upvotes: 1
Reputation: 2731
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();
}
}
Upvotes: 0