6

I have similar question like this one: Update layout with the animation

Basically: I have one vertical LinearLayout View with edittext, button and then list. I'd like to hide exittext after pressing button to make more space for list (button will go up). On second press edittext should be visible again. Edittext and button have "wrap_content" height.

I'd like to hide and show edittext with animation.

I succeeded to animate hiding by overloading Animation's applyTransformation:

final float edittextheight= edittext.getHeight();
[....]
@Override
protected void applyTransformation(float interpolatedTime,
        Transformation t)
{
    super.applyTransformation(interpolatedTime, t);
    android.view.ViewGroup.LayoutParams lp = edittext.getLayoutParams();
    lp.height = (int)(edittextheight*(1.0-interpolatedTime));
    edittext.setLayoutParams(lp);
}

Problem:

I don't know how to calculate height to animate showing - edittext.getHeight(); returns 0 when widget is hidden and in layout definition I'm using "wrap_content".

Help?

Community
  • 1
  • 1
tomash
  • 12,742
  • 15
  • 64
  • 81

2 Answers2

3

You can't get the height of edittext until activity finishes drawing it. So you can get it by adding onpredrawListner on your edit text.

ViewTreeObserver treeObserver = edittext.getViewTreeObserver();
treeObserver.addOnPreDrawListener(new OnPreDrawListener() {
    public boolean onPreDraw() {
            int height = edittext.getHeight();
            ViewTreeObserver removeTreeObserver = edittext.getViewTreeObserver();
     removeTreeObserver.removeOnPreDrawListener(this);
          }
}

Now you got the height, apply any animation you want.

RiksAndroid
  • 815
  • 11
  • 17
  • 1
    From my point of view this is too complicated. The same thing can be achieved by doing `view.post(runnable);`. Inside the runnable you have access to the height of the element. – Macarse Nov 23 '11 at 12:58
1

Could you cache the EditText's height? In other words, save its height (using getHeight()) in an int when you're hiding it, which you can later read even when the widget itself no longer visible.

However, I suspect you're also facing a similar problem I had, described in this question. Essentially it's that you can't get a View's height until after onCreate, onStart and onPause have all finished, because before then the activity hasn't finished drawing itself on the screen. A good way to know that the layout process is finished before you try to get its height is simply to get the height in your button ClickListener; you simply can't click the button until the layout is finished so it will have finished calculating the height.

Community
  • 1
  • 1
Steve Haley
  • 55,374
  • 17
  • 77
  • 85
  • 1
    The problem with saving the height as fixed value is that, after making it visible again, it will have a fixed height, and might not supply enough space for the input – banzai86 Oct 24 '11 at 07:37