By Barry Burd

As is true for any Android app, this Android Studio project contains about 100 files and about 170 different folders. To make an Android Twitter app, you want to concentrate on the project’s MainActivity.java file. But a few other files require some attention.

The Twitter4J API jar file

Android has no built-in support for communicating with Twitter. Yes, the raw materials are contained in Android’s libraries, but to deal with all of Twitter’s requirements, someone has to paste together those raw materials in a useful way. Fortunately, several developers have done all the pasting and made their libraries available for use by others. The library used here is Twitter4J.

You add the Twitter4J libraries by following these steps:

  1. Visit Twitter4J.

  2. Find the link to download the latest stable version of Twitter4J.

    Twitter4J version 4.0.2 is used in this example. If you download a later version, it’ll probably work. However, you’re on your own with the backward compatibility, forward compatibility, or sideward compatibility of the various Twitter4J versions.

  3. Click the link to download the Twitter4J software.

    You want the file twitter4j-4.0.2.zip.

  4. Look for a twitter4j-core.jar file in the downloaded .zip file.

    In the .zip file, you should find a file named twitter4j-core-4.0.2.jar.

  5. Extract the twitter4j-core.jar file to this project’s app/build directory.

    Use your operating system’s File Explorer or Finder to do the extracting and copying.

  6. In Android Studio’s main menu, choose File → Project Structure.

  7. In the Project Structure dialog box, in the panel on the left side, select app (in the Modules group).

  8. In the main body of the Project Structure dialog box, select the Dependencies tab.

    You want to add the twitter4j-core.jar file to the list of dependencies.

  9. Click the plus sign that’s to the right of the Dependencies list.

    A little pop-up menu appears.

    Adding a dependency to an app.

    Adding a dependency.
  10. In the pop-up menu, select File Dependency.

    Android Studio displays a Select Path dialog box.

  11. In the Select Path dialog box, expand the build directory and select the twitter4j-core.jar file.

    What is referred to here as your twitter4j-core.jar file is probably named twitter4j-core-4.0.2.jar or something similar.

  12. Click OK to close the Select Path dialog box.

    Doing so adds your twitter4j-core.jar file to the list of items on the Dependencies tab.

  13. In the Project Structure dialog box, click OK.

    Now Twitter4J’s .jar file is part of your project.

    Your project depends on Twitter4J.

    Your project depends on Twitter4J.

The manifest file

This code contains the AndroidManifest.xml file for a Twitter app.

<?xml version=“1.0” encoding=“utf-8”?>
<manifest xmlns:android=
“http://schemas.android.com/apk/res/android”
package=“com.allmycode.twitter”>
<uses-permission android:name=
“android.permission.INTERNET”/>
&lt;application
android:allowBackup=“true”
android:icon=“@drawable/ic_launcher”
android:label=“@string/app_name”
android:theme=“@style/AppTheme” >
&lt;activity
android:name=“com.allmycode.twitter.MainActivity”
android:label=“@string/app_name”
android:windowSoftInputMode=“adjustPan” >
<intent-filter>
<action android:name=
“android.intent.action.MAIN” />
<category android:name=
“android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
</manifest>

Above, you find the same stuff that you find in other AndroidManifest.xml files. In addition, you find the following:

  • The uses-permission element warns Android that the app requires Internet connectivity.

  • The windowSoftInputMode attribute tells Android what to do when the user activates the onscreen keyboard.

  • The adjustPan value tells Android not to squash together all of the screen’s widgets. (The app looks ugly without this adjustPan value.)

The main activity’s layout file

This layout file example has no extraordinary qualities. If you’re living large and creating the app on your own from scratch, you can copy the contents of the above code to the project’s res/layout/activity_main.xml file. Alternatively, you can use Android Studio’s Designer tool to drag and drop, point and click, or type and tap your way to the graphical layout.

The graphical layout of an app's main screen.

The graphical layout of the main activity’s screen.
<RelativeLayout xmlns:android=
“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:paddingBottom=“@dimen/activity_vertical_margin”
android:paddingLeft=“@dimen/activity_horizontal_margin”
android:paddingRight=
“@dimen/activity_horizontal_margin”
android:paddingTop=“@dimen/activity_vertical_margin”
tools:context=“.MainActivity” >
&lt;TextView
android:id=“@+id/textView2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignBaseline=“@+id/editTextUsername”
android:layout_alignBottom=“@+id/editTextUsername”
android:layout_alignLeft=“@+id/editTextTweet”
android:text=“@string/at_sign”
android:textAppearance=
“?android:attr/textAppearanceLarge” />
&lt;EditText
android:id=“@+id/editTextUsername”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_above=“@+id/timelineButton”
android:layout_toRightOf=“@+id/textView2”
android:ems=“10”
android:hint=“@string/type_username_here” />
&lt;TextView
android:id=“@+id/textViewTimeline”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/timelineButton”
android:layout_below=“@+id/timelineButton”
android:maxLines=“100”
android:scrollbars=“vertical”
android:text=“@string/timeline_here” />
&lt;Button
android:id=“@+id/timelineButton”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignLeft=“@+id/textView2”
android:layout_centerVertical=“true”
android:onClick=“onTimelineButtonClick”
android:text=“@string/timeline” />
&lt;Button
android:id=“@+id/tweetButton”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_above=“@+id/editTextUsername”
android:layout_alignLeft=“@+id/editTextTweet”
android:layout_marginBottom=“43dp”
android:onClick=“onTweetButtonClick”
android:text=“@string/tweet” />
&lt;EditText
android:id=“@+id/editTextTweet”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_above=“@+id/tweetButton”
android:layout_alignParentLeft=“true”
android:layout_marginLeft=“14dp”
android:ems=“10”
android:hint=“@string/type_your_tweet_here” />
&lt;TextView
android:id=“@+id/textViewCountChars”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignBaseline=“@+id/tweetButton”
android:layout_alignBottom=“@+id/tweetButton”
android:layout_toRightOf=“@+id/timelineButton”
android:text=“@string/zero” />
</RelativeLayout>