• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps get height of hidden views

id0001

Well-Known Member
Apr 20, 2010
142
23
Netherlands
I am trying to make a expand/collapse view animation. For the expanding part I need the total height of the view I am expanding but I don't know how to get it because all height methods/properties seem to return the current (partial) height.

How can I get the actual height of the view?
 
I figured it out:

the height measure code
Code:
public void AddReview(Review review) {
   final View v = LayoutInflater.from(context).inflate(R.layout.review_item, null, false);
   final LinearLayout reviewList = (LinearLayout)findViewById(R.id.ReviewContainer);
   final LinearLayout reviewExpander = (LinearLayout)findViewById(R.id.ReviewExpander);

   TextView comment = (TextView)v.findViewById(R.id.Comment);
   TextView author = (TextView)v.findViewById(R.id.Author);

   comment.setText(review.Comment);
   author.setText(review.User);

   reviewList.addView(v);

   v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      
      @Override
      public void onGlobalLayout() {
         v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
         
         if(addedReviews < minReviewsShowing) {
            initReviewListHeight += v.getHeight();
            initReviewListHeight = Math.min(initReviewListHeight, maximumInitHeight);
         }
         
         expandedreviewListHeight += v.getHeight();
         
         addedReviews++;
         
         if(addedReviews == reviewCount) {
            reviewList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, initReviewListHeight));
            
            if(expandedreviewListHeight <= initReviewListHeight) {
               reviewExpander.setVisibility(View.GONE);
            }
         }
      }
   });
}

The animation class
Code:
class ExpandCollapseAnimation extends Animation {
   private View v;
   private boolean expand;
   
   public ExpandCollapseAnimation(View v, boolean expand) {
      this.v = v;
      this.expand = expand;
   }
   
   @Override
   protected void applyTransformation(float interpolatedTime,
         Transformation t) {
      int newHeight;
      
      if(expand) {
         newHeight = initReviewListHeight + (int)((expandedreviewListHeight - initReviewListHeight) * interpolatedTime);
      } else {
         newHeight = initReviewListHeight + (int)((expandedreviewListHeight - initReviewListHeight) * (1-interpolatedTime));
      }
      
      this.v.getLayoutParams().height = newHeight;
      this.v.requestLayout();
   }

   @Override
   public boolean willChangeBounds() {
      return true;
   }
}

I want the view to display a minimum amount of reviews up to a maximum height. so In onGlobalLayoutListener I:
1. get the height of the review View
2. if addedReviewCount is lower then miniumShowing -> add it's height to the initReviewListHeight
3. use Math.min() to get the lowest height.
4. add it's height to the expandedReviewListHeight
5. check if all the Views are added.
6. if yes, set the height of the reviewList
7. done.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones