1

In my app I'm adding many views inside the Layout having below properties how shall i get the correct height of the layout at run time

        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(840,LayoutParams.WRAP_CONTENT));
        ll.setOrientation(LinearLayout.VERTICAL);

        ll.addView(view1); 
        ll.addView(view2);

        ll.getHeight();

how to get Height of layout at that point consider adding of view1 and view2 , irrespective of my property(LayoutParams.WRAP_CONTENT), beacause it always return 0 to me?

Strawberry
  • 667
  • 2
  • 10
  • 24

2 Answers2

3

Try this way,hope this will help you to solve your problem.

int width;
int height;

ll.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
     width = view.getMeasuredWidth();
     height = view.getMeasuredHeight();

   }
});
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
1

You can call

ll.post(new Runnable()
{
    public void run()
    {
        ll.getHeight(); 
    }
});

This will give you ll's height after adding the child views.

Apoorv
  • 13,470
  • 4
  • 27
  • 33