You can use the following snippets: For WhatsApp:
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(); } }
For Twitter:
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 ; }
Varun source share