Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
1
More Java: Encapsulation, Getters, Setters, Anonymous
Class
CS300
Java Packages
2
Provide a mechanism for grouping related types
together in a universally unique namespace.
java.lang
Java.util
How to name a package?
Domain
Ex.
Plus
domain android.com will be package com.android
name of the program
Ex.
CS300
name reversed
com.android.notepad
Access Modifiers
3
Keywords that modify the visibility of the
declarations to which they are applied
Private:
most restrictive. Not visible outside the block
that contains it.
Default or package access: next most restrictive. Visible
only from other classes in package.
Protected: permits all default access rights plus access
from within any subtype.
Public: allows access from anywhere.
CS300
Encapsulation
4
The idea that an object should never reveal details
about itself that it does not intend to support.
Getters and Setters:
Common
CS300
example of encapsulation in Java
Getters and Setters example
5
Create a class Contact that extends Comparable<Contact>:
Fields:
name, age, email
Create constructor with arguments: name, age, email
Create getters and setters for all three fields
Create method compareTo to sort contacts by e-mail
Test
CS300
Anonymous Class
6
Callback: your code needs to be notified when
something in the UI changes.
Ex.
a button is pushed and we need to change state,
new data has arrived from the network and it needs to
be displayed
Java provides idiom to pass blocks in code
Anonymous classes are a handy tool for expressing
many kinds of code blocks.
CS300
Without anonymous class
With anonymous class
public class myDataModel{
//Callback class
private class keyHandler implements
View.onKeyListener {
public boolean onKey(View v, int
keyCode, KeyEvent event) {
handleKey(v, keyCode,
event);
}
}
/* @param view in the view we model */
public myDataModel(View view){
view.setOnKeyListener(new KeyHandler())
}
/** Handle a key event **/
void handleKey(View v, int keyCode, KeyEvent
event){
// key handling code goes here …
}
}
public class myDataModel{
7
/* @param view in the view we model */
public myDataModel(View view) {
view.setOnKeyListener(
// this is an anonymous class!!
new View.OnKeyListener() {
public boolean onKey(View v, int
keyCode, KeyEvent event) {
handleKey(v, keyCode,
event);
}
} );
/** Handle a key event **/
void handleKey(View v, int keyCode, KeyEvent
event){
// key handling code goes here …
}
CS300
}
References
8
Programming Android by Zigurd Mednieks, Laird
Dornin, G. Blake Meike, Masumi Nakamura
CS300
Install ADT and AVD
9
http://developer.android.com/sdk/index.html#dow
nload
If
CS300
you already have eclipse use an Existing IDE option
Install notes
10
Don’t forget to configure the ADT (Eclipse, Window,
Preferences, Android)
Do not forget to set an AVD target (run
configuration)
Do not forget to assign SD memory
Check devices: use the proper one
CS300
Hello Android!!
11
New android project
Delete activity_main.xml
Right click layout -> create new xml. Choose
relative layout
Configure resolution: samsung nexus 4 inch screen,
480-by-800
Create AVD
CS300
Hello Android!!
12
Drawables:
Hdpi:
high density
Mdpi: medium density
Ldpi: low density
Xhdpi: extra high
Change ID property for relative layout
+:
create new variable with name
Change BG property
RGB
CS300
color map
Hello Android!!
13
Add a TextView
Configure
text property: create a new string resource
(good practice)
R.String:
name
String: content
Configure
Dp:
text size:
density independent pixel
Configure
layout margin top
Text color: #00F
Text Style: bold
CS300
Hello Android!!
14
Add image view
Create
new drawable: be careful with names! No
hyphens allowed
Change:
Id
Layout
below: put it under your textview
Layout center horizontal
Src: do not forget to add your image in a drawable
directory
CS300