You know that most applications give functionality to share their applications and increase the number of app downloads.
If your apps like to use, then there are share your apps on the social medial platform and increase the number of downloaders.
We can set the URL of the Google play store location or any location so that clicking on the URL will lead to the redirected destination.
How to create a share application option in android studio?
Follow the easy process to add sharing option in android studio.
Step 1:
Open android studio and start a new project.
Step 2:
Now, select the project template.
In this project, we will select an ‘empty activity.’
After, click on ‘Next‘.
Step 3
Put your application name,
save the location where you like to save your project and select API level.
After completing all processes, click on ‘finish’.
Your project starts building.
It takes some time to build Gradle.
After, you see default MainActivity.java and activity_main.xml.
First, we create a design layout. So to activity_main.xml copy and paste the below code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<ImageView
android:onClick="ShareApp"
android:layout_width="86dp"
android:layout_height="76dp"
android:src="@drawable/ic_baseline_share_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now, go to the MainActivity.java file, copy and paste the below code.
Change package-name otherwise error comes during run time.
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
String body;
String subject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ShareApp(View view) {
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
subject="Hi! I'm using this app. your can find it on google play store";
body=("https:play.google.com/store/apps/detail?id=com.example.myapp");
intent.putExtra(Intent.EXTRA_SUBJECT,subject);
intent.putExtra(Intent.EXTRA_TEXT,body);
startActivity(Intent.createChooser(intent,"share this app via"));
}
}
ACTION_SEND: to share your data.
Intent.settype(“text/plain”);: this is used to order to share your text or URL. In this project share URL.
We will create two variables named subject and body.
Subject: here you’ll set your subject.
Body: we will set the app URL that clicking user redirect play store.
After are all done, run the project.
When you click on ‘Share’ icon will open all available sharing social icons on your device.
Choose any social and share your app wherever you want to share.