Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Eng. Jehad Aldahdooh Mobile Computing Android Lab Lab 1: Getting Started With Android Programming To create a new Android Project follows the steps: 1. Open Eclipse. 2. Click the menu File New Android Application Project 1
3. Name the project : In this stage, as shown below there are exist three names described as: Application Name Shown in Play Store and Settings.Usually same as Project Name. Project Name Eclipse project name. Follow naming convention you use for Eclipse. Not used elsewhere. Package name Apps on a particular Android device must have unique packages, so use com.yourcompany.project 2
Target Build SDK The Android version used to build/compile your project. This can be any version, but the safest option is to make it match the minimum SDK below. Minimum Required SDK The Android version that you want to run on. 4. Click Next 5. Configure Launcher Icon Purpose To choose the picture displayed on the Android device, that, when clicked, launches the app. Defaults Use defaults for development and testing. Just press Next. 3
6. Choose Blank Activity Options for Blank Activity: Activity Name Name of main Java class. This is the class you will edit first. Class name often corresponds to project name. Layout Name Base name of XML file in res/layout folder. Used to give layout to app. often just called main. Will be referred to in main Java class with R.layout.layout_name. 4
5
Anatomy of an Android Application Source Code Version of the library you had chosen Resources that your application may need Use to construct user interfaces Value type resources, such as string Java files autogenerated by ADT Anything stored in the 'assets' folder has to be accessed in the classic 'file' manipulation style Hold image and animation files that you can use in your Contains all the information about the application 6
Run Hello World on the Emulator and Phone First: On the Emulator Before we can run the application, we need to setup an Android Vitual Device(AVD), or emulator, to run it on: Select the menu Window -> "Android SDK and AVD Manager". Select Virtual Devices on the left hand side. Click the New... button. Give your AVD a name. Select the target build that we would like to run the application on Click Create AVD and close out the SDK/AVD Manager. We're now ready to run our application. Select the menu Run -> Run. Note: The emulator may take a long time to start up. Note: Another way to run your application is to right-click on the project in the Package Explorer, then select Run As -> Android Application. 7
Second: On a Physical Device Project Modifications From the Package Explorer, double-click the file AndroidManifest.xml. Select the tab labeled AndroidManifest.xml along the bottom. Add android:debuggable="true" to the inside of the opening <application> tag. Save the file and close it. Phone Modifications o Select Settings -> Applications -> Development. o Enable the USB debugging option Ensure the device is properly connected. Run the application as you would normally. 8
Three Main Approaches in Android Programming 1. Java-based 1. XML-based 2. Hybrid Use Java to define Strings, lay out window, create GUI controls, and assign event handlers. Like Swing programming. Use XML files to define Strings, lay out window, create GUI controls, and assign event handlers. The Java method will read the layout from XML file and pass it to setcontentview. Use an XML file to define Strings, layout window and create GUI controls. Use Java to assign event handlers. 1. Java-Based Approach: Template Approach Use Java to define Strings, layout window, create GUI controls, and assign event handlers. Advantages Familiar to Java desktop developers. Like approach used for Swing, SWT, and AWT. Good for layouts that are dynamic (i.e., that change based on program logic). Disadvantages Harder to maintain. Not generally recommended except for dynamic layouts. But still acceptable for App Store. Whatever works best for your programmers and your app. No code police. 9
OnClickListener is a public inner class inside View. But, as long as you import android.view.view.onclicklistener, you use it just like a normal class. And, remember that Eclipse helps you with imports: just type in the class name. 2. XML-Based Approach: Template Approach Use XML files to define Strings, layout window, create GUI controls, and assign event handlers. Define layout and controls in res/layout/main.xml Define Strings in res/values/strings.xml Advantages Easier to maintain Can use visual layout editor in Eclipse 10
Disadvantages Works poorly for dynamic layouts 3. Hybrid Approach: Template 11
Activity Class Activity: Much like a Form for a web page, activities display a user interface for the purpose of performing a single task. An example of an Activity class would be one which displays a Login Screen to the user. Create the Activity Class create a new class that extends android.app.activity class and implements android.view.view.onclicklistener interface. Add a method with the following signature: public void oncreate(bundle savedinstancestate). o This method will be called when the Activity starts and is where initialization of local and member data will be done. Inside this method perform the following: 12
o make a call to super.oncreate(savedinstancestate) This should always be done and is to ensure that any necessary parent class initializations are performed. o make a call to this.setcontentview(r.layout.activity_main) When you created the XML layout file earlier, the Android Eclipse Plugin automatically added a static constant to the static R.layout class in the R.java file under the /gen folder. This constant variable has the same name of the file and it's value is used to identify the layout file. This call tells Android to create a screen based off of the layout file. o Make a call to this.findviewbyid(r.id.button) and set your Button member variable equal to the return value. Implement the OnClickListener interface by creating a method stub with the following signature: public void onclick(view v). Android Lifecycle The Activity base class defines a series of events that govern the life cycle of an activity. The Activity class defines the following events: oncreate() Called when the activity is first created onstart() Called when the activity becomes visible to the user onresume() Called when the activity starts interacting with the user onpause() Called when the current activity is being paused and the previous activity is being resumed onstop() Called when the activity is no longer visible to the user ondestroy() Called before the activity is destroyed by the system (either manually or by the system to conserve memory onrestart() Called when the activity has been stopped and is restarting again 13
14
15
1. Run the application. 2. When the activity is first loaded, you should see something very similar to the following in the LogCat window In the oncreate() event In the onstart() event In the onresume() event 3. If you click the Back button on the Android emulator, the following is printed: In the onpause() event In the onstop() event In the ondestroy() event 4. Click the Home button and hold it there. Click the Activities icon and observe the following: In the oncreate() event In the onstart() event In the onresume() event 5. Click the Phone button on the Android emulator so that the activity is pushed to the background.observe the output in the LogCat window: In the onpause() event In the onstop() event 6. Notice that the ondestroy() event is not called, indicating that the activity is still in memory. Exit the phone dialer by clicking the Back button. The activity is now visible again. Observe the output in the LogCat window: In the onrestart() event In the onstart() event In the onresume() event 16