Share App Data Android

Android Share App Data (ACTION_SEND)

The ACTION_SEND is an event of android.content.Intent class. This event is used in Android to send data from one activity to another and also from the current activity to outside the application. The data to be shared and its type is necessary to be defined by the Intent class. The URL of the built-in Browser app is sent by the ACTION_SEND action. The createChooser() method is called by the Intent while sharing the data to take the Intent object and to specify the title of the chooser dialog. To display the chooser, the Intent.createChooser() method is used.

Example of ACTION_SEND:

In the below example, we are demonstrating the usage of ACTION_SEND to share a plain text which is a URL of the browser.

activity_main.xml:

<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"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Good Morning!!"
        android:id="@+id/textView" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share"
        android:id="@+id/button"
        android:layout_marginBottom="95dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
 
</RelativeLayout>

Activity class:(File: MainActivity.java)

package com.example.radioapp;
 
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 sharebutton=findViewById(R.id.button);
        sharebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent shareIntent =   new Intent(android.content.Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Insert Subject here");
                String app_url = " https://play.google.com/store/apps/details?id=my.example";
                shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,app_url);
                startActivity(Intent.createChooser(shareIntent, "Share via"));
            }
        });
    }
}

Output 1:

Output 2: