Android Implementing GetSocialize Tutorial

In this tutorial, you will learn how to implement GetSocialize in your Android application. Socialize lets users share your content to other users within the application, as well as in top social networks. GetSocialize allows your users to place comments, likes, shares and show total views within your application. We will be creating two buttons on the main view, and on button click will show different GetSocialize Action Bar on each view. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project GetSocializeTutorial.

Application Name : GetSocializeTutorial

Project Name : GetSocializeTutorial

Package Name : com.androidbegin.getsocializetutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

package com.androidbegin.socializetutorial;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	// Declare variables
	private Button fvbutton; 
	private Button svbutton; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from activity_main.xml
		setContentView(R.layout.activity_main);

		// Locate buttons in activity_main.xml
		fvbutton = (Button)findViewById(R.id.firstview);
		svbutton = (Button)findViewById(R.id.secondview);

		// Listen for first view button click 
		fvbutton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
            	// Open FirstView.class
                Intent myIntent = new Intent(MainActivity.this, FirstView.class);
                MainActivity.this.startActivity(myIntent);
                // TODO Auto-generated method stub
            }
        });

		// Listen for second view button click 
		svbutton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
            	// Open SecondView.class
                Intent myIntent = new Intent(MainActivity.this, SecondView.class);
                MainActivity.this.startActivity(myIntent);
                // TODO Auto-generated method stub
            }
        });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

We have created two buttons and implemented an on item click to open two different activities with different views.

Next, create an XML graphical layout for your MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file main.xml and paste the following code.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <Button
        android:id="@+id/firstview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/FirstView" />

    <Button
        android:id="@+id/secondview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/firstview"
        android:layout_centerInParent="true"
        android:text="@string/SecondView" />

</RelativeLayout>

Output:

GetSocialize Main XML

Next, create an activity for the first view. Go to File > New > Class and name it FirstView.java. Select your package named com.androidbegin.getsocializetutorial and click Finish.

Open your FirstView.java and paste the following code.

FirstView.java

package com.androidbegin.socializetutorial;

import com.socialize.ActionBarUtils;
import com.socialize.Socialize;
import com.socialize.entity.Entity;
import com.socialize.ui.actionbar.ActionBarOptions;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;

public class FirstView extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Call Socialize in onCreate
		Socialize.onCreate(this, savedInstanceState);

		// Your entity key. May be passed as a Bundle parameter to your activity
		String entityKey = "FirstView";

		// Create an entity object including a name
		// The Entity object is Serializable, so you could also store the whole
		// object in the Intent
		Entity entity = Entity.newInstance(entityKey, "Socialize");
		// Create an options instance to specify your theme
		ActionBarOptions options = new ActionBarOptions();
		// Set the colors for the Action Bar
		options.setStrokeColor(Color.parseColor("#591100")); // The line between the buttons
		options.setAccentColor(Color.parseColor("#ffa229")); // The accent line below the buttons
		options.setFillColor(Color.parseColor("#831400")); // The main color of the buttons
		options.setBackgroundColor(Color.parseColor("#591100")); // The background color seen on the left
		options.setHighlightColor(Color.parseColor("#b05e08")); // The thin highlight line above the buttons
		options.setTextColor(Color.parseColor("#ffba00")); // The text color for all buttons

		// Wrap your existing view with the action bar.
		// your_layout refers to the resource ID of your current layout.
		android.view.View actionBarWrapped = ActionBarUtils.showActionBar(this,
				R.layout.firstview, entity, options);

		// Now set the view for your activity to be the wrapped view.
		setContentView(actionBarWrapped);
	}

	@Override
	protected void onPause() {
		super.onPause();

		// Call Socialize in onPause
		Socialize.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Call Socialize in onResume
		Socialize.onResume(this);
	}

	@Override
	protected void onDestroy() {
		// Call Socialize in onDestroy before the activity is destroyed
		Socialize.onDestroy(this);

		super.onDestroy();
	}

}

In this activity, we have created a GetSocialize Action Bar instance and each instance is bound to an Entity. An Entity is simply an item of content in your app. Each Socialize action (comment, share, like etc.) is associated with an Entity. We have given an unique Entity name for this view called “FirstView“.

Output:

GetSocialize ActionBar FirstView

Next, create an XML graphical layout for the first view. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file firstview.xml and paste the following code.

firstview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/FirstView" />

</RelativeLayout>

Next, create an activity for the second view. Go to File > New > Class and name it SecondView.java. Select your package named com.androidbegin.getsocializetutorial and click Finish.

Open your SecondView.java and paste the following code.

SecondView.java

package com.androidbegin.socializetutorial;

import com.socialize.ActionBarUtils;
import com.socialize.Socialize;
import com.socialize.entity.Entity;
import com.socialize.ui.actionbar.ActionBarOptions;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;

