Tuesday 17 December 2013

How to share Android Application programmatically.

Today I will explain you how to create a share button in android application programmatically. In this application we will use the Intent to share the application and a String variable to which will store the link of your application of the play store. This purpose can be done by the Action send of Intent.

By this action a pop up will occur on the mobile screen and it that no of application will be shown which can send any kind of message to the other person just chose one of the application.
XML file required :-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Share" />

</RelativeLayout>

Source Code:-

First Step:-  Initialize the button in the oncreate method of the activity.

Button share=(Button)findViewById(R.id.share);

Second Step:- set on click listener on the button and put the following code in the listener in this code we will set intent as a action to send and use put extra method on intent to add the subject which is title of the application and text in which add the string variable which contain the link of the application of the playstore

String shareBody = "https://play.google.com/store/apps/details?id=************************";

                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "APP NAME (Open it in Google Play Store to Download the Application)");

                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(sharingIntent, "Share via"));


Snapshots:-


Share Button Android Applicationshare Andorid application programmatically




Click here To Download The Project:-

No comments:

Post a Comment