1

I have a viewpager which displays text in textview from List.but when copied text from current viewpager it either copies next or previous text. here is my mainactivity method code :-

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.copyid:

            TextView  msg= (TextView) viewPager.findViewById(R.id.textView);
            String msgs=msg.getText().toString();
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT,msgs);
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent,"Share Via"));
            break;
    }

and in viewpager adapter class:-

@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layout=inflater.inflate(R.layout.viewpager_layour,null);
    msg= (TextView) layout.findViewById(R.id.textView);
    msg.setText(list.get(position));
    ((ViewPager) container).addView(layout, 0);

    return layout;
}
Kris
  • 4,595
  • 7
  • 32
  • 50
AkashK
  • 201
  • 1
  • 4
  • 13

1 Answers1

0

If you can access the list in onOptionsItemSelected then you can use

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
       case R.id.copyid:

        int i = viewPager.getCurrentItem();
        String msgs= i>=0?list.get(i):"";
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT,msgs);
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent,"Share Via"));
        break;


}

It will be faster. In your code the problem is with the duplicate id of TextView . Have a look on this answer.

Birendra Singh
  • 842
  • 11
  • 19