University of Stirling Computing Science Telecommunications Systems and Services CSCU9YH: Android Practical 1 Hello World

Similar documents
COMP4521 EMBEDDED SYSTEMS SOFTWARE

Create new Android project in Android Studio Add Button and TextView to layout Learn how to use buttons to call methods. Modify strings.

CSCU9YH Development with Android

Mobile Programming Lecture 1. Getting Started

Lab 1: Getting Started With Android Programming

Introduction To Android

Developed and taught by well-known Contact author and developer. At public for details venues or onsite at your location.

Configuring the Android Manifest File

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

Exercise 1: First Android App

Android Beginners Workshop

(Refer Slide Time: 1:12)

CE881: Mobile & Social Application Programming

Android Studio is google's official IDE(Integrated Development Environment) for Android Developers.

8/30/15 MOBILE COMPUTING. CSE 40814/60814 Fall How many of you. have implemented a command-line user interface?

XML Tutorial. NOTE: This course is for basic concepts of XML in line with our existing Android Studio project.

Real-Time Embedded Systems

UNDERSTANDING ACTIVITIES

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

LECTURE NOTES OF APPLICATION ACTIVITIES

Android Workshop: Model View Controller ( MVC):

Using Eclipse for Android Development

ECOM 5341 Mobile Computing(Android) Eng.Ruba A. Salamah

Mobile Application Development - Android

Vienos veiklos būsena. Theory

Embedded Systems Programming - PA8001

App Development for Android. Prabhaker Matet

CMSC 436 Lab 10. App Widgets and Supporting Different Devices

Our First Android Application

Computer Science E-76 Building Mobile Applications

CS 4518 Mobile and Ubiquitous Computing Lecture 2: Introduction to Android. Emmanuel Agu

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Lab 3. Accessing GSM Functions on an Android Smartphone

CS 234/334 Lab 1: Android Jump Start

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (1 &2 of 3) Hello-Goodbye App Tutorial

Fig. 2.2 New Android Application dialog. 2.3 Creating an App 41

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Android HelloWorld - Example. Tushar B. Kute,

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

CS 403X Mobile and Ubiquitous Computing Lecture 3: Introduction to Android Programming Emmanuel Agu

Getting started: Installing IDE and SDK. Marco Ronchetti Università degli Studi di Trento

Mobile Computing Practice # 2c Android Applications - Interface

Developing Android Applications

Making use of Android

CS 4518 Mobile and Ubiquitous Computing Lecture 2: Introduction to Android Programming. Emmanuel Agu

android-espresso #androidespresso

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Security model. Marco Ronchetti Università degli Studi di Trento

Figure 2.10 demonstrates the creation of a new project named Chapter2 using the wizard.

CS 4518 Mobile and Ubiquitous Computing Lecture 4: WebView (Part 2) Emmanuel Agu

Assignment 1: Port & Starboard

Produced by. Mobile Application Development. Eamonn de Leastar

Lab Android Development Environment

Getting Started With Android Feature Flags

(Refer Slide Time: 0:48)

Chapter 2 Welcome App

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

How to Set up Eclipse and Android SDK Manager Environment You need to download the following

Lifecycle Callbacks and Intents

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

CMSC436: Fall 2013 Week 3 Lab

Applications. Marco Ronchetti Università degli Studi di Trento

Introduction to Android Development

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Programming Concepts and Skills. Creating an Android Project

INTRODUCTION TO ANDROID

Java Basics A Simple Hit the Zombies Game

Android Tutorial: Part 3

ES E 3 3 L a L b 5 Android development

CS 528 Mobile and Ubiquitous Computing Lecture 2a: Introduction to Android Programming. Emmanuel Agu

Android Application Development. By : Shibaji Debnath

Mobile Application Development

10.1 Introduction. Higher Level Processing. Word Recogniton Model. Text Output. Voice Signals. Spoken Words. Syntax, Semantics, Pragmatics

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

EMBEDDED SYSTEMS PROGRAMMING Android NDK

Lecture 1 Introduction to Android. App Development for Mobile Devices. App Development for Mobile Devices. Announcement.

COPYRIGHTED MATERIAL. 1Getting Started with Android Programming

CS 528 Mobile and Ubiquitous Computing Lecture 1b: Introduction to Android. Emmanuel Agu

Hello World. Lesson 1. Create your first Android. Android Developer Fundamentals. Android Developer Fundamentals

In the first class, you'll learn how to create a simple single-view app, following a 3-step process:

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Mobile User Interfaces

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

Upcoming Assignments Quiz Friday? Lab 5 due today Alpha Version due Friday, February 26

