1. Code
  2. Mobile Development
  3. Android Development

Beginning Android: Getting Started with Fortune Crunch

Scroll to top

You’ve learned how to create Fortune Crunch on iPhone and Appcelerator here on Mobiletuts+. Now learn how to create it on Android!

If you’re not familiar with Fortune Crunch, it’s an app created by Mark Hammonds to show how to get started with iPhone development on this site. It was later picked up by John Schulz to show how to do cross platform development with Appcelerator. And now, we’re going to use it to show the creation of a complete Android application from start to finish. Fortune Crunch displays a fortune cookie and allows the user to tap it to crack it open and display a fortune!


Android 101 Tutorials:


Getting Started

We’re assuming that you have all of the tools necessary for Android development already installed. If not, make sure you download and install Eclipse and the Android SDK. To complete the image steps on your own, you’ll also need an image editor that can, at minimum, resize images. This tutorial is specific to Eclipse, but you can comfortably use it on Linux, Mac, or Windows operating systems. You do not need an Android device to complete this tutorial.

If you want to follow along without writing the code yourself, it’s available for download.

Step 1: Creating Your Eclipse Project

Start by creating a project named FortuneCrunch in Eclipse. Do this by choosing File->New…->Android Project. The resulting screen has a number of fields and options you need to fill out.

  • Enter “FortuneCrunch” for the project name
  • Keep “Create new project in workspace” selected
  • You may want to use a different location than the default; your choice
  • Choose your Build Target to be Google APIs Level 8 (Android 2.2)
  • Enter “Fortune Crunch” as the application name
  • Choose an appropriate package name, such as “com.yourname.tutorial.fortunecrunch”
  • Check the box for “Create Activity” and name it “FortuneCrunch”
  • Finally, set the Min SDK Version to 3 (Android 1.5)

If you’ve done all of this the dialog should look something like this:

Eclipse ProjectEclipse ProjectEclipse Project

Step 2: Creating Your Resources

Next, you need to create the images the application will work with. The application uses a pair of images: a whole fortune cookie and a cracked fortune cookie.

You should consider creating three different pairs of images for low, medium, and high pixel density screens. The original images are provided for you to edit, or you can use the images included in the source code download.

The resource directories for the specific images were created with the project: lpdi, mdpi, and hdpi. Inside the directory for each screen resolution, the two image resources must be named the same. We’ve named them fortune_cracked.jpg and fortune_uncracked.jpg. Once you’ve done this, the project explorer view will show them:

Creating Resources

Tip: If you edited them and saved them to these directories outside Eclipse, right-click on the project and choose refresh. They should then show up.

Step 3: Defining the Fortune String

When the fortune cookie is opened, it displays the user’s fortune. Now let’s define the fortune string as a resource. Do this by opening the /res/values/strings.xml resource file. Create a new string called “fortune” with the text “Happy Android Hacking!”

It should now look like:

1
2
<?xml version="1.0" encoding="utf-8"?>
3
<resources>
4
    <string name="fortune">Happy Android Hacking!</string>
5
    <string name="app_name">Fortune Crunch</string>
6
</resources>

Step 4: Creating Your Layout

Next you need to design your layout resource file. This file will dictate how the one screen of the Fortune Crunch application is displayed. The project wizard creates a default file for you called /res/layout/main.xml. Edit this file and remove all the controls.

The Fortune Crunch user interface has two different states. The first is an image of a complete fortune cookie. The second is an image of a cracked fortune cookie with text displaying over the fortune inside. The second of these two is the more complex ones, so let’s design that first as the design of the easier one will come out of it with almost no extra effort.

To do this, use a RelativeLayout to contain an ImageView and a TextView. The ImageView should be centered horizontally. You’ll want to give it appropriate height and width numbers to constrain the View size. The TextView should be set to layout_alignBottom for the ImageView. Then, using layout_marginBottom you can move it up to overlay the fortune. This sounds complex, so let’s take a look at the resulting XML:

1
2
<?xml version="1.0" encoding="utf-8"?>
3
<RelativeLayout
4
    xmlns:android="http://schemas.android.com/apk/res/android"
5
    android:orientation="vertical"
6
    android:layout_width="fill_parent"
7
    android:layout_height="fill_parent">
8
    <ImageView
