How to Share content with other apps.


How to Share Entire Android App and content with Share Intent. To add the Facebook,Whatsup, Gmail, Message etc.

How to create apps that share data between apps. Sending Simple Data to Other Apps. Interact with other apps by exchanging data with them.


Create New Project

1. First create a new project in Android Studio (File => New Project)
2. Add Internet Permission.

AndroidManifest.xml

									   
   <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.webview.appsshare">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Design App share

Open activity_main and Add below code.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="5dp"
    tools:context="com.webview.appsshare.MainActivity">

    <TextView
        android:id="@+id/facebook"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight=".2"
        android:drawableTop="@drawable/facebook"
        android:gravity="center"
        android:padding="5dp"
        android:text="Facebook" />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_below="@+id/facebook"
        android:layout_weight=".2"
        android:drawableTop="@drawable/readmessage"
        android:gravity="center"
        android:padding="5dp"
        android:text="Message" />

    <TextView
        android:id="@+id/mail"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignParentRight="true"
        android:layout_weight=".2"
        android:drawableTop="@drawable/mail"
        android:gravity="center"
        android:padding="5dp"
        android:text="Mail" />

    <TextView
        android:id="@+id/whatsup"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/mail"
        android:layout_weight=".2"
        android:drawableTop="@drawable/whatsapp"
        android:gravity="center"
        android:padding="5dp"
        android:text="Whatsup" />

    <TextView
        android:id="@+id/share"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_centerInParent="true"
        android:layout_weight=".2"
        android:drawableTop="@drawable/share"
        android:gravity="center"
        android:padding="5dp"
        android:text="Share" />

</LinearLayout>
	

Following code for share with mail.

MainActivity.java

									   
    private void mail() {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setClassName("com.google.android.gm", 
		"com.google.android.gm.ConversationListActivity");
        intent.putExtra(Intent.EXTRA_EMAIL, "");
        intent.setData(Uri.parse("mailto:" + ""));
        intent.putExtra(Intent.EXTRA_SUBJECT, "your App");
        intent.putExtra(Intent.EXTRA_TEXT, "Hey, 
		Install it from the play store here: https://play.google.com");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
        try {

            startActivity(intent);
        } catch (android.content.ActivityNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Intent intent2 = new Intent(Intent.ACTION_SENDTO);
            intent2.setType("message/rfc822");
            intent2.putExtra(Intent.EXTRA_EMAIL, "");
            intent2.setData(Uri.parse("mailto:" + ""));
            intent2.putExtra(Intent.EXTRA_SUBJECT, "your App");
            intent2.putExtra(Intent.EXTRA_TEXT, "Hey, 
			Install it from the play store here: https://play.google.com" +
                    "");
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent2.addFlags(Intent.FLAG_FROM_BACKGROUND);
            Log.d("Email error:", e.toString());
            startActivity(intent2);
        }
    }

Code for Share with Whatsup.

MainActivity.java

									   
    private void send_whatsapp() {
        PackageManager pm = getPackageManager();
        try {
            Intent whatintent = new Intent(Intent.ACTION_SEND);
            whatintent.setType("text/plain");
            String text = "";
            PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
            whatintent.setPackage("com.whatsapp");
            whatintent.putExtra(Intent.EXTRA_TEXT, text);
            startActivity(Intent.createChooser(whatintent, "Share with"));

        } catch (Exception e) {
            Toast.makeText(context, "Whatsapp not installed", Toast.LENGTH_SHORT).show();
        }
    }

Code for Share with Facebook.

MainActivity.java

									   
    private void postOnFB() {
        String urlToShare = "";
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra("android.intent.extra.TEXT", urlToShare);
        intent.putExtra(Intent.EXTRA_TITLE, "NICEE");

// See if official Facebook app is found
        boolean facebookAppFound = false;
        List matches = context.getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo info : matches) {
            if (info.activityInfo.packageName.toLowerCase().
			startsWith("com.facebook.katana")) {
                intent.setPackage(info.activityInfo.packageName);
                facebookAppFound = true;
                break;
            }
        }

        // As fallback, launch sharer.php in a browser
        if (!facebookAppFound) {
            String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" +
			urlToShare;
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
//            Toast.makeText(getActivity(), "Not found", Toast.LENGTH_SHORT).show();
        }

        startActivity(intent);

    }

Code for Share with Message.

MainActivity.java

									   
  private void message() {
        try {
            Intent smsintent = new Intent(Intent.ACTION_SENDTO);
            smsintent.putExtra("sms_body", "Hey, your message");
            smsintent.setData(Uri.parse("smsto:" + ""));
            startActivity(smsintent);
        } catch (Exception e) {
            Toast.makeText(context, "No Messanger Found.", Toast.LENGTH_SHORT).show();

        }
    }

