116

i want to add "Share" button to my android app.

Like that

:

I added "Share" button, but button is not active. I click, but nothing happens.

My code in MainActivity.java:

private ShareActionProvider mShareActionProvider;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.share_menu, menu);
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = menu.findItem(R.id.share_menu);
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share_menu).getActionProvider();
    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return true;
}

{
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
    startActivity(Intent.createChooser(sharingIntent, "Share using"));
}

I want to share text in my first tab (first_tab.xml) or second tab (second_tab.xml).

Code in tab (xml) (If need):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment" >

<TextView
    android:id="@+id/section_label1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/text"
    android:textColor="@color/text_color" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/sprite" />

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
Personal Jesus
  • 1,289
  • 3
  • 11
  • 14

5 Answers5

316

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

jesterjunk
  • 2,342
  • 22
  • 18
Basavaraj Hampali
  • 3,905
  • 1
  • 19
  • 22
  • Add the button where? I already created a menu item with the `share` icon in my Action Bar – Si8 Nov 08 '13 at 19:22
  • Hello, In above method it seems to display multiple applications. I want to know which application used for sharing and after sharing complete i have to call one API. Is to possible to check which application used and also how to call API after sharing? Thank you... – patel135 Jun 06 '16 at 06:00
  • It has been copied from https://code.tutsplus.com/tutorials/android-sdk-implement-a-share-intent--mobile-8433. – CoolMind Sep 13 '18 at 11:02
  • Works fine to me except to Facebook. It does not show anything there, unfortunately. – Vaggelis Manousakis Dec 02 '18 at 23:30
  • how to add an image ? can u please suggest me ?? – Tasnuva Tavasum oshin Apr 29 '20 at 19:56
16

Create a button with an id share and add the following code snippet.

share.setOnClickListener(new View.OnClickListener() {             
    @Override
    public void onClick(View v) {

        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = "Your body here";
        String shareSub = "Your subject here";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        startActivity(Intent.createChooser(sharingIntent, "Share using"));
    }
});

The above code snippet will open the share chooser on share button click action. However, note...The share code snippet might not output very good results using emulator. For actual results, run the code snippet on android device to get the real results.

Daniel Nyamasyo
  • 2,152
  • 1
  • 24
  • 23
6

in kotlin :

val sharingIntent = Intent(android.content.Intent.ACTION_SEND)
sharingIntent.type = "text/plain"
val shareBody = "Application Link : https://play.google.com/store/apps/details?id=${App.context.getPackageName()}"
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "App link")
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody)
startActivity(Intent.createChooser(sharingIntent, "Share App Link Via :"))
mhKarami
  • 844
  • 11
  • 16
1

Share Any File as below ( Kotlin ) :
first create a folder named xml in the res folder and create a new XML Resource File named provider_paths.xml and put the below code inside it :

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path
        name="files"
        path="."/>

    <external-path
        name="external_files"
        path="."/>
</paths>

now go to the manifests folder and open the AndroidManifest.xml and then put the below code inside the <application> tag :

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/provider_paths" /> // provider_paths.xml file path in this example
</provider>

now you put the below code in the setOnLongClickListener :

share_btn.setOnClickListener {
    try {
        val file = File("pathOfFile")
        if(file.exists()) {
            val uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file)
            val intent = Intent(Intent.ACTION_SEND)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setType("*/*")
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent)
        }
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
        toast("Error")
    }
}
Ramin eghbalian
  • 2,348
  • 1
  • 16
  • 36
1

Kotlin

Inside the click listener needed to add this module for sharing text via applications like whatsApp, email, Slack..

shareOptionClicked.setOnClickListener{
     val shareData = Intent(Intent.ACTION_SEND)
     shareData.type = "text/plain"
     val dataToShare = "Text from my application"
     shareData.putExtra(Intent.EXTRA_SUBJECT, "Subject from my application")
     shareData.putExtra(Intent.EXTRA_TEXT, dataToShare)    
     startActivity(Intent.createChooser(shareData, "Share Via"))
     }
Rohit S
  • 714
  • 5
  • 7