Assignment 1

Welcome to week one of Mobile Systems and Applications! (cs.upt.ro/~alext/msa/lab1)

Topics of discussion

  • Android OS, ADT installation in Eclipse / Android Studio. The structure of an Android project, API demos.

  • Hello World in Android using views and listeners.

Android OS supports hundreds of millions of mobile devices in more than 190 countries worldwide. It is the largest mobile platform and growing rapidly, with millions of new users placing their hand on Android for the first time starting the search for applications, games and other digital content. Thus, Android provides a platform to create applications and games for Android users everywhere, and an open market for their instant distribution. The openness of Android towards software developers turned into a favorite for consumers and developers alike, leading to a sharp increase in the consumption of apps.

Google Play is the market for selling and distributing Android apps, and publishing an app on Google Play means access to a vast user base around the world. As an open market, Google Play allows you to control how the products are sold. Apps can be published anytime, whenever, for any customer. They can be distributed broadly to all markets and devices, or can be focused on specific segments, devices, or ranges of hardware capabilities.

The two most popular development environments for Android are Eclipse and Android Studio, based on InetlliJ. This MSA guide is designed using the Android Studio platform, meaning that it provides code examples (screenshots) and explains the steps necessary to create, import, edit projects using Android Studio. However, being based on the Java programming language, there is no difference in function or API code relating to the examples provided.

Install Android Studio

The two mentioned IDE are easily found online for free and do not require any complicated settings. You may find the full Android Studio development package as a zip here, or you may download Eclipse and get the ADT Bundle for Android, so we can begin to develop our first app.

The official support for Android development is provided on developer.android.com and is one of the richest programming documentations available online! Combined with sites like stackoverflow and Vogella, you will definitely find everything you need.

Android SDK, emulator (AVD) and the device monitor (ADM)

Even if you have used IntelliJ before, you will definitely notice a few additions in Android Studio, which has integrated all the necessary tools to deploy apps as fast and smooth as possible. Looking at the top menu bar (Figure 1) we see the following important commands:

Fig.1.

  1. Run application - your app will start faster and run smoother than in debug mode, but with the loss of control. If you're confident you app is stable and just want to see it run, definitely use "run".

  2. Debug application - unleashes the breakpoint magic, but the the cost of less responsive app. Yet, it is essential for the development cycle.

  3. Stop application - the application can be stopped from the emulator (see later) or from this big red button in the IDE. It is always faster than pressing the back button a couple of times in the emulator to exit the app.

  4. Android Virtual Device Manager (AVD) - allows you to create one or more emulators for any type of Android phone. Since Android is an OS after all, you cannot run the resulting bytecode on your Windows or Linux host computer; you need a new host, namely a virtual machine. Figure 2 shows you an overview of the AVD manager with 5 emulators each with different settings (resolution, API, architecture). To start a phone on your PC, just press the run button.

    1. Fig.2.

  1. SDK manager - Android has a lot of versions and comes with a variety of extras. By default, you will have the latest sdk installed and a minimum to get you started. In Figure 3 you have the list of Android OS versions from 7.0 down to 2.1.

    1. Fig.3.

  1. As programmers, we won't call these versions 5 or 6 or 7 (marketing terms), but rather APIs 21, 22, 23, 24 etc. For this guide, we will rely on API 23, so make sure you download the SDK platform and the Intel x86 emulator image. Also, as depicted in Figure 4, download the following extras:

    1. Android support repository

    2. Android support library

    3. Google play services

    4. Google repository

    5. Google USB driver

    6. Intel x86 emulator accelerator (HAXM) - third party app requiring admin rights. It uses the Intel CPU virtualization facilities to speed-up your emulator (by a lot). May need tinkering in your BIOS.

      1. Fig.4.

  1. Android Device Monitor (ADM) - a powerful analysis and debugging tool. For example, you may explore the file system on your emulator/phone, make calls and send text to the emulator, send (fake) GPS coordinates (very useful for testing), and gather insights about resource usage like cpu, heap, threads, network etc. More details online.

Creating your first project

Once the IDE is set up, let's create our Hello World app in Android. To do so, go to File>New>New Project. You will see something like in Figure 5. Unlike Eclipse, you don't have a workspace folder in Android Studio, that is, you may run apps from your desktop, C, D, E partitions altogether. However, it is best to define your own workspace folder just for Android projects. Any project may be copied to another computer - with all the prerequisites installed - and imported using the File>Open command. As easy as that!

