Open page in Twitter app from other app - Android

AndroidTwitter

Android Problem Overview


I was looking for some way to launch Twitter app and open a specified page from my application, without webview. I found the solution for Facebook here: https://stackoverflow.com/questions/10788247/opening-facebook-app-on-specified-profile-page?answertab=active#tab-top

I need something similar.

EDIT I just found a solution:

try {
    Intent intent = new Intent(Intent.ACTION_VIEW,
    Uri.parse("twitter://user?screen_name=[user_name]"));
    startActivity(intent);
} catch (Exception e) {
    startActivity(new Intent(Intent.ACTION_VIEW,
    Uri.parse("https://twitter.com/#!/[user_name]"))); 
}

Android Solutions


Solution 1 - Android

Based on fg.radigales answer, this is what I used to launch the app if possible, but fall back to the browser otherwise:

Intent intent = null;
try {
	// get the Twitter app if possible
	this.getPackageManager().getPackageInfo("com.twitter.android", 0);
	intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID"));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
	// no Twitter app, revert to browser
	intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/PROFILENAME"));
}
this.startActivity(intent);

UPDATE

Added intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to fix an issue where twitter was opening inside my app instead of as a new activity.

Solution 2 - Android

This worked for me: twitter://user?user_id=id_num

Solution 3 - Android

Open page on Twitter app from other app using Android in 2 Steps:

1.Just paste the below code (on button click or anywhere you need)

Intent intent = null;
try{
   // Get Twitter app
   this.getPackageManager().getPackageInfo("com.twitter.android", 0);
   intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch () {
   // If no Twitter app found, open on browser
   intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERNAME"));
}

2.intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));

To get USER_ID just write username https://tweeterid.com/ and get Twitter User ID in there

Reference: https://solutionspirit.com/open-page-twitter-application-android/

Hope it will Help :)

Solution 4 - Android

My answer builds on top of the widely-accepted answers from fg.radigales and Harry. If the user has Twitter installed but disabled (for example by using App Quarantine), this method will not work. The intent for the Twitter app will be selected but it will not be able to process it as it is disabled.

Instead of:

getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));

You can use the following to decide what to do:

PackageInfo info = getPackageManager().getPackageInfo("com.twitter.android", 0);
if(info.applicationInfo.enabled)
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
else
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/wrkoutapp"));

Solution 5 - Android

Just try this code snippet. It will help you.

//Checking If the app is installed, according to the package name
        Intent intent = new Intent();
        intent.setType("text/plain");
        intent.setAction(Intent.ACTION_SEND);
        final PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

        for (ResolveInfo resolveInfo : list) 
        {
            String packageName = resolveInfo.activityInfo.packageName;

            //In case that the app is installed, lunch it.
            if (packageName != null && packageName.equals("com.twitter.android")) 
            {
                try
                {
                    String formattedTwitterAddress = "twitter://user/" ;
                    Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
                                    long twitterId = <Here is the place for the twitter id>
                    browseTwitter.putExtra("user_id", twitterId);
                    startActivity(browseTwitter);

                    return;
                }
                catch (Exception e) 
                {

                }
            }
        }

        //If it gets here it means that the twitter app is not installed. Therefor, lunch the browser.
        try
        { 
                            String twitterName = <Put the twitter name here>
            String formattedTwitterAddress = "http://twitter.com/" + twitterName;
            Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress)); 
            startActivity(browseTwitter);
        }
        catch (Exception e) 
        {

        }

Solution 6 - Android

For me this did the trick it opens Twitter app if you have it or goes to web browser:

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/"+"USERID"));
                    startActivity(intent);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjbc25View Question on Stackoverflow
Solution 1 - AndroidHarryView Answer on Stackoverflow
Solution 2 - Androidfg.radigalesView Answer on Stackoverflow
Solution 3 - AndroidShujat MunawarView Answer on Stackoverflow
Solution 4 - AndroidigordcView Answer on Stackoverflow
Solution 5 - AndroidAkilanView Answer on Stackoverflow
Solution 6 - AndroidHussein AlghazalView Answer on Stackoverflow