9
        android:id="@+id/fortune_view"
10
        android:layout_height="240dp"
11
        android:layout_width="360dp"
12
        android:layout_centerHorizontal="true"
13
        android:src="@drawable/fortune_cracked"></ImageView>
14
    <TextView
15
        android:layout_width="wrap_content"
16
        android:layout_height="wrap_content"
17
        android:layout_centerHorizontal="true"
18
        android:text="@string/fortune"
19
        android:textColor="#A00"
20
        android:textSize="16dp"
21
        android:id="@+id/fortune_text"
22
        android:layout_alignBottom="@+id/fortune_view"
23
        android:minLines="2"
24
        android:maxWidth="120dp"
25
        android:layout_marginBottom="105dp"></TextView>
26
</RelativeLayout>

This layout works well in portrait mode. We use dp for all measurements so it scales appropriately with the screen size. We also use dp for the text size, so it also scales with the screen size. You wouldn’t want to use sp for the text, because that will scale with the user’s font size – not what you want for a layout like this. The layout view will look something like this:

Creating Your LayoutCreating Your LayoutCreating Your Layout

However, if you switch the landscape mode, you’ll see that this doesn’t work!

Step 5: Creating An Alternate Landscape Layout

Let’s provide a separate landscape layout file, too. It’s a very similar design, except that the image size is swapped so it uses the vertical space. The following xml file must be named main.xml and placed in the /res/layout-land folder, which you must create first.

1
2
<?xml version="1.0" encoding="utf-8"?>
3
<RelativeLayout
4
    xmlns:android="http://schemas.android.com/apk/res/android"
5
    android:orientation="vertical"
6
    android:layout_width="fill_parent"
7
    android:layout_height="fill_parent">
8
    <ImageView
9
        android:id="@+id/fortune_view"
10
        android:layout_height="360dp"
11
        android:layout_centerHorizontal="true"
12
        android:layout_width="480dp"
13
        android:src="@drawable/fortune_cracked"></ImageView>
14
    <TextView
15
        android:layout_width="wrap_content"
16
        android:layout_height="wrap_content"
17
        android:layout_centerHorizontal="true"
18
        android:text="@string/fortune"
19
        android:textColor="#A00"
20
        android:id="@+id/fortune_text"
21
        android:layout_alignBottom="@+id/fortune_view"
22
        android:layout_marginBottom="115dp"
23
        android:minLines="2"
24
        android:maxWidth="150dp"
25
        android:textSize="18dp"></TextView>
26
</RelativeLayout>

When both files layout folders are present, the explorer view will look something like this:

Layout Folders

The layout view for the landscape layout will look like this:

Landscape LayoutLandscape LayoutLandscape Layout

Step 6: Configuring the Layout’s Initial View

Now that you have proper layout files for both portrait and landscape modes for the cracked cookie state, you need to revert it back to the not-yet-cracked version for the initial view. Do this by switching the src attribute of the ImageView to @drawable/fortune_uncracked and add a visibility attribute to the TextView and set it to “gone.”

Step 7: Adding Click Handler

When the user clicks or taps on the fortune cookie, it switches state to the cracked cookie with the fortune text. To enable this function, add the click handler to the appropriate control the user clicks on. A quick way watch for clicks on the whole screen is to register the click handler with the parent control of all child views, in this case, the RelativeLayout you organized all the screen controls in. Make sure that the RelativeLayout control has its clickable attribute set to true. Then add the onClick attribute and set it to toggleFortune.

Your portrait layout file should now look like this:

1
2
<?xml version="1.0" encoding="utf-8"?>
3
<RelativeLayout
4
    xmlns:android="http://schemas.android.com/apk/res/android"
5
    android:orientation="vertical"
6
    android:layout_width="fill_parent"
7
    android:layout_height="fill_parent"
8
    android:clickable="true"
9
    android:onClick="toggleFortune">
10
    <ImageView
11
        android:id="@+id/fortune_view"
12
        android:layout_height="360dp"
13
        android:layout_centerHorizontal="true"
14
        android:layout_width="480dp"
15
        android:src="@drawable/fortune_uncracked"></ImageView>
16
    <TextView
17
        android:layout_width="wrap_content"
18
        android:layout_height="wrap_content"
