Android – Added Follow Developer on Twitter Button

Background

There is a great piece of java code posted by koush on Github’s gist showing how to interface with the official Android Twitter application. The key was not only knowing the package name, but knowing the right extra information to add to the intent sent to the Twitter application.
The below code and two links below illustrate a simple method of firing off an intent loaded with the proper profile activity class name and screen name to pull up the user’s page in the official Twitter application. The second link shows how I added this to a custom Whats New dialog used in my applications.

Links

koush gist on github: https://gist.github.com/3087486
my gist on github with whats new dialog function: https://gist.github.com/3487283

Copy of my gist on github

    //Credits:
    //http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application
    //https://gist.github.com/3087486
    private void whatsNewDialog() {
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //Dialog dialog = new Dialog(main.this);
        final Dialog dialog = new Dialog(this, R.style.NoTitleDialog);
        dialog.setContentView(R.layout.whatsnew);
        //dialog.setTitle(getString(R.string.app_name) + " v" + currentAppVersion);
        dialog.setCancelable(false);
        //set up Title
        TextView textWhatsNewTitle = (TextView) dialog.findViewById(R.id.whatsNewTitle);
        textWhatsNewTitle.setText(getString(R.string.mainTitle) + " v" + currentAppVersion);
        //set up text content
        TextView textWhatsNewContent = (TextView) dialog.findViewById(R.id.tvWhatsNew);
        textWhatsNewContent.setText(R.string.whatsNew);
        //set up image view
        ImageView img = (ImageView) dialog.findViewById(R.id.whatsNewRootIcon);
        img.setImageResource(R.drawable.icon);
        //set up Okay button
        Button btnOkay = (Button) dialog.findViewById(R.id.whatsNewBtnOkay);
        btnOkay.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View v) {
                editor.putLong(PREF_WHATS_NEW_LAST_VERSION, currentAppVersionCode);
                editor.commit();
                dialog.dismiss();
            }
        });
        //check for Twitter application
        boolean twitterInstalled = false;;
        try {
            PackageManager packman = getPackageManager();
            packman.getPackageInfo("com.twitter.android", 0);
            twitterInstalled = true;
        } catch (Exception ex) {
            //ex.printStackTrace();
            twitterInstalled = false;
        }
        //set up Twitter button
        Button btnFollow = (Button) dialog.findViewById(R.id.followOnTwitter);
        btnFollow.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View v) {
                // this is the intent you actually want.
                // grabbed this by hooking a debugger up to twitter and debugging into android framework source.
                // this let me inspect the contents of the intent.
                Intent i = new Intent();
                i.setClassName("com.twitter.android", "com.twitter.android.ProfileActivity");
                i.putExtra("screen_name", "joeykrim");
                try {
                    startActivity(i);
                }
                catch (Exception ex) {
                    // uh something failed
                    ex.printStackTrace();
                }
            }
        });
        //Log.d(LOG_TAG, "twiterinstalled: " + twitterInstalled);
        if (twitterInstalled) btnFollow.setVisibility(VISIBILITY_VISIBLE);
        //now that the dialog is set up, it's time to show it
        dialog.show();
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*