Smartphones

Get social: Using Android intents to share a link

Social media is a great way to promote your Android app. Learn how to implement link sharing via social networking from within your app.

Last month Google announced it had 700,000 apps available for download in Google Play. That's good and bad news for Android developers. On one hand, it's great to know that Android is still growing and right on par with iTunes in terms of application offerings. On the other hand, all that competition means even if your app is the greatest thing since sliced bread, potential users may still have difficulty learning it even exists.

A few weeks ago I had the chance to interview Francisco Marzoa, author of the hit game Luckiest Wheel. Francisco was kind enough to share a number of techniques he used to get the word out about his app. One such method he recommended was social networking and in particular, the ability from within the app to share a link with your friends and family.

The tutorial that follows demonstrates a simple and straightforward mechanism for implementing simple link sharing via social networking. You can follow along with the step-by-step instructions that follow or download the entire project and import it directly into Eclipse.

1. Create a new Android project in Eclipse. Target Android 1.6 or higher. Be sure to change the startup file to Main.java, and the respective layout file to main.xml.

2. In the /res/layout folder, modify main.xml to consist of a linear layout with a couple of text views and a single button.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding = "20dip"
        android:background="#000000">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Sharing a Link Demo"
               android:textColor="#00ff00"/>
    <TextView
        android:layout_marginTop="20dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="18sp"
        android:text="Do you love Techrepublic?"
                android:textColor="#ffffff"/>
<Button
    android:layout_marginTop="10dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Tell your friends!"
    android:layout_gravity="center"
    android:id="@+id/share_button"/>
</LinearLayout>

3. Find the /src folder. We will want to use the on create override in the Main.java file to wire up our button.

Main.java

package com.authorwjf.sharing;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
public class Main extends Activity implements OnClickListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.share_button).setOnClickListener(this);
    }
}

4. Implement the on click handler. We will create a new send intent, allowing the OS to choose which applications installed on the device are capable of sharing our link.

Main.java

@Override
       public void onClick(View v) {
              if (v.getId()==R.id.share_button) {
                      Intent intent = new Intent(Intent.ACTION_SEND);
                      intent.setType("text/plain");
                      intent.putExtra(Intent.EXTRA_TEXT, "http://www.techrepublic.com");
                      intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Check out this site!");
startActivity(Intent.createChooser(intent, "Share"));
             }
}
Since generally the only application installed on the emulator capable of sharing is the email client, I recommend running this one (Figure A) on an actual device. You should see your email client, Gmail, the SMS app, and any other app you might have installed on the device that supports social posting. If you use this intent with the official Facebook app, Facebook's privacy policy prevents the intent from "pre-populating" a user's status update. If you want that level of control, you'll need to look into the official Facebook API. Figure A

About

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

1 comments
divyenduz
divyenduz

How to limit the things in the sharing intent.. for instance say, I want to use only twitter/fb and not email ?

Editor's Picks