Android ACTION_SEND event Example with Source Codes

In this article, we are going to learn use of Android - ACTION_SEND event. This is to send data from home activity to another and also from current activity to the outside of the app.
Submitted by Shamikh Faraz, on January 26, 2018

ACTION_SEND is an event in Android which is used to send data to other applications; it can be used to send text content, binary content, multiple pieces of content etc.

Read more: Sending Simple Data to Other Apps

1) XML code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.faraz.shareappdata_example.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="home activity"
        tools:layout_editor_absoluteX="154dp"
        tools:layout_editor_absoluteY="205dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Share App Button"
        tools:layout_editor_absoluteX="142dp"
        tools:layout_editor_absoluteY="298dp" />

</android.support.constraint.ConstraintLayout>

2) Java file: (MainActivity.java)

package com.example.faraz.shareappdata_example;

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 button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i =   new Intent(android.content.Intent.ACTION_SEND);
                i.setType("your text here");
                i.putExtra(Intent.EXTRA_SUBJECT,"Insert intent here");

                String app_url = " Insert URL for the app you want to open";
                i.putExtra(android.content.Intent.EXTRA_TEXT,app_url);
                startActivity(Intent.createChooser(i, "Share your app using"));
            }
        });
    }
}

Output

After click on Share App Button you will get different option like email, Bluetooth, shareit, drive etc to share your app

Android - ACTION_SEND Code Output

Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.