ANDROID – A FIRST PROGRAM L. Grewe Using AndroidStudio –basic Android  Lets do a “Hello World Project”  Start up AndroidStudio (assume you have installed.

Slides:



Advertisements
Similar presentations
CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Advertisements

Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Filip Debelić What is it? Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google Android,
Who Am I And Why Am I Here I’m professor Stephen Fickas in CIS – you can call me Steve. I have a research group that works with mobile devices (since 1995!)
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Basic, Basic, Basic Android. What are Packages? Page 346 in text Package statement goes before any import statements Indicates that the class declared.
Android GUI Project John Hurley CS 454. Android 1. Android Basics 2. Android Development 3. Android UI 4. Hello, World 5. My Project.
App Development for Android Prabhaker Mateti. Development Tools (Android) Java – Java is the same. But, not all libs are included. – Unused: Swing, AWT,
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
Layout and Control in UI The user interface (UI) is the graphical interface user can see and interact with your app comprising UI controls like textbox,
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
INTERNATIONAL SUMMER ACADEMIC COURSE UNIVESITY OF NIS ISAC – Android programming.
Android Programming. Outline Preparation Create new project Build and Run a project Debug a project Deploy on devices.
Creating Android user interfaces using layouts 1Android user interfaces using layouts.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
1 Mobile Software Development Framework: Android Activity, View/ViewGroup, External Resources, Listener 10/9/2012 Y. Richard Yang.
Better reference the original webpage :
Mobile Programming Lecture 1 Getting Started. Today's Agenda About the Eclipse IDE Hello, World! Project Android Project Structure Intro to Activities,
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
Introducing the Sudoku Example
Chapter 3 Navigating a Project Goals & Objectives 1.Get familiar with the navigation of the project. How is everything structured? What settings can you.
CS5103 Software Engineering Lecture 08 Android Development II.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Mobile Computing Monetizing An App Copyright 2014 by Janson Industries.
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
Chapter 2: Simplify! The Android User Interface
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Chapter 5 Creating User Interfaces GOALS and OBJECTIVES Begin our application by creating our user interface. More than one way to create a user interface.
ANDROID – INTERFACE AND LAYOUT L. Grewe. Interfaces: Two Alternatives Code or XML  You have two ways you can create the interface(s) of your Application.
DUE Hello World on the Android Platform.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
CE Applied Communications Technology Android lecture 2 - Structures Android File structure Resources Drawables Layout Values R Class Manifest Running.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
Android Boot Camp for Developers Using Java, 3E
User Interfaces: Part 1 (View Groups and Layouts).
Configuring Android Development Environment Nilesh Singh.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
BUILDING A SIMPLE USER INTERFACE. In this lesson, you create a layout in XML that includes a text field and a button. In the next lesson, your app responds.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
INTRODUCTION TO ANDROID. Slide 2 Introduction I take a top-down approach to describing an application’s anatomy.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Android Programming.
Chapter 2: Simplify! The Android User Interface
Lab7 – Appendix.
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
Android Application Development 1 6 May 2018
Android N Amanquah.
Android – Event Handling
Android Introduction Hello World.
MAD.
ITEC535 – Mobile Programming
תכנות ב android אליהו חלסצ'י.
Anatomy of an Android Application
Android GUI Project John Hurley CS 454.
CIS 470 Mobile App Development
Android GUI Project John Hurley CS 454.
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Presentation transcript:

ANDROID – A FIRST PROGRAM L. Grewe

Using AndroidStudio –basic Android  Lets do a “Hello World Project”  Start up AndroidStudio (assume you have installed with Android support)

Hello World Project – Android Studio 1. App name, package 2. Target 3. Activity type 4. Name of Activity 5. Now finish to setup

Hello World Project---basic folders &files Folders Number of folders and files are auto- generated when you create a project File of note AndroidManifest.xml – like a ship’s manifest it tells us the components of our app

Project basics…. Android Studio  manifests/AndroidManifest.xml: an XML file describing the application being built and what components – activities, services, etc. – are being supplied by that application  java/: holds the Java source code for the application  res/: contains  layout files  drawable =icons  menus  values = static files like strings.xml  Gradle/: an Ant script for compiling the application and installing it on the device (integrated with IDE— don’t see it there)  default.properties: a property file used by the compiler  res/ – contains multiple xml files and other resources used by application

Project basics…. Android Studio  Gradle Scripts/: an gradle script for compiling the application and installing it on the device (integrated with IDE—don’t see it there)  gradle.properties: a property file used by the compiler

Manifest file – AndroidManifest.xml  an XML file describing the application being built and what components – activities, services, etc. – are being supplied by that application  Initially created by IDE  must declare all activities, services, broadcast receivers and content provider of the application.  must contain the required permissions for the application.  For example if the application requires network access it must be specified here.

Hello World Project – AndroidManifest.xml Manifest file Here have intent created for This acitivity that is associated with launch of application Define Application –see res/strings.xml –see res/drawable-resolution/icon.png

Interface specifications The Layout

Interface ---2 options  Do it with XML file(s) Many modern frameworks whether for mobile programming like Android or iOS or for other platforms have gone to specifying GUI (graphical user interface) elements in static XML files rather than programming source code (like java). The reason –it allows separation of the look (view) from how the code works (model and controller). Have you ever heard of Model View Controller –it is a famous software engineering framework that programmers try to achieve in their software systems.  Do it with Java code. This has similarities if you have created desktop GUI Java applications

OPTION 1: The Layout with XML  We are going to discuss a specific resource in the res folder res/layout/activity_main.xml

The Layout-the interface  res/layout/main.xml = contains layout for interface <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" /> The above will create an interface in vertical (versus portrait) mode that fills the parent Both in width and write and wraps and content as necessary.

The Layout-the interface  res/layout/main.xml <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >  android:orientation = vertical (versus portrait) mode.  that fills the parent both in width and write and wraps and content as necessary. Landscape Vertical

Hello World Project Layout file This adds a “TextView” to the interface and this has the text referenced in it. Creating a Layout file—2 options 1)Do it manually –type it in 2)For Layouts corresponding to GUIs You have a drag and drop option we will learn about in a later lecture

Using IDE to Visually Create XML file  Visual creation of XML file  AndroidStudio: New-> Layout resource file  Select layout (root)  &qualifiers as needed  drag & drop

Visually Creating XML interface  I dragged and dropped an EditText view and a Button. Below I show you the corresponding code. res/layout/main2.xml <AbsoluteLayout xmlns:android=" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">

It is an Activity Activity is a class in the Android sdk (android.app.Activity) It represents us now the main application itself—we will learn more about what this means in a near future lecture –remember we are at the beginning! Lets look at the “Main” class

Create an Activity with simple text Interface ……first  An Android user interface is composed of hierarchies of objects called Views.  A View is a drawable object used as an element in your UI layout, such as a button, image, orView  In the next code will create a text label. Each of these objects is a subclass of the View class and the subclass that handles text is TextView.TextView

Hello World Project – here we are creating interface with programatically/code Helloworld.java  package com.teach.helloworld; import android.app.Activity; import android.os.Bundle; public class helloworld extends Activity { /** Called when the activity is first created. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android.....Lynne"); setContentView(tv); } } create a TextView with the class constructor, which accepts an Android Context instance asContext its parameter. Context is a handle to the system provides services like resolving resources, obtaining access to databases and preferences, etc. The Activity class inherits from Context, and because your HelloAndroid class is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView. text content is set with setText(CharSequence)setText(CharSequence) pass the TextView to setContentView() in order to displaysetContentView() it as the content for the Activity UI.

What we just learned  You will have a class created for you that is your android application, i.e. “ helloworld” and it extends Activity  Your application is an Activity and that also descends from Content  Context = class that gives us access to System provides services like resolving resources, obtaining access to databases and preferences, etc  View = is a GUI element, there are different kinds like the TextView we are using.constructor needs Context instance (we used our App)

Automatically created by IDE when created project R.java

Hello World Project R.Java == generated by project, this points to various resources in your project see res folder. DO NOT EDIT THIS –unless you know what you are doing. /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.example.helloworldandroid; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int button1=0x7f050001; public static final int editText1=0x7f050000; } public static final class layout { public static final int main=0x7f030000; public static final int main2=0x7f030001; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; }

It is used it in your code to point to resources. Lets look at an example where we alter our onCreate method to point to the interface described in the res/layout/main.xml file….so now interface specified with XML and not programmatically with code. Why have R.java? TIP: sometimes when you are compiling your code you can have problems with the R.java file (which you DON’T edit) –what is going on? Somehow the R.java is out of sync with the project resources…..WHAT TO DO? do a “Build->Clean Project” and it will regenerate R.java for you

Eclipse: gens/R.java Android Studio: build/generated/source/r/debug/your- package -name/R.java (Example: C:\Users\Lynne\AndroidStudioProjects\HelloWorldAndroid\app\build\g enerated\source\r\debug\com\example\lynne\helloworldandroid) Where is R.java Android studio: you can not see the R.java in IDE Go to file system

Alter your Activity to Use an XML layout file  package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Now going to change the code so it uses instead an XML file for its intervace This line of code says use the xml layout file called main.xml that is pointed to inside your R.java file in memory

Interfaces ---XML-based layout  Previous HelloWorld example has interface created in java code.  ALTERNATIVE: XML-based layout files. Example that duplicates previous.  Above located in res/layout/main.xml Here is our main.xml file that will be used and do the same thing as our previous java code –show a TextView

XML interface   xmlns:android XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute.  android:layout_width This attribute defines how much of the available width on the screen this View should consume. As it's the only View so you want it to take up the entire screen, which is what a value of "fill_parent" means. android:layout_height This is just like android:layout_width, except that it refers to available screen height.  android:text This sets the text that the TextView should display. In this example, you use a string resource instead of a hard-coded string value. The hello string is defined in the res/values/strings.xml file. Lets understand the XML Tag used to describe our TextView Wow! That’s a lot to remember ---aren’t you glad we have an API online to look it up ---better yet is the drag and drop option to make interfaces!!!

The res directory More on resources

We mentioned res folder Android Studio

res/ directory  res/layout/ : contains one or more xml files that define interfaces  res/values/strings.xml : file where you specify strings (constants) that you can use in your program  res/drawable-* : gives file use for different resolutions like icon.jpg. *=hdpi (high), mdpi(medium), ldpi (low). Depends on device what mode it can/may be running in.  res/menu/ : contains one or more xml files for menus

So we have an interface what can we do with it. Event Handling = code that responds when “events” happen. For GUI elements we usually have events associated with them. For example, a button has the event of hitting the button. Event Handling

Widget : Views that have events  For a list of the widgets provided by Android, see the android.widget package.android.widget  Some Examples  Button  CheckBox  DatePicker  EditText  ImageView  SearchView  Spinner There are more ---here are a few.

Event Handling  Decide what Widgets who’s events to process  Define an event listener and register it with the View.  View.OnClickListener (for handling "clicks" on a View), View.OnTouchListener (for handling touch screen events in a View), and View.OnKeyListener (for handling device key presses within a View) View.OnClickListener View.OnTouchListenerView.OnKeyListener  events.html details more events.html 3 steps: 1)Decide what events to respond to 2)Create a listener to respond to each event 3)Register the listener to the corresponding widget

Lets add a Button to a program- based interface  Step 1: Add button  Step 2: Register Event Handler TWO OPTIONS – separate class to handle event(s), OR have the Activity containing the button do the event handling  Step 3: Implement Event Handler…for a Button means implementing the View.OnClickListener interface

Event handling done by Activity itself –one option  Here code to handle is inside Activity itself  public class ExampleActivity extends Activity implements OnClickListener { protected void onCreate(Bundle savedValues) {... Button button = (Button)findViewById(R.id.corky); button.setOnClickListener(this); } // Implement the OnClickListener callback public void onClick(View v) { // do something when the button is clicked }... } This option is okay only if the event handling code is simple and you will not reuse it ever ---if the code is longer or will reuse make a separate class

Event Handling - here have a SEPARATE class EVENT HANDLING CODE in separate object mCorkyListner // Create an anonymous implementation of OnClickListener private OnClickListener mCorkyListener = new OnClickListener() { public void onClick(View v) { // do something when the button is clicked } }; //Now inside your Activity class protected void onCreate(Bundle savedValues) {... // STEP 1: Capture our button from layout Button button = (Button)findViewById(R.id.corky); // STEP 2: Register the onClick listener with the implementation above button.setOnClickListener(mCorkyListener);... } Better for readability and reuse

Depends on IDE Run your code

Running code  Will Run in the Emulator  Android Studio: Run->”Run app” or “Debug App” You should learn to run both on the emulator AND on a physical device. TO use features of a phone like GPS, etc. it is often required to run on a phone

Running code  TIP: Emulator can take a long time to load at first--- -be patient and keep it up---just re-run after changes and won’t have to relaunch emulator, will just load up new app.  Look if you have Intell Virtualization speed up, check out GPU and snapshot options –search online for current info on these tips