android – Linear layout animation-ThrowExceptions

Exception or error:

In my application 2 linear layout are there. those layouts contain some text view …etc data is came from the server. i have a requirement that when the data of Linear layout 1 is empty it goes hide and linear layout 2 is placed instead of linear layout 1 .

 l1 = (LinearLayout) findViewById(R.id.lnrlgn);
        l2 = (LinearLayout) findViewById(R.id.lnrlgn1);
if(l1.isEmpty){
l1.setVisibility(view.InVISIBLE);
  TranslateAnimation animate = new TranslateAnimation(
                0,                 // fromXDelta
                0,                 // toXDelta
                l1.getHeight(),  // fromYDelta
                0);                // toYDelta
        animate.setDuration(5500);
        animate.setFillAfter(true);
        l1.startAnimation(animate);
}

but it is not working

enter image description here

How to solve:

You can animate LinearLayout like this.

l1.animate().alpha(0.0f); //gone

l1.animate().alpha(1.0f); //appear

###

I have done beautifully it with alpha animation:

// for hide layout
alpha_invisible.xml

   <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>

// for visible layout
alpha_visible.xml

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>

and in java file:

Animation animVisible, animInvisible;

animVisible = AnimationUtils.loadAnimation(PasswordSetupActivity.this,
            R.anim.alpha_visible);
animInvisible = AnimationUtils.loadAnimation(
            PasswordSetupActivity.this, R.anim.alpha_invisible);

//hide first linearLayout
ll.startAnimation(animInvisible);

animInvisible.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation arg0) {

}

@Override
public void onAnimationRepeat(Animation arg0) {

}

@Override
public void onAnimationEnd(Animation arg0) {

    // show second linearlayout
    ll.startAnimation(animVisible);
}

});

###

I think there are multiple cases to fix this issue:

  1. try l1.setVisibility(View.GONE) in case of the order of layouts;
  2. some cases when using l1.getHeight() get '0', so you have to write the code when events come dynamically.

I checked the code and it worked fine.

Leave a Reply

Your email address will not be published. Required fields are marked *