Android Application Development 2013 PClassic Chris Murphy cdmurphy@cis.upenn.edu 1 First things first: WiFi! 1. Connect to AirPennNet-Guest. 2. Open a web browser. You will be redirected to a login page: Click the link for the conference code registration form. 3. Enter your personal information, along with the registration code: C-JAYAMAFT 4. Once you hit continue, you will be granted access for 15 mins. You will, by then, get an email with a confirmation link (this link may be sent to spam). 5. Click the link, and you will get internet access for the full day. 2 Overview What is Android? Creating an Android app Basic Android user interfaces: forms Handling button clicks and user interaction Dialogs 3 What is Android? An open source Linux-based operating system intended for mobile computing platforms Includes a Java API for developing applications It is not a device or product 4 5 Creating an Android App 6 Creating an Android App 1.Set up your development environment 2.Create a new Android project in Eclipse 3.Implement it 4.Run it 7 1. Set Up Your Android Environment http://developer.android.com/sdk Install Eclipse Install Android SDK (Android libraries) Install ADT plugin (Android development tools) Create AVD (Android virtual device) 8 2. Create an Android Project in Eclipse File → New → Android Application Project Fill in Project details... After first screen, just keep choosing “Next” button 9 Name that appears on device Project directory Android version Java package 10 3. Run the Android Application Run → Run As… → Android Application If it says “No AVD Found” or something like that, create a new one from Window → Android Virtual Device Manager The emulator may take a few minutes to start, so be patient! 11 12 Android Basic UI: Forms 13 Layout Files Android uses XML files to specify the arrangement of elements in the UI Activity: Java class that corresponds to a screen Layout: specifies the position of elements View: a visible element in the UI 14 1. Navigate to here 2. Then click here 15 activity_main.xml 16 We want something like this… TextView EditText Button 17 3. Then right click, select Edit Text, and change it 2. Then drag this to the top 1. Click here 18 1. Drag two Buttons to the bottom 19 2. Then click here 1. Right-click, select Edit Text, and change 20 2. Don’t forget to save it! 1. Drag this to the middle 21 The Layout should look like this now 22 The XML should look like this 23 Helpful Hints: Running your app The Graphical Layout tool only helps you edit the layout; it doesn’t actually run your app After you've edited activity_main.xml, run the project as an Android app and you should see it (re-)appear in the emulator Don't close the emulator! You don't need to restart it every time you rebuild your app!! 24 Handling Button Clicks 25 Handling Button Clicks The activity_main.xml file also can specify the method that should be called when the user clicks a Button This method should reside in the Java class that extends Activity Pay close attention to details when implementing the button click method! 26 2. Add this line 1. Click here 27 1. Navigate to here and open the file 2. Then add this method 28 29 Helpful Hints: Accessing Views In the class that extends Activity, you can get access to the Views in its Layout by using the findViewById method, along with the ID that is specified in activity_main.xml You do need to cast the View to the appropriate subclass, however 30 31 Helpful Hints: Debugging • Log.v(tag, message); • Window > Show View > Other > Android > LogCat • Stack traces go in LogCat, too • To show a pop-up window: Toast.makeText( getApplicationContext(), message, Toast.LENGTH_LONG).show(); 32 Dialogs 33 Dialogs Dialogs are pop-ups that have associated Buttons, which have associated actions (methods) Use a DialogBuilder to assemble the Dialog Note: the way I’m going to show you here is the “old” way (but it’s a bit easier) 34 1. Modify this line 2. Then this one 3. Then add this method 35 36 Helpful Hints: Activities Keep in mind that the class that extends Activity is just a regular old Java class Its methods can communicate via shared variables (aka “fields”) 37 1. Add this line 2. Add this line 3. Add/modify these three lines 4. Add this line 38 39 40 Just for fun! Add an “Are you ready?” Dialog that is shown when the app starts Hint: you’d need to call showDialog from the onCreate method Keep track of the best (lowest) time Randomly choose a sentence to display 41 Android Resources 42 Android Resources developer.android.com stackoverflow.com anddev.org 43 Installing your app on a device You can use Eclipse to install your app via a wired connection with your device Or... go into the /bin directory of the Eclipse project, find the .apk file, email it to yourself, and install it on the device via your email client You can also upload the .apk file to manymo.com to see how it would like on various other devices 44 Good luck today! 45