to Android Application Development Fourth Edition 2014

Please download to get full document.

View again

of 37
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Document Description
Document Share
Documents Related
Document Tags
Document Transcript
  Introduction to Android Application Development, FourthEdition (2014) /google/android_3/9.html Part III. Android User Interface Design EssentialsChapter 8. Designing with Layouts In this chapter, we discuss how to design user interfaces for Android applications. Here, wefocus on the various layout controls you can use to organize screen elements in differentways. We also cover some of the more complex View controls we call container views.These are View controls that can contain other View controls. Creating User Interfaces in Android  Application user interfaces can be simple or complex, involving many different screens or only a few. Layouts and user interface controls can be defined as application resources or created programmatically at runtime. Although it’s a bit confusing, the term layout   is used for two different but related purposes in Android user interface design: In terms of resources, the /res/layout directory contains XML resource definitions oftencalled layout resource files. These XML files provide a template for how to draw controls onthe screen; layout resource files may contain any number of controls. The term is also used to refer to a set of ViewGroup classes, suchas LinearLayout, FrameLayout, TableLayout, RelativeLayout, and GridLayout. Thesecontrols are used to organize other View controls. We talk more about these classes later in this chapter. Creating Layouts Using XML Resources  As discussed in previous chapters, Android provides a simple way to create layoutresource files in XML. These resources are stored in the /res/layout project directoryhierarchy. This is the most common and convenient way to build Android user interfacesand is especially useful for defining screen elements and default control properties that youknow about at compile time. These layout resources are then used much like templates.They are loaded with default attributes that you can modify programmatically at runtime.You can configure almost any ViewGroup or View (or View subclass) attribute using theXML layout resource files. This method greatly simplifies the user interface design process,moving much of the static creation and layout of user interface controls, and basic definitionof control attributes, to the XML instead of littering the code. Developers reserve the abilityto alter these layouts programmatically as necessary, but they should set all the defaults inthe XML template whenever possible. 1/37  You’ll recognize the following as a simple layout file with a LinearLayout and asingle TextView control. Here is the default layout file provided with any new Androidproject in the Android IDE, referred to as /res/layout/activity_main.xml:<?xml version= 1.0 encoding= utf-8 ?><LinearLayout xmlns:android=  android:orientation= vertical  android:layout_width= match_parent  android:layout_height= match_parent ><TextView android:layout_width= match_parent  android:layout_height= wrap_content  android:text= @string/hello /></LinearLayout>This block of XML shows a basic layout with a single TextView control. The first line, whichyou might recognize from most XML files, is required with the android layout namespace,as shown. Because it’s common across all the files, we do not show it in any other examples.Next, we have the LinearLayout element. LinearLayout is a ViewGroup that shows eachchild View either in a single column or in a single row. When applied to a full screen, itmerely means that each child View is drawn under the previous View if the orientation is setto vertical or to the right of the previous View if the orientation is set to horizontal.Finally, there is a single child View—in this case, a TextView. A TextView is a control that isalso a View. A TextView draws text on the screen. In this case, it draws the text defined inthe “@string/hello” string resource.Creating only an XML file, though, won’t actually draw anything on the screen. A particular layout is usually associated with a particular Activity. In your default Android project, thereis only one Activity, which sets the activity_main.xml layout by default. To associatetheactivity_main.xml layout with the Activity, use the method call setContentView() with theidentifier of the activity_main.xml layout. The ID of the layout matches the XML filenamewithout the extension. In this case, the preceding example came from activity_main.xml, sothe identifier of this layout is simply activity_main:setContentView(R.layout.activity_main);  Warning The Android tools team has made every effort to make the Android IDEgraphical layout designer feature complete, and this tool can be helpful for designing and previewing how layout resources will look on a variety of different devices. However, the preview can’t replicate exactly how the layout appears toend users. For this, you must test your application on a properly configured emulator and,more important, on your target devices. 2/37  Creating Layouts Programmatically You can create user interface components such as layouts at runtime programmatically,but for organization and maintainability, it’s best to leave this for the odd case rather thanthe norm. The main reason is that the creation of layouts programmatically is onerous anddifficult to maintain, whereas the XML resources are visual and more organized and couldbe used by a separate designer with no Java skills.  Tip The code examples provided in this section are taken fromthe SameLayout application. The source code for the SameLayout application isprovided for download on the book’s website.The following example shows how to programmatically have an Activity instantiatea LinearLayout and place two TextView controls within it as child controls. The same twostring resources are used for the contents of the controls; these actions are done atruntime instead.public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView text1 = new TextView(this); text1.setText(R.string.string1); TextView text2 = new TextView(this); text2.setText(R.string.string2); text2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 60); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.addView(text1); ll.addView(text2); setContentView(ll);}The onCreate() method is called when the Activity is created. The first thing this methoddoes is some normal housekeeping by calling the constructor for the base class.Next, two TextView controls are instantiated. The Text property of each TextView is setusing the setText() method. All TextView attributes, such as TextSize, are set by makingmethod calls on the TextView control. These actions perform the same function of settingthe properties Text andTextSize as when using the Android IDE layout resource designer,except these properties are set at runtime instead of defined in the layout files compiledinto your application package. 3/37   Tip The XML property name is usually similar to the method calls for getting andsetting that same control property programmatically. For instance, android:visibility maps to the methods setVisibility() and getVisibility().In the preceding sample TextView, the methods for getting and settingthe TextSize property are getTextSize() and setTextSize().To display the TextView controls appropriately, we need to encapsulate them within acontainer of some sort (a layout). In this case, we use a LinearLayout with the orientationset to VERTICAL so that the second TextView begins beneath the first, each aligned to theleft of the screen. The twoTextView controls are added to the LinearLayout in the order wewant them to display.Finally, we call the setContentView() method, part of the Activity class, to drawthe LinearLayout and its contents on the screen. As you can see, the code can rapidly grow in size as you add more View controls and youneed more attributes for each View. Here is that same layout, now in an XML layout file:<?xml version= 1.0 encoding= utf-8 ?><LinearLayout xmlns:android=  android:orientation= vertical  android:layout_width= match_parent  android:layout_height= match_parent > <TextView android:id= @+id/TextView1  android:layout_width= match_parent  android:layout_height= wrap_content  android:text= @string/string1 /> <TextView android:id= @+id/TextView2  android:layout_width= match_parent  android:layout_height= wrap_content  android:textSize= 60sp  android:text= @string/string2 /></LinearLayout>You might notice that this isn’t a literal translation of the code example from the previoussection, although the output is identical, as shown in Figure 8.1. 4/37
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks