Android - Adding A Facebook Share Button That Shares A Screen Shot
The purpose of this tutorial is to show you how to add a Facebook Share button to your Android Application that will share a screen shot.
I have added a new tutorial that shows just how to add a basic share button to your android application. You can read the tutorial here: Android - Add A Facebook Share Button
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.
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 functionality to this button. We are going to setup the button so that when it is clicked, it is going to call a function that will take a screen shot of our activity, and then share this image to Facebook. As we are adding code, Android Studio should update the imports that are needed. First we are going to declare some variables for our share button, the image we will upload, and a counter:
- Next, we will want to add a OnClickListener to our button, so we can call our function when it is clicked. In the onCreate() function, add the following code:
- Lastly, you will then need to create the postPicture() function that is called when the Share button is clicked. Inside this function we will want to do the following things:
- Take a screenshot of our current screen
- Create am image from this screenshot
- Have the sharing dialog pop up
- If the user click yes, have the user login to their Facebook account, and then post the image to their wall
- If the user clicks no, cancel the dialog
Here is the code for the function:
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 image to your wall.
Summary: This tutorial showed you how to add a Facebook Share Button to your application, and how to take a screen shot of your main view, and from there use the Share Button to share this image on Facebook.
Summary: This tutorial showed you how to add a Facebook Share Button to your application, and how to take a screen shot of your main view, and from there use the Share Button to share this image on Facebook.
I hope you enjoyed this tutorial. If you have any questions or comments, please feel free to post them below.
Update 10/08/2015:
Here is an example of what your "AndroidManifest.xml" file will look like if all you have is the share button:
Update 10/18/2015:
Here is a link to a gist that I created that contains all of the files that I modified during this tutorial: Gist
The only things that should be different is the package name, and the your app id for Facebook.
Update 12/10/2015:Update 10/08/2015:
Here is an example of what your "AndroidManifest.xml" file will look like if all you have is the share button:
Update 10/18/2015:
Here is a link to a gist that I created that contains all of the files that I modified during this tutorial: Gist
The only things that should be different is the package name, and the your app id for Facebook.
Here is a link to a new tutorial that shows how to add a basic share button: Android - Add Facebook Share Button