I would like to integrate Twitter into my Android application so that I can post messages to Twitter.
-
this is as simple as eating your favourite cake.. follow this tutorial http://tech-papers.org/integrate-twitter-with-android-application/ – Balwinder SIngh Oct 25 '14 at 16:40
7 Answers
This is how I do it
First i made a Dialog for the webview Twitter_Dialog.java
public class Twitter_Dialog extends Dialog
{
static final int BLUE = 0xFF6D84B4;
static final float[] DIMENSIONS_DIFF_LANDSCAPE =
{ 20, 60 };
static final float[] DIMENSIONS_DIFF_PORTRAIT =
{ 40, 60 };
static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
static final int MARGIN = 4;
static final int PADDING = 2;
static final String DISPLAY_STRING = "touch";
private String mUrl;
private ProgressDialog mSpinner;
private WebView mWebView;
private LinearLayout mContent;
private TextView mTitle;
public Twitter_Dialog(Context context, String url)
{
super(context);
mUrl = url;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mSpinner = new ProgressDialog(getContext());
mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
mSpinner.setMessage("Loading...");
mContent = new LinearLayout(getContext());
mContent.setOrientation(LinearLayout.VERTICAL);
setUpTitle();
setUpWebView();
Display display = getWindow().getWindowManager().getDefaultDisplay();
final float scale = getContext().getResources().getDisplayMetrics().density;
int orientation = getContext().getResources().getConfiguration().orientation;
float[] dimensions = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
addContentView(mContent, new LinearLayout.LayoutParams(display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)), display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
}
private void setUpTitle()
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
Drawable icon = getContext().getResources().getDrawable(R.drawable.twitter_icon);
mTitle = new TextView(getContext());
mTitle.setText("Website");
mTitle.setTextColor(Color.WHITE);
mTitle.setTypeface(Typeface.DEFAULT_BOLD);
mTitle.setBackgroundColor(BLUE);
mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
mTitle.setCompoundDrawablePadding(MARGIN + PADDING);
mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
mContent.addView(mTitle);
}
private void setUpWebView()
{
mWebView = new WebView(getContext());
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new Twitter_Dialog.DialogWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
System.out.println(" mURL = "+mUrl);
mWebView.loadUrl(mUrl);
mWebView.setLayoutParams(FILL);
mContent.addView(mWebView);
}
private class DialogWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
Twitter_Dialog.this.dismiss();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
mSpinner.show();
}
@Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
String title = mWebView.getTitle();
if (title != null && title.length() > 0){
mTitle.setText(title);
if(title.equals("Twitter")){
//This will close the Dialog after tweeting
Twitter_Dialog.this.dismiss();
}
}
mSpinner.dismiss();
}
}
}
//And then into your Main.java
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Twitter_Dialog(Main.this,"http://twitter.com/?status="+Uri.encode("Twitter Post")).show();
}
}

- 20,649
- 15
- 100
- 138

- 1,852
- 1
- 20
- 23
In addition to d.'s solid choices, you could:
- Use
ACTION_SEND
Intents
withcreateChooser()
, and if the user has a Twitter application installed (Twidroid) they can use it to update their status - Use an existing Twitter Java API, like JTwitter

- 986,068
- 189
- 2,389
- 2,491
-
I checked it out just to see how you did the integration and I ended up downloading. Great job! – SapphireSun Jan 09 '11 at 09:38
-
5`ACTION_SEND` is too vague. Gmail and dropbox are eligible. [I suggest you filter](http://regis.decamps.info/blog/2011/06/intent-to-open-twitter-client-on-android/) the list based on known package names. – rds Jun 02 '11 at 10:05
-
Deleted a comment, updated details: I don't use JTwitter in my Android app any more (and the Twitter API and Terms has entirely changed since then, making direct integration more challenging). The source code for the app I made that used to do this, Congress, has also moved, to: github.com/sunlightlabs/congress-android -- but it no longer has direct Twitter integration. – Eric Mill Jan 07 '14 at 18:56
Everything you need to know about communicating with Twitter is here.
For sending HTTP requests from your application, check out this guide.

- 128,221
- 31
- 203
- 222
You can use Twitter Helper for integrating Twitter into your Android app. Its very simple.

- 8,300
- 9
- 52
- 84
Try with this simple client TwitterEasyClient
Just add permissions in your manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
And use it in this way:
//setup
TwitterDialogFragment twitterDialog = new TwitterDialogFragment.Builder("message","url.com") //
.callbackUrl("http://www.website.com") //
.consumerKey("XXXXXXXXXXXXXXXXXXXXXX") //
.consumerSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") //
.urlOAuth("oauth_verifier") //
.build();
//show the dialog
twitterDialog.show(getSupportFragmentManager(), TwitterDialogFragment.class.getSimpleName());

- 5,747
- 2
- 28
- 26
Always go for the latest technologies as twitter integration can be done easily using Twitter4j, also the APIs provided by twitter does change from time to time. Twiter sdk would a good option. U can find the details for it here for twitter4j.
For some people who want to use twitter4j and DialogFragment also support orientation changing check out my gist https://gist.github.com/zeroarst/10071064adcf171277f9

- 3,098
- 1
- 35
- 42