After hanging out you with my friends from Oregon to the SeaWorld in San Diego, CA I decided that it is might be cool to develop a simple Android application which keeps our memory about the adventure parks.

Code: surfsnippets/shamu

Sea World Tiles

So here is the app called Sea World Tiles. Feel free to download it from Android Market and play around. In this app you need to compose the picture of a sea creature from left and right sides by clicking the appropriate buttons.

The source code is available in the repository surfsnippets in directory shamu. Here is the short description of source code to show how simple it is to write Android applications.

The layout in Android applications is described in .xml file, main.xml and includes TableLayout with two TableRow. The upper TableRow displays two tiles of ImageView and the bottom TableRow keeps left and right buttons:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3    android:layout_width="fill_parent"
 4    android:layout_height="fill_parent"
 5    android:orientation="vertical"
 6    android:id="@+id/main_layout">
 7     <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
 8         <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
 9             <ImageView android:layout_width="wrap_content"
10                        android:layout_height="wrap_content"
11                        android:src="@drawable/seaworld_l"
12                        android:id="@+id/picture_l"></ImageView>
13             <ImageView android:layout_width="wrap_content"
14                        android:layout_height="wrap_content"
15                        android:src="@drawable/seaworld_r"
16                        android:id="@+id/picture_r"></ImageView>
17         </TableRow>
18         <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
19             <Button android:text="Left"
20                     android:id="@+id/button_left"
21                     android:layout_width="wrap_content"
22                     android:layout_height="wrap_content"
23                     android:layout_weight="0.5"
24                     android:layout_margin="5dip"></Button>
25             <Button android:text="Right"
26                     android:id="@+id/button_right"
27                     android:layout_width="wrap_content"
28                     android:layout_height="wrap_content"
29                     android:layout_weight="0.5"
30                     android:layout_margin="5dip"></Button>
31         </TableRow>
32     </TableLayout>
33 </LinearLayout>

Layout specifies some important parameters such as widget id: @android:id="+id/button_left", sizes: android:layout_weight="0.5" or relation to container widget: android:layout_margin="5dip". Some of these parameters can be used in program.

The program is consists of just one Shamu.java:

 1 package com.surfingbits.shamu;
 2 
 3 import java.util.HashMap;
 4 
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.ImageView;
10 
11 import java.util.Random;
12 
13 public class Shamu extends Activity {
14     /** Called when the activity is first created. */
15 
16     HashMap<Integer, String> num2pic;
17     private static Random randNum;
18     public static int randl, randr;
19 
20     @Override
21     public void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.main);
24 
25         initValues();
26 
27         Button btnLeft = (Button) findViewById(R.id.button_left);  // Left button
28         btnLeft.setOnClickListener(new Button.OnClickListener() {
29             public void onClick(View v) {
30                 Shamu.this.replaceImage("l");
31             }
32         });
33         Button btnRight = (Button) findViewById(R.id.button_right); // Right button
34         btnRight.setOnClickListener(new Button.OnClickListener() {
35             public void onClick(View v) {
36                 Shamu.this.replaceImage("r");
37             }
38         });
39     }
40 
41     public void initValues()
42     {
43         randNum = new Random();
44         randl   = 0;
45         randr   = 0;
46         num2pic = new HashMap<Integer, String>();
47         num2pic.put(new Integer(0), "shamu");
48         num2pic.put(new Integer(1), "dolphin");
49         num2pic.put(new Integer(2), "seastar");
50         num2pic.put(new Integer(3), "rayfish");
51     }
52 
53     public int getRandNum(String side)
54     {
55         // Two static variables randl and randr are set by corresponding button
56         try {
57             int temp    = Shamu.class.getField("rand"+side).getInt(null);   // randr or randl
58             int tempB   = temp;
59             while (tempB == temp)
60             {   // try until you get different int
61                 temp    = randNum.nextInt(num2pic.size());
62             }
63             Shamu.class.getField("rand"+side).setInt(Shamu.class, temp);
64             return temp;
65         } catch (NoSuchFieldException e) {
66             return 0;
67         } catch (SecurityException e) {
68             return 0;
69         } catch (IllegalAccessException e) {
70             return 0;
71         }
72     }
73 
74     public int layoutId(String base, Class cls, String side)
75     {
76         // Trying to use reflection getField (similar to Python getattr) to get field value
77         int id;
78         // Check if side is "l" or "r"
79         try {
80             id  = cls.getField(base+"_"+side).getInt(null); // Example: R.id.picture_l
81         } catch (NoSuchFieldException e) {
82             id = 0;
83         } catch (SecurityException e) {
84             id = 0;
85         } catch (IllegalAccessException e) {
86             id = 0;
87         }
88         return id;
89     }
90 
91     public void replaceImage(String side)
92     {
93         ImageView pic   = (ImageView) findViewById(layoutId("picture", R.id.class, side));
94         pic.setImageResource(layoutId(num2pic.get(getRandNum(side)), R.drawable.class, side));
95     }
96 }

When Android application is started the onCreate() method is called. First, it loads the main layout:

setContentView(R.layout.main);

initializes attributes and and then creates button widget specified by id R.id.button_left and defines the OnClickListener():

1 Button btnLeft = (Button) findViewById(R.id.button_left);  // Left button
2 btnLeft.setOnClickListener(new Button.OnClickListener() {
3     public void onClick(View v) {
4         Shamu.this.replaceImage("l");
5     }
6 });

When the user clicks the button replaceImage() method will be called:

1 public void replaceImage(String side)
2 {
3    ImageView pic    = (ImageView) findViewById(layoutId("picture", R.id.class, side));
4    pic.setImageResource(layoutId(num2pic.get(getRandNum(side)), R.drawable.class, side));
5 }

This method takes the ImageView by id and replaces it with another image by randomly generating number and looking up the appropriate id in the HashMap for the next picture.

To make the source code more compact I used Java Reflection: Reflection (computer programming) that can refer to class attributes or methods by name string. This approach is similar to Python functions getattr() or setattr().

Screenshot Tiles

Screenshot Tiles