1. Download our Official Android App: Forums for Android!

Apps get height of hidden views

Discussion in 'Android Development' started by id0001, Mar 16, 2011.

  1. id0001

    id0001 Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    38
    Posts:
    142
    Joined:
    Apr 20, 2010

    Apr 20, 2010
    142
    23
    38
    Student.. also software developer
    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?
     

    Advertisement

  2. id0001

    id0001 Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    38
    Posts:
    142
    Joined:
    Apr 20, 2010

    Apr 20, 2010
    142
    23
    38
    Student.. also software developer
    Netherlands
    I figured it out:

    the height measure code
    Code (Text):
    1. public void AddReview(Review review) {
    2.    final View v = LayoutInflater.from(context).inflate(R.layout.review_item, null, false);
    3.    final LinearLayout reviewList = (LinearLayout)findViewById(R.id.ReviewContainer);
    4.    final LinearLayout reviewExpander = (LinearLayout)findViewById(R.id.ReviewExpander);
    5.  
    6.    TextView comment = (TextView)v.findViewById(R.id.Comment);
    7.    TextView author = (TextView)v.findViewById(R.id.Author);
    8.  
    9.    comment.setText(review.Comment);
    10.    author.setText(review.User);
    11.  
    12.    reviewList.addView(v);
    13.  
    14.    v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    15.      
    16.       @Override
    17.       public void onGlobalLayout() {
    18.          v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    19.          
    20.          if(addedReviews < minReviewsShowing) {
    21.             initReviewListHeight += v.getHeight();
    22.             initReviewListHeight = Math.min(initReviewListHeight, maximumInitHeight);
    23.          }
    24.          
    25.          expandedreviewListHeight += v.getHeight();
    26.          
    27.          addedReviews++;
    28.          
    29.          if(addedReviews == reviewCount) {
    30.             reviewList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, initReviewListHeight));
    31.            
    32.             if(expandedreviewListHeight <= initReviewListHeight) {
    33.                reviewExpander.setVisibility(View.GONE);
    34.             }
    35.          }
    36.       }
    37.    });
    38. }
    The animation class
    Code (Text):
    1. class ExpandCollapseAnimation extends Animation {
    2.    private View v;
    3.    private boolean expand;
    4.    
    5.    public ExpandCollapseAnimation(View v, boolean expand) {
    6.       this.v = v;
    7.       this.expand = expand;
    8.    }
    9.    
    10.    @Override
    11.    protected void applyTransformation(float interpolatedTime,
    12.          Transformation t) {
    13.       int newHeight;
    14.      
    15.       if(expand) {
    16.          newHeight = initReviewListHeight + (int)((expandedreviewListHeight - initReviewListHeight) * interpolatedTime);
    17.       } else {
    18.          newHeight = initReviewListHeight + (int)((expandedreviewListHeight - initReviewListHeight) * (1-interpolatedTime));
    19.       }
    20.      
    21.       this.v.getLayoutParams().height = newHeight;
    22.       this.v.requestLayout();
    23.    }
    24.  
    25.    @Override
    26.    public boolean willChangeBounds() {
    27.       return true;
    28.    }
    29. }
    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.
     

Share This Page

Loading...