Documentos de Académico
Documentos de Profesional
Documentos de Cultura
HOME
ANDROID
XAMARIN
OTHER TUTORIALS
CODE SNIPPETS
BLOG
Share
ANDROID
53 COMMENTS
-- Advertisement --
Connect with us
Javatechig
Subscribe
1/16
4/20/2016
Popular Posts
1.
android
activity_main.xml
<LinearLayout 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:orientation="vertical"
android:padding="@dimen/activity_vertical_margin" >
<RelativeLayout
android:id="@+id/login_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="visible" >
2.
3.
4.
5.
6.
7.
8.
9.
YouTubePlayerView Example in
Android Using YouTube API
<TextView
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
2/16
4/20/2016
AdvertiseHere
</RelativeLayout>
<LinearLayout
android:id="@+id/share_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="@+id/user_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="@string/hello"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0080B4" />
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@drawable/lakeside_view" />
<EditText
android:id="@+id/share_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#cceaf3"
android:hint="@string/share_instructions"
android:inputType="textMultiLine"
android:minLines="5"
android:padding="10dp" />
<Button
android:id="@+id/btn_share"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#0080B4"
android:text="@string/btn_share"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
activity_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
3/16
4/20/2016
</LinearLayout>
The above layout files using few of the strings which are defined in
strings.xml. This file also contains the mandatory twitter
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Strings used in app ui-->
<string name="app_name">TwitterShare</string>
<string name="action_settings">Settings</string>
<string name="hello">Hello, </string>
<string name="login_instructions">Login to twiter</string>
<string name="share_instructions">Enter share message</string>
<string name="btn_login">Login to Twitter</string>
<string name="btn_share">Share</string>
<!-- Twitter Configurations -->
<string name="twitter_callback">http://javatechig.android.app</string>
<string name="twitter_consumer_key">YOUR_CONSUMER_KEY_HERE</string>
<string name="twitter_consumer_secret">YOUR_CONSUMER_SECRET_HERE</string>
<string name="twitter_oauth_verifier">oauth_verifier</string>
<!-- End Configurations -->
</resources>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.twittershare"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<!-- Permission - Internet Connect -->
<uses-permission android:name="android.permission.INTERNET" />
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
4/16
4/20/2016
</manifest>
MainActivity.java
package com.example.twittershare;
import java.io.InputStream;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
/* Shared preference keys */
private static final String PREF_NAME = "sample_twitter_pref";
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
5/16
4/20/2016
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
6/16
4/20/2016
/**
* Saving user information, after user is authenticated for the first time.
* You don't need to show user to login, until user has a valid access toen
*/
private void saveTwitterInfo(AccessToken accessToken) {
long userID = accessToken.getUserId();
User user;
try {
user = twitter.showUser(userID);
String username = user.getName();
/* Storing oAuth tokens to shared preferences */
Editor e = mSharedPreferences.edit();
e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken
e.putString(PREF_KEY_OAUTH_SECRET, accessToken.getTokenSecret
e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
e.putString(PREF_USER_NAME, username);
e.commit();
} catch (TwitterException e1) {
e1.printStackTrace();
}
}
/* Reading twitter essential configuration parameters from strings.xml */
private void initTwitterConfigs() {
consumerKey = getString(R.string.twitter_consumer_key);
consumerSecret = getString(R.string.twitter_consumer_secret
callbackUrl = getString(R.string.twitter_callback);
oAuthVerifier = getString(R.string.twitter_oauth_verifier
}
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
7/16
4/20/2016
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
8/16
4/20/2016
WebViewActivity.java
package com.example.twittershare;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
9/16
4/20/2016
5. Output
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
10/16
4/20/2016
Share
Tweet
Nilanchala
A blogger, speaker, author, a bit of tech freak and a
so ware developer. He is a thought leader in the fusion of
design and mobile technologies.
Follow @npanigrahy
RELATED POSTS:
53 Comments
javatechig
Share
Recommend 5
Login
Sort by Newest
2 months ago
great code but i want for studio in fabric which i found on below link.
http://androidtechtutorials.bl...
gr8 code step by step explanation.
Reply Share
Aryan Roy
2 months ago
Reply Share
raman rayat
2 months ago
hi thanks for tutorial but after login my app is moving to call back url
it is not coming back and showing share layout screen after loging
...please help me
Reply Share
11/16
4/20/2016
Samarth Shah
3 months ago
Reply Share
rakesh kushwaha
4 months ago
Reply Share
rakesh kushwaha
4 months ago
Tirta
Reply Share
4 months ago
Reply Share
Nilanchala Panigrahy
Mod
Tech Morphosis
5 months ago
Reply Share
Hamza Anas
6 months ago
I've used your code and it works great but you havent explained a lot
of your java code. It would have been helpful to know what these
methods and reserved keywords are and how to use them.
Reply Share
Mu Baig
8 months ago
Reply Share
Astha Dangi
10 months ago
Reply Share
Jaideep Gupta
10 months ago
Reply Share
Jaideep Gupta
10 months ago
Hello Sir
this code works for me but I want to get Twitter friend list. How is it
possible?
Reply Share
Anand Kumar
a year ago
12/16
4/20/2016
anii
Reply Share
a year ago
Joy
Reply Share
a year ago
Reply Share
Priyanka
a year ago
hello sir, thanks for sharing code. But i want code in which user can
only follow the twitter account. Please help me
Reply Share
Nilanchala Panigrahy
Mod
Reply Share
a year ago
Reply Share
Nilanchala Panigrahy
Mod
a year ago
> Priyanka
Reply Share
a year ago
thanx sir but how where can i add this code? please
explain
Reply Share
a year ago
Reply Share
a year ago
Reply Share
a year ago
Thank You so much for this tutorial , this helped me a lot and i want
Facebook integration also can you send that link please . and your
code is perfectly working.
Reply Share
Nilanchala Panigrahy
Mod
a year ago
Hac zdemir
Reply Share
a year ago
hi javatechig
twitter.shutdown is not recognized for logout from twitter.
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
13/16
4/20/2016
Reply Share
a year ago
hi javatechig
I am using your code but my question is: How I can logout from
twitter?
thanks in advance
Reply Share
JavaTechig
Mod
Reply Share
a month ago
10 months ago
Mohammad Rababah
a year ago
hi all
How I can log out from twitter after using your code
thanks in advance
Reply Share
JavaTechig
Mod
BINIL S
Reply Share
a year ago
Reply Share
JavaTechig
Mod
Did you create your fresh set of keys? if yes, it takes a while
to get live.
Reply Share
a year ago
Reply Share
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
14/16
4/20/2016
JavaTechig
Mod
Alright!
Anupreet kaur
Reply Share
a year ago
i Got the toast image posted ...when i check that, there is no new
tweet
Reply Share
Anupreet kaur
a year ago
Reply Share
Jenifa mary
a year ago
Reply Share
Tom Bruyneel
a year ago
Reply Share
a year ago
Reply Share
peter
a year ago
Hi When i tried this code, it always crashes after i tried to login the
very rst time. Do you know what are the possible reasons?
Reply Share
Arturo Villela
a year ago
I dont know who you are... but i will nd you.. and i will thank you.. :)
atte: charlito.
Reply Share
JavaTechig
Mod
Justin Young
Reply Share
a year ago
Thank you! Been searching everywhere for the last few days on how
to get Twitter4J to work on Android. Even though their ocial site
says it's Android ready. Yours if the rst to work and not have very
complex issues. Now I can make progress on my class's nal
project. =D
For reference my setup is: Eclipse Juno with Android SDK. Twitter4J
4.0.2. Android versions 17, 18, and 19.
Reply Share
JavaTechig
Mod
Mohamed
Reply Share
a year ago
Is it possible to get the user's prole picture after logging in and set it
to an imageview ?
Reply Share
15/16
4/20/2016
ANDROID
JAVA
WORDPRESS
http://javatechig.com/android/how-to-integrate-twitter-in-android-application
16/16