Linearlayout Height in Oncreate Is 0

LinearLayout's width and height are zero

it is returning zero because the size of your layout has not been calculated. To overcame this situation you can post a Runnable on the mainview queue this way:

  mainView.post(new Runnable() {
@Override
public void run() {
view = new GameLayout(this, container.getWidth(), container.getHeight());
} );

the runnable is going to be executed as last element of the mainview`s queue. At that time, the size of container should be already calculated.

Edit. container has to be marked as final to be accessed in the inner runnable

Why height of linearlayout is zero

Refer to this question

Can't get height of Widgets

the wrong height of linearlayout after oncreate() method, why?

Layout has not completed by the end of onCreate. You can register a layout listener to be notified when layout is completed and the (correct) height is available. For example:

layout_course_summary.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = layout_course_summary.getHeight();

...

layout_course_summary.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});

View's getWidth() and getHeight() returns 0

You are calling getWidth() too early. The UI has not been sized and laid out on the screen yet.

I doubt you want to be doing what you are doing, anyway -- widgets being animated do not change their clickable areas, and so the button will still respond to clicks in the original orientation regardless of how it has rotated.

That being said, you can use a dimension resource to define the button size, then reference that dimension resource from your layout file and your source code, to avoid this problem.

getMeasuredHeight() and width returning 0 after measure()

Add the below method to your Activity and call your method from it.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
setMeasuredDimensions();
super.onWindowFocusChanged(hasFocus);
}

In onCreate() you can't assure that your view is drawn. It might take some time. But this above method gets called only when the view is drawn completely. So YOu should be able to get the width and height exactly here.

getHeight returns 0 for all Android UI objects

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?

getWidth() returns 0 if set by android:layout_width=match_parent

A view's size isn't available until after onMeasure(), particularly if its set to wrap_content, or fill_parent.

You should either access the size in or after onMeasure() in the View code, or in a a Layout Tree Observer:

LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();

}
});

You will also need to add android:id="@+id/mylayout" to your LinearLayout for the second one to work.



Related Topics



Leave a reply



Submit