Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to go to next activity after ViewPager in android?

I created a "Slide Image" in first activity; which has five images. After I slide the last image(fifth), it should go to next activity,say-activity_next.xml.

Below is my code to slide Image i.e., SlideActivity.java

public class SlideActivity extends Activity {

    public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
         ImagePagerAdapter adapter = new ImagePagerAdapter();
         viewPager.setAdapter(adapter);  

    }

     private class ImagePagerAdapter extends PagerAdapter {
            private int[] mImages = new int[] {
                R.drawable.one,
                R.drawable.two,
                R.drawable.three,
                R.drawable.four,
                R.drawable.five
            };

    @Override
    public int getCount() {
    return mImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
    return view == ((ImageView) object);
        }

     @Override
     public Object instantiateItem(ViewGroup container, int position) {
       Context context = Illustrations.this;
       ImageView imageView = new ImageView(context);
       int padding = context.getResources().getDimensionPixelSize(
              R.dimen.padding_medium);
       imageView.setPadding(padding, padding, padding, padding);
       imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
       imageView.setImageResource(mImages[position]);
       ((ViewPager) container).addView(imageView, 0);
       return imageView;
      }

}
}

I'm not getting where to pass Intent for next activity i.e., activity_next.xml

**** UPDATED CODE as per your suggestions ****

public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

  final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
     ImagePagerAdapter adapter = new ImagePagerAdapter();
     viewPager.setAdapter(adapter);  



  viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub
         if(position==viewPager.getAdapter().getCount()){
    Intent reg = new Intent(SlideActivity.this,activity_next.class);
                startActivity(reg);
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });


    }

        private class ImagePagerAdapter extends PagerAdapter {
        private int[] mImages = new int[] {
            R.drawable.one,
            R.drawable.two,
            R.drawable.three,
            R.drawable.four,
            R.drawable.five
        };

      @Override
      public int getCount() {
      return mImages.length+1;
      }

      @Override
      public boolean isViewFromObject(View view, Object object) {
      return view == ((ImageView) object);
       }

         @Override
     public Object instantiateItem(ViewGroup container, int position) {
     Context context = Illustrations.this;
     ImageView imageView = new ImageView(context);
     int padding = context.getResources().getDimensionPixelSize(
          R.dimen.padding_medium);
     imageView.setPadding(padding, padding, padding, padding);
     imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
     if(position < getCount()-1)
     imageView.setImageResource(mImages[position]);
     ((ViewPager) container).addView(imageView, 0);
     return imageView;
   }




    @Override
    public void destroyItem(ViewGroup container, int position, Object   object) {
      ((ViewPager) container).removeView((ImageView) object);
    }
 }
 }

****activity_next.java code is below ****

public class activity_next extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
}

}

**** activity_next.xml code is as below****

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome to Nowhere Else" />

</LinearLayout>
like image 715
Kirataka Avatar asked Jul 03 '15 11:07

Kirataka


2 Answers

I'm not getting where to pass Intent for next activity i.e., activity_next.xml

To start Activity on reach of last item in ViewPager add OnPageChangeListener to ViewPager and in onPageSelected start next Activity:

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
     @Override
     public void onPageSelected(int position) 
     {     
            if(position==viewPager.getAdapter().getCount()){
                //start next Activity here with activity_next.xml layout
             }
     } 
     ....
});
like image 75
ρяσѕρєя K Avatar answered Oct 23 '22 03:10

ρяσѕρєя K


If you want to slide the last image to go to the next Activity, like a "dismiss" efect, you should try something like this:

private class ImagePagerAdapter extends PagerAdapter {
    private int[] mImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three,
        R.drawable.four,
        R.drawable.five
    };

@Override
public int getCount() {
  return mImages.length+1;
}

@Override
public boolean isViewFromObject(View view, Object object) {
   return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
   Context context = Illustrations.this;
   ImageView imageView = new ImageView(context);
   int padding = context.getResources().getDimensionPixelSize(
      R.dimen.padding_medium);
   imageView.setPadding(padding, padding, padding, padding);
   imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
if(position < getCount()-1)
   imageView.setImageResource(mImages[position]);
   ((ViewPager) container).addView(imageView, 0);
   return imageView;
}

and then:

public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
     ImagePagerAdapter adapter = new ImagePagerAdapter();
     viewPager.setAdapter(adapter);
     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) 
        {     
            if(position==viewPager.getAdapter().getCount()-1){
            //start next Activity
            }
        } 
     });  

}
like image 36
Sérgio Moura Avatar answered Oct 23 '22 04:10

Sérgio Moura