From this Blog, you can easily find out the source code for how to enable sharing content in your android app. Android provides a rooted Intent called ACTION_SEND for this reason. Let’s start code:
1. activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<Button
android:layout_width=”100dp”
android:layout_height=”wrap_content”
android:text=”Share”
android:textStyle=”bold”
android:id=”@+id/button”
android:layout_centerVertical=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>
2. MainActivity.java
package(….)
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button share=(Button)findViewById(R.id.button);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType(“text/plain”);
sharingIntent.putExtra(Intent.EXTRA_TEXT,”extra text that you want to put”);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, “Subject test”);
startActivity(Intent.createChooser(sharingIntent, “Share via”));
}
});
}
}
3. AndroidManifest.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”your package name”>
<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>