From users to developers, mobile applications used to be (not so long ago) uninteresting. Complicated flows, non-intuitive screens and limited features available used to discourage anyone. PDAs had a poor user interface with limited colors (the first ones were only black and white). It was very complicated and boring to write any code for it. Cell phones used to be a device where you could make and receive phone calls. Eventually you could play some games. But you know what? Even the games were boring most of the time!

Things change, though. With all these new technologies that came in the last two years, almost everyone has a kind of gadget such as netbook, iPhone, iPad, iTouch, Windows Phone and of course, Android phone and tablet! The Android market is growing each day and the coolest thing about its development is that you have a free SDK available to anyone, as well as free IDEs such as Eclipse and IntelliJ IDEA.

All you have to do to start Android development is to download the right tools and SDKs and you’re ready to go.

This article will show you how to setup the Android environment and write a Twitter client, step by step, using IntelliJ IDEA. This article assumes that you are familiar with the basic Java language and basic concepts of oriented-object programming. The application will allow users to search for tweets, display the results and store the criteria.

Introducing Android

Android is an operating system based on the Linux kernel. Google created Android in 2005 and it is now the world’s best-selling smartphone platform. Hardware manufacturers are using Android to build tablets such as Galaxy (Samsung) and Xoom (Motorola) running the latest Android firmware (3.0).

Android has a large community of developers and there are over 200,000 apps available on Android Market (online app store).

Here are some more Android facts: according to Wikipedia, “The Android open source software stack consists of Java applications running on a Java-based, object-oriented application framework on top of Java core libraries running on a Dalvik virtual machine featuring JIT compilation. Libraries written in C include the surface manager, OpenCore media framework, SQLite relational database management system, OpenGL ES 2.0 3D graphics API, WebKit layout engine, SGL graphics engine, SSL, and Bionic libc… Including the Linux kernel, Android consists of roughly 12 million lines of code including 3 million lines of XML, 2.8 million lines of C, 2.1 million lines of Java, and 1.75 million lines of C++.”

Setting up the Environment

Make sure that you have the JAVA SDK installed on your computer. You may want to check for the latest version at The latest version available as I write this is the Java SE 6 Update 25.

Next, you may want to visit the Android SDK website at You’ll find useful information regarding installation, download, dev guide, references, resources and so forth.

