How To Add The Share Option In Android Applications

Introduction

 
Android is one of the most popular operating systems for mobile. Sharing is an important thing in our lives especially technology and knowledge sharing. If you create a photo editing app, it will share the photos on social media apps after editing. Functionality is the most important thing in Android development. So, I will show you how to add the "Share" option in Android applications using Android Studio. Android is a kernel-based operating system that allows users to modify the GUI components and source code.
 
Prerequisites

The Process

 
Carefully follow my steps to create the "Share" option in your Android application using Android Studio. I have included the source code too.
 
Step 1
 
Open the Android Studio and start a new project.
 
android
 
Step 2
 
Put the application name and company domain. If you wish to use C++ for coding the project, mark the "Include C++ support" and then click Next.
 
Step 3
 
Select the Android Minimum SDK. After you chose the minimum SDK, it will show the approximate percentage of people using that SDK. Just click Next.
 
android
 
Step 4
 
Choose the basic activity then click Next.
 
android
 
Step 5
 
Put activity name and layout name. Android Studio basically takes Java class name to what you provide the activity name. Click Finish.
 
android
 
Step 6
 
First, you need to design the application. So, go to activity_main.xml and click the text button. This XML file contains the designing code for the Android app. Into the activity_main.xml, copy and paste the below code.
 
Activity_main.xml code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     android:orientation="vertical"  
  11.     tools:context="ganeshannt.sharing.MainActivity">  
  12.   
  13.     <EditText  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:hint="Enter the text that you want to share!"  
  17.         android:textSize="20sp"  
  18.         android:id="@+id/editText"/>  
  19.     <Button  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_margin="80dp"  
  23.         android:text="Share"  
  24.         android:onClick="Clicked"  
  25.         android:layout_gravity="center_horizontal"/>  
  26. </LinearLayout>   
The above code is used to create the edit text row and button.
 
android
 
Step 7
 
Into the MainActivity.java file, copy and paste the below code. Java programming is the back-end language for Android. Do not replace your package name, otherwise, this app will not run.
 
MainActivity.java code
  1. package ganeshannt.sharing;  
  2.   
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.view.View;  
  7. import android.widget.EditText;  
  8.   
  9. public class MainActivity extends AppCompatActivity {  
  10.     private EditText editText;  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         editText = (EditText)findViewById(R.id.editText);  
  18.   
  19.     }  
  20.   
  21.     public void Clicked(View view)  
  22.     {  
  23.        /* ACTION_SEND: Deliver some data to someone else. 
  24.         createChooser (Intent target, CharSequence title): Here, target- The Intent that the user will be selecting an activity to perform. 
  25.             title- Optional title that will be displayed in the chooser. 
  26.         Intent.EXTRA_TEXT: A constant CharSequence that is associated with the Intent, used with ACTION_SEND to supply the literal data to be sent. 
  27.         */  
  28.         Intent sendIntent = new Intent();  
  29.         sendIntent.setAction(Intent.ACTION_SEND);  
  30.         sendIntent.putExtra(Intent.EXTRA_TEXT,editText.getText().toString());  
  31.         sendIntent.setType("text/plain");  
  32.         Intent.createChooser(sendIntent,"Share via");  
  33.         startActivity(sendIntent);  
  34.     }  
  35. }   
Step 8
 
Create a new dimens.xml file into the values folder (File ⇒ New ⇒Activity⇒values resource file).
 
Go to dimens.xml and click the text bottom. This XML file contains the designing code for the Android app. Into the dimens.xml, copy and paste the below code.
 
dimens.xml code
  1. <resources>  
  2.     <!-- Default screen margins, per the Android Design guidelines. -->  
  3.     <dimen name="activity_horizontal_margin">16dp</dimen>  
  4.     <dimen name="activity_vertical_margin">16dp</dimen>  
  5. </resources>   
Step 9
 
This is our user interface of the application. Click the "Make Project" option.
 
android
 
Step 10
 
Run the application, choose the desired Virtual Machine, and click OK.
 
android
 
Deliverables
 
Here, we have successfully created a share option in the Android application.
 
Compiler: HTC DESIRE 820S, API 21 (lollipop version)
Enter text message into the Edit text box.
 
android
 
Click the "Share" button.
 
android
 
It will show some shareable applications.
 
android
 
Let's choose to share the messages application.
 
android
 
This will open the message application, and it shows my sharing content. Now, choose the contact number to share the message.
 
android
 
Once more, I want to share the content with another app. I have selected the Gmail application, then clicked JUST ONCE.
 
android
 
Here also, our shareable content is shown in the mail body. Give the recipient's id and share the data.
 
android
 
Don’t forget to like and follow me. If you have any doubts, just commend below.