Sunday 1 January 2017

Share action for Android Application

Share action for Android Application

In this blog, you can learn how to add share button on app. If user clicks in share button many items will available there to share like email, Gmail, Facebook, twitter  and so on.


Create a new android project Share Button. Open your app main XML layout file and add a button or text view with onClick attribute. I have added a TextView with onClick attribute. Following is the complete content of XML layout file.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ddd"
        android:clickable="true"
        android:gravity="center"
        android:onClick="shareText"
        android:padding="18dp"
        android:text="Click to open sharing option" />
</LinearLayout>

Now open your java activity file and If you want to share something when user click the button you need to add little bit code in android button onClick like the below. Following is the complete code of java activity file.

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

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

    public void shareText(View view) {
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject/Title");
        intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your default message");
        startActivity(Intent.createChooser(intent, "Choose sharing method"));
    }
}


Full Code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ddd"
        android:clickable="true"
        android:gravity="center"
        android:onClick="shareText"
        android:padding="18dp"
        android:text="Click to open sharing option" />
</LinearLayout>


MainActivity.xml

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

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

    public void shareText(View view) {
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject/Title");
        intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your default message");
        startActivity(Intent.createChooser(intent, "Choose sharing method"));
    }
}