Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Integrating Social Networking Using HTTP

Published? true
FormatLanguage: WikiFormat

Problem:

Instead of diving into the API you can simply add Social networking support.

Solution:

For Facebook, Twitter and LinkedIn integration, just follow 3 simple steps to get started:

  1. Get the logo's for Facebook, Twitter and Linked-in
  2. Create image buttons for each of them
  3. Implement the event handler

Discussion:

Here goes the detailed explanation:

1. Get the logo:

Just download the logo from the site, or use a web search engine.

2. Create image buttons for each of them

 <source lang='XML'>

<ImageView android:src="@drawable/icon_facebook"
	 android:layout_width="28dip"
	android:layout_height="28dip" android:id="@+id/facebookBtn"
	android:clickable="true" 
	android:onClick="facebookBtnClicked" />
<ImageView android:src="@drawable/icon_twitter"
	android:clickable="true" 
	android:layout_width="30dip" android:layout_height="28dip" android:id="@+id/twitterBtn"
	android:layout_marginLeft="3dp" android:layout_marginRight="3dp"
	android:onClick="twitterBtnClicked"
	 />
<ImageView android:src="@drawable/icon_linkedin"
	android:layout_width="28dip"
	android:layout_height="30dip" android:clickable="true"
	android:id="@+id/linkedinBtn" 
	android:onClick="linkedinBtnClicked"
	/>

3. Implement the click event

/*URL used here is for application to which I want user to redirect and comment about here I am
	 * using http://goo.gl/eRAD9 as the URL. But you can use URL of your app. Take the URL from Market 
	 * and shorten the URL with bit.ly or Google URL shortener
	 * */
	
	public void facebookBtnClicked(View v)
	{
		Toast.makeText(this, "Facebook Loading...\n Please make sure you are connected to internet.", Toast.LENGTH_SHORT).show();
		String url="http://m.facebook.com/sharer.php?u=http%3A%2F%2Fgoo.gl%2FeRAD9";
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		startActivity(i);
	}
	public void twitterBtnClicked(View v)
	{
		Toast.makeText(this, "Twitter Loading... \n Please make sure you are connected to internet.", Toast.LENGTH_SHORT).show();
		/**/
		String url = "http://www.twitter.com/share?text=Checkout+This+Demo+http://goo.gl/eRAD9+";
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		startActivity(i);
	}
	public void linkedinBtnClicked(View v)
	{
		Toast.makeText(this, "Linked-In Loading... \n Please make sure you are connected to internet", Toast.LENGTH_SHORT).show();
		String url="http://www.linkedin.com/shareArticle?url=http%3A%2F%2Fgoo.gl%2FeRAD9&mini=true&source=SampleApp&title=App+on+your+mobile";
		Intent intent=new Intent(Intent.ACTION_VIEW);
		intent.setData(Uri.parse(url));
		startActivity(intent);
	}

This is how in 3 simple steps you can get a Social Networking feature for your application.