Social-Media Link Share In Android Using Android Studio

Introduction

 
In this article, we are going to see how to create a share button in the Android app using Android Studio. It is used to share the social links to any other sources in our mobile. This link can be provided by us.
 
Step 1
 
Create a new project in Android Studio.
 
Android
 
Give a name to the project and click "Next".
 
Android
 
Select the "Phone and Tablet" and click "Next".
 
Android
 
Select an empty activity and click "Next".
 
Android
 
At last, give the activity name and click on "Finish".
 
Android 
 
Step 2
 
Set up the Gradle by just locating the Gradle Scripts>>Build.Gradle
 
Android
 
And type the following dependency in your app's build.gradle.
 
Android
 
Code copy is here, 
  1. maven  
  2. {  
  3.    url "https://jitpack.io"  
  4. }  
Step 3
 
Next, go to app >> res >> layout >> activity_main.xml. Select activity page
 
Android
 
And just type the code as follows.
 
Android
 
Code copy is here
  1. <Button  
  2.    android:id="@+id/button"  
  3.    android:layout_width="wrap_content"  
  4.    android:layout_height="wrap_content"  
  5.    android:text="Share It"  
  6.    tools:layout_editor_absoluteX="141dp"  
  7.    tools:layout_editor_absoluteY="231dp"  
  8. />  
Step 4
 
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page,
 
Android
 
And just type the code as follows,
 
Android
 
Code copy is here 
  1. Button button;  
  2. @Override  
  3. protected void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.activity_main);  
  6.         button = (Button) findViewById(R.id.button);  
  7.         button.setOnClickListener(new View.OnClickListener() {  
  8.             @Override  
  9.             public void onClick(View view) {  
  10.                 Intent myIntent = new Intent(Intent.ACTION_SEND);  
  11.                 myIntent.setType("text/plain");  
  12.                 String shareBody = "Your body is here";  
  13.                 String shareSub = "Your subject";  
  14.                 myIntent.putExtra(Intent.EXTRA_SUBJECT, shareBody);  
  15.                 myIntent.putExtra(Intent.EXTRA_TEXT, shareBody);  
  16.                 startActivity(Intent.createChooser(myIntent, "Share using"));  
  17.             }  
  18.         });  
Note
 
You have to provide your link in the 13th line inside sharebody.
 
Step 5
 
After step 4, Sync all the dependency gradles and Mainactivity.java resource files by clicking the Sync button on the top right corner of the Gradle page.
 
Android
 
Step 6
 
Verify the preview.
->After the code is applied, the preview will appear like this.
 
Android
 
Step 7
 
Next, go to Android Studio and deploy the application. Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
 
Android
 
Run the application in your desired emulator (Shift + F10).
 
Android
 
Explanation of source code
 
The source code provided in this article is just the dependencies of share option and the code used in activity_main.xml will make the button sharable and define its attributes.
 

Summary

 
In this article we created the app named Share Data, then we have inserted a Gradle and we learned how to give a share option to the button and finally, we have deployed that as an output.
 
*Support and Share, Thank You*


Similar Articles