Hello Friend,
Today we are create fb login with our app.
So this is simple to fb integration in our android app.
Step 1 :- Go to fb developer site and click on add a new app and fill the display name and emial than click on create app id.
Step 2 :- Now you are in deshboard of fb, now select fb login.
Step 3 :- Click on set up and select android.
Step 4 :- After do above step you can see all next step on developer site.
Code.
AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<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" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
build.gradle
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
MainActivity.java
public class MainActivity extends AppCompatActivity {
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login);
loginButton.setReadPermissions("email");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
getUserDetails(loginResult);
}
@Override
public void onCancel() {
// App code
Toast.makeText(getApplicationContext(), "cancle", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
});
}
protected void getUserDetails(LoginResult loginResult) {
GraphRequest data_request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject json_object,
GraphResponse response) {
Intent intent = new Intent(MainActivity.this,
FacebookSignup.class);
intent.putExtra("userProfile", json_object.toString());
startActivity(intent);
}
});
Bundle permission_param = new Bundle();
permission_param.putString("fields", "id,name,email, picture.width(120).height(120)");
data_request.setParameters(permission_param);
data_request.executeAsync();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent
android:orientation="vertical">
<com.facebook.login.widget.LoginButton
android:id="@+id/login"
android:layout_width="150dp"
android:layout_height="61dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:paddingBottom="18dp"
android:paddingTop="18dp"
android:text="Sign in" />
</LinearLayout>
FacebookSignup.java
public class FacebookSignup extends AppCompatActivity {
private ImageView imgProfilePic;
private TextView txtName, txtEmail;
JSONObject response, profile_pic_data, profile_pic_url;
private boolean isSMSBroadCastReceiver = false;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebooksignup);
imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
txtName = (TextView) findViewById(R.id.txtName);
txtEmail = (TextView) findViewById(R.id.txtEmail);
Intent intent = getIntent();
String jsondata = intent.getStringExtra("userProfile");
TextView txtName = (TextView) findViewById(R.id.txtName);
ImageView imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
try {
response = new JSONObject(jsondata);
txtEmail.setText(response.get("email").toString());
txtName.setText(response.get("name").toString());
profile_pic_data = new JSONObject(response.get("picture").toString());
profile_pic_url = new JSONObject(profile_pic_data.getString("data"));
Picasso.with(this).load(profile_pic_url.getString("url"))
.into(imgProfilePic);
} catch (Exception e) {
e.printStackTrace();
}
}
}
facebooksignup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="50dp"
android:gravity="center"
android:text="Profile"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="@+id/avatar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="@+id/imgProfilePic"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/txtName"
android:layout_width="290dp"
android:layout_height="30dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/imgProfilePic"
android:ellipsize="end"
android:hint="Your name"
android:maxLength="25"
android:maxLines="1"
android:textSize="20dp" />
</RelativeLayout>
<TextView
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:hint=" E-mail address"
android:maxLength="50"
android:maxLines="1"
android:textSize="20dp" />
</LinearLayout>
</RelativeLayout>
Today we are create fb login with our app.
So this is simple to fb integration in our android app.
Step 1 :- Go to fb developer site and click on add a new app and fill the display name and emial than click on create app id.
Step 2 :- Now you are in deshboard of fb, now select fb login.
Step 3 :- Click on set up and select android.
Step 4 :- After do above step you can see all next step on developer site.
Code.
AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<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" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
build.gradle
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
MainActivity.java
public class MainActivity extends AppCompatActivity {
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login);
loginButton.setReadPermissions("email");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
getUserDetails(loginResult);
}
@Override
public void onCancel() {
// App code
Toast.makeText(getApplicationContext(), "cancle", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
});
}
protected void getUserDetails(LoginResult loginResult) {
GraphRequest data_request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject json_object,
GraphResponse response) {
Intent intent = new Intent(MainActivity.this,
FacebookSignup.class);
intent.putExtra("userProfile", json_object.toString());
startActivity(intent);
}
});
Bundle permission_param = new Bundle();
permission_param.putString("fields", "id,name,email, picture.width(120).height(120)");
data_request.setParameters(permission_param);
data_request.executeAsync();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent
android:orientation="vertical">
<com.facebook.login.widget.LoginButton
android:id="@+id/login"
android:layout_width="150dp"
android:layout_height="61dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:paddingBottom="18dp"
android:paddingTop="18dp"
android:text="Sign in" />
</LinearLayout>
FacebookSignup.java
public class FacebookSignup extends AppCompatActivity {
private ImageView imgProfilePic;
private TextView txtName, txtEmail;
JSONObject response, profile_pic_data, profile_pic_url;
private boolean isSMSBroadCastReceiver = false;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebooksignup);
imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
txtName = (TextView) findViewById(R.id.txtName);
txtEmail = (TextView) findViewById(R.id.txtEmail);
Intent intent = getIntent();
String jsondata = intent.getStringExtra("userProfile");
TextView txtName = (TextView) findViewById(R.id.txtName);
ImageView imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
try {
response = new JSONObject(jsondata);
txtEmail.setText(response.get("email").toString());
txtName.setText(response.get("name").toString());
profile_pic_data = new JSONObject(response.get("picture").toString());
profile_pic_url = new JSONObject(profile_pic_data.getString("data"));
Picasso.with(this).load(profile_pic_url.getString("url"))
.into(imgProfilePic);
} catch (Exception e) {
e.printStackTrace();
}
}
}
facebooksignup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="50dp"
android:gravity="center"
android:text="Profile"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="@+id/avatar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="@+id/imgProfilePic"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/txtName"
android:layout_width="290dp"
android:layout_height="30dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/imgProfilePic"
android:ellipsize="end"
android:hint="Your name"
android:maxLength="25"
android:maxLines="1"
android:textSize="20dp" />
</RelativeLayout>
<TextView
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:hint=" E-mail address"
android:maxLength="50"
android:maxLines="1"
android:textSize="20dp" />
</LinearLayout>
</RelativeLayout>
Comments
Post a Comment