Fig.5.

The name you choose for your app, combined with the company domain create the fully qualified named that will be used by your app on Google Play. That means, it will be a unique identified used for downloading and sharing your app online. Hint: don't use silly names of you want to actually publish your app at some point.

On the next page (Figure 6) you have to choose you development platform. Whether it is a phone or tablet, we will always go for the first checkbox. Important: the minimum SDK you choose influences a couple of things:

  • the lower it is, the higher the compatibility with all existing phones will be. As a general rule. API 9 or 11 are low enough and will offer >99% compatibility with all phones.

  • the lower it is, the less advanced API calls you are allowed to make. For example, adding animations on a button, or using material design will not work on API 11.

  • the higher it is, the more flexibility you have with some API calls.

    • Fig.6.

As a general rule, since 2016, it is considered safe to to with minimum SDK 15. Any higher and your app will simply not run on devices, and will not even be visible on Google Play for download. Always ask yourself: do I really need this flashy effect in my app, at the cost of not being able to target, let's say, Samsung S3 and S4 users? Probably not.

Next, we have to choose the type of our first activity (Figure 7). This is the layout of the main app window. For the sake of keeping things simple, let's choose an empty activity. On the next page we just press finish.

Fig.7.

Project structure

Once the new project has been created, you will see MainActivity.java and activity_main.xml opened in the main view of Android Studio. On the left pane you will find a vertical button labeled 1:Project which opens/hides the

project structure of your Hello Android app. Similar to Figure 8, your project will have the following key elements:

Fig.8.

  1. Manifest file - configuration file for your app. Here you specify the package name of your app (the one also used by Google Play), special permissions your app requires (e.g. Internet, location, user contacts, send sms), all activities and services your project uses.

  2. Java class folder - here lies all the java code for your application. Everything you know about Java still holds in Android Studio, so feel free to make new packages and use nice design patterns.

  3. Resources folder (res) - contains the resources that will be used programatically in java code. To access them, class R is automatically created at build time and contains integer ids for all resources defined. In code, you get them by calling, for example, R.string.text1 and R.drawable.icon1.

  4. Drawable - drawables are image resources, in jpg, png, gif format, with lowercase letters and digits. On the disk, there are several drawable folders, like drawable-ldpi, -mdpi, hdpi, -xhdpi and so on. These are shadows of the same folder in which the OS expects you to put the same image designed for different resolutions (DPI). Ideally, when creating an app, you will have a icon set rather than an icon for a specific button. This will ensure an optimal resource usage and user experience across all devices. If you have only one image, you can simply place it in any of the folders. Alternatively, you may use an icon generator to create the full set of icons.

  5. Layout - contains the graphical layouts of all your windows (activites) and allows you to edit them in either a drag-and-drop mode (not encouraged at all; see Figure 9), or an xml-manual mode (see Figure 10).

    1. Fig.9.

      1. Fig.10.

  1. Values - contains other data such as strings, colors, themes, dimensions that need not be hardcoded in your java code. The best example here is that while you define labels and buttons in the layout.xml, you should not hardcode their text in the xml or load a predefined string at runtime. Android encourages you to define all strings in res/values/strings.xml as a key-value pair, and thus refer to any text as R.string.text1.

  2. Gradle file(s) - configuration for the automatic build toolkit. Contains important details about the minimum and target SDK, the version of this app (important for uploading on Google Play), and all dependencies. Unlike Eclipse, you will almost never have to download a jar library and place it in the libs folder. While this can be easily done, gradle allows you to specify the fully qualified name of the library you require, and it is automatically downloaded at build time. More about gradle to read online. See Figure 11 for an example gradle file.

    1. Fig.11.

Running your app

Once your application builds successfully, you may run it either on the emulator or on your Android smartphone. To run the app on your phone follow these steps:

  • Connect the phone to the computer via the USB charging cable, and make sure it is detected by the device manager.

  • Go to phone settings - developer options - enable USB debugging.

In case of the emulator, you have to go to the AVD manager and start the emulator, then wait 1-2 minutes for it to come online. After these steps, simply press the run/debug buttons and you will see the deployment target selection window, similar to Figure 12.