19
        android:layout_centerHorizontal="true"
20
        android:text="@string/fortune"
21
        android:textColor="#A00"
22
        android:id="@+id/fortune_text"
23
        android:layout_alignBottom="@+id/fortune_view"
24
        android:minLines="2"
25
        android:maxWidth="120dp"
26
        android:layout_marginBottom="105dp"
27
        android:textSize="18dp"
28
        android:visibility="gone"></TextView>
29
</RelativeLayout>

You will need to make similar changes to the landscape layout file.

Step 8: Implementing the Click Handler

Your layout files are complete. Now you need to implement the click handler in Java within your application’s only Activity class. Open the FortuneCrunch.java file and add the following method to it:

1
2
    public void toggleFortune(View view) {
3
        TextView fortune = (TextView) findViewById(R.id.fortune_text);
4
        ImageView cookie = (ImageView) findViewById(R.id.fortune_view);
5
        
6
        // use the text visibility to determine mode
7
        if (fortune.getVisibility() == View.GONE) {
8
            fortune.setVisibility(View.VISIBLE);
9
            cookie.setImageResource(R.drawable.fortune_cracked);
10
        } else {
11
            fortune.setVisibility(View.GONE);
12
            cookie.setImageResource(R.drawable.fortune_uncracked);
13
        }
14
    }

This method retrieves Java objects representing both the TextView and the ImageView. First, the visibility of the TextView is determined. This information can be used to toggle the screen state between the cracked and whole cookie. Based on the fortune text state, the TextView visibility and source image to display are determined. The source image in the ImageView is changed by calling the setImageViewResource() method with the identifier of the correct image to display.

Step 9: Examining the onCreate() Method

While you were editing FortuneCrunch.java, you probably noticed the Activity’s onCreate() method. It looks like this and is created when the project is created:

1
2
    /** Called when the activity is first created. */
3
    @Override
4
    public void onCreate(Bundle savedInstanceState) {
5
        super.onCreate(savedInstanceState);
6
        setContentView(R.layout.main);
7
    }

The only method call here you care about right now is the setContentView() method. It references the layout file you created: main.xml. This is why the landscape and portrait mode layout files must share the same file name. If you had named them both “crunch.xml” the call would have to be changed to:

1
2
setContentView(R.layout.crunch);

Now the Android platform will load the appropriate main.xml layout file based upon whether the device is in landscape or portrait mode. If the files were named differently, the system would either not find the layout resource or not load it properly.

Step 10: Creating a Debug Configuration

Now that you’re done coding, you probably want to run and test your application. The first thing you should do is create an Eclipse Debug Configuration. Do this by clicking on Run->Debug Configurations… On the Android tab, set the project to FortuneCrunch. You can name your configuration as you like. I like naming my configurations exactly the same as the project.

Debug ConfigurationDebug ConfigurationDebug Configuration

In the Target tab, I prefer to manually choose the target each time. You can also use the debug configuration toconfigure network speeds for the emulator, and many other options. These don’t apply for Fortune Crunch.

Set Target TabSet Target TabSet Target Tab

Step 11: Running Fortune Crunch in the Emulator and on the Device

You can run Fortune Crunch by selecting the Debug Configuration and choosing Debug. Choose either a connected Android device, a running emulator, or launch a new emulator. This screen handles how to launch your app:

And there you have it!

Here is Fortune Crunch running in the HVGA emulator in landscape mode:

Fortune Crunch PreviewFortune Crunch PreviewFortune Crunch Preview

Here is Fortune Crunch running on the Nexus One in portrait mode, using the DDMS perspective of Eclipse to capture it:

Fortune Crunch PortraitFortune Crunch PortraitFortune Crunch Portrait

Conclusion

In this tutorial, you’ve learned how to implement the Fortune Crunch application on the Android platform. You did this by creating appropriately sized graphics. Then you created layout files for both landscape and portrait modes. Finally, you brought everything together by handling clicks on the screen to crunch that cookie. You’ve only scratched the surface of Android development. Check out all the other great tutorials on Mobiletuts+ to dive deeper into Android development.

We hope you liked this and look forward to your feedback.

Happy Android Hacking!

About the Authors

Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams TeachYourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to androidwirelessdev+mt@gmail.com, via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon


Related Tutorials:


Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.