Finally, you may want to choose an IDE for your Android development. The most popular free IDEs in the market are Eclipse ( and IntelliJ IDEA ( For this article I will demonstrate Android development using IntelliJ IDEA.

Installing Android SDK

You can easily install the Android SDK by downloading the exe file on the Android SDK website (Figure 1).

Figure 1: Choose the second installer option for Windows.

The Android SDK won’t install if you don’t have the JAVA SDK previously installed. Only the Java runtime is not enough to develop for Android (Figure 2).

Figure 2: If you don’t have the Java SDK installed, this dialog box will pop up.

Once you install the Java JDK, you’ll be able to correctly run the Android SDK installer. You may want to install all the packages available, as shown in Figure 3.

Figure 3: Select the “Accept All” option and click Install.

Installing the IDE

Downloading all packages may take some time, so while the Android SDK is installing, you can get IntelliJ IDEA at, as shown in Figure 4.

Figure 4: Select the Community Edition, which is a free version of IntelliJ IDEA.

IntelliJ IDEA is not only a JAVA IDE development tool, but also an Android development tool. Because it’s a JetBrains product, if you are familiar with tools such as Re-Sharper (a nice refactoring tool for C# and Visual Basic), you will notice similar features regarding refactoring in it.

All you need to build an Android application is free: Java SDK, Android SDK and an IDE for development such as Eclipse or IntelliJ IDEA.

Creating a New Android Project

To create an Android project, open the ItelliJ IDEA IDE and click “Create New Project” (Figure 5).

Figure 5: Creating your first Android project.

Choose the first option to create a project from scratch and click “Next” (Figure 6).

Figure 6: Create new IDEA project structure.

You will name your project “MyTwitterSearcher” and select the “Android Module” type, according to Figure 7.

Figure 7: Select the Android Module type.

Next, the New Project wizard will let you choose where you want to store your source files. In this case, keep the default folder “src” and click “Next” (Figure 8).

Figure 8: Choose where to put the source files.

Finally, choose the Android SDK. For this example you’ll work with the version 2.3.3. IntelliJ IDEA gives you the option to create a default “Hello World” activity (I’ll discuss this subject later). Figure 9 shows what you need to choose.

Figure 9: IntelliJ IDEA can automatically create the "Hello World" activity when you are setting up the new project.

Theoretically you are ready to run your very first Android application. If you have an Android device, such as a smartphone (Nexus One, Motorola Droid), or a tablet (Galaxy, Motorola Xoom, ViewSonic G-Tablet), you can test it already. Make sure that you installed all the drivers for your device and try to click SHIFT+F10. If you don’t have a physical device, you’ll have to set up the Android emulator through the AVD Manager (Android Virtual Device).

To launch the AVD Manager, from the Tools menu choose Android and then choose Android SDK and AVD Manager (Figure 10).

Figure 10: Launching the AVD Manager.

After launching the AVD Manager, click New... to create a new emulator (Figure 11).

Figure 11: Creating a new Android emulator.

Because you’re using version 2.3.3, all you have to do is fill the Name TextBox, choose the target as “Android 2.3.3 - API Level 10” and click Create AVD as shown on Figure 12.

Figure 12: Setting the name and Android target.

You will see a confirmation dialog box (Figure 13). Click OK.

Figure 13: Finished creating the AVD.

If there’s no device attached to your computer and you click SHIFT+F10, you will notice the AVD being launched and your first Android application running (Figure 14).

Figure 14: Finally launching the emulator.

Basic Android Concepts

To build and understand an Android project, there are some objects that every developer should be familiar with. The most important ones are:

  • Activities
  • Intents
  • Services
  • Content Providers


An activity is the user interface screen. It means that in every activity that you write you are actually “drawing” what the user will see on the screen: buttons, lists, images, anything.


Intents allow your application to send or receive data from and to other activities or services, as well as allow your application to broadcast that a certain event has occurred. You can also communicate your application’s intents between any installed applications on the device.


Services are simply tasks that run in the background. Say that you want to update your local database every minute. A service could easily do that for you while you are navigating in other parts of your application, or even other applications.

Content Provider

A content provider is the mechanism you can use to share information between applications. For instance, your application may want to access the contacts and change some information. Same thing with sounds and pictures stored. You may want to access them as part of one of your application’s features.


Now let’s take a look at the following code:

package com.example;
import android.os.Bundle;
public class MyActivity extends Activity
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)

Notice that this class extends Activity. It means also that *ALL* UIs that you’re going to write should extend the Activity. Also you *always* should override the method onCreate. But don’t worry! Every time you create a new activity, IntelliJ IDEA will extend the Activity class and add this override automatically.

The onCreate method is the first thing that Android calls when an Activity is launched. The setContentView specifies which layout this Activity should use. In this case, the layout will be based on a XML file called “main.xml” under the folder res\layout (Figure 15).

Figure 15: The main layout is located under the layout folder.

Common Android Layouts

In our project example we’re going to use the LinearLayout. It’s very important to know, though, that Android has a few different layouts that you may want to choose, depending on the user needs. The available layouts are:

  • FrameLayout
  • LinerLayout
  • TableLayout
  • RelativeLayout


This is the simplest Android layout. Basically it is a blank space on your screen that you can later fill with a single object, for example an image. All child elements are pinned to the top corner of the screen and you cannot specify a different location for a child view.


It aligns all children in a single direction, either horizontally or vertically, depending on how you specify the orientation attribute.


The TableLayout positions its children into rows and columns. The interesting part of this layout is that it doesn’t display any borders, columns or cells. You can use as many columns you want as the row with the most cells. You can leave a cell empty, but unfortunately you cannot span columns, like standard HTML.


The RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center.

In the example for this article, we want a textbox to type in search words and a button to do the actual search. Change the “main.xml” file to the following code and then launch the application.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=

The result should be something similar to Figure 16.

Figure 16: The search layout.

Creating the Twitter Classes

Let’s start creating the Twitter classes that will be needed in the application. The first one will place the Twitter properties, as shown in Listing 1. To create a new Java class, right-click on the com.example package under the src folder, select new and then Java Class, as shown on Figure 17

Figure 17: Adding a new Java class.

This is the class that will represent the tweets. Now you need to implement the search engine. To do so, we’ll use the Twitter API that will return a JSON object. Good news: Android SDK fully supports JSON, so implementing it will definitely be a piece of cake. Let’s name this class as “TwitterSearcher”.

Creating the TwitterSearcher class

First, we need to import all the libraries that we’ll use in the class as shown in Listing 2.

Now you need to declare some private fields to use in your methods (Listing 3). You’ll use the address “” to retrieve tweets. “/search.json?q=” will be the query command and 80 is the default port to access the site.

The getKeywords method is responsible to invoke and process the call to, passing all the parameters, as shown on Listing 4.

Finally we have the method that returns an array of tweets. The “getTweetsByText” method gets a String result from getKeywords method and converts to an ArrayList<Tweet>, as shown in Listing 5.

We need work on some refactoring before we proceed. The activity called “MyActivity” can use a more meaningful name such as “SearchActivity”. To do so, click on the MyActivity file in the project explorer and click SHIFT+F6. Change the name and click “Refactor” as shown in Figure 18.

Figure 18: Renaming the MyActivity class to SearchActivity.

Next you’ll build a layout to display the tweets. You also want to add a save button where you’ll be able to save a search criteria. Let’s make some changes to the main.xml file, the layout file, as shown in Listing 6.

Now the main form should be like Figure 19 when launching the application.

Figure 19: Adding the Save button and List to the Main layout.

There’s a table control and a list view placed on the main layout. You can’t see these yet because no criteria was inserted nor has the Search button been pressed.

Going back to the SearchActivity, you need to implement all the functionality. The first method that any activity calls when first launched is onCreate. On this method you need to set which layout the activity will be using. In this case the main layout will be used. Also, we need to identify the objects in our layout through the findViewById method. Then, you need to set the Save Button as disabled since there’s nothing to be saved yet. Finally, we’ll set the OnClickListener event for the search button. The click will call the search method.

The search method instantiates the TwitterSearcher class to get the list of the tweets based on the criteria. It created an adapter (create a new Java class called on Listing 8) for the list view in order to support a custom layout. Finally we check whether there are tweets to make the save button available or not.

Check Listing 7 for complete implementation.

The uses a customized way to build the list. At some point in the code you will notice the usage of a specific layout called “tweetrowlayout”. We need to add this row layout in order to set the user picture, name and the tweet itself.

To do so, right-click on the layout folder under res, choose New from the popup window, and choose Layout resource file as shown in Figure 20, and name it “tweetrowlayout.xml”.

Figure 20: Creating a new Android layout.

Now change the code according to Listing 9.

Test the application by filling the search textbox with the word “Android” and then click the Search button. The results should be similar to Figure 21.

Figure 21: Listing the tweets shows the user name, their picture and the text.

Database on Android

To store the search criteria we’ll definitely need a database to do so. Android devices have a built-in database called SQLite. Unfortunately, by default it doesn’t have any management interface to create and maintain databases and tables, so we need to create them in code. We’ll create a class that inherits SQLiteOpenHelper class, which provides two main methods:

  1. onCreate(SQLiteDatabase db): invoked when the database is created. You can create tables and columns, create views or triggers.
  2. onUpgrade(SQLiteDatabse db, int oldVersion, int newVersion): invoked when we make a modification to the database such as altering, dropping or creating new tables.

In our project, we want to store only the words used in a search. It’s going to be a very simple database with one single table and one single field. Create a new class called “”, as listed in Listing 10.

Now we need to go back to SearchActivity class and make some adjustments.

Set a listener for the Save button:

cmdSave.setOnClickListener(new Button.OnClickListener()
{ public void onClick (View v) { saveSearch();}});

And create a method that will save the data into the database:

void saveSearch() {
   TwitterDatabaseHelper twitterHelper = new
   SQLiteDatabase database =
   ContentValues values = new ContentValues();
   database.insert("History", null, values);
   AlertDialog alertDialog;
   alertDialog = new
   alertDialog.setTitle("Search Twitter");
   alertDialog.setMessage("Search Saved!");;

The application uses the AlertDialog class to display a message to the user (Figure 22).

Figure 22: Displaying a message to the user via AlertDialog class.

Now that we’re saving the search to the database, we need to find a way to retrieve and see a list of the saved searches. Let’s create a new activity (Figure 23) for this list, and name it “SearchListActivity” (Figure 24).

Figure 23: Creating a new Android Component.
Figure 24: Creating the SearchListActivity.

Name it “SearchListActivity”, select the Kind “Activity” and click OK.

Now we need to create a layout for our activity. Create a new layout called “searchlistlayout” and change its content with the code below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
   <TableLayout android:id="@+id/TableLayoutHistory"
         <ListView android:id="@+id/lsttViewHistory"

Once the searchlistlayout is created, the List View should also have its own layout to represent each row. So we create another layout file called “searchlistrowlayout.xml” and change its content to the following code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
   <TextView android:id="@+id/text"

Now that we have the layout files ready, we need to code the SearchListActivity. Listing 11 shows the complete code for this activity.

Remember that the first thing when an activity is instantiated is to execute the onCreate method. So we’re defining which layout the Activity will be using and then we build the list. Here’s what happens inside this code:

  • The buidList method creates a List View based on the object lstVIewHistory, set on the searchlistlayout.xml.
  • An ArrayList of HashMap is created and will be used by the adapter to set the value of the rows.
  • Access the database and create a cursor containing information from the “History” table.
  • A Simple Adapter is created to be used on the List View.
  • The List View sets the adapter and is ready to show the rows.

Navigating Between Two Activities

The SearchListActivity is ready but we still need to define from where this Activity will be called. The fastest way to do this is by adding a button on the Main Search Activity displaying “Saved Searches”. We need to modify the main.xml layout, by adding the new control as shown below:

     android:text="Saved Searches"

This control will be placed between the last TableLayout tag and the LinearLayout tag. When running the application again, it should look like Figure 25.

Figure 25: The new "Saved Searches" button.

The last step is to modify the SearchActivity class:

  1. Create a private Button, cmdSavedSearches.
  2. Set the value of this button in the “onCreate” method using the findByViewId method.
  3. Set a listener for this button to call the openSavedSearches method.
  4. Create the openSavedSearches method.
  5. Inside the openSavedSearches method, create a new Intent for the SearchListActivity class and start the activity.

You can see the complete code in Listing 12.

Now when you click the “Saved Searches” button, the new activity will be called and you should see something similar to Figure 26.

Figure 26: List of saved searches.

Our last task is to navigate back to the main activity when we click an item from the list, fill the search textbox control and do the search automatically. As we have a listener for the buttons, we also have listeners for the List View. So it would be fair if we wrote a listener for it on, before closing the database, on the buildList method:

         public void onItemClick(AdapterView<?> a, View
v, int position, long id)
             HashMap<String, String> map =
             (HashMap<String, String>)lv.
            String searchText = map.get("text");
            Intent intent = new
                Bundle bundle = new Bundle();

At this point we need to go back to the SearchActivity, but we would need to pass the search text as a parameter. To do so, notice the Bundle object. With this object we can put in memory some additional information that we may want to pass to other activities. In this case, we want to send the text found on the list. Each item on the list is actually a HashMap<String, String>. But we need to pass only the actual value of the HashMap. That’s the reason we cast the item, get the value and finally put the actual value in memory.

There’s one last modification that we need to do on SearchActivity as well. When it is instantiated, we may want to check for possible parameters in memory for that intent. In our case, if we found any, than we can fill the search textbox and make a call to the search method. We should put the code below in the end of the onCreate method:

if (this.getIntent() != null &&
    this.getIntent().getExtras() != null) {
         String searchText = this.getIntent().
         if (searchText != null) {


We just wrote a complete Android application. We learned how to create controls, layout, activities, listeners, database, and tables and how to interact with all of them as well as how to use the JSON library and the Twitter API. This is only the beginning, though. There’s much more!

Android has a lot to offer. It is a very powerful operating system and anyone can take full advantage of its built-in features such as contacts list, photo shooting, voice recording, 3D graphics, GPS information, access the Internet and more. Because you can write Android applications in native JAVA, C# users won’t have any difficulty learning some small particularities of the language. They have and use the same concepts: OOP.

Not only does Twitter offer an API for developers, but big companies like Facebook and Google offer APIs that Android can take advantage of. Most of these APIs return a JSON object, which is very valuable for the Android SDK, since the Android SDK fully supports JSON objects.

Writing applications for small devices is not taboo anymore. Each day, more and more people use these gadgets. Smart phones and tablets are the future! In a certain way, we’re still in the beginning of this new age. And even though there are three main systems in the market (Android, iOS, webOS), I think that Android is the easiest to learn. It’s easy to deploy, easy to search for answers and even easy to put your apps in the market. All you need is a standard computer, a little bit of creativity and put your hands on the code.