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.
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.
Step 4
Choose the basic activity then click Next.
Step 5
Put activity name and layout name. Android Studio basically takes Java class name to what you provide the activity name. Click Finish.
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
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:orientation="vertical"
- tools:context="ganeshannt.sharing.MainActivity">
-
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter the text that you want to share!"
- android:textSize="20sp"
- android:id="@+id/editText"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="80dp"
- android:text="Share"
- android:onClick="Clicked"
- android:layout_gravity="center_horizontal"/>
- </LinearLayout>
The above code is used to create the edit text row and button.
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
- package ganeshannt.sharing;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.EditText;
-
- public class MainActivity extends AppCompatActivity {
- private EditText editText;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- editText = (EditText)findViewById(R.id.editText);
-
- }
-
- public void Clicked(View view)
- {
-
-
-
-
-
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_TEXT,editText.getText().toString());
- sendIntent.setType("text/plain");
- Intent.createChooser(sendIntent,"Share via");
- startActivity(sendIntent);
- }
- }
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
- <resources>
- <!-- Default screen margins, per the Android Design guidelines. -->
- <dimen name="activity_horizontal_margin">16dp</dimen>
- <dimen name="activity_vertical_margin">16dp</dimen>
- </resources>
Step 9
This is our user interface of the application. Click the "Make Project" option.
Step 10
Run the application, choose the desired Virtual Machine, and click OK.
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.
Click the "Share" button.
It will show some shareable applications.
Let's choose to share the messages application.
This will open the message application, and it shows my sharing content. Now, choose the contact number to share the message.
Once more, I want to share the content with another app. I have selected the Gmail application, then clicked JUST ONCE.
Here also, our shareable content is shown in the mail body. Give the recipient's id and share the data.
Don’t forget to like and follow me. If you have any doubts, just commend below.