i have tired by using button,but nothing happpens. -what i have tried is below :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instruction);
preferenceHelper = new PreferenceHelper(Instruction.this);
viewPager = (ViewPager) findViewById(R.id.myviewpager);
myPagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(myPagerAdapter);
txtrank = (TextView)findViewById(R.id.rank);
tvRight=(TextView)findViewById(R.id.tvRight);
tvLeft=(TextView)findViewById(R.id.tvLeft);
/* tvRight.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(viewPager.getCurrentItem() < viewPager.getRight())
viewPager.setCurrentItem(viewPager.getCurrentItem()+1,true);
}
});
tvLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewPager.getCurrentItem() > viewPager.getLeft())
viewPager.setCurrentItem(viewPager.getCurrentItem()-1,true);
}
});*/
// Generate sample data
rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
/* sharedPreferences = getSharedPreferences("ShaPreferences",
Context.MODE_PRIVATE); SharedPreferences.Editor editor =
sharedPreferences.edit(); boolean firstTime =
sharedPreferences.getBoolean("first", true);
if (firstTime) {
editor.putBoolean("first", false); editor.commit();
viewPager.setAdapter(myPagerAdapter);
Intent intent = new Intent(Instruction.this, LoginActivity.class);
startActivity(intent);
} else { Intent intent = new Intent(Instruction.this,
LoginActivity.class); startActivity(intent); }*/
}
private class MyPagerAdapter extends PagerAdapter {
int NumberOfPages = 9;
int[] res = { R.drawable.a0, R.drawable.b1, R.drawable.c2,
R.drawable.d3, R.drawable.e4, R.drawable.f5, R.drawable.g6,
R.drawable.h7, R.drawable.i8 };
/*
* int[] backgroundcolor = { 0xFF101010, 0xFF202020, 0xFF303030,
* 0xFF404040, 0xFF505050, 0xFF101010, 0xFF202020, 0xFF303030,
* 0xFF404040, 0xFF303030};
*/
@Override
public int getCount() {
return NumberOfPages;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
/*
* TextView textView = new TextView(Instruction.this);
* textView.setTextColor(Color.WHITE); textView.setTextSize(20);
* textView.setTypeface(Typeface.DEFAULT_BOLD);
* textView.setText(String.valueOf(position));
*/
txtrank.setText(rank[position]);
ImageView imageView = new ImageView(Instruction.this);
imageView.setImageResource(res[position]);
LayoutParams imageParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
imageView.setLayoutParams(imageParams);
LinearLayout layout = new LinearLayout(Instruction.this);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
// layout.setBackgroundColor(backgroundcolor[position]);
layout.setLayoutParams(layoutParams);
// layout.addView(textView);
layout.addView(imageView);
final int page = position;
/*layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
* Toast.makeText(Instruction.this, "Page " + page +
* " clicked", Toast.LENGTH_LONG) .show();
}
});*/
if (viewPager.getCurrentItem() == 7) {
Intent intent = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(intent);
}
Log.e("at page number", String.valueOf(viewPager.getCurrentItem()));
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.left:
if(viewPager.getCurrentItem() < viewPager.getLeft())
viewPager.setCurrentItem(viewPager.getCurrentItem()-1,true);
break;
case R.id.right:
if(viewPager.getCurrentItem() < viewPager.getRight())
viewPager.setCurrentItem(viewPager.getCurrentItem()+1,true);
break;
}
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
-what i want is..user can move to next image by sliding as well as by using right or left arrow.. i am able to slide..but arrows are not working.
If you want it to work with sliding as well as arrows then you have to use left and right listeners like this.
right.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(pager.getCurrentItem() < pager.getRight())
pager.setCurrentItem(pager.getCurrentItem()+1,true);
}
});
left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (pager.getCurrentItem() > pager.getLeft())
pager.setCurrentItem(pager.getCurrentItem()-1,true);
}
});