How to share ViewPager String or Text via SMS
How to share ViewPager String or Text via SMS
I design a android application. I face a problem. I show string in listview and when user click listview, string show in ViewPager. I want to share my string or text via sms. When I click share button string always share in first click position text. I swipe my viewpager page, it change current position text but not change shareable text.
ListView on Main_Activity:
public class MainActivity extends FragmentActivity
PagerFragment pagerFragment;
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<PageData> data = new ArrayList<PageData>();
data.add(new PageData("1", R.drawable.store, "Picture 1 jsdj kjdkjk kjk kalskfj sk ksjfksj kjfksjfk "));
data.add(new PageData("2", R.drawable.home, "Picture 2"));
data.add(new PageData("3", R.drawable.help, "Picture 3"));
data.add(new PageData("4", R.drawable.ic_launcher_background, "Picture 4"));
final ListView lv = (ListView) findViewById(R.id.list_view);
final ListViewAdapter lva = new ListViewAdapter(this, R.layout.item_list, data);
lv.setAdapter(lva);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
final String Category = "Hello Nasir";
final int positionview = lv.getPositionForView(view);
PagerFragment pagerFragment = new PagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("CURRENT_POSITION", positionview);
bundle.putParcelableArrayList("DATA_LIST", data);
bundle.putString("Category", Category);
pagerFragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, pagerFragment, "swipe_view_fragment").commit();
);
@Override
public void onBackPressed()
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentByTag("swipe_view_fragment");
if (f != null)
fm.beginTransaction().remove(f).commit();
else
super.onBackPressed();
ViewPager :
public class PagerFragment extends Fragment
private List<PageData> data;
private int currentPosition;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_pager, container, false);
final ViewPager mViewPager = v.findViewById(R.id.pager_view);
currentPosition = getArguments().getInt("CURRENT_POSITION");
data = getArguments().getParcelableArrayList("DATA_LIST");
ImageButton share = v.findViewById(R.id.share);
share.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareString = data.get(currentPosition).getTitle();
// String shareString = mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1).;
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);
startActivity(Intent.createChooser(sharingIntent, "Share Application Via"));
);
FragmentItemPagerAdapter fragmentItemPagerAdapter = new FragmentItemPagerAdapter(getFragmentManager(), data);
mViewPager.setAdapter(fragmentItemPagerAdapter);
mViewPager.setCurrentItem(currentPosition);
return v;
ViewPagerAdapter:
public class FragmentItemPagerAdapter extends FragmentStatePagerAdapter
private List<PageData> data;
public FragmentItemPagerAdapter(FragmentManager fm, List<PageData> data)
super(fm);
this.data = data;
@Override
public Fragment getItem(int position)
Fragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(PageFragment.COUNT, data.get(position).getCount());
args.putString(PageFragment.TITLE, data.get(position).getTitle());
args.putInt(PageFragment.PICTURE, data.get(position).getPicture());
fragment.setArguments(args);
return fragment;
@Override
public int getCount()
return data.size();
public static class PageFragment extends Fragment
public static final String COUNT = "count";
public static final String TITLE = "title";
public static final String PICTURE = "picture";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_item, container, false);
TextView title = rootView.findViewById(R.id.item_label);
title.setText(getArguments().getString(TITLE));
ImageView Picture = rootView.findViewById(R.id.image_view);
Picture.setImageResource(getArguments().getInt(PICTURE));
TextView count = rootView.findViewById(R.id.Nasir);
count.setText("SMS : " + getArguments().getString(COUNT) + " of 20");
return rootView;
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy