Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-pageradapter

Android Not able to share correct text from viewpager


I am trying to share the text that i am showing on viewpager on a button click but it's sending the text from the next page. Like on the first page of viewpager its showing '0' and when I am sharing it to whats app or sms, its sending the text '1' i.e next text from string array.
Here is my code.

SampleFragmant.java
public class SampleFragmant extends Fragment {
    ViewPager viewPager;
    SampleAdapter adapter;
    ImageView shareButton, saveButton;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.sample,
                container, false);

        viewPager = (ViewPager) rootView
                .findViewById(R.id.view_pager);
shareButton = (ImageView) rootView.findViewById(R.id.shareButton);       
shareButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
Intent sharingIntent = new Intent(
                        android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");

                String shareBody = adapter.textposition;
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                        "Test Application:");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                        shareBody);
                startActivity(Intent.createChooser(sharingIntent, "Share via"));

            }
        });

        adapter = new SampleAdapter(getActivity());

        viewPager.setAdapter(adapter);
        return rootView;
    }
}

SampleAdapter.java

public class SampleAdapter extends PagerAdapter {
    Context context;
    public String textposition;
    private String[] textToDisplay = new String[] {
            "0","1","2","3","4","5","6"};
@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return textToDisplay.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        // TODO Auto-generated method stub
        return view == ((TextView) object);
    }
@Override
    public Object instantiateItem(View container, int position) {
        // TODO Auto-generated method stub

        TextView textView = new TextView(context);
        textView.setText(textToDisplay[position]);
         textposition= textToDisplay[position];
        textView.setTextSize(16);
        textView.setTypeface(null, Typeface.BOLD_ITALIC);
        textView.setGravity(Gravity.CENTER);
((ViewPager) container).addView(textView, 0);
        return textView;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((TextView) object);
    }
}

Solution

  • By default ViewPager creates several views at once. For instance, if a view with position 5 is shown to user, it will create views with positions 4, 5 and 6. This is needed for smooth transition animations and controlled by setOffscreenPageLimit(int limit) (by default limit is 1). So, it will instantiate views for these positions by invoking instantiateItem method. And that's why it's incorrect to assume that the position you get in instantiateItem is the position of a view that is shown to user.

    Instead of that you should use onPageSelected(int position) of ViewPager.OnPageChangeListener to determine a position of current view.