11

Click here to load reader

21 android2 updated

Embed Size (px)

DESCRIPTION

Presentation given by Dr Nathan Amanquah at Ashesi during GhanaGTUG Android warm-up event

Citation preview

Page 1: 21 android2 updated

AndroidAndroidN AmanquahN Amanquah

Page 2: 21 android2 updated

Installation:Installation: Install jdkInstall jdk Install android sdk starter packInstall android sdk starter pack

• (this also installs sdk tools)(this also installs sdk tools) Install platform tools Install platform tools

• (eg unzip platform-tools_r03-windows, rename to (eg unzip platform-tools_r03-windows, rename to “platform tools”, directly under android-sdk/)“platform tools”, directly under android-sdk/)

Install sdkPlatform Install sdkPlatform • (eg unzip android-2.2_r02-windows into “platforms”)(eg unzip android-2.2_r02-windows into “platforms”)

Create an AVD using android sdk and avd mgrCreate an AVD using android sdk and avd mgr Netbeans: Install nbandriod into netbeansNetbeans: Install nbandriod into netbeans Eclipse: install ADTEclipse: install ADT add SDK's tools/ and platform-tools to your PATH add SDK's tools/ and platform-tools to your PATH http://developer.android.com/sdk/installing.htmlhttp://developer.android.com/sdk/installing.html

Page 3: 21 android2 updated

Steps Design interface

• Code it• Use xml

Create objects in code Provide implementation /events in code

Page 4: 21 android2 updated

Building Blocks There are four building blocks to an Android

application: Activity - composed of Views, responds to events Intent Receiver Service – long lived running code eg media plyr Content Provider eg store in SQLite db

AndroidManifest.xml. • declare the components of your application • capabilities and requirements of your

components

Page 5: 21 android2 updated

Hello world: programmaticallyHello world: programmaticallypackage com.example.helloandroid;package com.example.helloandroid;

import android.app.Activity;import android.app.Activity;import android.os.Bundle;import android.os.Bundle;import android.widget.TextView;import android.widget.TextView;

public class HelloAndroid extends Activity {public class HelloAndroid extends Activity {   /** Called when the activity is first created. */   /** Called when the activity is first created. */   @Override   @Override   public void onCreate(Bundle savedInstanceState) {   public void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       super.onCreate(savedInstanceState);              TextView tv = new TextView(this);TextView tv = new TextView(this);       tv.setText("Hello, Android");       tv.setText("Hello, Android");       setContentView(tv);       setContentView(tv);   }   }}}

Note: copying and pasting this code results in additional invisible characters Note: copying and pasting this code results in additional invisible characters Past in Notepad firstPast in Notepad first

Page 6: 21 android2 updated

Hello world: by XMLHello world: by XMLpackage com.example.helloandroid;

import android.app.Activity;import android.os.Bundle;public class HelloAndroid extends Activity {

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.main); }

}

Configure the xml file to indicate what is loaded in the activity

Page 7: 21 android2 updated

XML Layout- a hierarchy of views<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content“android:text="Hello World"/>

</LinearLayout>

•View class is base for all widgets•ViewGroup

Page 8: 21 android2 updated

View Hierarchy- view w button<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a Button" /></LinearLayout>

Page 9: 21 android2 updated

Comparison to Swing Activities i-(J)Frame in Swing. Views -(J)Components in Swing. TextViews -(J)Labels in Swing. EditTexts -(J)TextFields in Swing. Buttons -(J)Buttons in Swing.

Handling Events is similar:myView.setOnClickListener(new OnClickListener(){ ... // Swing myButton.addActionListener(new ActionListener() {...

Page 10: 21 android2 updated

XML layoutXML layout<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"<TextView xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/textview"  android:id="@+id/textview"  android:layout_width="fill_parent"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:layout_height="fill_parent"  android:text="@string/hello"/>   android:text="@string/hello"/>

•These refer to string.xml and also create a new id called textview which can be referred to in code. •A compiled object representation of the layout defined in /res/layout/main.xml is R.layout.main•Textview can be referred to subsequently as R.id.textview

Page 11: 21 android2 updated

A button (View) and eventA button (View) and event Views may have an integer id associated with them. –Views may have an integer id associated with them. –

assigned in xml fileassigned in xml file Eg: define btn and assign an id:Eg: define btn and assign an id: <Button<Button

     android:id="@+id/my_button"     android:id="@+id/my_button"     android:layout_width="wrap_content"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_height="wrap_content"     android:text="@string/my_button_text"/>     android:text="@string/my_button_text"/>

In the onCreate method of activity, find the button:In the onCreate method of activity, find the button:• Button myButton = (Button) findViewById(R.id.my_button);Button myButton = (Button) findViewById(R.id.my_button);

EventEvent button.setOnClickListener(new OnClickListener() {button.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {    public void onClick(View v) {        // Perform action on clicks        // Perform action on clicks        Toast.makeText(HelloFormStuff.this, “Msg displayed in msgbox", Toast.LENGTH_SHORT).show();        Toast.makeText(HelloFormStuff.this, “Msg displayed in msgbox", Toast.LENGTH_SHORT).show();    }    }}); });