Created
February 2, 2017 06:26
-
-
Save ojhariddhish/2492dedee6e5a4dc1a4e825045303af1 to your computer and use it in GitHub Desktop.
Integrating Twitter In Android App
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"?> | |
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_twitter" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.dnsoftindia.featuredemo.activities.TwitterActivity"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:layout_margin="10dp"> | |
<com.twitter.sdk.android.core.identity.TwitterLoginButton | |
android:id="@+id/btnTwitter" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" /> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="120dp" | |
android:maxLines="3" | |
android:maxLength="140" | |
android:hint="@string/say_something" | |
android:id="@+id/etTweet" /> | |
<Button | |
android:layout_width="200dp" | |
android:layout_height="50dp" | |
android:text="@string/tweet" | |
android:id="@+id/btnTweet" | |
android:layout_gravity="center"/> | |
<TextView | |
android:text="" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/tvTwitterUser" | |
android:textSize="18sp" | |
android:lines="5" | |
android:gravity="center"/> | |
</LinearLayout> | |
</ScrollView> |
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
buildscript { | |
repositories { | |
maven { url 'https://maven.fabric.io/public' } | |
} | |
dependencies { | |
classpath 'io.fabric.tools:gradle:1.+' | |
} | |
} | |
apply plugin: 'com.android.application' | |
apply plugin: 'io.fabric' | |
apply plugin: 'com.neenbedankt.android-apt' | |
repositories { | |
maven { url 'https://maven.fabric.io/public' } | |
} | |
android { | |
compileSdkVersion 25 | |
buildToolsVersion "25.0.2" | |
defaultConfig { | |
applicationId "com.dnsoftindia.featuredemo" | |
minSdkVersion 16 | |
targetSdkVersion 25 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
lintOptions { | |
abortOnError false | |
} | |
dexOptions { | |
preDexLibraries = 'true' != System.getenv('CI') | |
javaMaxHeapSize "4g" | |
} | |
packagingOptions { | |
exclude 'META-INF/DEPENDENCIES' | |
exclude 'META-INF/LICENSE' | |
exclude 'META-INF/LICENSE.txt' | |
exclude 'META-INF/license.txt' | |
exclude 'META-INF/NOTICE' | |
exclude 'META-INF/NOTICE.txt' | |
exclude 'META-INF/notice.txt' | |
exclude 'META-INF/ASL2.0' | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { | |
exclude group: 'com.android.support', module: 'support-annotations' | |
}) | |
compile('com.twitter.sdk.android:twitter:2.3.1@aar') { | |
transitive = true; | |
} | |
compile 'com.android.support:appcompat-v7:25.1.0' | |
compile 'com.android.support:support-v4:25.1.0' | |
compile 'com.android.support:support-annotations:25.1.0' | |
compile 'com.android.support:cardview-v7:25.1.0' | |
compile 'com.android.support:customtabs:25.1.0' | |
compile 'com.android.support:design:25.1.0' | |
compile 'com.jakewharton:butterknife:8.2.1' | |
apt 'com.jakewharton:butterknife-compiler:8.2.1' | |
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4' | |
testCompile 'junit:junit:4.12' | |
} |
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.dnsoftindia.featuredemo.activities; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ScrollView; | |
import android.widget.TextView; | |
import com.dnsoftindia.featuredemo.R; | |
import com.dnsoftindia.featuredemo.utils.UIUtils; | |
import com.twitter.sdk.android.core.Callback; | |
import com.twitter.sdk.android.core.Result; | |
import com.twitter.sdk.android.core.TwitterException; | |
import com.twitter.sdk.android.core.TwitterSession; | |
import com.twitter.sdk.android.core.identity.TwitterLoginButton; | |
import com.twitter.sdk.android.tweetcomposer.TweetComposer; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
import butterknife.OnClick; | |
public class TwitterActivity extends AppCompatActivity { | |
// Note: Your consumer key and secret should be obfuscated in your source code before shipping. | |
private static final String TWITTER_KEY = "YOUR_TWITTER_API_KEY"; | |
private static final String TWITTER_SECRET = "YOUR_TWITTER_API_SECRET"; | |
@BindView(R.id.btnTwitter) | |
TwitterLoginButton btnTwitter; | |
@BindView(R.id.etTweet) | |
EditText etTweet; | |
@BindView(R.id.btnTweet) | |
Button btnTweet; | |
@BindView(R.id.tvTwitterUser) | |
TextView tvTwitterUser; | |
@BindView(R.id.activity_twitter) | |
ScrollView activityTwitter; | |
TwitterSession session = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_twitter); | |
ButterKnife.bind(this); | |
setTitle("Twitter"); | |
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); | |
Fabric.with(this, new Twitter(authConfig), new TweetComposer()); | |
btnTwitter.setCallback(new Callback<TwitterSession>() { | |
@Override | |
public void success(Result<TwitterSession> result) { | |
// The TwitterSession is also available through: | |
// Twitter.getInstance().core.getSessionManager().getActiveSession() | |
session = result.data; | |
String msg = "@" + session.getUserName() | |
+ " \n logged in! (#" + session.getUserId() + ")"; | |
tvTwitterUser.setText(msg); | |
// Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public void failure(TwitterException exception) { | |
Log.d("TwitterKit", "Login with Twitter failure", exception); | |
} | |
}); | |
} | |
@OnClick({R.id.btnTweet}) | |
public void onClick(View view) { | |
switch (view.getId()) { | |
case R.id.btnTweet: | |
if (etTweet.getText().toString()!=null && etTweet.getText().toString().length()>0) { | |
TweetComposer.Builder builder = new TweetComposer.Builder(this) | |
.text(etTweet.getText().toString()); | |
builder.show(); | |
} | |
else { | |
UIUtils.showToast(this, "Please enter your tweet."); | |
} | |
break; | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
// Make sure that the loginButton hears the result from any | |
// Activity that it triggered. | |
btnTwitter.onActivityResult(requestCode, resultCode, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment