A Developers Blog: Android - Adding A Facebook Share Button

This blog has moved to GitHub, and you should be redirected shortly...

Thursday, December 10, 2015

Android - Adding A Facebook Share Button

I have gotten a lot of questions about adding a basic share button to your android app from one of my previous posts, so I decided to add a new tutorial that shows just how to add a basic share button.

The purpose of this tutorial is to show you how to add a Facebook Share button to your Android Application.

For this tutorial I was using: Android Studio 1.1.0, and Facebook SDK v4.0.0. You should be able to follow along with other versions of this software.

In order to follow along you will need the following:
  • Android Studio. If you do not already have this installed, you can get a copy from here: Android Studio. Just follow the download and install instructions from the website to get started.
  • You will need to have an existing project already created in Android Studio that is integrated with the Facebook SDK. If you do not have one, you can follow this tutorial: Android Facebook Integration Tutorial
If you have completed the following steps, we can get started.

1. Open up your Android Studio project, and navigate to your Android Manifest file. Once this is open, you will want to add the following activity:

2. Now, you will have to set up a content provider in your Android Manifest file. You will want to add the following code and replace "{APP_ID}" with your App Id:

3. To actually share your content, we will have to set up a Share Button for your application. To start with, open your "strings.xml" file located here:


4. In this file, add the following: <string name="share">Share</string>

5. Next, we will add the Share button to our "activity_main.xml" file. Open this file and add the following code:

You will want to update the "layout_width" and the "layout_height" with appropriate values for your application.

6. Now that we have the button setup, we will want to update our "MainActivity.java" file to add the button to our app. Once the button is added, we are going to define the content that we would like to share, for this tutorial we are going to share a link. Once the content is created, then we just need setup our button to share that content.

Here is an example of what your "MainActivity.java" file will look like if all you have is the share button:


Now, when you test out your application, you should be able to click the Share Button, login to your Facebook account, and then share the link to your wall.

Summary: This tutorial showed you how to add a Facebook Share Button to your application, and how to share a link.

Labels: , ,

2 Comments:

At November 29, 2016 at 10:57 PM , Blogger Unknown said...

Hi. Thank you so much for your tutorial. It helps me a lot. But I have some problems. I cant figure out why. I can share the link successfully but without signing in. It just signed in with my facebook account without putting in my account name or passwords.. So do you know why does it happen?

 
At December 15, 2016 at 7:21 PM , Blogger Scott Westover said...

Hi,

This can happen if the SDK finds a valid authentication token when you run your app. When the SDK finds the token, it will not require you to login to authenticate again. This can occur if you have the Facebook App installed on your device.

If you want to make the user sign in, then you could have your App force a logout.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home