Click here to load reader
Upload
ghanagtug
View
3.882
Download
0
Embed Size (px)
DESCRIPTION
Presentation given by Dr Nathan Amanquah at Ashesi during GhanaGTUG Android warm-up event
Citation preview
AndroidAndroidN AmanquahN Amanquah
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
Steps Design interface
• Code it• Use xml
Create objects in code Provide implementation /events in code
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
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
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
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
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>
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() {...
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
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(); } }}); });