Changes

Jump to: navigation, search

Creating an Example Kotlin Android App in Android Studio

701 bytes added, 15:53, 22 November 2017
no edit summary
The first step in the application development process is to create a new project within the Android Studio environment. Begin, therefore, by launching Android Studio so that the “Welcome to Android Studio” screen appears as illustrated in Figure 3-1:
[[Image:android_studio_3.0_welcome.png]]
Once this window appears, Android Studio is ready for a new project to be created. To create the new project, simply click on the Start a new Android Studio project option to display the first screen of the New Project wizard as shown in Figure 3-2:
[[Image:android_studio_3.0_welcome.png]]
== Defining the Project and SDK Settings ==
To help in the decision process, selecting an API level from the chart will display the features that are supported at that level.
[[Image:as_3.0_help_me_choose.png]]
Since the project is not intended for Google TV, Android Auto or wearable devices, leave the remaining options disabled before clicking Next. Instant Apps will not be covered until later in this book so make sure that the Include Android Instant App support option is disabled.
The next step is to define the type of initial activity that is to be created for the application. A range of different activity types is available when developing Android applications. The Empty, Master/Detail Flow, Google Maps and Navigation Drawer options will be covered extensively in later chapters. For the purposes of this example, however, simply select the option to create a Basic Activity. The Basic Activity option creates a template user interface consisting of an app bar, menu, content area and a single floating action button.
[[Image:as_3.0_new_project_basic_activity.png]]
With the Basic Activity option selected, click Next. On the final screen (Figure 3-5) name the activity and title AndroidSampleActivity. The activity will consist of a single user interface screen layout which, for the purposes of this example, should be named activity_android_sample. Finally, enter My Android App into the title field as shown in Figure 3-5:
[[Image:as_3.0_new_project_configure_activity.png]]
Since the AndroidSampleActivity is essentially the top level activity for the project and has no parent activity, there is no need to specify an activity for the Hierarchical parent (in other words AndroidSampleActivity does not need an “Up” button to return to another activity).
At this point, Android Studio has created a minimal example application project and opened the main window.
[[Image:as_3.0_kotlin_main_ui.png]]
The newly created project and references to associated files are listed in the Project tool window located on the left-hand side of the main project window. The Project tool window has a number of modes in which information can be displayed. By default, this panel will be in Android mode. This setting is controlled by the menu at the top of the panel as highlighted in Figure 3-7. If the panel is not currently in Android mode, use the menu to switch mode:
[[Image:as_3.0_change_project_tool_window_mode.png]]
The example project created for us when we selected the option to create an activity consists of a user interface containing a label that will read “Hello World!” when the application is executed.
The user interface design for our activity is stored in a file named activity_android_sample.xml which, in turn, is located under app -> res -> layout in the project file hierarchy. This layout file includes the app bar (also known as an action bar) that appears across the top of the device screen (marked A in Figure 3-8) and the floating action button (the email button marked B). In addition to these items, the activity_android_sample.xml layout file contains a reference to a second file containing the content layout (marked C):
[[Image:as_3.0_new_helloworld.png]]
By default, the content layout is contained within a file named content_android_sample.xml and it is within this file that changes to the layout of the activity are made. Using the Project tool window, locate this file as illustrated in Figure 3-9:
[[Image:as_3.0_new_project_content_layout.png]]
Once located, double-click on the file to load it into the user interface Layout Editor tool which will appear in the center panel of the Android Studio main window:
[[Image:as_3.0_layout_editor.png]]
In the toolbar across the top of the Layout Editor window is a menu (currently set to Nexus 4 in the above figure) which is reflected in the visual representation of the device within the Layout Editor panel. A wide range of other device options are available for selection by clicking on this menu.
To change the orientation of the device representation between landscape and portrait simply use the drop down menu immediately to the left of the device selection menu showing the following icon: [[Image:as_3.0_new_project_rotate_button.png]]
As can be seen in the device screen, the content layout already includes a label that displays a “Hello World!” message. Running down the left-hand side of the panel is a palette containing different categories of user interface components that may be used to construct a user interface, such as buttons, labels and text fields. It should be noted, however, that not all user interface components are obviously visible to the user. One such category consists of layouts. Android supports a variety of layouts that provide different levels of control over how visual user interface components are positioned and managed on the screen. Though it is difficult to tell from looking at the visual representation of the user interface, the current design has been created using a ConstraintLayout. This can be confirmed by reviewing the information in the Component Tree panel which, by default, is located in the lower left-hand corner of the Layout Editor panel and is shown in Figure 3-11:
[[Image:as_3.0_component_tree.png]]
As we can see from the component tree hierarchy, the user interface layout consists of a ConstraintLayout parent with a single child in the form of a TextView object.
Before proceeding, check that the Layout Editor’s Autoconnect mode is enabled. This means that as components are added to the layout, the Layout Editor will automatically add constraints to make sure the components are correctly positioned for different screen sizes and device orientations (a topic that will be covered in much greater detail in future chapters). The Autoconnect button appears in the Layout Editor toolbar and is represented by a magnet icon. When disabled the magnet appears with a diagonal line through it (Figure 3-12). If necessary, re-enable Autoconnect mode by clicking on this button.
[[Image:as_3.0_disable_auto_connect.png]]
The next step in modifying the application is to delete the TextView component from the design. Begin by clicking on the TextView object within the user interface view so that it appears with a blue border around it. Once selected, press the Delete key on the keyboard to remove the object from the layout.
The Palette panel consists of two columns with the left-hand column containing a list of view component categories. The right-hand column lists the components contained within the currently selected category. In Figure 3-13, for example, the Button view is currently selected within the Widgets category:
[[Image:as_3.0_button_in_palette.png]]
Click and drag the Button object from the Widgets list and drop it in the center of the user interface design when the marker lines appear indicating the center of the display:
[[Image:as_3.0_new_project_adding_button.png]]
The next step is to change the text that is currently displayed by the Button component. The panel located to the right of the design area is the Attributes panel. This panel displays the attributes assigned to the currently selected component in the layout. Within this panel, locate the text property and change the current value from “Button” to “Demo” as shown in Figure 3-15:
[[Image:as_3.0_setting_button_text.png]]
A useful shortcut to changing the text property of a component is to double-click on it in the layout. This will automatically locate the attribute in the attributes panel and select it ready for editing.
At this point it is important to explain the warning button located in the top right-hand corner of the Layout Editor tool as indicated in Figure 3-16. Obviously, this is indicating potential problems with the layout. For details on any problems, click on the button:
[[Image:as_3.0_errors_and_warnings.png]]
When clicked, a panel (Figure 3-17) will appear describing the nature of the problems and offering some possible corrective measures:
[[Image:as_3.0_layout_editor_warnings_panel.png]]
Currently, the only warning listed reads as follows:
Click on the Fix button in the Issue Explanation panel to display the Extract Resource panel (Figure 3-18). Within this panel, change the resource name field to demostring and leave the resource value set to Demo before clicking on the OK button.
[[Image:as_3.0_new_project_extract_string_resource.png]]
It is also worth noting that the string could also have been assigned to a resource when it was entered into the Attributes panel. This involves clicking on the button displaying three dots to the right of the property field in the Attributes panel and selecting the Add new resource -> New String Value… menu option from the resulting Resources dialog. In practice, however, it is often quicker to simply set values directly into the Attributes panel fields for any widgets in the layout, then work sequentially through the list in the warnings dialog to extract any necessary resources when the layout is complete.
Before moving on to the next chapter, we are going to look at some of the internal aspects of user interface design and resource handling. In the previous section, we made some changes to the user interface by modifying the content_android_sample.xml file using the Layout Editor tool. In fact, all that the Layout Editor was doing was providing a user-friendly way to edit the underlying XML content of the file. In practice, there is no reason why you cannot modify the XML directly in order to make user interface changes and, in some instances, this may actually be quicker than using the Layout Editor tool. At the bottom of the Layout Editor panel are two tabs labeled Design and Text respectively. To switch to the XML view simply select the Text tab as shown in Figure 3-19:
[[Image:as_3.0_layout_editor_text_mode.png]]
As can be seen from the structure of the XML file, the user interface consists of the ConstraintLayout component, which in turn, is the parent of the Button object. We can also see that the text property of the Button is set to our demostring resource. Although varying in complexity and content, all user interface layouts are structured in this hierarchical, XML based way.
Resource strings may also be edited using the Android Studio Translations Editor. To open this editor, right-click on the app -> res -> values -> strings.xml file and select the Open Editor menu option. This will display the Translation Editor in the main panel of the Android Studio window:
[[Image:as_3.0_translation_editor.png]]
This editor allows the strings assigned to resource keys to be edited and for translations for multiple languages to be managed. The Order a translation… link may also be used to order a translation of the strings contained within the application to other languages. The cost of the translations will vary depending on the number of strings involved.

Navigation menu