Thursday, 21 August 2014

Android Tutorial Android Application updated version released today.

Here i am sharing the link of my Android Tutorial application.

From past few years i am maintaining this blog to help others who are learning android and who are already in the development .

So here my kind request is please download this application and give me rating and comments so that i can help people through this android tutorial application .

you can download  by clicking this here : Android Tutorial Application

Thanks,
yours Ram

Wednesday, 13 August 2014

Facebook SDK Integration in Android Tutorial Part 3 (Post status to Facebook and Share image to Facebook )

Download this project-->SampleFIntegration.zip

strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SampleFBIntegration</string>
    <string name="hello_world">Hello world!</string>
    <string name="app_id">739274269466534</string>

</resources>

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.samplefintegration"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <activity android:name="com.facebook.LoginActivity" />
    </application>

</manifest>

main.xml:
<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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Login" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_login"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button_postStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Post Status" />

    

    <Button
        android:id="@+id/button_postImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button_postStatus"
        android:layout_below="@+id/button_postStatus"
        android:layout_marginTop="20dp"
        android:text="Post Image" />

</RelativeLayout>

MainActivity.java:
package com.ram.samplefintegration;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;

public class MainActivity extends Activity implements OnClickListener
{
    Button loginButton, postStatusButton;
    Button postImageButton;
    TextView text;
    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
    private UiLifecycleHelper uiHelper;
    Session fSession;

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

        uiHelper = new UiLifecycleHelper(this, statusCallback);
        uiHelper.onCreate(savedInstanceState);

        text = (TextView) findViewById(R.id.textView);
        loginButton = (Button) findViewById(R.id.button_login);
        postStatusButton = (Button) findViewById(R.id.button_postStatus);
        postImageButton = (Button) findViewById(R.id.button_postImage);

        postImageButton.setOnClickListener(this);
        loginButton.setOnClickListener(this);
        postStatusButton.setOnClickListener(this);

        if (Session.getActiveSession().isOpened())
        {
            Session.openActiveSession(this, true, statusCallback);
            loginButton.setEnabled(false);
        } else
        {
            buttonsDisable();
        }

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        case R.id.button_login:
            // start facebook login
            Session.openActiveSession(this, true, statusCallback);
            break;

        case R.id.button_postStatus:
            // To post status to facebook
            postStatusMessage("Hi to all....Sample status shared from android app");
            break;

        case R.id.button_postImage:
            postImage();
            break;
        }

    }

    Session.StatusCallback statusCallback = new Session.StatusCallback()
    {
        @Override
        public void call(Session session, SessionState state, Exception exception)
        {
            if (state.isOpened())
            {
                fSession = session;
                getUserProfie();
            } else
            {
                buttonsDisable();
            }
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

    public void postStatusMessage(String message)
    {
        if (checkPermissions())
        {
            Request request = Request.newStatusUpdateRequest(fSession, message,
                    new Request.Callback()
                    {
                        @Override
                        public void onCompleted(Response response)
                        {
                            if (response.getError() == null)
                                Toast.makeText(MainActivity.this,
                                        "Status updated successfully", Toast.LENGTH_LONG)
                                        .show();
                        }
                    });
            request.executeAsync();
        } else
        {
            requestPermissions();
        }
    }

    void getUserProfie()
    {
        // make request to the /me API to get user details
        Request.newMeRequest(fSession, new Request.GraphUserCallback()
        {

            // callback after Graph API response with user
            // object
            @Override
            public void onCompleted(GraphUser user, Response response)
            {
                if (user != null)
                {
                    // To display facebook user name of the user
                    text.setText("Hello " + user.getName() + "!");

                    buttonsEnable();
                }
            }
        }).executeAsync();
    }

    public void postImage()
    {
        if (checkPermissions())
        {
            Bitmap img = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_launcher);

            Request uploadRequest = Request.newUploadPhotoRequest(
                    Session.getActiveSession(), img, new Request.Callback()
                    {
                        @Override
                        public void onCompleted(Response response)
                        {
                            Toast.makeText(MainActivity.this,
                                    "Photo uploaded successfully", Toast.LENGTH_LONG)
                                    .show();
                        }
                    });
            uploadRequest.executeAsync();
        } else
        {
            requestPermissions();
        }
    }

    public void requestPermissions()
    {
        Session s = Session.getActiveSession();
        if (s != null)
            s.requestNewPublishPermissions(new Session.NewPermissionsRequest(this,
                    PERMISSIONS));
    }

    public boolean checkPermissions()
    {
        Session s = Session.getActiveSession();
        if (s != null)
        {
            return s.getPermissions().contains("publish_actions");
        } else
            return false;
    }

    @Override
    public void onSaveInstanceState(Bundle savedState)
    {
        super.onSaveInstanceState(savedState);
        uiHelper.onSaveInstanceState(savedState);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        uiHelper.onResume();

    }

    @Override
    public void onPause()
    {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    void buttonsEnable()
    {
        loginButton.setEnabled(false);
        postStatusButton.setEnabled(true);
        postImageButton.setEnabled(true);
    }

    void buttonsDisable()
    {
        postStatusButton.setEnabled(false);
        postImageButton.setEnabled(false);
        loginButton.setEnabled(true);
    }
}

ScreenShot:




Monday, 11 August 2014

Facebook SDK Integration in Android Tutorial Part 2 (Sign in with facebook)

To add an option call sign in with facebook we need to follow following procedure:

First we need to add facebook app id to our project.
-->To add the Facebook app ID into your project, open up the strings.xml file in the res/values folder of your project. we need to add an app_id string containing the ID that we obtained  from the https://developers.facebook.com website . To know how to create facebook app id you can read part1.

strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SampleFBIntegration</string>
    <string name="hello_world">Hello world!</string>
    <string name="app_id">739274269466534</string>

</resources>


Next add Internet permission , meta data tag and Activity in AndroidManifest.xml as follows:

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.samplefintegration"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />


        <activity android:name="com.facebook.LoginActivity" />
    </application>

</manifest>


Next create login button and one TextView in xml  layout as follows:

activity_main.xml:
<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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:text="Login" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button_login"
        android:layout_marginTop="27dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>





Next add following java code in Activity as follows:
MainActivity.java:
package com.ram.samplefintegration;

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

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

public class MainActivity extends Activity implements OnClickListener
{
    Button loginButton;
    TextView text;

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

        text = (TextView) findViewById(R.id.textView);
        loginButton = (Button) findViewById(R.id.button_login);
        loginButton.setOnClickListener(this);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v)
    {
        // start facebook login
        Session.openActiveSession(this, true, new StatusCallback()
        {
            // callback when session changes state
            @Override
            public void call(Session session, SessionState state, Exception exception)
            {
                if (session.isOpened())
                {
                    // make request to the /me API to get user details
                    Request.newMeRequest(session, new Request.GraphUserCallback()
                    {

                        // callback after Graph API response with user
                        // object
                        @Override
                        public void onCompleted(GraphUser user, Response response)
                        {
                            if (user != null)
                            {
                                // To display facebook user name of the user
                                text.setText("Hello " + user.getName() + "!");
                            }
                        }
                    }).executeAsync();
                }

            }
        });

    }
}







Output Screen Shots: