1 / 14

More Java: Encapsulation, Getters, Setters, Anonymous Class

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

afra
Download Presentation

More Java: Encapsulation, Getters, Setters, Anonymous Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Java: Encapsulation, Getters, Setters, Anonymous Class CS300

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. CS300

  8. References • Programming Android by ZigurdMednieks, Laird Dornin, G. Blake Meike, Masumi Nakamura CS300

  9. Install ADT and AVD • http://developer.android.com/sdk/index.html#download • If you already have eclipse use an Existing IDE option CS300

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

More Related