Reputation: 283
So I have an app that when the user hits a button
will do an animation
for a layout
with buttons (like a sliding menu
) and then if he hits another button it has to make invisible or gone the first layout and then so the new one.
But when I am trying to make on the AnimationStart
my layout
with buttons invisible, it doesn't do that.
I already tried some solutions from here:
Why doesn't setvisibility work after a view is animated
Setvisibilityview Gone doesn't disappear a view
but nothing worked!
Any help??
Java code
(it's the same for both buttons)
btn_home1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.VISIBLE);
btn_home.setVisibility(View.VISIBLE);
btn_book.setVisibility(View.VISIBLE);
btn_find_us.setVisibility(View.VISIBLE);
btn_menu.setVisibility(View.VISIBLE);
TranslateAnimation slide = new TranslateAnimation(-100, 0, 0,0 );
slide.setDuration(1000);
slide.setFillAfter(true);
slide.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
btn_home2.setVisibility(View.GONE);
btn_book2.setVisibility(View.GONE);
btn_find_us2.setVisibility(View.GONE);
btn_menu2.setVisibility(View.GONE);
layout2.setVisibility(View.GONE);
}
}, 0);
btn_home.setClickable(false);
btn_book.setClickable(false);
btn_find_us.setClickable(false);
btn_menu.setClickable(false);
}
@Override
public void onAnimationEnd(Animation animation) {
btn_home.setClickable(true);
btn_book.setClickable(true);
btn_find_us.setClickable(true);
btn_menu.setClickable(true);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
btn_menu.startAnimation(slide);
btn_book.startAnimation(slide);
btn_find_us.startAnimation(slide);
btn_home.startAnimation(slide);
layout1.startAnimation(slide);
}
});
XML
code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#BE2625" >
<Button
android:id="@+id/btn_home1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="342"
/>
<Button
android:id="@+id/btn_home11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:text="34243"
/>
<LinearLayout
android:id="@+id/lala"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80000000"
android:visibility="gone"
android:orientation="vertical">
<Button
android:id="@+id/btn_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_find_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/lala1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80000000"
android:visibility="gone"
android:orientation="vertical">
<Button
android:id="@+id/btn_home2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_book2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_find_us2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_menu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
</LinearLayout>
</RelativeLayout>
Upvotes: 3
Views: 2659
Reputation: 381
calling clearAnimation of the View fixed the problem. In my case I wanted to set the View back to its original position after doing a translation with fillAfter set to true.
Upvotes: 2
Reputation: 283
I have done again a research on this link: Why doesn't setVisibility work after a view is animated?
And found the answer of @Chris Knight:
Another way to work around this is to wrap your animated view in another view and set the visibility of that wrapper view.
So I used as he did two FrameLayout
and then set the setVisibility(View.GONE)
for one at a time, because the user would hit one button at a time so it would open a Slide Menu
at a time.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#BE2625" >
<Button
android:id="@+id/btn_home1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="342"
/>
<Button
android:id="@+id/btn_home11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:text="34243"
/>
<FrameLayout
android:id="@+id/lsd1"
android:layout_height="match_parent"
android:layout_width="240dp">
<LinearLayout
android:id="@+id/lala"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80000000"
android:visibility="gone"
android:orientation="vertical">
<Button
android:id="@+id/btn_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_find_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="@+id/lsd2"
android:layout_height="match_parent"
android:layout_width="240dp">
<LinearLayout
android:id="@+id/lala1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80000000"
android:visibility="gone"
android:orientation="vertical">
<Button
android:id="@+id/btn_home2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_book2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_find_us2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
<Button
android:id="@+id/btn_menu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
Upvotes: 0