Android graphical interface

Android graphical interface

Android graphical interface: “layout” files User interfaces (views) are described in XML files that define controls’ positioning (buttons, images, text boxes, etc.) and how they are arranged with one another (below, on the right, on the left, etc.) in a linear container, in absolute position, in a grid, horizontal, vertical, etc. NOTE.– The screen orientation (according to the position detected by inertial sensors: portrait or landscape) can be managed at the activity declaration level in the manifest file.

Designing a layout Two design modes are available from Eclipse: the visual designer (“Graphical layout” tab) that enables to drag and drop components and to graphically place them in the view, and the XML view.

NOTE.– A layout can refer to another layout defined in another XML file; this allows reuse of atomic pieces of views. codeshoppy.com/android-projects-titles-ieee.html In order to do so, we use the include tag referencing the layout file that is to be included the coding wizard menu in Eclipse that is activated by positioning the cursor at the right location and pressing the “Ctrl”+”space” key combination; then, you just need to choose the element you wish to insert from the list of available codes by scrolling (with the arrow keys) and to press “enter” to confirm.

Associating a layout to an activity and handling controls A layout can be “set” (method “setContentView”) to one or several activities (usually in the “onCreate” method). From this layout,  controls can be differently displayed in order to adapt the targeted devise regardless of the type of display and regardless of the published version of your Android app; for example, it could be installed on a smartphone, on a tablet, on a smart watch, as well as on a smart oven and any other smart object running under OS Android with a display. NOTE.– Fragments15, introduced since the Honeycomb version 3.0 and Android API 11, ease the modular management of views and the display on screens of different sizes and shapes. In this way, a fragment is a piece of code in charge of controlling a view. It has its own lifecycle, and can thus be dynamically added or removed from the activity based on the events or on the type of display detected during the creation of the activity (onCreate method).

Handling the user’s actions The user’s actions are handled at the activity and/or at the layout and/or at the control levels with inherited callback methods overriding or by implementing an interface or a callback object. For example, onClickListener, onTouchListener, onScrollListener, etc. The source code gives an example on how touch events are triggered and processed after a finger sliding on the screen by managing the x and y coordinates corresponding to the top left and the bottom right corners of the virtual rectangle shaped by the fingers position compared to the screen landmark at the start of the movement (ACTION_DOWN), during the movement (ACTION_POINTER_DOWN), and when the movement finishes when the user lifts his/her fingers from the screen (ACTION_MOVE). In the same way, when the user presses “return” and “home” keys of the system, the event is sent to the onKeyDown callback that can be overridden in the activity as illustrated in the code. Actions on menus can also be intercepted (or processed), for example, by overriding the onMenuItemSelected method.

Compiling and testing an Android application By default in Eclipse IDE, the Android project compilations are done automatically. This can sometimes be a source of troubles when the coding is in progress (error messages, slowness due to compilation in the background). You can deactivate this option through the Project menu by unchecking the “Build automatically” option of automated compilation. The “Build project” option is then available in the shortcut menu of the project (right click at the project root in the packages explorer). Sometimes, a project can contain errors you might not understand: it can be due to binary objects previously generated and which are no longer compatible with the last version of the source code (e.g., removal of layouts). In this case, you should “clean” the generated binary objects from the Project|Clean menu (remember to do this every time this kind of problem occurs!).

Launching the application Once the application is ready for testing (no compilation errors), it can be launched from the Run|Run.as|Android Application menu, which is also available in the project’s shortcut menu. to launch our application project directly on our Android device, because the emulator requires resources allocated to it, and this can drive into our system overhead and result in a very, even extremely slow display.

Using the Android device emulator The menu Window|Android Virtual Device (AVD) manager as well as the associated tool framed in Figure 2.21 enable management of the Android device emulator: the NOTE.– We may note that NFC applications are not easy to test using a virtual device: indeed, you will also need to install an NFC reader emulator as well as an NFC tag or contactless card emulator16, which is very cumbersome to configure, but this will not ensure the same behaviors as on an NFC-enabled Android device. We thus strongly recommend testing applications with ALL types of devices you wish to deploy your application on.

Using an Android device connected to the USB port The ADT ADB component detects the Android device connected to the USB port and allows the application launching directly on the plugged device. NOTE.– We will need to make sure that the installed OS Android version and the features of the device on which we launch the application are compatible with what was declared in the AndroidManifest file. Moreover, for the Android device to be detected by ADB, we need the appropriate ADB driver according the model of the Android device you connect to your system. NOTE.– Google’s drivers comply with a broad range of devices and can be manually installed from the folder extras found in the Android SDK directory (for example in usual Windows environments, it can be found at C:\Program Files (x86)\Android\android-sdk\extras\google\ usb_driver).