Code for Share with Other Apps.

MainActivity.java

									   
    private void share() {

        try {
            Toast.makeText(context, "You in Share", Toast.LENGTH_SHORT).show();

            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, "your content");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));

        } catch (Exception e) {

            Toast.makeText(context, "Nothing to Share ", Toast.LENGTH_SHORT).show();
        }
    }

Full code with share with Mail, Whatsup, Message, Facebook, And Other apss .

MainActivity.java

									   
package com.webview.appsshare;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    Context context;
    TextView facebook, mail, message, share, whatsup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        facebook = (TextView) findViewById(R.id.facebook);
        message = (TextView) findViewById(R.id.message);
        share = (TextView) findViewById(R.id.share);
        whatsup = (TextView) findViewById(R.id.whatsup);
        mail = (TextView) findViewById(R.id.mail);
        facebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postOnFB();
            }
        });
        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                message();
            }
        });
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                share();
            }
        });
        whatsup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_whatsapp();
            }
        });
        mail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mail();
            }
        });
    }

    private void mail() {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setClassName("com.google.android.gm", 
		 "com.google.android.gm.ConversationListActivity");
        intent.putExtra(Intent.EXTRA_EMAIL, "");
        intent.setData(Uri.parse("mailto:" + ""));
        intent.putExtra(Intent.EXTRA_SUBJECT, "your App");
        intent.putExtra(Intent.EXTRA_TEXT, 
	"Hey, Install it from the play store here: https://play.google.com");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
        try {

            startActivity(intent);
        } catch (android.content.ActivityNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Intent intent2 = new Intent(Intent.ACTION_SENDTO);
            intent2.setType("message/rfc822");
            intent2.putExtra(Intent.EXTRA_EMAIL, "");
            intent2.setData(Uri.parse("mailto:" + ""));
            intent2.putExtra(Intent.EXTRA_SUBJECT, "your App");
            intent2.putExtra(Intent.EXTRA_TEXT, 
			"Hey, Install it from the play store here: https://play.google.com" +"");
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent2.addFlags(Intent.FLAG_FROM_BACKGROUND);
            Log.d("Email error:", e.toString());
            startActivity(intent2);
        }
    }

    private void send_whatsapp() {
        PackageManager pm = getPackageManager();
        try {
            Intent whatintent = new Intent(Intent.ACTION_SEND);
            whatintent.setType("text/plain");
            String text = "";
            PackageInfo info = pm.getPackageInfo("com.whatsapp", 
			PackageManager.GET_META_DATA);
            whatintent.setPackage("com.whatsapp");
            whatintent.putExtra(Intent.EXTRA_TEXT, text);
            startActivity(Intent.createChooser(whatintent, "Share with"));

        } catch (Exception e) {
            Toast.makeText(context, "Whatsapp not installed", Toast.LENGTH_SHORT).show();
        }
    }

    private void postOnFB() {
        String urlToShare = "";
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra("android.intent.extra.TEXT", urlToShare);
        intent.putExtra(Intent.EXTRA_TITLE, "NICEE");

// See if official Facebook app is found
        boolean facebookAppFound = false;
        List matches = 
		context.getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo info : matches) {
            if (info.activityInfo.packageName.toLowerCase().
			startsWith("com.facebook.katana")) {
                intent.setPackage(info.activityInfo.packageName);
                facebookAppFound = true;
                break;
            }
        }

        // As fallback, launch sharer.php in a browser
        if (!facebookAppFound) {
            String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" 
			+ urlToShare;
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
//            Toast.makeText(getActivity(), "Not found", Toast.LENGTH_SHORT).show();
        }

        startActivity(intent);

    }

    private void message() {
        try {
            Intent smsintent = new Intent(Intent.ACTION_SENDTO);
            smsintent.putExtra("sms_body", "Hey, your message");
            smsintent.setData(Uri.parse("smsto:" + ""));
            startActivity(smsintent);
        } catch (Exception e) {
            Toast.makeText(context, "No Messanger Found.", Toast.LENGTH_SHORT).show();
        }
    }

    private void share() {

        try {
            Toast.makeText(context, "You in Share", Toast.LENGTH_SHORT).show();

            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, "your content");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));

        } catch (Exception e) {

            Toast.makeText(context, "Nothing to Share ", Toast.LENGTH_SHORT).show();
        }
    }
}

Output :


Tags

  • Android

  • Android Studio

  • Android development

  • Java