1 / 37

Objectives

Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface. Objectives. In this chapter, you learn to: Develop a user interface using the TextView , ImageView , and Button controls

lemuel
Download Presentation

Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface Android Boot Camp for Developers Using Java, 2nd Ed.

  2. Objectives In this chapter, you learn to: • Develop a user interface using the TextView, ImageView, and Button controls • Create an Android project that includes a Button event • Describe relative and Linear layouts for the user interface • Create multiple Android Activities • Add activities to the Android Manifest file • Add a Java class file Android Boot Camp for Developers Using Java, 2nd Ed.

  3. Objectives (continued) • Write code using the onCreate method • Display content using the setContentView command • Open a second screen using a Button event handler • Use an OnClickListener to detect user interaction • Launch a second screen using a startActivity method • Correct errors in Java code • Run the completed app in the emulator Android Boot Camp for Developers Using Java, 2nd Ed.

  4. Designing an Android App • Designing apps is like constructing a building • The Big Picture • Follow these steps: • Create the user interface for every screen • Create an Activity for every screen • Update the Android Manifest file • Code each Java class with objects and actions Android Boot Camp for Developers Using Java, 2nd Ed.

  5. Using the Android User Interface • The interface is a window on the screen of any mobile device • The layout is designed with XML code • Special Android-formatted XML code is extremely compact • Relative Layouts and Linear Layouts • A Relative Layout organizes layout components in relation to each other • Provides more flexibility in positioning than Linear layouts • Must be changed from the linear default Android Boot Camp for Developers Using Java, 2nd Ed.

  6. Using the Android User Interface (cont’d) • A Linear Layout organizes layout components in a vertical column or horizontal row • Objects are placed directly below each other • Can be switched from vertical to horizontal orientation • Linear layout is the default layout Android Boot Camp for Developers Using Java, 2nd Ed.

  7. Using the Android User Interface (cont’d) Android Boot Camp for Developers Using Java, 2nd Ed.

  8. Using the Android User Interface (cont’d) • Designing the Healthy Recipes Opening User Interface Android Boot Camp for Developers Using Java, 2nd Ed.

  9. Using the Android User Interface (cont’d) • Android Text Properties • Text Property – changes the text written in the control • Text Size Property –can be adjusted in inches, millimeters, pixels, density-independent pixels, and scaled-independent pixels Android Boot Camp for Developers Using Java, 2nd Ed.

  10. Using the Android User Interface (cont’d) Android Boot Camp for Developers Using Java, 2nd Ed.

  11. Using the Android User Interface (cont’d) Android Boot Camp for Developers Using Java, 2nd Ed.

  12. Using the Android User Interface (cont’d) • Adding a File to the Resources Folder • Before you can use images, they must be placed in the resources folder • Res folder contains three subfolders • All folder names begin with drawable • xhdpi(resources for extra high-density screens) • hdpi (resources for high-density screens) • mdpi (resources for medium-density screens) • ldpi (resources for low-density screens) • Android supports three graphic formats • .png (preferred), .jpg (acceptable), .gif(discouraged) Android Boot Camp for Developers Using Java, 2nd Ed.

  13. Using the Android User Interface (cont’d) • Adding an ImageView Control • An ImageView control displays icons or graphics on the Android screen • Adding a Button Control • There are four types of Buttons • Button (buttons that perform a function) • Small Button (Smaller buttons) • ToggleButton (buttons that can be on or off) • ImageButton (buttons that have a picture on them) Android Boot Camp for Developers Using Java, 2nd Ed.

  14. Using the Android User Interface (cont’d) Android Boot Camp for Developers Using Java, 2nd Ed.

  15. Using the Android User Interface (cont’d) • Planning a Program • Gather and analyze program requirements • Design the user interface • Design the program processing objects • Code the program • Test the program Android Boot Camp for Developers Using Java, 2nd Ed.

  16. Creating Activities • Each screen is considered an activity • Constructed using XML layout files and a Java class • Creating an XML Layout file • All layout files are placed in the res/layout directory • Adding a Class File • A class describes a group of objects and serves as a blueprint, or template, for creating those objects • An object is a specific, concrete instance of a class • When you create an object, you instantiate it; meaning you define one particular variation of the object Android Boot Camp for Developers Using Java, 2nd Ed.

  17. Creating Activities (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  18. The Android Manifest File • The Android Manifest file contains: • the name of the Java application • a listing of each activity • permissions needed to access other Android functions (like accessing the Internet) • the minimum level of the Android APL • Adding an Activity to the Android Manifest • When applications have more than one activity, the Manifest must have an intent to navigate among multiple activities Android Boot Camp for Developers Using Java, 2nd Ed.

  19. The Android Manifest File (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  20. Coding the Java Activity • A method is a set of Java statements that can be included inside a Java class • Methods perform specific tasks • The method body contains a collection of statements that defines what the method does. • Coding an onCreate Method • public void onCreate(Bundle savedInstanceState) { • super.onCreate(savedInstanceState); } • Note the use of Curly braces { that begin and end the method code and the use of the semi-colon ; • SetContentView(R.layout.main); is added to display the screen Android Boot Camp for Developers Using Java, 2nd Ed.

  21. Coding the Java Activity (continued) • Displaying the User Interface Android Boot Camp for Developers Using Java, 2nd Ed.

  22. Coding the Java Activity (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  23. Coding the Java Activity (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  24. Coding the Java Activity (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  25. Coding the Java Activity (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  26. Coding the Java Activity (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  27. Coding the Java Activity (continued) • Creating a Button Event Handler • An event handler is part of a program coded to respond to a specific event • Tapping the button is called a click event • Java code must contain the following sections • Class property to hold a reference to the Button object • onClickListener() method to await the button click action • onClick() method to respond to the click event Code Syntax: Button b=(Button)findViewById(R.id.btnRecipe); Android Boot Camp for Developers Using Java, 2nd Ed.

  28. Coding the Java Activity (continued) • When you import the Button type as an Android widget, the button package becomes available throughout the app • An import statement makes more Java functions available to your app • A stub is a code placeholder module b.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub } }); Android Boot Camp for Developers Using Java, 2nd Ed.

  29. Coding the Java Activity (continued) • Coding a Button Event Handler Android Boot Camp for Developers Using Java, 2nd Ed.

  30. Coding the Java Activity (continued) • Coding a Button Event Handler Android Boot Camp for Developers Using Java, 2nd Ed.

  31. Coding the Java Activity (continued) • Coding a Button Event Handler Android Boot Camp for Developers Using Java, 2nd Ed.

  32. Coding the Java Activity (continued) • Coding a Button Event Handler Android Boot Camp for Developers Using Java, 2nd Ed.

  33. Coding the Java Activity (continued) • Correcting Errors in Code Android Boot Camp for Developers Using Java, 2nd Ed.

  34. Coding the Java Activity (continued) • Saving and Running the Application • Testing the App automatically saves it • The Save All button will save the project • Select Android Application from the dialog window the first time an app runs Android Boot Camp for Developers Using Java, 2nd Ed.

  35. Summary • Relative layouts arrange components freely on the screen • Linear layouts arrange screen components in a vertical column or horizontal row • Text Property and TextSize property are used when creating text • To display graphics (pictures and icons), use the ImageView control • An Activity is when the app makes contact with the user and is a core component of the app Android Boot Camp for Developers Using Java, 2nd Ed.

  36. Summary (continued) • Each screen in your app is an Activity • Every app has an Android Manifest file containing the name if the app and a list of each activity • When an app has more than one activity, it must have an intent so the app can navigate among multiple activities • A method is a set of Java statements included in a Java class • The onCreate method initializes an Activity Android Boot Camp for Developers Using Java, 2nd Ed.

  37. Summary (continued) • The setContentView command displays the content on the screen • Tapping or clicking a button invokes the event listener (OnClickListener) and runs the code in that button • Use the startActivity() method to start an Activity in an app that has multiple Activities • The intent contains a context - initiating Activity class is making the request - and the name of the Activity • Red error icons and red curly lines indicate errors Android Boot Camp for Developers Using Java, 2nd Ed.

More Related