Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate setVisibility for an imagebutton on Android?

I have a layout that has a listview and imagebutton:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="4px"
    android:background="@color/bgColor">

    <ImageButton
    android:id="@+id/imageButton"
    android:src="@drawable/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="0dp"
    android:layout_alignParentBottom="true"/>

    <ListView
    android:layout_above="@+id/imageButton" 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:background = "@drawable/list_bg"
    android:divider="@drawable/grade"
    android:dividerHeight ="1px"
    android:cacheColorHint="#00000000"
    android:fastScrollEnabled= "true"
    android:scrollingCache ="true"
    android:smoothScrollbar="false"
    >
   </ListView>
</RelativeLayout>

Based on user selection I am using this to hide or show the image button:

       ImageButton myButton = (ImageButton) findViewById(R.id.imageButton);
        myButton.setVisibility(View.GONE);

The code is working fine, however I would like to animate the hide/show process. How do I achieve this effect?

like image 407
Kalimah Avatar asked Aug 24 '11 10:08

Kalimah


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.

Can we do animation in Android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.

What is Alpha animation in Android?

An animation that controls the alpha level of an object. Useful for fading things in and out. This animation ends up changing the alpha property of a Transformation.

Can you add text to an imageButton Android?

Answer: You can not display text with imageButton . Method that tell in Accepted answer also not work. If you use android:drawableLeft="@drawable/buttonok" then you can not set drawable in center of button . If you use android:background="@drawable/button_bg" then color of your drawable will be changed.


2 Answers

You can use the System animation fade in and fade out.

 Animation animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out);
 Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in);
myButton.setAnimation(animFadeOut)
//if necessary then call:
myButton.setVisibility(View.GONE);

that should work

if you want to make your own animations: Look at Android Res and here is a very good tutorial for animations.

like image 135
Fabian Avatar answered Oct 05 '22 09:10

Fabian


To show the button call this

AlphaAnimation fade_in = new AlphaAnimation(0.0f, 1.0f);
fade_in.setDuration(500);
fade_in.setAnimationListener(new AnimationListener()
{
    public void onAnimationStart(Animation arg0)
    {
    }
    public void onAnimationRepeat(Animation arg0)
    {
    }

    public void onAnimationEnd(Animation arg0)
    {
        myButton.setVisibility(View.VISIBLE);
    }
});
myButton.startAnimation(fade_in);

Then to hide the button:

AlphaAnimation fade_out = new AlphaAnimation(1.0f, 0.0f);
fade_out.setDuration(upcoming_animation_time);
fade_out.setAnimationListener(new AnimationListener()
{
    public void onAnimationStart(Animation arg0)
    {
    }
    public void onAnimationRepeat(Animation arg0)
    {
    }

    public void onAnimationEnd(Animation arg0)
    {
        myButton.setVisibility(View.GONE);
    }
});
myButton.startAnimation(fade_out);
like image 31
Kurru Avatar answered Oct 05 '22 11:10

Kurru