tyczj
tyczj

Reputation: 74066

ImageView height is always 0

I am trying to get the height and width of my imageview so I can scale my image down the the appropriate size but everytime I try to get the height I get the height as 0. I thought maybe it just was not measured yet so I put a onPreDrawListener on the imageview so that I know it would be measured at that time but I still get 0 so I am not sure the problem.

this is my full layout

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/content"
        android:layout_above="@+id/comment_layout">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView"/>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/comments"
            android:background="@android:color/transparent"/>

    </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/comment_layout">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText"
                android:layout_alignParentBottom="true"
                android:layout_toLeftOf="@+id/send_button"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/send_button"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"/>

        </RelativeLayout>

</RelativeLayout>

here is where I try to get the height

ViewTreeObserver obs = imageView.getViewTreeObserver();
        if(obs != null){
            obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    if(image != null){
                        imageView.getViewTreeObserver().removeOnPreDrawListener(this);
                        int height = imageView.getHeight(); // 0
                        int width = imageView.getWidth(); // 720
                    }
                    return false;
                }
            });
        }

the width displays 720 but the height always displays 0.

putting in a hard width also has no effect

Upvotes: 0

Views: 1815

Answers (1)

0xDEADC0DE
0xDEADC0DE

Reputation: 2473

This should work with an OnGlobalLayoutListener like so

    final ImageView image = new ImageView(this);
    image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // TODO Auto-generated method stub
            int height = image.getHeight();
        }
    });

This callback is invoked when the global layout state or the visibility changes

Upvotes: 2

Related Questions