How to activate Share button in android app?
Add a Button
and on click of the Button
add this code:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
Useful links:
For basic sharing
For customization
Adding a share button to share the app on social networks
Solution 1: Launch ACTION_SEND Intent
When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
# change the type of data you need to share,
# for image use "image/*"
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, URL_TO_SHARE);
startActivity(Intent.createChooser(intent, "Share"));
Solution 2: Use ShareActionProvider
If you are just looking to add a Share button in Overflow Menu, also have a look at ShareActionProvider.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share, menu);
MenuItem item = menu.findItem(R.id.share_item);
actionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
String shareText = URL_TO_SHARE;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(shareText).getIntent();
actionProvider.setShareIntent(shareIntent);
return true;
}
Hope this helps. :)
Related Topics
Android and Getting a View with Id Cast as a String
How to Update One Field from All Documents Using Pojo in Firestore
Android Intent for Sending Email with Attachment
Android Runtime Permissions- How to Implement
What Does '->' (Arrow) Mean in Gradle's Dependency Graph
Android How to Sort JSONarray of JSONobjects
Install Xlsx and Rjava on MACos Mavericks 10.9.5
Simple Calculator with Jsp/Servlet and Ajax
Why Should Wait() Always Be Called Inside a Loop
How to Persist Permission in Android API 19 (Kitkat)
Using Asynctask with Passing a Value
How to Move/Rename File from Internal App Storage to External Storage on Android
Recyclerview Painfully Slow to Load Cached Images Form Picasso
Android - How to Change Fragments in the Navigation Drawer