Fix If Else Statement for Buttons Animation in Android Studio using Java

package id.ac.amikom.students.sukubudayaindonesia;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class Beranda extends AppCompatActivity {
    //Deklarasi
    ImageView pencet;
    Button tombolSuku,tombolSejarah,tombolKawasan,tombolKebudayaan,tombolBahasa,tombolDeretan,iconCari,tombolCari,tombolPengaturan;
    EditText txtPencarian;
    private boolean isPanelShown;

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //* Sembunyikan Notifikasi Bar
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_beranda);

    pencet=(ImageView)findViewById(R.id.buttonimageView);
    tombolSuku=(Button) findViewById(R.id.tombolsuku);
    tombolSejarah=(Button) findViewById(R.id.tombolsejarah);
    tombolKawasan=(Button) findViewById(R.id.tombolkawasan);
    tombolKebudayaan=(Button) findViewById(R.id.tombolkebudayaan);
    tombolBahasa=(Button) findViewById(R.id.tombolbahasa);
    tombolDeretan=(Button) findViewById(R.id.tombolderetan);
    iconCari=(Button) findViewById(R.id.iconcari);
    tombolCari=(Button) findViewById(R.id.tombolcari);
    tombolPengaturan=(Button) findViewById(R.id.tombolpengaturan);
    txtPencarian=(EditText)findViewById(R.id.txtpencarian);
    pencet.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if (isPanelShown) {
                Animation animasiTurun = AnimationUtils.loadAnimation(Beranda.this, R.anim.toptobottom);
                //turun
                tombolSuku.startAnimation(animasiTurun);
                tombolSuku.setVisibility(View.GONE);
                tombolSejarah.startAnimation(animasiTurun);
                tombolSejarah.setVisibility(View.GONE);
                tombolKawasan.startAnimation(animasiTurun);
                tombolKawasan.setVisibility(View.GONE);
                tombolKebudayaan.startAnimation(animasiTurun);
                tombolKebudayaan.setVisibility(View.GONE);
                tombolBahasa.startAnimation(animasiTurun);
                tombolBahasa.setVisibility(View.GONE);

                Animation animasiNaik = AnimationUtils.loadAnimation(Beranda.this, R.anim.bottomtotop);
                //naik
                tombolDeretan.startAnimation(animasiNaik);
                tombolDeretan.setVisibility(View.GONE);
                iconCari.startAnimation(animasiNaik);
                iconCari.setVisibility(View.GONE);
                txtPencarian.startAnimation(animasiNaik);
                txtPencarian.setVisibility(View.GONE);
                tombolCari.startAnimation(animasiNaik);
                tombolCari.setVisibility(View.GONE);
                tombolPengaturan.startAnimation(animasiNaik);
                tombolPengaturan.setVisibility(View.GONE);

            } else {
                Animation animasiTurun = AnimationUtils.loadAnimation(Beranda.this, R.anim.toptobottom);
                //turun
                tombolDeretan.startAnimation(animasiTurun);
                iconCari.startAnimation(animasiTurun);
                txtPencarian.startAnimation(animasiTurun);
                tombolCari.startAnimation(animasiTurun);
                tombolPengaturan.startAnimation(animasiTurun);

                Animation animasiNaik = AnimationUtils.loadAnimation(Beranda.this, R.anim.bottomtotop);
                //naik
                tombolSejarah.startAnimation(animasiNaik);
                tombolKawasan.startAnimation(animasiNaik);
                tombolSuku.startAnimation(animasiNaik);
                tombolKebudayaan.startAnimation(animasiNaik);
                tombolBahasa.startAnimation(animasiNaik);
            }
        }
    });}}}

I'm stuck at this point, don't know how to fix this. I want to make those buttons do animation tricks by using 1 button and it's called "pencet" it's an imageButton.

so the first press on "pencet" it would make those group of buttons moveup&down, and the second press would make them return in first position. Please help me to fix that if else statement.

1 answer

  • answered 2018-11-08 06:12 Lev M.

    The problem seems to be that you are calling setVisibility(View.GONE) on all your views immediately after starting the animation (in the if block). So I am guessing your widgets disappear before the animation finishes.

    Then, in the else block, you never set the views back to VISIBLE.

    You need to call setAnimationListener on you animation, and put setVisibility(View.GONE) int the onAnimationEnd method so it will only be called after your animation finishes.

    You also need to make your views visible again in the else block.

    Here is a code example:

    animasiTurun.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}
    
        @Override
        public void onAnimationEnd(Animation animation) {
            tombolSuku.setVisibility(View.GONE);
            tombolSejarah.setVisibility(View.GONE);
            /** put all other views you want GONE here **/
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) {}
    });
    

    Put his before you first call to startAnimation (where turun comment is).