How to get the width and height of a web browser in android

I want to determine the width and height WebView. I have already tried this using:

webView.getWidth();
webView.getHeight();

but the resulting log always shows that they are 0.

+5
source share
10 answers

Here is a more elegant solution.

public void onCreate(Bundle savedInstanceState) {


        webView = (WebView) findViewById(R.id.webView1);

        webView.addOnLayoutChangeListener(new OnLayoutChangeListener() {

            @Override
            public void onLayoutChange(View v, int left, int top, int right,int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                 int w= webView.getWidth();
                 int h= webView.getHeight();


            }
        });



    }   
+3
source

The onPageFinishedcallback approach does not work when using loadDataor loadDataWithBaseURL. I saw another Andromedev solution using JavaScript, but I'm not sure if this is the best way to make it.

+2
source

, . , -.

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

- WevView, , View # getWidth View # getHeight WebView :

/*
 * Return the width of the view where the content of WebView should render
 * to.
 */
private int getViewWidth() {
    if (!isVerticalScrollBarEnabled() || mOverlayVerticalScrollbar) {
        return getWidth();
    } else {
        return getWidth() - getVerticalScrollbarWidth();
    }
}

, , , , . setContentView(R.layout.mylayout);

+1

, , , onCreate. :

webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView webView, String url) {
                super.onPageFinished(webView, url);
                Log.i(TAG, findViewById(R.id.my_component).getWidth(););
            }
        });
+1

- .

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
            webView.getWidth();
            webView.getHeight();
}
+1

Height Width, , . webview Touchevent . click view .

webView.getWidth();
webView.getHeight();

.

0

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

, -.

webview , .

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;
               }
       });
0

You need the width and height of WebView CONTENTS after loading your HTML. YES, but there is no getContentWidth method (only the view port value), And getContentHeight () is inaccurate!

Answer: subclass of 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
//=========
0
source

All Articles