public class SecondView extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Call Socialize in onCreate
		Socialize.onCreate(this, savedInstanceState);

		// Your entity key. May be passed as a Bundle parameter to your activity
		String entityKey = "SecondView";

		// Create an entity object including a name
		// The Entity object is Serializable, so you could also store the whole
		// object in the Intent
		Entity entity = Entity.newInstance(entityKey, "Socialize");
		// Create an options instance to specify your theme
		ActionBarOptions options = new ActionBarOptions();
		// Set the colors for the Action Bar
		options.setStrokeColor(Color.parseColor("#591100")); // The line between the buttons
		options.setAccentColor(Color.parseColor("#ffa229")); // The accent line below the buttons
		options.setFillColor(Color.parseColor("#831400")); // The main color of the buttons
		options.setBackgroundColor(Color.parseColor("#591100")); // The background color seen on the left
		options.setHighlightColor(Color.parseColor("#b05e08")); // The thin highlight line above the buttons
		options.setTextColor(Color.parseColor("#ffba00")); // The text color for all buttons

		// Optionally alter the images
		options.setLikeIconResourceId(R.drawable.autumn_like);
		options.setLikeIconActiveResourceId(R.drawable.autumn_like_hi);
		options.setCommentIconResourceId(R.drawable.autumn_comment);
		options.setShareIconResourceId(R.drawable.autumn_share);
		options.setViewIconResourceId(R.drawable.autumn_view);

		// Wrap your existing view with the action bar.
		// your_layout refers to the resource ID of your current layout.
		android.view.View actionBarWrapped = ActionBarUtils.showActionBar(this,
				R.layout.secondview, entity, options);

		// Now set the view for your activity to be the wrapped view.
		setContentView(actionBarWrapped);	}

	@Override
	protected void onPause() {
		super.onPause();

		// Call Socialize in onPause
		Socialize.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Call Socialize in onResume
		Socialize.onResume(this);
	}

	@Override
	protected void onDestroy() {
		// Call Socialize in onDestroy before the activity is destroyed
		Socialize.onDestroy(this);

		super.onDestroy();
	}

}

In this activity, we have create an GetSocialize Action Bar with customized icons by using GetSocialize ActionBarOptions. We have given an unique Entity name for this view called “SecondView“.

We have prepared some sample icons for this tutorial. Insert your downloaded sample icons into your res > drawable-hdpi.

Sample ActionBar Icons

[wpfilebase tag=file id=67 tpl=download-button /]

Output:

GetSocialize SecondView

Next, create an XML graphical layout for your second view. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file secondview.xml and paste the following code.

secondview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/SecondView" />

</RelativeLayout>

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

<resources>

    <string name="app_name">GetSocialize Tutorial</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">GetSocialize Tutorial</string>
    <string name="FirstView">FirstView</string>
    <string name="SecondView">SecondView</string>   

</resources>

In your AndroidManifest.xml, we need to declare some permissions, which you can refer to the comments. Open your AndroidManifest.xml and paste the following code.
AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.socializetutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- Optional but recommended -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <!-- Optionally add ONE of the following to include location data in comments -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".FirstView" />
        <activity android:name=".SecondView" />
       <!-- Activities Required by Socialize -->
        <activity android:name="com.socialize.ui.comment.CommentActivity" android:configChanges="orientation|keyboardHidden|screenSize"/>
        <activity android:name="com.socialize.ui.action.ActionDetailActivity"/>
        <activity android:name="com.socialize.ui.profile.ProfileActivity"/>
        <activity android:name="com.socialize.auth.facebook.FacebookActivity"/>
        <activity android:name="com.socialize.ui.SocializeLaunchActivity" android:noHistory="true"/>

        <!-- Activities Required by Facebook -->
        <activity android:name="com.facebook.LoginActivity"/>
    </application>

</manifest>

GetSocialize.com

Sign up for GetSocialize

GetSocialize Register

Select My Apps and click on Add New App.

GetSocialize Add New App

In your Mobile Platform select Android, and insert your package name and app name.

GetSocialize Create App

Open your application named GetSocializeTutorial in My Apps

GetSocialize Open App

Scroll down and look for oAuth Keys, URL, & ID.

GetSocialize Consumer

Import GetSocialize Libraries

Download the libraries provided below or download it from GetSocialize.

[wpfilebase tag=file id=68 tpl=download-button /]

Import the downloaded libraries into Eclipse.

GetSocialize Import

Right click on sdk library and select properties. Add android-ioc and FacebookSDK as reference. See below Screenshot.

GetSocialize Reference

Register Facebook Developer

Create a developer account in Facebook Developer. Insert an App name for your application.

GetSocialize Facebook

This is your App ID in Facebook Developer.

GetSocialize Facebook AppID

Import GetSocialize Library into project

Next, import GetSocialize library into your app.

Next, import sdk library into GetSocializeTutorial app.

GetSocialize Import SDK

Create a configuration file in the assets path of your project called socialize.properties.

Right click on assets folder in your app, select New > Untitled Text File

Paste the following codes into your Untitled Text File, replace the consumer keysecret key and facebook id. If you do not want to integrate Facebook authentication into your your application, just remove Facebook App ID.

# Socialize App Key and Secret
socialize.consumer.key=00000000-0000-0000-000000000000
socialize.consumer.secret=00000000-0000-0000-000000000000

# Facebook App ID
facebook.app.id=1234567890

Save the Untitled Text File into your GetSocializeTutorial assets folder and name it socialize.properties.

GetSocialize Assets

Output:

GetSocialize ScreenShot

Source Code

[purchase_link id=”7975″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

hey this is not working with the LDPI devices please help

Mayur Devgaonkar

Android Implementing GetSocialize Tutorial

How do i set up GetSocialize SDK in android studio.

Jade

Android Implementing GetSocialize Tutorial