Online Tutorials & Training Materials | STechies.com
Register Login

Share Button in Android App

|| 0

Share Button in Android App
Stechies

How to add a Share Button/ Action in Android App

This tutorial explains step by step for how to add a Share Button/ Action in Android Application via Android Studio. Share button will help share our app content.

Please follow the steps below in order to add Share Button/ Action in Android Application:

1.Go to Android studio. Remove “hello world” text and add a Button.

2.Click on the button icon and drag it to the center of the screen.

3.Click on the text tab. Change “Button” to “Share it”.

4.Go to MainActivity.java. Inside the OnClickListener, add an intent to force the application.

5.Change shareBody into shareSub.

6.Go to the live device for the explanation. You can see the button.

7.On pressing the button, you will see a pop-up screen.

8.Copy the screen to the clipboard.

9.Now click on messages.

10. Add recipients and share your subject and description.

Complete Code to Create Share Button in Android App

app > res > layout > activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schema.android.com/apk/res/android"
    xmlns:app="http://schema.android.com/apk/res-auto"
    xmlns:tools="http://schema.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:orientation="vertical"
    tools:context="com.stechies.shareData.MainActivity">
    
    <Button
        android:layout_width="wrap_parent"
        android:layout_height="wrap_parent"
        android:layout_centerVeritcal="true"
        android:layout_centerHorizontal="true"
        android:text="SHARE IT"
        android:id="@+id/button"/>

</RelativeLayout>

app > java > com.stechies.shareData.MainActivity > MainActivity.java

package com.stechies.shareData;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity{

    Button bt;

    @Override
    protected void onCreate(Bundle SavedInstanceState) {
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.activity_main);

        bt = (Button) findViewById(R.Id.button);

        bt.setOnClickListner(new View.OnClickListner () {

            @Override
            protected void onClick(View v){
                Intent myIntent = new Intent(Intent.ACTION_SEND);
                myIntent.setType("text/plain");
                String body = "Your body here";
                String sub = "Your Subject";
                myIntent.putExtra(Intent.EXTRA_SUBJECT,sub);
                myIntent.putExtra(Intent.EXTRA_TEXT,body);
                startActivity(Intent.createChooser(myIntent, "Share Using"))

            }
        });
    }
}

 


Related Articles