Reputation: 11645
I try to get the size of a linearlayout. I get always iactualHeight=0 in the following code:
li=(LinearLayout)findViewById(R.id.textviewerbuttonlayout);
li.requestLayout();
int iactualHeight=li.getLayoutParams().height;
My Layout is defined as follow:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:id="@+id/textviewerlayout"
android:orientation="vertical">
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="22" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textviewerbuttonlayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#FFFFFF"
android:orientation="horizontal" >
.... BUTTONS .....
</LinearLayout>
</LinearLayout>
Somebody any idea ?
Upvotes: 0
Views: 100
Reputation: 30557
The problem is you are requesting the height too early. Take a look at how android draws views.
The easiest way to get the guaranteed height is to use addOnLayoutChangedListener:
View myView = findViewById(R.id.my_view);
myView.addOnLayoutChangedListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom) {
// its possible that the layout is not complete in which case
// we will get all zero values for the positions, so ignore the event
if (left == 0 && top == 0 && right == 0 && bottom == 0) {
return;
}
int height = top - bottom;
}
});
Upvotes: 0
Reputation: 5068
you will not get value unitl onCreate
finishes.so add these in onResume()
or in onStart()
int height= li.getHeight();
int width = li.getWidth();
the other option is to use globallayoutlistener
(if you want to get height in onCreate) so you will get notified when li(your layout) is added.
ViewTreeObserver observer= li.getViewTreeObserver();
observer.addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.d("Log", "Height: " + li.getHeight());
Log.d("Log", "Width: " + li.getWidth());
}
});
Upvotes: 2