5

i am trying to get the height of a view and it keeps returning 0.

this is what i have tried:

View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null);
hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight();

i also tried hourViewHeight = hourView.getHeight(); as well but no joy.

i call those two lines inside a initialize() method located here:

@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();
    }

here is my xml layout:

<?xml version="1.0" encoding="utf-8"?>

<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">

    <TextView android:text="12am" android:id="@+id/hourTextView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</RelativeLayout>

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

edit: i have also tried doing this below:

@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);
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jono
  • 17,341
  • 48
  • 135
  • 217

3 Answers3

7

try

hourView.measure(hourView.getWidth(), hourView.getHeight());
hourView.getMeasuredHeight()
Rubycon
  • 18,156
  • 10
  • 49
  • 70
  • I don't think the `.measure` method is necessary if this is in the GlobalLayoutListener, but I believe this is spot on as far as using getMeasuredHeight() and getMeasuredWidth() instead of the standard getHeight() and getWidth(). – Kevin Coppock Jan 17 '11 at 14:09
1

In the first example it didn't worked because when you are querying them, the view still haven't performed the layout and measure steps. You only told the view how it would "behave" in the layout, but it still didn't calculated where to put each view.

(Similar question: How to get the width and height of an android.widget.ImageView?)

In the second example you posted, I don't think you added the view to the activity so the activity won't draw it hence, you will never read that log message. call setContentView or addView to some layout.

Community
  • 1
  • 1
Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
-2

You have provided the height as wrap_content and you don't fill that content. Because of that it will display as 0.

sth
  • 222,467
  • 53
  • 283
  • 367
chikka.anddev
  • 9,569
  • 7
  • 38
  • 46
  • well i tried specifying a height on something like 100dip and it still returns 0 – Jono Jan 17 '11 at 12:00
  • you are specifying height of linearlayout? – chikka.anddev Jan 17 '11 at 12:14
  • im using relativelayout as shown in the xml code above. i specified the height myself using android:layout_height="50dip" and still doesnt work. i have also tried it using a OnGlobalLayoutListener pasted in the onStart and even that diddnt work. why is it so much hard work just to return the view height ! – Jono Jan 17 '11 at 12:17
  • It's wrap_content, with a child TextView that contains text, set to wrap_content. It *has* a size once it's been measured. – Kevin Coppock Jan 17 '11 at 14:07