How to Integrate Facebook ,Twitter and LinkedIn in Android – Socialauth Library

Posted on .
SocialAuth

Hello guyz , How are you all ? Today I am going to show you how to integrate all in one Social Media in your Android Application.

Sharing of your Application or Post status on Wall of social media is very common these days.But it takes too much pressure and load on developer to integrate all the social media differently like integrate Facebook then Twitter and LinkedIn are seriously very hard task.

Download Code

So To escape you from this situation I am going to show you how to integrate All in one social Media within 5 minutes.

This library is devloped by 3pillarlabs and is very interesting to integrate your social Media , So lets start Quickly .

Create Your Project Project and just copy the libraries to your libs folder . You just need to change the App-id of your Social Media , by going to developer Account of corresponding Social Media.

Download Libraries

Now MainActivity.java 

package org.brickred.socialbar;

import java.io.File;

import org.brickred.socialauth.android.DialogListener;
import org.brickred.socialauth.android.SocialAuthAdapter;
import org.brickred.socialauth.android.SocialAuthAdapter.Provider;
import org.brickred.socialauth.android.SocialAuthError;
import org.brickred.socialauth.android.SocialAuthListener;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ShareBarActivity extends Activity {

	// SocialAuth Component
	SocialAuthAdapter adapter;
	boolean status;

	// Android Components
	Button update;
	EditText edit;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Welcome Message
		TextView textview = (TextView) findViewById(R.id.text);
		textview.setText("Welcome to SocialAuth Demo. Connect any provider and then press Update button to Share Update.");

		LinearLayout bar = (LinearLayout) findViewById(R.id.linearbar);
		bar.setBackgroundResource(R.drawable.bar_gradient);

		// Add Bar to library
		adapter = new SocialAuthAdapter(new ResponseListener());

		// Add providers
		adapter.addProvider(Provider.FACEBOOK, R.drawable.facebook);
		adapter.addProvider(Provider.TWITTER, R.drawable.twitter);
		adapter.addProvider(Provider.LINKEDIN, R.drawable.linkedin);

		// For twitter use add callback method. Put your own callback url here.
		adapter.addCallBack(Provider.TWITTER,
				"http://socialauth.in/socialauthdemo/socialAuthSuccessAction.do");

		// Add keys and Secrets
		try {
			adapter.addConfig(Provider.FACEBOOK, "297841130364674",
					"dc9c59d0c72d4f2533580e80ba4c2a59", null);
			adapter.addConfig(Provider.TWITTER, "5jwyYJia583EEczmdAmlOA",
					"j0rQkJjTjwVdv7HFiE4zz2qKJKzqjksR2aviVU8fSc", null);
			adapter.addConfig(Provider.LINKEDIN, "bh82t52rdos6",
					"zQ1LLrGbhDZ36fH8", null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		adapter.enable(bar);
	}

	private final class ResponseListener implements DialogListener {
		@Override
		public void onComplete(Bundle values) {

			// Variable to receive message status
			Log.d("Share-Bar", "Authentication Successful");

			// Get name of provider after authentication
			final String providerName = values
					.getString(SocialAuthAdapter.PROVIDER);
			Log.d("Share-Bar", "Provider Name = " + providerName);
			Toast.makeText(ShareBarActivity.this, providerName + " connected",
					Toast.LENGTH_SHORT).show();

			update = (Button) findViewById(R.id.update);
			edit = (EditText) findViewById(R.id.editTxt);

			// Please avoid sending duplicate message. Social Media Providers
			// block duplicate messages.

			update.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					adapter.updateStatus(edit.getText().toString(),
							new MessageListener(), true);
				}
			});

		}

		@Override
		public void onError(SocialAuthError error) {
			error.printStackTrace();
			Log.d("Share-Bar", error.getMessage());
		}

		@Override
		public void onCancel() {
			Log.d("Share-Bar", "Authentication Cancelled");
		}

		@Override
		public void onBack() {
			Log.d("Share-Bar", "Dialog Closed by pressing Back Key");

		}
	}

	// To get status of message after authentication
	private final class MessageListener implements SocialAuthListener<Integer> {
		@Override
		public void onExecute(String provider, Integer t) {
			Integer status = t;
			if (status.intValue() == 200 || status.intValue() == 201
					|| status.intValue() == 204)
				Toast.makeText(ShareBarActivity.this,
						"Message posted on " + provider, Toast.LENGTH_LONG)
						.show();
			else
				Toast.makeText(ShareBarActivity.this,
						"Message not posted on" + provider, Toast.LENGTH_LONG)
						.show();
		}

		@Override
		public void onError(SocialAuthError e) {

		}
	}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000" >

    <LinearLayout
        android:id="@+id/topbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/blue_gradient"
        android:orientation="horizontal"
        android:padding="7dp" >

        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Share Bar"
            android:textColor="#ffffff"
            android:textSize="14sp"
            android:textStyle="bold" />
    </LinearLayout>

    <View
        android:id="@+id/view01"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/topbar"
        android:background="#313437" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view01"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:textColor="#ffffff"
        android:textSize="14sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/dialogTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="30dp"
        android:text="Share Update"
        android:textColor="#ffffff"
        android:textSize="14sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editTxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialogTitle"
        android:layout_margin="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:hint="Enter Message"
        android:textColor="#000000"
        android:textSize="12sp" />

    <Button
        android:id="@+id/update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editTxt"
        android:layout_below="@+id/editTxt"
        android:layout_marginRight="10dp"
        android:background="#147af8"
        android:padding="5dp"
        android:text="Update"
        android:textColor="#ffffff"
        android:textSize="12sp" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/layout_gradient" >

        <LinearLayout
            android:id="@+id/linearbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:layout_marginTop="20dp" >
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

Now Run the App and you will check the real time integration of Social Media.

Output-

screenfbauth

 

authtwitter

 

aututhlinked

 

7 thoughts on “How to Integrate Facebook ,Twitter and LinkedIn in Android – Socialauth Library

  1. Shivani Bajpai

    This code is not in working Both twitter and Linkedin is not started and Facebook submitting the issue of invaild Scopes-Publish_Stream
    can u tell me how to sort out this issue

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *