‘Share to WhatsApp’ Code for Android Developers

whatsapp

Most applications provide sharing to WhatsApp using the generic ‘Share’ intent. However, you may want to make the user directly share the content to WhatsApp without showing the App Chooser. Here’s a quick Java snippet for Android developers who want to directly integrate Share on WhatsApp functionality on their Android app. Create a button and set its onClick attribute to the onClickWhatsApp function below.

public void onClickWhatsApp(View view) {

    PackageManager pm=getPackageManager();
    try {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "YOUR TEXT HERE";

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

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

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

Credit: http://stackoverflow.com/questions/15462874/sending-message-through-whatsapp

If you added Twitter Sharing functionality to your app, you may also want to Add Twitter 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 WhatsApp 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.

One thought on “‘Share to WhatsApp’ Code for Android Developers

Leave a Reply

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

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.