Giter Site home page Giter Site logo

facebooklogin's Introduction

Facebook Login

Origami Studios presents a fully integrated Facebook library for android platform

Dependency

Step 1

Add the JitPack repository to your build file,

Gradle

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2

Add the dependency

dependencies {
    implementation 'com.github.mshehzad1:FacebookLogin:1.1'
}

Step 3

Facebook App Id

Create your facebook app on facebook developer console and get facebook app id.

AndroidManifest

Permissions: Add internet permissions

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

Meta Data: Add following metadata in app manifest file

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

Facebook App id: Add your facebook app id in strings file.

<resources>
    <string name="facebook_app_id">your facebook app id goes here .... </string>
</resources>

Step 4

Getting started ( First Method )

Add following code in your Login activity class. Or copy the code and paste in your Login class

public class Login extends AppCompatActivity {

    private OSFacebookLogin osFacebookLogin;

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

        Button fbSignIn = (Button) findViewById(R.id.fbLogin);
        Button fbSignOut = (Button) findViewById(R.id.fbLogout);

        osFacebookLogin = new OSFacebookLogin(new OSFacebookListener() {
            @Override
            public void onFacebookSuccess(OSResponse osResponse) {
                Toast.makeText(getApplicationContext(), osResponse.getName(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFacebookFailed(String s) {
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFacebookSignOut() {
                Toast.makeText(getApplicationContext(), "Sign out successfully", Toast.LENGTH_SHORT).show();
            }
        });


        fbSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                osFacebookLogin.performSignIn((Activity) getApplicationContext());
            }
        });

        fbSignOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                osFacebookLogin.performSignOut();
            }
        });
        
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        osFacebookLogin.onActivityResult(requestCode, resultCode, data);
    }
}

Second Method

Create a private OSFacebookLogin variable

 private OSFacebookLogin osFacebookLogin;

Initialize the osFacebookLogin in activity onCreate method

 osFacebookLogin = new OSFacebookLogin(this);

Implements OSFacebookListener in your Login activity, it will add following methods in your Login activity class

public class Login extends AppCompatActivity implements OSFacebookListener{

    private Button fbSignIn, fbSignOut;
    private OSFacebookLogin osFacebookLogin;

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

        fbSignIn = (Button) findViewById(R.id.fbLogin);
        fbSignOut = (Button) findViewById(R.id.fbLogout);
        
        // Initilizaing facebook login listener
        osFacebookLogin = new OSFacebookLogin(this);

        fbSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // For facebook signin, call this in click linstener. 
                osFacebookLogin.performSignIn(this);
            }
        });

        fbSignOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // For facebook signout, call this in click linstener. 
                osFacebookLogin.performSignOut();
            }
        });      
    }

@Override
    public void onFacebookSuccess(OSResponse osResponse) {
        Toast.makeText(getApplicationContext(), osResponse.getName(), Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onFacebookFailed(String errorMessage) {
        Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onFacebookSignOut() {
        Toast.makeText(getApplicationContext(), "Sign out successfully", Toast.LENGTH_SHORT).show();
    }
}

Override the onActivityResult method like this

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     osFacebookLogin.onActivityResult(requestCode, resultCode, data);
 }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.