Open In App

How to Share Text of Your App with Another App and Vice Versa in Android?

Last Updated : 29 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Most of the time while using an app we want to share text from the app to another app. While using Many Social Media Platforms we find this feature to be very useful when we want to share information from one app to another. The Android intent resolver is used when sending data to another app as part of a well-defined task flow. To use the Android intent resolver, create an intent and add extras. Here we are going to understand how to do that. Also, we are going to implement the vice versa (Another App to Your App) case in this article.

Step by Step Implementation

Step 1: Create a new Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.  

Step 2: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--Here we will be input out=r text share-->
    <EditText
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="write Something here to share"
        android:textColor="#000"
        android:textSize="22sp" />
 
    <!--We will click on it then shareonlytext
        function will be called-->
    <Button
        android:id="@+id/share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@color/black"
        android:padding="5dp"
        android:text="Share"
        android:textSize="10dp" />
     
</LinearLayout>


Step 3: Working with the MainActivity.java file 

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail. 

Java




import androidx.appcompat.app.AppCompatActivity;
 
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends AppCompatActivity {
 
    Button share;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        share = findViewById(R.id.share);
         
        // initialising text field where we will enter data
        final EditText editText = findViewById(R.id.text);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Now share text only function will be called
                // here we  will be passing the text to share
                shareTextOnly(editText.getText().toString());
            }
        });
    }
 
    private void shareTextOnly(String titlee) {
        String sharebody = titlee;
         
        // The value which we will sending through data via
        // other applications is defined
        // via the Intent.ACTION_SEND
        Intent intentt = new Intent(Intent.ACTION_SEND);
         
        // setting type of data shared as text
        intentt.setType("text/plain");
        intentt.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
         
        // Adding the text to share using putExtra
        intentt.putExtra(Intent.EXTRA_TEXT, sharebody);
        startActivity(Intent.createChooser(intentt, "Share Via"));
    }
}


Output:

Vice Versa Case

Let’s think that we are developing a chat App. In that case, we may want to share text from another app with some users of our app. Here Basically what we are doing is taking text from google and sharing that in our app and entering that text in edit text. In that case, we need to write some code to implement this feature in our app (Share text from Another App to our App).

Step by Step Implementation

Step 1: Create a new Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.  

Step 2: Working with the AndroidManifest.xml file

For adding data to Firebase we should have to give permissions for accessing the internet. For adding these permissions navigate to the app > AndroidManifest.xml and Inside that file add the below permissions to it.  

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

Also, make the following changes in your manifest file. The complete AndroidManifest.xml file is given below.

XML




<?xml version="1.0" encoding="utf-8"?>
    package="com.example.sharetext">
 
    <uses-permission android:name="android.permission.INTERNET" />
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ShareText">
        <activity android:name=".MainActivity">
 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
 
                <category android:name="android.intent.category.DEFAULT" />
 
                <data android:mimeType="image/*" />
            </intent-filter>
 
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
 
                <category android:name="android.intent.category.DEFAULT" />
 
                <data android:mimeType="text/plain" />
            </intent-filter>
 
        </activity>
    </application>
 
</manifest>


Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">
 
    <EditText
        android:id="@+id/gettext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Get text"
        android:textSize="22sp"
        android:textStyle="bold" />
 
</LinearLayout>


Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    EditText gettext;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gettext = findViewById(R.id.gettext);
         
        // initiating the intent
        Intent intent = getIntent();
        String action = intent.getAction();
         
        // getting type of content shared
        String type = intent.getType();
         
        // if value is not null then show the content
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            handlesendText(intent);
        }
    }
 
    private void handlesendText(Intent intent) {
        // here we are getting the text
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            // showing the text in edittext
            gettext.setText(sharedText);
        }
    }
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads