A Developers Blog: July 2015

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

Tuesday, July 14, 2015

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.

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:

7. 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:

8. 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.

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:

Here is a link to a new tutorial that shows how to add a basic share button: Android - Add Facebook Share Button

Labels: , ,

Monday, July 13, 2015

July Ask the Community Experts Session: Advanced AEM Component Development

Adobe will be holding another "Ask the Community Experts Session" this month on July 28th at 11 A.M. - 12:00 P.M. EST. This session will be discussing the following topics:
  • Creating components that use CQ Ext APIs
  • Building custom Xtypes
  • Invoke Sling Servlets
  • Building advanced dialogs for components
If you have not attended one of these AEM sessions, they are a great chance to listen to some of the best experts in the industry, a great place to ask questions, and a good way to start networking. I look forward to seeing you all there.

Here is a link to sign up if you are interested: Ask the Community Experts

This session will be hosted by Scott Macdonad, Jaison Sunny, and Praveen Dubey.

Labels: ,

Sunday, July 12, 2015

Android Facebook Integration Tutorial

The purpose of this tutorial is to show you how to integrate the Facebook SDK with your Android application. Throughout this tutorial, we will register our application with Facebook and then add the SDK to our project.

For this tutorial I was using: Android Studio 1.1.0, OpenSSL for Windows, 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.
  • SSL. If you do not already have this installed, you can get a copy from here: OpenSSL. 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.
  • You will need to have a developer account with Facebook in order to register your application, and work with their SDK. You can do this here: https://developers.facebook.com/
If you have completed the following steps, we can get started.

1. Go to the Facebook developers website, hover over the "My Apps" button, and then click on "Add a New App".


2. In the "Add a New App" window that appears, choose "Android".


3. Next, type in the name for the app you want to create, and then click the "Create New Facebook App ID".


4. Then, in the "Create a New App ID" window that appears, choose "No" for "is this a test version of another app", and choose a relevant category for your application. Lastly, click on the "Create App ID" button.


5. Open your Android Studio project,  and open the "build.gradle" file at the app level.


6. In this file, add " compile 'com.facebook.android:facebook-android-sdk:4.4.0' " to your dependencies and add "mavenCentral()" to your repositories. You will want to rebuild your project after this.


9. Go back to the Facebook Developers site, and in the spot "Tell us about your Android project", you will want to enter the package name for your application, and the default activity class name.


10. You will now need to generate an Android key hash for your development environment in order to test your application. In order to do this, you will need to have SSL installed. Open a command prompt window and navigate to your "Java JRE" folder. Once here you will want to run the following command:

11. The command prompt should generate a 28-character key hash. You will want to enter this in the "Development Key Hashes" field on the Facebook Developers site:

12. Then, you will want to click on the "Dashboard" link to take you to the app dashboard. You will see that your app now has an "App ID" and an "App Secret". You will need to add the APP ID to your Android App.


13. Returning back to your Android project, open the "MainActivity" file for your app. At the top of the file, in the imports section add the following: import com.facebook.FacebookSdk;

14. You will need to initialize the Facebook SDK before you can use it. This can be done by calling the Facebook.SDK.sdkInitialize from onCreate function. Your onCreate function should look similar to this:

15. Now, we just need to add your Facebook App ID to your Application. Open your "strings.xml" file located here:


16. You will want to add a new string to this file. The name of the string should be "facebook_app_id", and the value of the string should be your apps id. Here is an example: <string name="facebook_app_id">Your App ID Goes Here</string>

17. Finally, you will need to update your Android manifest. Once you have this file open, you will need to add the following:
  • <uses-permission android:name="android.permission.INTERNET"/>
  • <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
You have now integrated the Facebook SDK with your Android application. Now, you will be able to use the Facebook SDK to add a Facebook Login, Share content to Facebook, or even send App Invites to friends on Facebook.

Summary: This tutorial showed you how to crate a Facebook Developers App, how to add the Facebook SDK to your Android Application, and how to sync the two together.

I hope you enjoyed this tutorial. If you have any questions or comments, please feel free to post them below.

Labels: , ,