getHeight returns 0 for all Android UI objects

百般思念 提交于 2019-11-26 11:50:00
DeeV

In short, the views are not built yet in onCreate(), onStart(), or onResume(). Since they technically don't exist (as far as the ViewGroup is concerned), their dimensions are 0.

In long, you can go here for a better explanation on how to handle it.

How to retrieve the dimensions of a view?

It's 0 because in both onCreate and onStart, the view hasn't actually been drawn yet. You can get around this by listening for when the view is actually drawn:

final TextView tv = (TextView)findViewById(R.id.venueLabel);
final ViewTreeObserver observer= tv.getViewTreeObserver();
       observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
              tv.getHeight()
              observer.removeGlobalOnLayoutListener(this);
            }
        });

The call to remove the listener is there to prevent repeated invocations of your custom handler on layout changes... if you want to get those, you can omit it.

Use this function to get Height or Width of View

private int getHeightOfView(View contentview) {
    contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    //contentview.getMeasuredWidth();
    return contentview.getMeasuredHeight();
}

I am answering this again to make sure people like me will understand how it works completely before implementing.

Q. Why do I get my view's height as 0?
A. Because the view's height that you are trying to get is not yet built in onCreate(), onStart(), or onResume().

Q. Now where and how am I supposed to get the view's height?
A. You can still get the view's params wherever you want it like, onCreate(), onStart(), or onResume(). Like below:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       TextView tv = (TextView)findViewById(R.id.textview1);

       ViewTreeObserver viewTreeObserver = tv.getViewTreeObserver();
       if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                viewHeight = tv.getHeight();

            }
        });
      }
   }

However this time you will wait until you get the view's height before removing observer.

tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);

Q. So what should I check before removing this listener?
**A.**Just put a if condition before removing treeObserver like,

 if (viewHeight != 0)                     
 tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);

Final code:

ViewTreeObserver viewTreeObserver = tv.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int viewHeight = tv.getHeight();
                    if (viewHeight != 0)
                        tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                }
            });
        }

Hope this explains why the getHeight returns 0 and how to get it properly. Thanks.

Abhilab Das

By getting your button in a View you can easily fetch its height etc. You can define an onClickListener like so:

Button b=(Button) findViewById(R.id.searchButton);
b.setOnClickListener(ButtonClick);
android.view.View.OnClickListener ButtonClick= new android.view.View.OnClickListener() {
    public void onClick(View v) { 
 v.getHeight();
}};

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!