Fadi A.
Fadi A.

Reputation: 93

How to add Show/Hide multiple buttons with animations

I am working on show/hide my buttons with animation. I did that but I have a serious problem.

When I hide the first button, and then I hide the second button, I can see the first button show up then hide so fast. I tried everything.

Showing buttons:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"><scale
android:duration="450"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
/></set>

Hiding Buttons :

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"><scale
    android:duration="300"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0.0"
    android:toYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    /></set>

Java :

public class MainActivity extends AppCompatActivity {

Button  btn1,btn2,btn3;
Animation show , hide;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button)findViewById(R.id.btn1);
    btn2 = (Button)findViewById(R.id.btn2);
    btn3 = (Button)findViewById(R.id.btn3);

    show = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.Show);
    hide = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.Hide);
}

public void setBtn1(View v) {
    btn1.startAnimation(hide);
    btn1.setClickable(false);
}

public void setBtn2(View v) {
    btn2.startAnimation(hide);
    btn2.setClickable(false);
}

public void setBtn3(View v) {
    btn3.startAnimation(hide);
    btn3.setClickable(false);
}

Here is the activity_main.xml :

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:onClick="setBtn1"
    android:text="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toTopOf="@+id/btn3"
    android:layout_marginStart="3dp"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="65dp"
    app:layout_constraintLeft_toRightOf="@+id/btn2"
    android:layout_marginLeft="3dp" />

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:onClick="setBtn2"
    android:text="2"
    android:layout_marginStart="57dp"
    app:layout_constraintBaseline_toBaselineOf="@+id/btn3"
    tools:layout_constraintBaseline_creator="1"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="57dp" />

<Button
    android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:onClick="setBtn3"
    android:text="3"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="71dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginBottom="191dp"
    android:layout_marginRight="71dp" />
</android.support.constraint.ConstraintLayout>

Upvotes: 1

Views: 1360

Answers (1)

Vova
Vova

Reputation: 1091

You need remove android:fillAfter="true" form your Hide.xml and Show.xml files. And please rename the files to hide.xml and show.xml, (It is Java code convetion)

And use my MainActivity.java implementation

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

public class MainActivity extends AppCompatActivity {

Button btn1, btn2, btn3;

Animation show, hide;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(final View v) {
            setBtn1();
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(final View v) {
            setBtn2();
        }
    });

    btn3.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(final View v) {
            setBtn3();
        }
    });

    show = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.show);
    hide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.hide);
}

public void setBtn1() {
    btn1.startAnimation(hide);
    btn1.setClickable(false);
    btn1.setVisibility(View.GONE);
}

public void setBtn2() {
    btn2.startAnimation(hide);
    btn2.setClickable(false);
    btn2.setVisibility(View.GONE);
}

public void setBtn3() {
    btn3.startAnimation(hide);
    btn3.setClickable(false);
    btn3.setVisibility(View.GONE);
}  
}

Upvotes: 1

Related Questions