Hello friends if you want login with Facebook with your own login custom button then use this sample.
First you create new project and follow step by step ....
Step 1>
Add sdk in grale dependency
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.samset.user.customfblogin"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.facebook.android:facebook-android-sdk:4.6.0'
}
Add permission and configure need to fb login resources in android mainisfeast
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samset.user.customfblogin">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- if you want share your content then enable this commented code
<provider android:authorities="com.facebook.app.FacebookContentProvider788910871208730"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
-->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 2> Create fb_app_id go to this link.https://developers.facebook.com/apps/788910871208730/dashboard/ and create id.
now you write your android code
MainActvity.java
package com.samset.user.customfblogin;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.ProfilePictureView;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ImageView imgFacebookLogin, logout;
public static CallbackManager callbackManager;
private Intent intent;
private ImageView imageView;
private ProfilePictureView profile;
private TextView tvName, tvEmail;
private LinearLayout linearLayout;
private RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
imgFacebookLogin = (ImageView) findViewById(R.id.imgFacebookLogin);
logout = (ImageView) findViewById(R.id.imgFacebooklogout);
tvEmail = (TextView) findViewById(R.id.useremail);
tvName = (TextView) findViewById(R.id.username);
profile = (ProfilePictureView) findViewById(R.id.picture);
linearLayout = (LinearLayout) findViewById(R.id.profilelayout);
relativeLayout = (RelativeLayout) findViewById(R.id.logoutlayout);
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logout();
}
});
imgFacebookLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fblogin();
}
});
}
public void getHashKey() {
try {
PackageInfo info = getPackageManager().getPackageInfo("com.samset.user.customfblogin", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("HASH KEY:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
private void fblogin() {
callbackManager = CallbackManager.Factory.create();
final List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "user_friends", "public_profile");
//List<String> permissionNeed = Arrays.asList("publish_actions");
LoginManager.getInstance().logInWithReadPermissions(this, permissionNeeds);
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResults) {
GraphRequest request = GraphRequest.newMeRequest(loginResults.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
linearLayout.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.VISIBLE);
imgFacebookLogin.setVisibility(View.GONE);
String email = object.getString("email");
String name = object.getString("name");
String id = object.getString("id");
URL fbUserImageUrl = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
tvName.setText(name);
tvEmail.setText(email);
profile.setProfileId(object.getString("id"));
/* try {
Bitmap bmp = BitmapFactory.decodeStream((InputStream) new URL(fbUserImageUrl.toString()).getContent());
imageView.setImageBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
*/
Log.e("Fb", "Fb details" + name + " email " + email + " image" + fbUserImageUrl.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.e("FB", "facebook login canceled");
try {
LoginManager.getInstance().logOut();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(FacebookException e) {
Log.e("FB", "facebook login failed error");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void logout() {
LoginManager.getInstance().logOut();
linearLayout.setVisibility(View.GONE);
relativeLayout.setVisibility(View.GONE);
imgFacebookLogin.setVisibility(View.VISIBLE);
}
}
actvity_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samset.user.customfblogin.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:text="Facebook Login" />
<LinearLayout
android:id="@+id/profilelayout"
android:visibility="gone"
android:layout_below="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="35dp"
>
<com.facebook.login.widget.ProfilePictureView
android:id="@+id/picture"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_width="200sp"
android:layout_height="200sp"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/username"
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="10dp"
android:id="@+id/useremail"
android:text="Email"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:layout_centerInParent="true"
android:id="@+id/imgFacebookLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:src="@drawable/facebook_blue_circle" />
<RelativeLayout
android:id="@+id/logoutlayout"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content">
<ImageView
android:layout_centerInParent="true"
android:id="@+id/imgFacebooklogout"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:src="@drawable/sighout" />
<TextView
android:layout_marginTop="10dp"
android:id="@+id/logout"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Logout"
android:textColor="#fff"
android:textSize="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
styring.xml
<resources>
<string name="app_name">CustomFbLogin</string>
<string name="facebook_app_id">788910871208730</string>
</resources>
finally done and enjoy
Thank you
Full Source code CustomFbLogin
Live Sample
First you create new project and follow step by step ....
Step 1>
Add sdk in grale dependency
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.samset.user.customfblogin"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.facebook.android:facebook-android-sdk:4.6.0'
}
Add permission and configure need to fb login resources in android mainisfeast
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samset.user.customfblogin">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- if you want share your content then enable this commented code
<provider android:authorities="com.facebook.app.FacebookContentProvider788910871208730"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
-->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 2> Create fb_app_id go to this link.https://developers.facebook.com/apps/788910871208730/dashboard/ and create id.
now you write your android code
MainActvity.java
package com.samset.user.customfblogin;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.ProfilePictureView;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ImageView imgFacebookLogin, logout;
public static CallbackManager callbackManager;
private Intent intent;
private ImageView imageView;
private ProfilePictureView profile;
private TextView tvName, tvEmail;
private LinearLayout linearLayout;
private RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
imgFacebookLogin = (ImageView) findViewById(R.id.imgFacebookLogin);
logout = (ImageView) findViewById(R.id.imgFacebooklogout);
tvEmail = (TextView) findViewById(R.id.useremail);
tvName = (TextView) findViewById(R.id.username);
profile = (ProfilePictureView) findViewById(R.id.picture);
linearLayout = (LinearLayout) findViewById(R.id.profilelayout);
relativeLayout = (RelativeLayout) findViewById(R.id.logoutlayout);
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logout();
}
});
imgFacebookLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fblogin();
}
});
}
public void getHashKey() {
try {
PackageInfo info = getPackageManager().getPackageInfo("com.samset.user.customfblogin", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("HASH KEY:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
private void fblogin() {
callbackManager = CallbackManager.Factory.create();
final List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "user_friends", "public_profile");
//List<String> permissionNeed = Arrays.asList("publish_actions");
LoginManager.getInstance().logInWithReadPermissions(this, permissionNeeds);
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResults) {
GraphRequest request = GraphRequest.newMeRequest(loginResults.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
linearLayout.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.VISIBLE);
imgFacebookLogin.setVisibility(View.GONE);
String email = object.getString("email");
String name = object.getString("name");
String id = object.getString("id");
URL fbUserImageUrl = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
tvName.setText(name);
tvEmail.setText(email);
profile.setProfileId(object.getString("id"));
/* try {
Bitmap bmp = BitmapFactory.decodeStream((InputStream) new URL(fbUserImageUrl.toString()).getContent());
imageView.setImageBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
*/
Log.e("Fb", "Fb details" + name + " email " + email + " image" + fbUserImageUrl.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.e("FB", "facebook login canceled");
try {
LoginManager.getInstance().logOut();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(FacebookException e) {
Log.e("FB", "facebook login failed error");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void logout() {
LoginManager.getInstance().logOut();
linearLayout.setVisibility(View.GONE);
relativeLayout.setVisibility(View.GONE);
imgFacebookLogin.setVisibility(View.VISIBLE);
}
actvity_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samset.user.customfblogin.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:text="Facebook Login" />
<LinearLayout
android:id="@+id/profilelayout"
android:visibility="gone"
android:layout_below="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="35dp"
>
<com.facebook.login.widget.ProfilePictureView
android:id="@+id/picture"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_width="200sp"
android:layout_height="200sp"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/username"
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="10dp"
android:id="@+id/useremail"
android:text="Email"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:layout_centerInParent="true"
android:id="@+id/imgFacebookLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:src="@drawable/facebook_blue_circle" />
<RelativeLayout
android:id="@+id/logoutlayout"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content">
<ImageView
android:layout_centerInParent="true"
android:id="@+id/imgFacebooklogout"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:src="@drawable/sighout" />
<TextView
android:layout_marginTop="10dp"
android:id="@+id/logout"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Logout"
android:textColor="#fff"
android:textSize="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
styring.xml
<resources>
<string name="app_name">CustomFbLogin</string>
<string name="facebook_app_id">788910871208730</string>
</resources>
finally done and enjoy
Thank you
Full Source code CustomFbLogin
Live Sample
No comments:
Post a Comment