Reputation: 11545
I have a LinearLayout
inside a LinearLayout
and child linearlayout
have its layout params
where height
and width
are defined as
LinearLayout linearLayout=new LinearLayout(context);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(50,50);
linearLayout.setLayoutParams(lp);
now after view created i am trying to get height of a view from getHeight()
and getMeasuredHeight()
but both methods returning 0 . ?
int heightofView=linearLayout.getHeight();
int heightofview=linearLayout.getMeasuredHeight();
why both these methods are returning 0 height of a view ? when these methods return the actual result ? how to get height of my created view ?
Upvotes: 1
Views: 2795
Reputation: 2737
Do as below -
linearLayout.post(new Runnable() {
@Override
public void run() {
int linearLayoutHeight = linearLayout.getHeight();
Log.e("MS", "linearLayout Ht = " + linearLayoutHeight);
}
});
Upvotes: 3
Reputation: 1740
Try this code.
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int width = imageView.getWidth();
int height = imageView.getHeight();
//you can add your code here on what you want to do to the height and width you can pass it as parameter or make width and height a global variable
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Upvotes: 6