0

Problem: I am building an android application where I am using viewpager method to swipe the screens. I need to display the toast message with respect to each screen, for example "msg1" with screen 1, "msg2" with screen 2 and so on. But with my code as below, nothing is displayed: code:

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.login_screen_1, R.drawable.login_screen_2};
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {

    Toast.makeText(getApplicationContext(), position, Toast.LENGTH_LONG).show();

}

@Override
public void onPageScrollStateChanged(int state) {

}

private class ImagePagerAdapter extends PagerAdapter {
    private int[] mImages = new int[]{R.drawable.login_screen_1, R.drawable.login_screen_2};

    @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 = FaceBook.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.FIT_XY);
        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);
    }
}

QUESTION 2

In my app, I am calling an async task on a button click and I am using onpreexecute method to start "progress dialog" and onpostexecute to end the same.

I am getting this error while implementing the above - "View not attached to window manager." Here is my async code -

 /**
 * Async task class to get json by making HTTP call
 */
private class questionfeed_async extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Hi "+userName+ "! Loading your question feed ");
        pDialog.show();

    }


    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub


    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
       if (pDialog.isShowing()) {
           pDialog.dismiss();
       }


    }

1 Answers1

1

Have your Activity or Fragment implement the OnPageChangeListener interface.

Define the onPageSelected() method like this:

@Override
public abstract void onPageSelected (int position){
    Toast.makeText(getActivity(),"We are in page " + position, 
            Toast.LENGTH_SHORT).show();
}

And don't forget to call

viewPager.setOnPageChangeListener(this);
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • I have posted one more question. it would be very helpful if you help me in that..? – Nikhil Agrawal Feb 28 '15 at 22:04
  • is it a separate question ? post a link to it here – Yash Sampat Feb 28 '15 at 22:05
  • sorry i am unable to post the question right now. could you please help me here only. this might work – Nikhil Agrawal Feb 28 '15 at 22:15
  • I request you to post a separate question, then send me a comment with a link, it will be easier for me that way :) – Yash Sampat Feb 28 '15 at 22:17
  • as you know the rules of SOF. i can only post another question after 3 hrs. but its urgent for me. – Nikhil Agrawal Feb 28 '15 at 22:19
  • ok sure ... ask we'll probably have to start the chatroom in a bit – Yash Sampat Feb 28 '15 at 22:19
  • the reason is this line of code: `pDialog = new ProgressDialog(getActivity());`. Here you are holding a strong reference to the `Activity`, and a configuration change happens before the `AsyncTask` ends – Yash Sampat Feb 28 '15 at 22:29
  • see [this](http://stackoverflow.com/questions/2224676/android-view-not-attached-to-window-manager) and [this](http://stackoverflow.com/questions/22924825/view-not-attached-to-window-manager-crash) ... its the same problem with its solution – Yash Sampat Feb 28 '15 at 22:32
  • can you please tell me what is the meaning of mcontext in your that you provided with..?? – Nikhil Agrawal Feb 28 '15 at 22:59
  • `mContext` is an instance of the `Context` class. In your case it needs be a reference to the `Activity`, by passing `Activity.this`. The point is that you need to find another way to display a dialog. There are two methods in `AsyncTask` called `onProgressUpdate()` and `publishProgress` that you can try using ... just do some research and find a tutorial on it – Yash Sampat Feb 28 '15 at 23:04
  • i used both of the links that you suggested. but still then my problem is not solved. it would be very helpful if you suggest some links for me..!\ – Nikhil Agrawal Feb 28 '15 at 23:12
  • @NikhilAgrawal: I request you to post a separate question whenever you can and then add a comment here with the link ... I will be busy for the next hour or so, but I will be happy to help you when I am free :) – Yash Sampat Feb 28 '15 at 23:14
  • @ZygoteInit I am also facing this same issue here is the link - http://stackoverflow.com/questions/28789783/how-to-solve-the-error-view-not-attached-to-window-manager-in-android. It will be very helpful If you reply to that question. – Hitesh Matnani Mar 01 '15 at 03:33
  • @HiteshMatnani: will take a look at this question soon ... just add a comment here to remind me if I forget – Yash Sampat Mar 01 '15 at 09:21
  • Thanks that problem is solved. It will we great if you help me other problem where I am stuck – Hitesh Matnani Mar 01 '15 at 09:39
  • @ZygoteInit could please help me to solve this problem - http://stackoverflow.com/questions/28793627/how-to-add-image-to-a-framelayout-in-android-app – Nikhil Agrawal Mar 01 '15 at 12:41