129

I want my application user to be able to share/recommend my app to other users. I use the ACTION_SEND intent. I add plain text saying something along the lines of: install this cool application. But I can't find a way to enable users to directly go to the install screen of market place for instance. All I can provide them with is a web link or some text. In other words I am looking for a very direct way for android users to have my app installed.

Thanks for any help/pointers,

Thomas

14 Answers 14

321

This will let you choose from email, whatsapp or whatever.

try { 
    Intent shareIntent = new Intent(Intent.ACTION_SEND);  
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
    String shareMessage= "\nLet me recommend you this application\n\n";
    shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
    shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);  
    startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch(Exception e) { 
    //e.toString();
}   
9
  • 1
    Firebase share application. read more about firebase.google.com/docs/invites/android Commented Dec 9, 2017 at 1:12
  • Why is this wrapped in a try/catch block ?
    – Louis
    Commented Feb 20, 2018 at 9:03
  • 15
    Android loves to make so many changes in their updates that who knows if this code will fail in a future update
    – Ton
    Commented Feb 20, 2018 at 13:32
  • 1
    @Ton How can I send app icon image along with Subject and text.
    – Arbaz Alam
    Commented Apr 8, 2018 at 16:08
  • do it on background thread , it is somehow heavy for main thread. users won't feel the lag on background thread
    – Karan
    Commented Jun 10, 2018 at 10:02
48

You can use also ShareCompat class from support library.

ShareCompat.IntentBuilder.from(activity)
    .setType("text/plain")
    .setChooserTitle("Chooser title")
    .setText("http://play.google.com/store/apps/details?id=" + activity.getPackageName())
    .startChooser();

https://developer.android.com/reference/android/support/v4/app/ShareCompat.html

3
  • could we set language also? I know it would be simple as appending &hl tag to the url but just curious if it is available in library like setLang or something.
    – kiranking
    Commented Apr 8, 2019 at 8:13
  • 5
    Please always use https instead of http in links. Commented Aug 27, 2019 at 14:51
  • 1
    this is best answer but it is deprecated. removing .from will resolve it. Commented Apr 10, 2022 at 12:34
21

Thomas,

You would want to provide your users with a market:// link which will bring them directly to the details page of your app. The following is from developer.android.com:

Loading an application's Details page

In Android Market, every application has a Details page that provides an overview of the application for users. For example, the page includes a short description of the app and screen shots of it in use, if supplied by the developer, as well as feedback from users and information about the developer. The Details page also includes an "Install" button that lets the user trigger the download/purchase of the application.

If you want to refer the user to a specific application, your application can take the user directly to the application's Details page. To do so, your application sends an ACTION_VIEW Intent that includes a URI and query parameter in this format:

market://details?id=

In this case, the packagename parameter is target application's fully qualified package name, as declared in the package attribute of the manifest element in the application's manifest file. For example:

market://details?id=com.example.android.jetboy

Source: http://developer.android.com/guide/publishing/publishing.html

3
  • +1 for the link. That looks good. As I want the user to share I still need to use the ACTION_SEND and add the market link in my text. If the user clicks on it it shoudl redirect him to the install page. Sounds good I have to try it. Let me know if I understood you right.
    – Thomas
    Commented Feb 11, 2011 at 20:06
  • Yeah you can use the market link in the text of an ACTION_SEND, it will only work if clicked on while on an android device though.
    – Will Tate
    Commented Feb 11, 2011 at 20:56
  • 1
    but this link will not open the market link if we use ACTION_SEND, link will stay there as a simple text. but the link was helpful to open market WITH ACTION_VIEW. Commented Sep 10, 2011 at 10:47
15

Call this method:

public static void shareApp(Context context)
{
    final String appPackageName = context.getPackageName();
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out the App at: https://play.google.com/store/apps/details?id=" + appPackageName);
    sendIntent.setType("text/plain");
    context.startActivity(sendIntent);
}
12

To be more exact

   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.android.example"));
   startActivity(intent);

or if you want to share your other apps from your dev. account you can do something like this

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/developer?id=Your_Publisher_Name"));
startActivity(intent);
11

To automatically fill in the application name and application id you could use this:

