getHeight returns 0 for all Android UI objects
Asked Answered
X

6

71

I'm building a UI, and it's all static defined in the XML. All of it has weights all over the place, and while it looks right, I wanted to see that things actually have the right height and all. The problem is that no matter where I call .getHeight() for my format layout I got 0. I tried in both onCreate() and onStart(). Same thing. Happens for all UI objects too. Any idea?

package com.app.conekta;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class Conekta extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    @Override
    public void onStart() {

        super.onStart();


    }

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

        FrameLayout fl1 = (FrameLayout) findViewById(R.id.headerFrameLayout);
        FrameLayout fl2 = (FrameLayout) findViewById(R.id.footerFrameLayout);
        Button b=(Button) findViewById(R.id.searchButton);

        Log.d("CONEKTA", String.valueOf(b.getHeight()));

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <FrameLayout
        android:id="@+id/headerFrameLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.05"
        android:background="#597eAA" >

        <ImageView
            android:id="@+id/logoImage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#7ba1d1"
            android:src="@drawable/logo_conekta" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/bodyLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:background="#f3f3f3"
        android:orientation="horizontal" >

        <FrameLayout
            android:id="@+id/leftBlankFrameLayout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="0.15"
            android:background="#f3f3f3" >
        </FrameLayout>

        <LinearLayout
            android:id="@+id/centerVerticalLayout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="0.7"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@+id/topCenterFrameLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.35" >
            </FrameLayout>

            <TextView
                android:id="@+id/venueLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.025"
                android:text="What are you looking for?"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000" />

            <EditText
                android:id="@+id/venueTextField"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.025" >

                <requestFocus />
            </EditText>

            <FrameLayout
                android:id="@+id/middleCenterFrameLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.05" >
            </FrameLayout>

            <TextView
                android:id="@+id/locationLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.025"
                android:text="Where?"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000" />

            <AutoCompleteTextView
                android:id="@+id/locationTextField"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.025"
                android:text="" />

            <LinearLayout
                android:id="@+id/buttonLinearLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.05"
                android:background="#f3f3f3"
                android:orientation="horizontal" >

                <FrameLayout
                    android:id="@+id/leftButtonLinearLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.1" >
                </FrameLayout>

                <Button
                    android:id="@+id/searchButton"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.8"
                    android:background="#6fa8dc"
                    android:text="Search" />

                <FrameLayout
                    android:id="@+id/rightButtonLinearLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.1" >
                </FrameLayout>
            </LinearLayout>

            <FrameLayout
                android:id="@+id/bottomCenterFrameLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.35" >
            </FrameLayout>
        </LinearLayout>

        <FrameLayout
            android:id="@+id/rightBlankFrameLayout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="0.15"
            android:background="#f3f3f3" >
        </FrameLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/footerFrameLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.15"
        android:background="#7ba1d1" >
    </FrameLayout>

</LinearLayout>
Xmas answered 17/11, 2011 at 16:42 Comment(1)
Possible duplicate of getWidth() and getHeight() of View returns 0Mosley
U
42

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?

Unconstitutional answered 17/11, 2011 at 16:48 Comment(0)
Z
90

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.

Zymometer answered 17/11, 2011 at 16:48 Comment(5)
In which Activity lifecyle method should this be implemented? I am still getting 0 for tv.getHeight()Handicapped
@IgorGanapolsky, this method worked for my fragment in onCreateView. For a normal activity, I expect onCreate would be appropriate.Kerri
removeGlobalOnLayoutListener is deprecated nowadays, you should use removeOnGlobalLayoutListener instead.Biotite
Better check observer.isAlive() before calling removeGlobalOnLayoutListener or see #26193186Virtuous
This is working, but this will not serve my purpose, if I want to create multiple views programmitically at the same time knowing other's dimension (although I am giving the dimension).Hochman
U
42

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?

Unconstitutional answered 17/11, 2011 at 16:48 Comment(0)
A
20

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();
}
Agora answered 30/8, 2017 at 12:50 Comment(2)
the best answerSophi
it didnt answer for meSketchbook
A
6

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.

Abduce answered 18/7, 2018 at 8:2 Comment(3)
Thank you for the detailed answer. But is it necessary to remove the OnGlobalLayoutListener? for example, if I need to do something every time the size of the view changedIridescent
No it is not necessary, only if you want to do a task every time a view size is changed. But make sure to remove listener once you are done and you no longer need to listen to it.Abduce
By the way, I remove if (viewTreeObserver.isAlive()) it still works as expected.Roehm
S
1

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.

Stethoscope answered 23/6, 2012 at 12:44 Comment(0)
I
0

I've faced this problem, and given that nobody mentioned this solution, I feel it is useful to share.

You can @Override the onSizeChange method, which I guess happens at the inflation process.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    getDimensions();
    
}

where getDimensions() is a method of your custom View, in which you call getHeight and getWidth and assign to your class fields and re-setup as needed.

Indigenous answered 21/3, 2021 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.