This is simple example for a viewpager in android.First add "android-support-v4.jar" library in your libs folder.than create an activity and add this code.
public class MainActivity extends Activity implements OnClickListener {
private ViewPager viewPager;
private TextSwipping pagerAdapter;
private ArrayList<String> alText = new ArrayList<String>();
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcomepage);
alText.add("Hi");
alText.add("This");
alText.add("is");
alText.add("Manu");
viewPager = (ViewPager) findViewById(R.id.welcome_text_pager);
pagerAdapter = new TextSwipping(alText, this);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int pos) {
// TODO Auto-generated method stub
if (viewPager.getCurrentItem() == 0) {
// set your conditions here
} else if (viewPager.getCurrentItem() == 1) {
// set your conditions here
} else if (viewPager.getCurrentItem() == 2) {
// set your conditions here
} else if (viewPager.getCurrentItem() == 3) {
// set your conditions here
}
}
});
}
}
add this viewpager in your welcomepage.xml file
<android.support.v4.view.ViewPager
android:id="@+id/welcome_text_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
and create Adapter for the text displaying in your viewpager.
public class TextSwipping extends PagerAdapter {
ArrayList<String> alText;
Context context;
public TextSwipping(ArrayList<String> alText, Context context) {
this.alText = alText;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// View view= inflater.inflate(R.layout.textswitcher, null);
// I am using a dynamic text view you could use
//a layout using inflater or use a fragment as your necessity.
TextView tvTextSwitcher = new TextView(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tvTextSwitcher.setTextColor(Color.parseColor("#ff78ff"));
tvTextSwitcher.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
tvTextSwitcher.setLayoutParams(lp);
tvTextSwitcher.setGravity(Gravity.CENTER_HORIZONTAL);
tvTextSwitcher.setText(" " + alText.get(position));
tvTextSwitcher.setTypeface(WidgetProperties
.setTextTypefaceRegular(context));
((ViewPager) container).addView(tvTextSwitcher);
return tvTextSwitcher;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
android:id="@+id/welcome_text_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
and create Adapter for the text displaying in your viewpager.
public class TextSwipping extends PagerAdapter {
ArrayList<String> alText;
Context context;
public TextSwipping(ArrayList<String> alText, Context context) {
this.alText = alText;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// View view= inflater.inflate(R.layout.textswitcher, null);
// I am using a dynamic text view you could use
//a layout using inflater or use a fragment as your necessity.
TextView tvTextSwitcher = new TextView(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tvTextSwitcher.setTextColor(Color.parseColor("#ff78ff"));
tvTextSwitcher.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
tvTextSwitcher.setLayoutParams(lp);
tvTextSwitcher.setGravity(Gravity.CENTER_HORIZONTAL);
tvTextSwitcher.setText(" " + alText.get(position));
tvTextSwitcher.setTypeface(WidgetProperties
.setTextTypefaceRegular(context));
((ViewPager) container).addView(tvTextSwitcher);
return tvTextSwitcher;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}