Android, Xml

How to create a share button in android ? | Android share intent

share button in android

share button in android

Hello folks, so in this article I am going to discuss about How to create a share button in android ? , Android share intent , How to use Intent to share in android and share button android example .

Share button in android :

Android provides all apps to share data / content with each other using Intent . For example you can get images from the gallery and share it with other android app , you can also share the URL link over to any text messaging app to send it to your friends .

Read Also : How to make a phone call in android ? | Make a phone call using intent

To share some data from your application to another application, we need to use intents to share your content and the action to be performed here is ACTION_SEND, and data to be provided is the type of file and the content.

It is a type of implicit Intent. Here, it will share your content from your app by using an Intent object that launches existing list of sharing applications available on their own devices to share, how you wish to share your content .

Here few lines of code is enough to create a share button to share your content using Intent .

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Android Development Tutorials");
shareIntent.putExtra(Intent.EXTRA_TEXT, "www.technxt.net");
shareIntent.setType("text/plain");
startActivity(shareIntent);

1. Create a New Project :

Create a new project in Android Studio from File > New Project and select Empty Activity from templates to create a share button in android .

2. Add a Button to your layout :

Add a button to your activity_main.xml or xml layout file .

<Button
        android:id="@+id/button"
        android:layout_width="172dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="215dp"
        android:text="Share" />

Now open res -> layout -> activity_main.xml or xml layout file and just implement the following xml code .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".TestActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="172dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="215dp"
        android:text="Share" />
</RelativeLayout>

3. Implement setOnClickListener on your share button to make listener on it :

share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Android Development Tutorials");
                shareIntent.putExtra(Intent.EXTRA_TEXT, "www.technxt.net");
                shareIntent.setType("text/plain");
                startActivity(shareIntent);
            }
        });

4. Open your MainActivity.java and add the following code :

package net.technxt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class TestActivity extends AppCompatActivity {
    Button share;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        share = (Button)findViewById(R.id.button);

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Android Development Tutorials");
                shareIntent.putExtra(Intent.EXTRA_TEXT, "www.technxt.net");
                shareIntent.setType("text/plain");
                startActivity(shareIntent);
            }
        });
    }
}

Now run the project/app to test share button in android app through android device or any Emulator .

Leave a Reply

Created by Bhashkar Poddar

%d bloggers like this: