How can I post on Twitter with Intent Action_send?

I have been struggling to send text from my app to Twitter.

The code below works to bring up a list of apps such as Bluetooth, Gmail, Facebook and Twitter, but when I select Twitter it doesn’t prefill the text as I would have expected.

  • What are widthMeasureSpec and heightMeasureSpec in Android custom Views?
  • Why does android get the wrong ssl certificate? (two domains, one server)
  • Android browser garbles events for two taps in quick succession
  • Set span for items in GridLayoutManager using SpanSizeLookup
  • Google Analytics on Android
  • Clear SingleChoice ListView selection
  • I know that there are issues around doing this with Facebook, but I must be doing something wrong for it to not be working with Twitter.

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, "Example Text");
    startActivity(Intent.createChooser(intent, "Share Text"));

    Related posts:

    How to change Toolbar home icon color
    HTML scrolling events in an Android WebView that's inside a ScrollView
    java libgdx move perspective camera
    Run code when Android app is closed/sent to background
    After sending data to server how to print response in next activity?
    Assign static IP address for Wifi network on Android 3.x and 4.x
  • How to create a slider screen (As in Tweetdeck) in Android?
  • why should i use android service instead of java thread
  • Improving Scroll Smoothness in an Android ListView
  • Custom arrows without image: Android
  • Sharing an Eclipse workspace between Linux and Windows
  • Ubuntu 64 with Android 64 Bundle cannot find adb executable
  • 5 Solutions collect form web for “How can I post on Twitter with Intent Action_send?”

    I’m using this snippet on my code:

    Intent tweetIntent = new Intent(Intent.ACTION_SEND);
    tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
    PackageManager packManager = getPackageManager();
    List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent,  PackageManager.MATCH_DEFAULT_ONLY);
    boolean resolved = false;
    for(ResolveInfo resolveInfo: resolvedInfoList){
            resolved = true;
        Intent i = new Intent();
        i.putExtra(Intent.EXTRA_TEXT, message);
        Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
    private String urlEncode(String s) {
        try {
            return URLEncoder.encode(s, "UTF-8");
        }catch (UnsupportedEncodingException e) {
  , "UTF-8 should always be supported", e);
            return "";

    Hope it helps.

    you can simply open the URL with the text and Twitter App will do it. 😉

    String url = "";
    Intent i = new Intent(Intent.ACTION_VIEW);

    and it will also open the browser to login at the tweet if twitter app is not found.

    First you have to check if the twitter app installed on the device or not then share the text on twitter:

            // Check if the Twitter app is installed on the phone.
            getActivity().getPackageManager().getPackageInfo("", 0);
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setClassName("", "");
            intent.putExtra(Intent.EXTRA_TEXT, "Your text");
        catch (Exception e)
            Toast.makeText(getActivity(),"Twitter is not installed on this device",Toast.LENGTH_LONG).show();

    Try this, I used it and worked great

      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(""));

    For sharing text and image on Twitter, more controlled version of code is below, you can add more methods for sharing with WhatsApp, Facebook … This is for official App and does not open browser if app not exists.

    public class IntentShareHelper {
        public static void shareOnTwitter(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
            if (fileUri != null) {
                intent.putExtra(Intent.EXTRA_STREAM, fileUri);
            try {
            } catch (android.content.ActivityNotFoundException ex) {
                showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
        public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
        private static void showWarningDialog(Context context, String message) {
            new AlertDialog.Builder(context)
                    .setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

    For getting Uri from File, use below class:

    public class UtilityFile {
        public static @Nullable Uri getUriFromFile(Context context, @Nullable File file) {
            if (file == null)
                return null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                try {
                    return FileProvider.getUriForFile(context, "", file);
                } catch (Exception e) {
                    return null;
            } else {
                return Uri.fromFile(file);
        // Returns the URI path to the Bitmap displayed in specified ImageView       
        public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
            Drawable drawable = imageView.getDrawable();
            Bitmap bmp = null;
            if (drawable instanceof BitmapDrawable) {
                bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            } else {
                return null;
            // Store image to default external storage directory
            Uri bmpUri = null;
            try {
                // Use methods on Context to access package-specific directories on external storage.
                // This way, you don't need to request external read/write permission.
                File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
                FileOutputStream out = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
                bmpUri = getUriFromFile(context, file);
            } catch (IOException e) {
            return bmpUri;

    For writing FileProvider, use this link:

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.