Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting WebView Content height once its loaded Android

I'm trying to apply expand collapse feature on Webview Based on its Content height but i'm always getting wrong value Here is my code

public class MesuredHeightWebView extends WebView {
    public MesuredHeightWebView(Context context) {
        super(context);
    }

    public MesuredHeightWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MesuredHeightWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



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

        if (getContentHeight() > 0) {
            // WebView has displayed some content and is scrollable.
            if(listener!=null)
                listener.updateContentHeight(getContentHeight());
        }
    }



    WebViewContentHeight listener;

    public void setChangeContentListener(WebViewContentHeight listener) {
        this.listener = listener;
    }
}

and then in the fragment i tried to get the content height

 webView.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

            super.onProgressChanged(view, newProgress);
            if(newProgress==100)
            {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        webView.setChangeContentListener(new WebViewContentHeight() {
                            @Override
                            public void updateContentHeight(int height) {

                                if(height>0 && getActivity()!=null) {

                                    System.out.println("the height2 is" + height + " " + Utils.convertPixelsToDp(height, getActivity()));
                                    final int text_height = height;

but my problem is that i'm always getting wrong result Thanks

like image 922
Antwan Avatar asked Mar 29 '16 10:03

Antwan


1 Answers

Use the following methods instead of getContentHeight() method:

computeHorizontalScrollRange(); -> for width 
computeVerticalScrollRange(); -> for height

these two methods will return the entire scrollable width/height rather than the actual widht/height of the webview on screen

like image 86
eyadMhanna Avatar answered Nov 09 '22 06:11

eyadMhanna