Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twitter integration on android app

I would like to integrate Twitter into my Android application so that I can post messages to Twitter.

like image 914
mudit Avatar asked Nov 23 '09 12:11

mudit


People also ask

Does Twitter work on Android?

The Twitter for Android app is available for phones running Android 7.93. 4 and above. We no longer support older versions. If you remain on these versions, please note that you may not be able to install or update Twitter for Android app in the Google Play Store.

Can you use TweetDeck on Android?

TweetDeck Teams — a feature that lets users share access to Twitter accounts without having to share a password — will now work in the Twitter app for iOS and Android.

Why is my device not compatible with Twitter?

Some users have complained about the "Your Video File Is Not Compatible" error on Twitter. Poor network speed, defective video, and an unsupported video standard or format are all reasons why Twitter won't upload video. The third is the primary reason for Twitter's failure to upload media.


2 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();     }  } 
like image 161
Cjames Avatar answered Sep 19 '22 14:09

Cjames


In addition to d.'s solid choices, you could:

  • Use ACTION_SEND Intents with createChooser(), 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
like image 20
CommonsWare Avatar answered Sep 23 '22 14:09

CommonsWare