int applicationNameId = context.getApplicationInfo().labelRes;
final String appPackageName = context.getPackageName();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, activity.getString(applicationNameId));
String text = "Install this cool application: ";
String link = "https://play.google.com/store/apps/details?id=" + appPackageName;
i.putExtra(Intent.EXTRA_TEXT, text + " " + link);
startActivity(Intent.createChooser(i, "Share link:"));
11

Share application with title is you app_name, content is your application link

fun shareApp(context: Context) {
    val appPackageName = BuildConfig.APPLICATION_ID
    val appName = context.getString(R.string.app_name)
    val shareBodyText = "https://play.google.com/store/apps/details?id=$appPackageName"

    val sendIntent = Intent(Intent.ACTION_SEND).apply {
        type = "text/plain"
        putExtra(Intent.EXTRA_TITLE, appName)
        putExtra(Intent.EXTRA_TEXT, shareBodyText)
    }
    context.startActivity(Intent.createChooser(sendIntent, null))
}
2
  • how to share app icon
    – John dahat
    Commented Aug 16, 2020 at 8:16
  • can I use "applicationContext" instead of passing context?
    – ikmazameti
    Commented Oct 24, 2022 at 17:00
6

I know this question has been answered, but I would like to share an alternate solution:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareSubText = "WhatsApp - The Great Chat App";
String shareBodyText = "https://play.google.com/store/apps/details?id=com.whatsapp&hl=en";
shareIntent.putExtra(Intent.EXTRA_SUBJECT, shareSubText);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareBodyText);
startActivity(Intent.createChooser(shareIntent, "Share With"));
3

finally, this code is worked for me to open the email client from android device. try this snippet.

Intent testIntent = new Intent(Intent.ACTION_VIEW);
                    Uri data = Uri.parse("mailto:?subject=" + "Feedback" + "&body=" + "Write Feedback here....." + "&to=" + "[email protected]");
                    testIntent.setData(data);
                    startActivity(testIntent);
2

Kotlin extension for share action. You can share whatever you want e.g. link

fun Context.share(text: String) =
    this.startActivity(Intent().apply {
        action = Intent.ACTION_SEND
        putExtra(Intent.EXTRA_TEXT, text)
        type = "text/plain"
    })

Usage

context.share("Check https://stackoverflow.com")
2

According to official docs in 2021 preferred way is

fun shareTextToOtherApps(message: String) {
        val sendIntent: Intent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_TEXT, message)
            type = "text/plain"
        }

        val shareIntent = Intent.createChooser(sendIntent, null)
        startActivity(shareIntent)
    }
1

Actually the best way to sheared you app between users , google (firebase) proved new technology Firebase Dynamic Link Through several lines you can make it this is documentation https://firebase.google.com/docs/dynamic-links/ and the code is

  Uri dynamicLinkUri = dynamicLink.getUri();
      Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLink(Uri.parse("https://www.google.jo/"))
            .setDynamicLinkDomain("rw4r7.app.goo.gl")
            .buildShortDynamicLink()
            .addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
                @Override
                public void onComplete(@NonNull Task<ShortDynamicLink> task) {
                    if (task.isSuccessful()) {
                        // Short link created
                        Uri shortLink = task.getResult().getShortLink();
                        Uri flowchartLink = task.getResult().getPreviewLink();
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_SEND);
                        intent.putExtra(Intent.EXTRA_TEXT,  shortLink.toString());
                        intent.setType("text/plain");
                        startActivity(intent);
                    } else {
                        // Error
                        // ...
                    }
                }
            });
1
try { 
Intent shareIntent = new Intent(Intent.ACTION_SEND);  
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Application name");
String shareMessage= "\n Your Message \n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);  
startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch(Exception e) { 
//e.toString();
}   
0

@Linh answer is almost good but causing a crash because of the missing FLAG_ACTIVITY_NEW_TASK, here is what worked for me

    public static void shareApp(Context context) {
    final String appPackageName = context.getPackageName();
    Intent sendIntent = new Intent();
    sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out the App at: https://play.google.com/store/apps/details?id=" + appPackageName);
    sendIntent.setType("text/plain");
    context.startActivity(sendIntent);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.