ActivityViewSwitcher.java
- Code:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ViewSwitcher;
public class ActivityViewSwitcher extends Activity {
private ViewSwitcher viewSwitcher ;
private Button btnSwitcher1;
private Button btnSwitcher2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_switcher);
viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
btnSwitcher1 = (Button) findViewById(R.id.btnSwitcher1);
btnSwitcher2 = (Button) findViewById(R.id.btnSwitcher2);
btnSwitcher1.setOnClickListener(onClickListener);
btnSwitcher2.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnSwitcher1:
//animation down.
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.animation_down_exit);
viewSwitcher.setAnimation(animation);
viewSwitcher.showNext();
break;
case R.id.btnSwitcher2:
viewSwitcher.setAnimation(AnimationUtils.makeInAnimation(getBaseContext(), true));
viewSwitcher.showPrevious();
break;
default:
break;
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_view_switcher, menu);
return true;
}
}
- Spoiler:
- <LinearLayout 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"
tools:context=".ActivityViewSwitcher" >
<ViewSwitcher
android:id="@+id/viewSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/llSwitcher1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tvSwitcher1"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="Switcher1" />
<Button
android:id="@+id/btnSwitcher1"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="Next" />
</LinearLayout>
<LinearLayout
android:id="@+id/llSwitcher2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tvSwitcher2"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:text="Switcher2" />
<Button
android:id="@+id/btnSwitcher2"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="Back" />
</LinearLayout>
</ViewSwitcher>
</LinearLayout>
- Code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="500"/>
</set>