Share function on Android

Share function on Android

Oct 06

How to use Intent.ACTION_SEND in your android application to quickly share information.

This is a great way of implementing a simple sharing function because it requires very little work from your end as a developer.

Firstly, put this method in your code somewhere.

public void share(String subject,String text) {
	Intent intent = new Intent(Intent.ACTION_SEND);
	intent.setType("text/plain");
	intent.putExtra(Intent.EXTRA_SUBJECT, subject);
	intent.putExtra(Intent.EXTRA_TEXT, text);
	startActivity(Intent.createChooser(intent, "Share"));
}

To use it, simply call the share method with the two arguments.

Note that for twitter and stuff, it only reads “EXTRA_TEXT” and ignores “EXTRA_SUBJECT” – but things like email don’t ignore “EXTRA_SUBJECT” for obvious reasons.

Leave a Reply