Step 03 - Our version of “HelloWorld”

In this Step, you will be required to develop and run your own version of the “Hello World” Android Project from the lectures (as seen below).

Step 03 - 图1

Launch Eclipse and create a new Android Project called SayHello (as above) similar to what you did in Step 02. Name your package ‘ie.wit’ (or accept the default again). Accept all the defaults, and it’s recommended you select Android 4.4 as the launch target platform (but any target will suffice for this particular lab). It’s also probably a good idea to run the App at this stage, so you can set up your AVD (if you haven’t done so already).

Edit your “strings.xml” file (in your res folder) and replace the current “resources” tag with the following - be careful if you have created an app which contains a ‘menu’ folder, this also includes associated resources, so don’t overwrite those resources, just add our ones at the end.

  1. <resources>
  2. <string name="app_name">Say Hello Application</string>
  3. <string name="window_text">Press the button below to receive a friendly greeting from Android.</string>
  4. <string name="button_label">Show Greeting</string>
  5. <string name="greeting_text">Hello from Android!</string>
  6. </resources>

If you return to the “Resources View” you can see the graphical representation of the String resources you have set up (and edit them here if you need to).

Step 03 - 图2

Edit your “activity_hello.xml” in your layout folder and replace with the following

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".HelloActivity" >
  6. <TextView
  7. android:id="@+id/textView2"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello_world" />
  11. <TextView
  12. android:id="@+id/textView1"
  13. android:gravity="center"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_below="@+id/textView2"
  17. android:layout_marginTop="20dp"
  18. android:text="@string/window_text"
  19. android:textAppearance="?android:attr/textAppearanceMedium" />
  20. <Button
  21. android:id="@+id/greetingButton"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_alignParentBottom="true"
  25. android:layout_centerHorizontal="true"
  26. android:layout_marginBottom="59dp"
  27. android:onClick="showGreeting"
  28. android:text="@string/button_label" />
  29. </RelativeLayout>

This will give you the following layout:

Step 03 - 图3

Once again, it’s worth running the app at this point to confirm everything is displayed the way we want it.

The last thing we need to do is add in our event handling code so that a short message is displayed when the user presses the ‘Show Greeting’ button.

Firstly, open up the “HelloActivity.java” source file and add the following method

  1. public void showGreeting(View v) {
  2. // TODO Auto-generated method stub
  3. String greetingText = getString(R.string.greeting_text);
  4. Toast tempMessage = Toast.makeText(this, greetingText, Toast.LENGTH_LONG);
  5. tempMessage.show();
  6. }

Note that we have no need for soem kind of Listener interface (ala swing developement) - our event handling is taken care of via the ‘onClick’ attribute in our xml layout, here’s what your completed Activity class should look like.

Step 03 - 图4

We will investigate this code more closely in the lectures.