Fig.12.

Let's develop the Hello Android app

Starting from the empty app skeleton we created, let's add a very simple functionality to the Hello Android app - an input text box for our name, and a button to transfer the written name to a label box. First of all, we need to define a few concepts. While many programming languages make use of the terms label (non-editable) and textbox (editable), in Android we have different names, as follows:

  • TextView - non-editable view which holds text (like a label)

  • EditText - ecitable version of the textview

  • Button - clickable view used to trigger commands

  • ImageButton - interactible view that can hold text and an image

  • ImageView - interactible (but not by default) view that can hold an image

  • Layouts - placement managers for all other views inside them

  • CheckBox, ToggleButton, Switch - views for selecting items

  • Spinner - a drop-down list for selecting single items

  • ProgressBar, SeekBar, RatingBar - views for user progress

  • WebView - holder for viewing html content

  • ListView, RadioGroup, GridView, ScrollView, TabHost - containers for multiple items of the same type

  • many others, feel free to explore the design view in any layout xml.

First, we will design our layout. Place at will, one edittext, one button and one textview in your main activity layout xml file. You can use the graphical design mode for this purpose. Try to make them fit the screen space nicely. You may try to achieve something similar to Figure 13.

Fig.13.

Second, we need to add the functionality, as suggested in Figure 13 (right). By pressing the "Say hello" button, we will print a greeting based on the input text. There are two ways to achieve this programatically.

  • One solution is to attach an OnClickListener to the button. This listener must implement the onClick method which is the code that will run whenever you tap the button on the screen. To obtain the text content from the edittext we need to instantiate it in our MainActivity class. As such, refer to the id defined in the xml file. for convenience, we will call the view eName, the button bClick, and the textview tName. To dynamically load the reference of any view in our java code we will use the method findViewById(id) in the onCreate method.

EditText eName = (EditText) findViewById(R.id.eName);

Button bClick = (Button) findViewById(R.id.bClick);

TextView tName = (TextView) findViewById(R.id.tName);

bClick.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// ...

}

});

  • The second solution is to add an onClick="clicked" tag to your button definition in the xml file. This tag requires that whenever the button is clicked a clicked method defined in your activity to be called. This method has to be defined exactly as public void clicked(View view), otherwise the app will crash. The view parameter is the actual view that was clicked. It may be any of the views declaring the onClick tag with the same value. To determine which view was clicked, the following code is standard:

public void clicked(View view) {

switch(view.getId()) {

case R.id.bClick:

// .... todo

break;

case R.id.otherView: break;

}

}

Note: both of the solutions above are correct, and you will use them for all sorts of views, like clicking on list items, image views, radio buttons etc. However, the code may become cumbersome if you add a lot of listeners in your code, so the second solution is more elegant in terms of readability. However, the second solution is only applicable for the click event. Sometimes you might need to add other types of listeners, like onTextChanged, onLongClick, case in which you have to use the first solution.

Extras

  • What happens if we have both the listener and the clicked method implemented? Will they both run? Try it out.

  • Try adding a popup window with the same greeting text when you click the button. See dialogs in the online Android documentation for implementation details.

  • Add a positive and negative button to your dialog and toast any message on the screen based on which one you click. See dialog builder and Toast in the online documentation.

  • If running your app on a smartphone, try rotating the phone from portrait to landscape. You can rotate the emulator with Ctrl+F6. Does the layout still look the same? If not, we will fix this issue next week once we talk about layout managers.

Have a fruitful week.

Dragon fruit

Pitaya Fruit, Pitahaya Fruit or commonly known as the Dragon. It is a favorite to the people of Asian origin. It features a mouth watering light sweet taste, with an intense shape and color, not to forget its outstanding flowers. In addition to being tasty and cool, this is a beautiful fruit with a lot of water and other vital minerals with varied nutritional ingredients.

The dragon fruit is exactly of the Hycocereus undatus family, thus making it one of the extensive red dragon fruit in asian region. Typically, the fruit has red squash, or red flesh. The fruit originates from the dragon fruit cactus plant, which is usually appears like as vines.

http://www.fruitsinfo.com/Dragon-Fruit-Exotic-fruits.php