Fade Out Current Activity's View

Junaid picture Junaid · Jul 16, 2011 · Viewed 9.4k times · Source

i want my activity's view to be faded out when there is a click on a button. I put the animation code inside OnClickListener() of the button but the fade out is not happening. So any idea how can i manage to fade out the current activity's view when the button is clicked ? Thanks in advance .....

Actually , My purpose is, In my application, as a activity starts, the view of that activity will fade in and as the activity ends, it's view will fade out and then the new activity's view will fade in. Below , i m giving my code, please need help to figure out the problem there .....

public class First extends Activity {

Animation slide;
View view;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    view = (View) findViewById(android.R.id.content);

    slide = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    view.startAnimation(slide);

    Button btn = (Button) findViewById(R.id.retour);
    btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(),Second.class);
                slide = AnimationUtils.loadAnimation(First.this, R.anim.fade_out);
                view.startAnimation(slide);
                Test.group.replaceContentView("Second", intent, 0);

            }
    });

}

}

Answer

vsm picture vsm · Jul 16, 2011

On your onCreate() method add something like this:

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            final View l = findViewById(R.id.main);

            Animation a = AnimationUtils.loadAnimation(
                    YourActivity.this, android.R.anim.fade_out);
            a.setDuration(200);
            a.setAnimationListener(new AnimationListener() {

                public void onAnimationEnd(Animation animation) {
                        // Do what ever you need, if not remove it.  
                }

                public void onAnimationRepeat(Animation animation) {
                        // Do what ever you need, if not remove it.  
                }

                public void onAnimationStart(Animation animation) {
                        // Do what ever you need, if not remove it.  
                }

            });
            l.startAnimation(a);
        }
    });

and your Layout xml should start with a View with id="@+id/main" and contain a button with id="@+id/button"

Example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:id="@+id/main"
     android:drawingCacheQuality="high"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
.....

    <Button
        android:id="@+id/button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
                    android:text="Fade out">
    </Button>