X

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

Most 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);
  waIntent.setType("text/plain");
  String text = "Insert Tweet Here";

  @SuppressWarnings("unused")
  PackageInfo info=pm.getPackageInfo("com.twitter.android", PackageManager.GET_META_DATA);
  //Check if package exists or not. If not then code 
  //in catch block will be called
  waIntent.setPackage("com.twitter.android");

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

 } catch (NameNotFoundException e) {
  Toast.makeText(this, "Twitter not Installed", Toast.LENGTH_SHORT)
  .show();
  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.

Comment Policy: Comments adding value to the article are encouraged. Relevant links will be allowed in such comments.
If you think that you have a link that adds value to this article please contact us at techie[at]techzog[dot]com for evaluation of inclusion into the article.
Comments left solely for spamming links will be deleted. Thank you for understanding.
Varun: Passionate blogger and main author at Techzog.com. Feel free to reach me at techie[at]techzog[dot]com

View Comments (1)

Related Post