Friday, March 23, 2012

How to change the default transition between activities?


How to change the default transition between activities?

In android the default transition between activities is to slide from left to right.
But with custom animations we can change that.
First create a folder inside the res/drawable folder called “anim”.
Then create a file named “fade.xml” and copy this code into it.
?
Drag and copy the code
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="0.0" android:toAlpha="1.0"
       android:duration="@android:integer/config_longAnimTime" />
create another file named “hold.xml” in the same place
hold.xml.
?
Drag and copy the code
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromXDelta="0" android:toXDelta="0"
       android:duration="@android:integer/config_longAnimTime" />
activity_animation.xml
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
 
    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="Sample Animation"/>
 
    <Button android:id="@+id/fade_animation"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="fade In">
        <requestFocus />
    </Button>
</LinearLayout>
Now the main java file.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package pack.coderzheaven;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class ActivityAnimation extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
 
        Button button = (Button)findViewById(R.id.fade_animation);
        button.setOnClickListener(mFadeListener);
    }
 
    private OnClickListener mFadeListener = new OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(ActivityAnimation.this, SecondClass.class));
            overridePendingTransition(R.anim.fade, R.anim.hold);
        }
    };
}
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
[/java]
package pack.coderzheaven;
 
import android.app.Activity;
import android.os.Bundle;
 
public class SecondClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
1
main.xml
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Second Class"
    />
</LinearLayout>

No comments: