Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dan Maia
1,078 PointsAndroid development - Facebook SDK with Android Studio.
Been trying to add the Facebook SDK to my project and followed all the steps they have provided to get it to work. I have googled this question and cant seem to get an solution so getting to a point of pulling my hair out now lol
3 Answers
Dan Maia
1,078 PointsOk these are the steps i took exactly:
I added this before the dependecies: repositories { mavenCentral() }
Then added the Facebook SDK through the library dependency which imported the dependency in my app gradel as - compile 'com.facebook.android:facebook-android-sdk:4.0.0'
Added the <uses-permission android:name="android.permission.INTERNET"></uses-permission> to my android Manifest
4.Then added this to the manifest before any activity tag but after the apptheme <meta-data android:name="com.facebook.FacebookSdk" android:value="@string/app_id"></meta-data>
Then created a new activity for the login as follows: <activity android:name="com.facebook.FacebookActivity" android:configChanges= "keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" />
At which point i then initiated FacebookSdk.sdkInitialize(getApplicationContext());
I created the Login button through the main activity xml file as which shown rendering problems:
<com.facebook.login.widget.LoginButton android:id="@+id/login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:layout_marginBottom="30dp" />
I am sure this has been followed correctly but still cant seem to get it to work. I have also added the app_id key to my strings resource file. When i run the application it crashes. I am not sure what is going on. :(
Ben Junya
12,365 PointsHey Dan, can I see your StackTrace?
You really shouldn't have to add much code in MainActivity.class - the button is programmed to automatically fire off what it needs to fire.
Ben Junya
12,365 PointsAlso, just wanted to check -
Did you make a Facebook app on developers.facebook.com? Let's try to work on this together. I'm going to fork your repo.
Dan Maia
1,078 PointsYes, I created a developer app on the Facebook developers page. I added the app id to the manifest etc... but still couldn't get it work. This seems to be such a pain and hassle to integrate the API into apps for developers. Although it seems to be just me though so I must be doing something wrong but not sure what as i have followed all the instructions to get started :/
Ben Junya
12,365 PointsIt can be pretty confusing. Understanding oAuth can be a real pain.
The getting started should be able to get you through most of it. - https://developers.facebook.com/docs/android/getting-started
You'll also need a development hash key. You can get this using the Terminal on a Mac.
Check this out. I made a little side project to get FB login in a standalone app. Just replace my API keys and secret keys with your own. https://github.com/MrBenJ/FBLogin-android.git
Hope this helps! Ben
Dan Maia
1,078 PointsStill cant seem to get the login button to appear: https://github.com/dannymaia92/Social_Media_Test.git
Ben Junya
12,365 PointsHey Dan,
Make sure you add another activity to your manifest file. It should look something like this: https://github.com/MrBenJ/FBLogin-android/blob/master/app/src/main/AndroidManifest.xml
I grabbed this from the getting started docs on FB for Android:
Using Login or Share
To use Facebook Login or Share, also add the FacebookActivity to the manifest:
<activity android:name="com.facebook.FacebookActivity" android:configChanges= "keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" />
Let's keep this conversation going. It took me about an hour or 2 to figure out FB login and OAuth 2, but once you get it, you can pretty much use it for any social network.
Best, Ben
Dan Maia
1,078 PointsThank you very much, I appreciate the help. I am new to android development so want to play with other API's. The first link was the one I used to get me started but could never get it to work. After researching they mentioned about the hash key which i then got but still couldnt get it to work still.
i noticed you added the SDK using the built maven, did you make any other changes to the build gradle? I also noticed you used the SDK version 3.21.1 i have been trying to use the 4.0.0 version is there a reason why the 4.0.0 wont work?
Ben Junya
12,365 PointsI added the Facebook SDK through Grade in the dependencies - via the mavenCentral() repository - that's the only other change I've made.
I used the older SDK version because that's what was available. Version 4.0 may have some differences in it, and that must have been released since Facebook had it's F8 developer event not too long ago. Regardless, try rolling back your version to see what's happening.
I have an app I've been building with that version, and it's been fine so far. Let me know if you have any more trouble.
Dan Maia
1,078 PointsDan Maia
1,078 PointsFollowed the steps from here: https://developers.facebook.com/docs/android/getting-started