140 likes | 372 Views
More Java: Encapsulation, Getters, Setters, Anonymous Class. Java Packages. Provide a mechanism for grouping related types together in a universally unique namespace. java.lang Java.util How to name a package? Domain name reversed Ex. domain android.com will be package com.android
E N D
More Java: Encapsulation, Getters, Setters, Anonymous Class CS300
Java Packages • Provide a mechanism for grouping related types together in a universally unique namespace. • java.lang • Java.util • How to name a package? • Domain name reversed • Ex. domain android.com will be package com.android • Plus name of the program • Ex. com.android.notepad CS300
Access Modifiers • 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 • The idea that an object should never reveal details about itself that it does not intend to support. • Getters and Setters: • Common example of encapsulation in Java CS300
Getters and Setters example • 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 • 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
References • Programming Android by ZigurdMednieks, Laird Dornin, G. Blake Meike, Masumi Nakamura CS300
Install ADT and AVD • http://developer.android.com/sdk/index.html#download • If you already have eclipse use an Existing IDE option CS300
Install notes • 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!! • 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!! • 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 color map CS300
Hello Android!! • Add a TextView • Configure text property: create a new string resource (good practice) • R.String: name • String: content • Configure text size: • Dp: density independent pixel • Configure layout margin top • Text color: #00F • Text Style: bold CS300
Hello Android!! • 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