CS 4518 Mobile and Ubiquitous Computing Lecture 3: Android UI Design in XML + Examples. Emmanuel Agu

Upon completion of the second part of the lab the students will have:

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

Google Android on the Beagleboard

Microsoft Expression Web is usually obtained as a program within Microsoft Expression Studio. This tutorial deals specifically with Versions 3 and 4,

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

Microsoft Expression Web Quickstart Guide

LECTURE NOTES ON MOBILE APPLICATION DEVELOPMENT

Android Development Tools = Eclipse + ADT + SDK

Android/Java Lightning Tutorial JULY 30, 2018

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

Learn about Android Content Providers and SQLite

Android Software Development Kit (Part I)

Transcription:

University of Stirling Computing Science Telecommunications Systems and Services CSCU9YH: Android Practical 1 Hello World Before you do anything read all of the following red paragraph! For this lab you are given a memory stick. Do not plug it in until you are fully logged-in. Do not plug it in at all if you can see the E: drive. If you cannot see the E: drive then please plug it in and check the stick appears on the E: drive. It must be the E: drive. Only then should you start Android Studio. (Use the USB socket in the keyboard if it is available to minimise any chance of damage.) If necessary seek advice. Warning! Last year we have moved from using the Eclipse IDE to Android Studio. This has not been without its challenges! As the industrial standard for Android development is very much in the Android Studio camp, we thought it was important that this course reflected this reality. It does provide a useful user interface design capabilities and it is good at supporting a single development for a wide range of device types and screen sizes. However Android studio is very much designed to work on a dedicated computer, and so when we use it the lab in a secure and flexible networked environment we may find it slow at times - in particular for starting the emulator. This is always slow, but can at times be particularly slow in the lab. So please do have some patience. You will often need to wait. It is best to think of starting the emulator as powering up your phone it takes time so do it as little as possible. Start it at the beginning, and leave it running. Android Studio in the lab has been configured to use your H: drive for the source files to provide flexible working in the lab. Each installation will also use the E: drive for working files but this need not concern you except to ensure the memory stick is on the E: drive. If you are working at home on your own computer or in the lab on your own laptop, you should find things rather faster! Some prefer to use their own phones rather than the emulator, however not all the necessary drivers are in place for every type of phone. Background If you are familiar with the Eclipse development environment you should find the user interface of Android Studio familiar. The course book Hello, Android is useful background reading on using Android Studio but in addition useful web references on Android include: http://code.tutsplus.com/tutorials/getting-started-with-android-studio--mobile-22958 which is a useful tutorial on getting started with Android studio. developer.android.com is a good Android development reference. A screenshot at the start of each practical will show what your application should look like. Some of the practicals require you to create lengthy code. Rather than typing this from scratch, you can copy-and-paste it (as plain text) from the online PDF of the practical. Android Laboratory 1 1

In this practical you will create a simple hello world Android application. Screen shots have been added as indicative guides but may not be exactly what you see. For example you should select API 24 in the window title not API 22 as shown here. Names and text may differ too. Create a New Android Project When you start up Android select a new project and then choose an application name, and then API 24 with Android 7.0 (Nougat): Should be API 24 with Android 7.0 Should be on your H: drive The screenshots are only indicative here you may well want to use your own names. Now choose an empty activity and give it a meaningful name: Now navigate your way to your activity class (you may have called it something else to this screenshot and indeed have a different package name!) but you will have something along the lines of the code given below: package uk.ac.stir.cs.android; import android.app.activity; import android.os.bundle; On the current AS this come up as AppCompatActivity See text on next page. public class HelloActivity extends Activity { /** Called when the activity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Android Laboratory 1 2

} } Note that the class is based on the Activity class that performs actions. An application may have many separate activities, but the user interacts with one at a time. The oncreate method is called by the Android system when your activity starts. This is where you should perform all initialisation and user interface setup. Note: The AppCompat support library enables the use of the ActionBar and implementations such as Toolbar for older devices down to Android v2.1. Currently, new projects created through Android Studio incorporate this library by default. At this stage we do not need to be too concerned with this change. For simplicity the notes here will use Activity, but you should leave it as AppCompatActivity in your code. https://developer.android.com/reference/android/support/v7/app/appcompatactivity.html may be useful. Construct the User Interface Modify your code by making the changes shown below in colour (again you will need to adapt this to suit the names you have used): package uk.ac.stir.cs.android; import android.app.activity; import android.os.bundle; import android.widget.textview; public class HelloActivity extends Activity { /** Called when the activity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // setcontentview(r.layout.main); TextView textview = new TextView(this); textview.settext("hello Android"); setcontentview(textview); } } An easy way to import missing packages into your project is to use Alt+Enter. This identifies missing imports based on your code and adds them for you. An Android user interface uses a hierarchy of graphical objects called Views. A view is a drawable element in your UI layout such as a button, image or (in this case) a text field. Each of these is a subclass of the View class. The subclass that handles text is TextView. A TextView is created with an Android Context instance as its parameter. A Context is a handle on the system that can resolve resources, can access databases and preferences, etc. Activity inherits from Context so it is also an instance of this class. The text content is defined with settext. Finally, the TextView is provided to setcontentview to display it. If an activity does not call this method (directly or indirectly) then no UI is shown and the system displays a blank screen. Create an Android Virtual Device To try out your app you need to run it on a phone. Connecting a real phone is very time consuming and older (and very new) phones can prove problematic, so we will use a software emulator. This imitates a phone and is very flexible as a wide variety of devices, screens, and Android versions can be accommodated. Before running your app, it is a good idea to start up the emulator and keep in running. Treat the emulator as a phone and start it once in advance and leave it running, as (like a real phone) it will take some time for the phone to start so please be patient. The emulator can take several minutes. The first time through you will need to set-up out some configuration parameters before you create the AVD (Android Virtual Device). This is in effect the Android Laboratory 1 3

emulator configuration you will run. This process defines the system image and device settings used by the emulator. To use an AVD from within Android studio: Choose Tools > Android > AVD Manager : This allows you to create a virtual device, choose Nexus 6 with API 24: The first launch of a particular device configuration can be very slow. Subsequent ones are relatively faster. You should see the following sequence, although the Android is starting message tends not to occur the first time a device is launched. When the emulator has finished booting, you will see the Android home screen: Android Laboratory 1 4

In the future, do make sure you have an emulator running before you try to run an Android project. Run the Application Android Studio automatically creates a new run configuration for your project and then launches the Android Emulator if you have not already started one. However as mentioned earlier we recommend you treat the emulator as a phone and start it once in advance and leave it running, as it will take several minutes for the phone to start. When the emulator is booted, you will see the Android home screen. Android Studio then installs your application and launches the starting activity. You should then see your Hello application running. (The bottom left hand tab (run) helps show progress but there will be long pauses!) The Hello you see in the grey bar is actually the application title. Android Studio creates this automatically (the app_name string is defined in res\values\strings.xml and referenced by AndroidManifest.xml). The text below the title is the actual text created in the TextView object. Modifying the User Interface Modify your code to go back to what it originally said: setcontentview(r.layout.main); // TextView textview = new TextView(this); // textview.settext("hello Android"); // setcontentview(textview); Within Android Studio open strings.xml under res\values. Your will see that the display string and app_name have been automatically generated. Click on these and alter their values, e.g. add a!. Now open main.xml (you may have a different name) under res\layout and choose the Text tab from the bottom of the panel to see the XML that has been created (see figure below). Although you could edit the XML directly, it can be easier to do this using the design interface provided by Android Studio. Switch to the graphical view by choosing the Design tab at the bottom of the edit panel to see the changes here too. Now save (shortcut Ctrl-S). Run the application again to see the result. Stay in main.xml (you may have a different name) under res\layout in the Design tab. This allows you to edit your user interface visually. You can edit existing elements, or can add new ones from the palette on the left. Double click on the text at the top of the application window (on the following figure it says Hello Android!!!) or select TextView in the Component tree pane. The TextView properties are shown on a pane on the right. Click on the triangle to the left of the TextAppearance label to expose textsize and textcolor properties. Find textsize and change it to 48sp, where sp is scale independent pixels. Once you type in 48sp you will see that the graphical preview change. Choose the Text tab from the bottom of the panel to see the XML that has been created. Switch back to the graphical view by choosing the Design tab. Again save and run the application to check how it looks. Android Laboratory 1 5

Staying in main.xml under Design, locate the property textcolor and select the to its right to generate a new dialogue box. On this select Color on the left hand pane and choose a colour from the palette. Your text should now appear in that colour. Now switch to the Text tab and note the value of textcolor. Again save and run the application to check how it looks. Design and Text tabs You have now reached a checkpoint that you should show to a lab demonstrator. Also be prepared to answer the following questions: Is it preferable to use main.xml for the layout or to set the layout using code instructions? Your program has Hello Android in English. How could it be adapted to display this in other languages? If you have time, why not try more significant changes. Can you add another line of text near the bottom for example? What else, other than text can you add? Android Laboratory 1 6