• No results found

Developing an Android App. CSC207 Fall 2014

N/A
N/A
Protected

Academic year: 2021

Share "Developing an Android App. CSC207 Fall 2014"

Copied!
22
0
0

Loading.... (view fulltext now)

Full text

(1)

Developing an Android App

(2)

Overview

Android is a mobile operating system first released in 2008.

Currently developed by Google and the Open Handset Alliance.

The OHA is a consortium of 84 firms that develop

standards for mobile devices.

It includes Dell, Intel, NVidia, Samsung, Sony, and

others.

Members of the consortium are not permitted to run

non-standard versions of Android.

(3)

Android OS Versions

Image source: Wikipedia

We will use Android 4.3 (Jellybean) on CDF

(demo today is on 4.4)

(4)

Android Software Development

For your project, you’ll develop a software application for the Android operating system.

You’ll implement your app using Java and the Android Software Development Kit (SDK).

The SDK includes tools such as a debugger and an emulator, plus libraries and documentation.

Eclipse is (still) the officially supported IDE. (Android Studio is still in beta.)

(5)

Emulator

We’ll run our apps using a mobile device emulator. It is a virtual mobile device

that runs on our computers, so no mobile device is

required for software development.

It has the software and

hardware features of a real device, but you can’t actually make calls!

(6)

Demo

We’ll write an application based on our running example. A Registry application that collects information about the

population and stores the data in a file. The remaining slides summarize the demo.

(7)

New Android Project

These are the

settings you must use for your

(8)

The main

Activity

An app is made up of multiple Activity’s. In this example, an Activity corresponds to a single screen with a user interface.

The first Activity is

typically called the

(9)

WYSIWYG view of main

Activity

This is a

(10)

XML View of main

Activity

This displays the text “Hello world!”

@string/hello_world is set to “Hello world!” in file

(11)

strings.xml

The string used by the TextView.

(12)

Changing the text displayed by

TextView

We’ll add a new string to string.xml:

<string name="header">Register a person</string>

We’ll change the variable used by the TextView in activity_main.xml:

<TextView

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/header" />

(13)

Changing the layout

Two types of layouts are:

- LinearLayout: components are arranged horizontally or

vertically.

- RelativeLayout: components are arranged relative to something

else (e.g., at the left edge of a component). We’ll edit activity_main.xml to use a LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical"

(14)

Adding two

EditText

fields

In activity_main.xml, below the TextView: <LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<EditText android:id="@+id/first_name_field"

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:hint="@string/first_name" />

<EditText android:id="@+id/last_name_field"

android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="@string/last_name" /> </LinearLayout> In strings.xml:

<string name="first_name">First name</string>

<string name="last_name">Last name</string>

These are

EditTexts.

(15)

Adding a

Button

In activity_main.xml, below the 2nd EditText:

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/submit_text"

android:onClick="registerPerson" />

In strings.xml, add a new string:

<string name="submit_text">Submit</string>

But nothing happens when we click the button! We need to implement method registerPerson.

(16)

MainActivity.java

(17)

Implementing

registerPerson

When the button is clicked, we specified that the method

registerPerson will be called:

// Note: an onClick method must return void and have one View parameter. public void registerPerson(View view) {

// Specifies the next Activity to move to: DisplayActivity.

Intent intent = new Intent(this, DisplayActivity.class);

// Gets the first name from the 1st EditText field.

EditText firstNameText = (EditText) findViewById(R.id.first_name_field);

String firstName = firstNameText.getText().toString();

// Gets the last name from the 2nd EditText field.

EditText lastNameText = (EditText) findViewById(R.id.last_name_field);

String lastName = lastNameText.getText().toString();

String[] name = {firstName, lastName};

Person person = new Person(name, 11, 21, 2010); // To do: add EditTexts for dob.

// Passes the Person object to DisplayActivity.

intent.putExtra("personKey", person);

startActivity(intent); // Starts DisplayActivity.

(18)

Adding our

Person

class

File -> New -> Class

Add our Person class to the package.

We’ll modify the Person class so that it implements Serializable

and has a UID:

public class Person implements Serializable {

// This UID was generated by Eclipse.

private static final long serialVersionUID = -9021932579037461512L;

...

}

To pass complex objects from one Activity to another, the data

must be serialized. Serializing data translates it into a

(19)

Adding Another

Activity

File -> New -> Other -> Android -> Android Activity

(20)

Edit

DisplayActivity.java

The onCreate method is called when the Activity is created. Get

the Person object passed from MainActivity and display the

name of the Person:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_display);

// Gets the Intent passed from MainActivity

Intent intent = getIntent();

// Uses key "PersonKey" to get Person object.

Person person = (Person) intent.getSerializableExtra("personKey"); String[] names = person.getName();

// Sets TextView to the Person's name.

TextView newlyRegistered = (TextView) findViewById(R.id.newly_registered);

newlyRegistered.setText(names[1] + ", " + names[0] + " has been registered."); }

Part of activity_display.xml:

<TextView android:id="@+id/newly_registered"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

(21)

Transition between activities

MainActivity DisplayActivity

Complex objects pass via the Intent from one Activity to another must be serialized. Multiple

objects can be passed by giving each a different key.

(22)

References

Related documents

At the March 19, 2013, Public Works &amp; Infrastructure Committee meeting the General Manager, Toronto Water, presented a report on: progress made in the construction of a

In both TBM and drill and blast tunnels it may be preferable to use a flat waterproof liner with a drainage layer between the rock or concrete surface and the waterproofing

parameters for newer anticoagulant medications; • Recent changes in guidelines for the management.. of high

The study population was constituted by 100 consecutive euthyroid obese patients (BMI &gt;30 kg/m 2 ), classified as severely obese (50 females/50 males; mean age, 40.4±12.7 yr;

HFMA KPI #1 Measure: Net Days in Accounts Receivable A/R Purpose: Trending indicator of overall A/R Performance Value: Indicates revenue cycle RC efficiency Metric Calculation:

make java documentation android app building game android development nexus 7 2013 android sdk ndk ubuntu android apps development tools how to make calculator app in android

development training courses create your own android app easy android sdk ubuntu command line free android app builder no ads game engineer salary range android developer ebook

Volume  StatusBar– NotificationLine Camera Numeric ID:5554 Hang up Home Call Power Search 19 19 Back Menu Tab Launch Pad Android Emulator Keyboard OSfunction