6
votes

I'm working on an Android application that allows users to share an article via Facebook and/or Twitter. Facebook share works well using ShareDialog, which opens up Facebook share dialog in my application.

The problem I'm having is with posting to Twitter. If the user has a Twitter app installed, share works perfectly. When there is no Twitter app installed on the device, then the Twitter share page is opened in the default browser and user never gets returned to my application after tweeting, which is kind of a bad user experience.

My code for tweet posting is:

Intent intent = new TweetComposer.Builder(context).text("Tweet text.").createIntent();
startActivityForResult(intent, SHARE_ACTION_TWITTER);

I have also tried this:

TweetComposer.Builder builder = new TweetComposer.Builder(this).text("Tweet text.");
builder.show();

Is there a way to get a dialog in my application (similar to Facebook share behavior) when the user does not have the Twitter app installed?

Additionally, for statistics, I would like to know if the user has successfully posted a tweet. How can this be achieved with Fabric Twitter API if user does not have Twitter app installed? Should I use a different API?

3

3 Answers

4
votes

The solution was to create a custom webview for tweeting. It doesn't even require the Fabric Twitter API.

Most important part is to create a webview activity:

public class TweetCustomWebView extends AppCompatActivity {

    android.webkit.WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_activity);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            final String stringToShow = extras.getString("tweettext");
            webView = (android.webkit.WebView) findViewById(R.id.wv);

            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
                    if (url.contains("latest_status_id=")) {
                        // Twitted
                        setResult(Activity.RESULT_OK, new Intent());
                        TweetCustomWebView.this.finish();
                    }
                    view.loadUrl(url);
                    return true;
                }

                public void onPageFinished(android.webkit.WebView view, String url) {
                    // Finished loading url
                }

                public void onReceivedError(android.webkit.WebView view, int errorCode, String description, String failingUrl) {
                    Log.e("", "Error: " + description);
                    setResult(Activity.RESULT_CANCELED, new Intent());

                }
            });
            webView.loadUrl("https://twitter.com/intent/tweet?text=" + stringToShow);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setResult(Activity.RESULT_CANCELED, new Intent());
    }

}

And a layout like this:

<WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/wv"/>

In the AndroidManifest.xml we must add the activity for our webview inside the <application> tag:

<activity android:name=".TweetCustomWebView" />

Last step is to call our vebview when user taps Share on Twitter button:

Intent intent = new Intent(MainActivity.this, TweetCustomWebView.class);
intent.putExtra("tweettext", "Text to tweet");
startActivityForResult(intent, 100);

That should be it. I hope this will help someone.

3
votes

use below code to post status to twitter.it use's twitter's rest api calls: i am sharing successfully from my app. it doesn't require twitter app to be installed.

TwitterAuthClient mTwitterAuthClient = new TwitterAuthClient();
     mTwitterAuthClient.authorize(this, new Callback<TwitterSession>() {
                @Override
                public void success(Result<TwitterSession> result) {
                    TwitterSession session = result.data;
                    twitterApiClient = TwitterCore.getInstance().getApiClient(session);
                    statusesService = twitterApiClient.getStatusesService();

                            postToTwitter("here goes your share message to post status");
                        }
                    } 

public void postToTwitter(String Message) {
        String message;

        StatusesService statusesService = twitterApiClient.getStatusesService();
        statusesService.update(message, null, null, null, null, null, null, null, mediaId, new Callback<Tweet>() {
            @Override
            public void success(Result<Tweet> result) {
               //handle success case
            }

            @Override
            public void failure(TwitterException exception) {
               //handle failure case

            }
        });
    }
0
votes

I guess the correct answer is using Update Status API call. I'm using the latest Twitter SDK version: com.twitter.sdk.android:twitter:3.1.1

public void publishTwitter(final String message) {
    final TwitterApiClient apiClient = TwitterCore.getInstance().getApiClient();
    final StatusesService statusesService = apiClient.getStatusesService();
    final Call<Tweet> update = statusesService.update(message, null, null, null, null, null, null, null, null);
    update.enqueue(new com.twitter.sdk.android.core.Callback<Tweet>() {
        @Override
        public void success(Result<Tweet> result) {
            Log.d("TweetTest", "Tweet generated");
        }

        @Override
        public void failure(TwitterException exception) {
            Log.e("TweetTest", exception.getLocalizedMessage());
        }
    });
}


NOTE
The user must be authenticated before calling this API call. For further details, check this post on Twitter Dev Forum.

Best regards