-
-
Save scottwestover/1514ea0404c14da5ff06 to your computer and use it in GitHub Desktop.
Files for simple Facebook Share button on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | |
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> | |
<TextView android:text="@string/hello_world" android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<com.facebook.share.widget.ShareButton | |
android:id="@+id/share_btn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:contentDescription="@string/share" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.creativerendering.myapplication" > | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<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" /> | |
<provider android:authorities="com.facebook.app.FacebookContentProviderYOURAPPIDHERE" | |
android:name="com.facebook.FacebookContentProvider" | |
android:exported="true"/> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 21 | |
buildToolsVersion "22.0.1" | |
defaultConfig { | |
applicationId "com.creativerendering.myapplication" | |
minSdkVersion 14 | |
targetSdkVersion 21 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile 'com.android.support:appcompat-v7:22.0.0' | |
compile 'com.facebook.android:facebook-android-sdk:4.0.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.creativerendering.myapplication; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.graphics.Bitmap; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import com.facebook.FacebookSdk; | |
import com.facebook.share.model.SharePhoto; | |
import com.facebook.share.model.SharePhotoContent; | |
import com.facebook.share.widget.ShareButton; | |
public class MainActivity extends ActionBarActivity { | |
// share button | |
private ShareButton shareButton; | |
//image | |
private Bitmap image; | |
//counter | |
private int counter = 0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
//initialize facebook sdk | |
FacebookSdk.sdkInitialize(getApplicationContext()); | |
setContentView(R.layout.activity_main); | |
//share button | |
shareButton = (ShareButton) findViewById(R.id.share_btn); | |
shareButton.setOnClickListener(new View.OnClickListener() { | |
public void onClick(View view) { | |
postPicture(); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
public void postPicture() { | |
//check counter | |
if(counter == 0) { | |
//save the screenshot | |
View rootView = findViewById(android.R.id.content).getRootView(); | |
rootView.setDrawingCacheEnabled(true); | |
// creates immutable clone of image | |
image = Bitmap.createBitmap(rootView.getDrawingCache()); | |
// destroy | |
rootView.destroyDrawingCache(); | |
//share dialog | |
AlertDialog.Builder shareDialog = new AlertDialog.Builder(this); | |
shareDialog.setTitle("Share Screen Shot"); | |
shareDialog.setMessage("Share image to Facebook?"); | |
shareDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
//share the image to Facebook | |
SharePhoto photo = new SharePhoto.Builder().setBitmap(image).build(); | |
SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build(); | |
shareButton.setShareContent(content); | |
counter = 1; | |
shareButton.performClick(); | |
} | |
}); | |
shareDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.cancel(); | |
} | |
}); | |
shareDialog.show(); | |
} | |
else { | |
counter = 0; | |
shareButton.setShareContent(null); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">My Application</string> | |
<string name="hello_world">Hello world!</string> | |
<string name="action_settings">Settings</string> | |
<string name="facebook_app_id">YOURAPPIDHERE</string> | |
<string name="share">Share</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment