Fade out current activity view

I want my activity view to disappear when I click on the button. I put the animation code inside the OnClickListener () button, but fading does not happen. So, any idea, how can I fade out the current activity when a button is clicked? Thanks in advance.

Actually, my goal: in my application, when an action begins, the kind of activity will disappear, and as the action ends, it will disappear, and then the new representation of the activity will disappear. Below im giving my code, please help 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); } }); } } 
+4
source share
2 answers

In 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 layout 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> 
+8
source

Can you try the code below.

 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); startActivity(intent); overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out); } }); 

may be useful to you.

+1
source

All Articles