Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to hide and then show View with animation effect?

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?

like image 359
tomash Avatar asked Mar 22 '10 21:03

tomash


People also ask

How do I animate a view in Android?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.

What is crossfade animation?

Crossfade animations (also know as dissolve) gradually fade out one UI component while simultaneously fading in another. This animation is useful for situations where you want to switch content or views in your app. Crossfades are very subtle and short but offer a fluid transition from one screen to the next.


1 Answers

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.

like image 92
RiksAndroid Avatar answered Nov 09 '22 13:11

RiksAndroid