CIS 350 Software Engineering - Spring 2012

Android Programming Exercise #1

In this exercise, you will create an app that shows a button and keeps track of the number of times it is clicked.

First, create a new Android project called ButtonClicks. Set the Java package to "edu.upenn.cis350".

Then, modify res/layout/main.xml so that the app displays two TextViews (one next to the other) and a Button beneath them. See slides 29-33 of Tuesday's lecture notes or "XML Layouts" in the developer guide if you need help.

Set the text of the leftmost TextView to "Clicks:". Initialize the other to "0".You can do this right in the main.xml file, or use strings.xml to hold the string constantsd.

Set the "android:onClick" attribute for the Button to "onButtonClick" in main.xml.

In your Activity class, implement the onButtonClick method so that every time the user clicks the button, the value in the second TextView should increase: "1", then "2", and so on. In order to do this, you must use the Activity's findViewById method to get the appropriate TextView obejct, and then call its setText method to update the text that appears.

See slides 36 and 37 of Tuesday's lecture notes or "Input Events" in the developer guide if you need help.