Here is a simple example to implement ViewFlipper.
![ViewFlipper ViewFlipper](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV3-2YzxpjNa1IXRYysj-1whj_bVC3zZpwfWO8uTkYJFyZR1IbP6v6hgxFxa09ahNLqdjyQ4jqdb-dsLpXXdp_IVHhgWp84i-X6D6RGln6dvrTt2nVFK4XCaf7AuNMd5IgZm-56fkpVn_C/s400/AndroidViewFlipper_01.png)
Modify main.xml to have a ViewFlipper, with two LinearLayout inside.
<?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="@string/hello"
/>
<ViewFlipper
android:id="@+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Screen"/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Flip to second page"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Screen"/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Flip back"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
main code
package com.exercise.AndroidViewFlipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
public class AndroidViewFlipper extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ViewFlipper MyViewFlipper = (ViewFlipper)findViewById(R.id.viewflipper);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyViewFlipper.showNext();
}});
button2.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyViewFlipper.showPrevious();
}});
}
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8p8OYSJ34x9VZ9cj4IzAOVtRlBOzcLrPRJWQ4iuiJI1eCKjrLYOEL2p2RYTNBidfvJuDOpoiZjXICYCF5ifVUBp09IMGbHsHFSOko1_Xdf4gYrG9MbcBSZDywv5SWAQhCav5OT9OGPE5T/s200/icons_download.gif)
next:
- Auto flipping of ViewFlipper
- ViewFlipper Animation
No comments:
Post a Comment