android: get viewHeight programatically
<p>i am trying to get the height of a view and it keeps returning 0.</p>

<p>this is what i have tried:</p>

<pre><code>View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null);
hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight();
</code></pre>

<p>i also tried <code>hourViewHeight = hourView.getHeight();</code> as well but no joy.</p>

<p>i call those two lines inside a initialize() method located here:</p>

<pre><code>@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_day_screen);

        Intent intent = getIntent();

        mDate = new DateTime(intent.getStringExtra("date"));
        circleIcon = (ImageView) findViewById(R.id.circle);

        mHoursLayout = (RelativeLayout) findViewById(R.id.dayLayout);

        TopBarUtil topBar = new TopBarUtil(getApplicationContext());

        circleIcon.setOnClickListener(topBar.onActionClickListener());

        mContext = getApplicationContext();



        initialize();
    }
</code></pre>

<p>here is my xml layout:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/mainLayout" android:background="@layout/border"&gt;

    &lt;TextView android:text="12am" android:id="@+id/hourTextView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/TextView&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>i basicllay want to get the height of the above xml layout but it always returns 0.</p>

<p>edit: i have also tried doing this below:</p>

<pre><code>@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

         final View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null);
        ViewTreeObserver observer = hourView.getViewTreeObserver();

        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                hourView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                hourViewHeight = hourView.getHeight();
                Log.d(TAG, "hourViewHeight = " + hourViewHeight);
            }
        });

//      hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight();
//      Log.d(TAG, "hourViewHeight = " + hourViewHeight);
    }
</code></pre>
