I was developing an android application to
a customer. One of the requirement was to share some media and text with the
installed social android apps like Google+, Email, Gmail, Facebook, Drive,
Messaging, Whatsapp etc. Also to share the application recommendation message
to these apps
In Android, you can share the text or the
media with the social apps installed in your device. We take the example of sharing a Predefined
text.
I am using our Panchanga app to
demonstrate it. In the application, I have added a menu item as “Share”. Here I
am going to show only the required code. You can modify or include the below
code according to your project
Creating Menu Item:
MenuItem share;
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
share = menu.add("Share");
share.setIcon(R.drawable.share);
share.setOnMenuItemClickListener(this);
return true;
}
Note: share.png icon is added to the
drawable folder. Now we will see how we can implement the
onMenuItemClickListener:
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem == share) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
return true;
}
Here the system picks all the social apps
installed in your device and show. You can select the required app to send.
Here “This is my text to send.” is the text I am sending to other apps. You can
add text or media as your requirement. Below are the images how it works:
If you want to share with only specific
app instead of giving the selecting option to user, you can do that by using setPackage
in your intent. Eg: If you want to share only with whatsapp, then:
sendIntent.setPackage("com.whatsapp");
“com.Whatsapp” is the package name of the
sharing app. You can use the required app there. That’s it. Happy Coding
No comments:
Post a Comment