‘Share to Twitter’ Code for Android (Without SDK)

Share to Twitter AndroidMost applications provide sharing to Twitter using the generic ‘Share’ intent.  Some others may want to use the Twitter SDK but that increases the application install size. Most developers may want to make the user directly share the content to Twitter without showing the App Chooser.

Here’s a quick code snippet for Android developers who want to directly integrate Share on Twitter functionality on their Android app. Create a button and set its onClick attribute to the onClickWhatsApp function below.

void shareOnTwitter()
	PackageManager pm=getPackageManager();
	try {
		Intent waIntent = new Intent(Intent.ACTION_SEND);
		String text = "Insert Tweet Here";

		PackageInfo info=pm.getPackageInfo("", PackageManager.GET_META_DATA);
		//Check if package exists or not. If not then code 
		//in catch block will be called

		waIntent.putExtra(Intent.EXTRA_TEXT, text);
		startActivity(Intent.createChooser(waIntent, "Share with"));

	} catch (NameNotFoundException e) {
		Toast.makeText(this, "Twitter not Installed", Toast.LENGTH_SHORT)
		return ;
	return ;	

If you added Twitter Sharing functionality to your app, you may also want to Add WhatsApp sharing to your Android application.

As an Android developer, you might also want to check out our free GCM Notifications Test Tool and bookmark How to Recover Lost Android Keystore Password.

Leave us a quick comment or a thank you note if you found the tip to incorporate Twitter sharing functionality in your Android application useful.

One thought on “‘Share to Twitter’ Code for Android (Without SDK)

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge