1

I have a FrameLayout, containing various overlayed ImageViews:

<FrameLayout
    android:id="@+id/australia_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/australia"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/map_australia"
        />
    <ImageView
        android:id="@+id/nsw"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/map_nsw"
        android:visibility="invisible"
        />
    <ImageView
        android:id="@+id/victoria"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/map_victoria"
        android:visibility="invisible"
        />
        ...etc...
</FrameLayout>

In my code I try and render my FrameLayout:

final Dialog dialog = new Dialog((Context) parent);
dialog.setContentView(R.layout.dialog_region);

FrameLayout fl = (FrameLayout) dialog.findViewById(R.id.australia_frame);
final int height = fl.getHeight();
Validate.isTrue(height > 0);

My code crashes with:

java.lang.IllegalArgumentException: The validated expression is false
    at org.apache.commons.lang3.Validate.isTrue(Validate.java:180)

The line that's crashing is Validate.isTrue(height > 0);.

Why does my FrameLayout not have a height? Is there a way to get the exact px height and width that my FrameLayout is rendering at?

George
  • 2,110
  • 5
  • 26
  • 57

1 Answers1

5

This may help you

     FrameLayout target = (FrameLayout) dialog.findViewById(R.id.australia_frame);
    target.post(new Runnable() {

        @Override
        public void run() {             
            int width = target.getWidth();
                            int height = width/2;
            /*your code with width and height*/
        }

    });
}   
Sree
  • 3,136
  • 2
  • 31
  • 39
  • 7
    For those curious why this works, when `onCreate` is executed in your Activity, the UI has not been drawn to the screen yet, so nothing has dimensions yet since they haven't been laid out on the screen. When `setContentView` is called, a message is posted to the UI thread to draw the UI for your layout, but will happen in the future after `onCreate` finishes executing. Posting a `Runnable` to the UI thread will put the `Runnable` at the end of the message queue for the UI thread, so will be executed _after_ the screen has been drawn, thus everything has dimensions. – Jason Robinson Jan 25 '13 at 07:05