cakan cakan - 1 year ago 62
Android Question

Tweeting from Android application without Twitter app

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.");;

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?

Answer Source

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;

    protected void onCreate(Bundle savedInstanceState) {

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

            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());
                    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("" + stringToShow);

    public void 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download