Monday, 24 June 2013

ViewFlipper in android

XML File

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="6dip" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"
android:textColor="#00ff00"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
      android:text="A dog limps into a saloon and says...najsndfiaimfi"
        android:singleLine="true"
         android:layout_marginLeft="35dp"
        android:layout_gravity="center"
       android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:duplicateParentState="true">
    <requestFocus android:focusable="true" android:focusableInTouchMode="true"
        android:duplicateParentState="true" />
</TextView>

</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m looking for the man who shot my paw!"
android:textColor="#00ff00"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</ViewFlipper>
   

</RelativeLayout>



JAVA File

package com.example.viewflipper;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
private ViewFlipper vf;
private float lastX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vf = (ViewFlipper) findViewById(R.id.view_flipper);
}
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
if (vf.getDisplayedChild() == 0)
break;

vf.showNext();
}
if (lastX > currentX) {
if (vf.getDisplayedChild() == 1)
break;

vf.showPrevious();
}
break;
}
}
return false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

No comments:

Post a Comment