How to activate "Share" button in android app? How to activate "Share" button in android app? xml xml

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


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.


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 :"))