950 likes | 1.08k Views
Welcome to Android!. Let’s Review What we know about the web. Structure is _________ Presentation is ___________ Behavior is ___________. HTML. CSS. Javascript. In Android. Structure is _________ Presentation is ___________ Behavior is ___________. XML. HTML. XML. CSS. Java.
E N D
Let’s Review What we know about the web • Structure is _________ • Presentation is ___________ • Behavior is ___________ HTML CSS Javascript
In Android • Structure is _________ • Presentation is ___________ • Behavior is ___________ XML HTML XML CSS Java Javascript
Web Review • HTML has specific elements for certain jobs • <p> for non editable text • <h1-h6> header • <li> for list items • HTML has specific elements to contain other elements • <div> • <span> • <ol>/<ul>
Android • Android has certain elements for certain jobs as well. • In android, we call these elements VIEWS • Android also has specific elements to contain other elements • In android, we call these elements View Groups
When the HTML document get loaded what object is created? <body> <divid="box1"class="box">1</div> <divid="box2"class="box">2</div> <divid="box3"class="box">3</div> <divid="box4"class="box">4</div> <body> body div#box1 div#box2 div#box3 div#box4
Android XML Layout <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
Android XML Layout <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> LinearLayout TextView Button In Android Lingo, the DOM is known as the View Hierarchy
Web Review Using the DOM, how do we find elements? • ID • Class • Tag • Attribute • Traversing the DOM tree (firstChild, parent, children, etc.)
In Android We find views by • ID • Traversing the View Hierarchy
What is Android? • A software stack for mobile devices that includes an operating system, middleware, and key applications. • Uses large parts of the Java API • Doesn’t use Swing or AWT • Uses XML to declare layouts • Supports SQLite • Awesome!
Android Toolkit • Eclipse is the premier IDE for developing Android. • Integrates the Android SDK for easy building, virtual device creation, and more. • Android apps can be built with Apache Ant; therefore, you could use any text editor of your preference.
Eclipse Perspectives • Eclipse “Perspectives” determine the visible actions and views within a window/tab. • For android there are 3 important “Perspectives”: • Java • Debugging • DDMS
Java Perspective • This is the window where you will do the majority of your coding.
Debugging Perspective • This is the default debugging tool built into Eclipse. Very similar to other debuggers (Netbeans, Visual Studio, etc). • Allows the ability to set break points in code and pause the application’s execution and step through code line by line. • Allows the ability to view a variable’s value(s) in real time, step into functions, etc.
DDMS Perspective • Usually the place for “advanced” features. • Provides info for: • Devices • Shows connected android devices and emulators • Emulator Control • Change network state, speed, latency • Spoof calls or SMS text messages • Set location of phone • Logcat • Outputs messages that you print out using the Log class along with other system messages such as stack traces when exceptions are thrown.
Logging Messages in Android • System.out.println does not work like in regular Java programs. • Instead make use of the Log class.
Logging in Android • Log.d(String tag, String msg) – Send a DEBUG log message. • Log.e(String tag, String msg) – Send an ERROR log message. • Log.i (String tag, String msg) – Send an INFO log message. • Log.v (String tag, String msg) - Send a VERBOSE log message. • Log.w(String tag, String msg) – Send a WARN log message.
Logging in Android • In the DDMS Perspective, inside the Logcat window, you can filter Log messages. • You can filter log messages to reduce “noise” and only see certain types of Log messages.
Filtering Log Messages • By TAG – the first parameter of Log method • By type: • DEBUG • ERROR • INFO • VERBOSE • WARN
Log Example This Log message could be filtered either by adding a filter for “INFO” Log messages or by adding a filter for the “HelloWorld” tag.
Log Documentation • See http://developer.android.com/reference/android/util/Log.html for more details.
What makes an Android App? Most Apps Consists of • Android manifest • Activities • Views • Event logic for user interaction • XML for defining layout, widgets, shapes, and colors • OpenGL ES 1.1 or 2.x GLSurfaceView or Skia Canvas (android.graphics) if creating your own widgets. • SQL Lite database • JSON or XML for data to and from servers
Main components of an Android App • Android Manifest • Activities • Views • Application Resources • Application Logic
Android Manifest • Presents essential information about the application to the Android system. The system must have this info before running any of the application’s code. 1
Android Manifest’s role • Names the java package for the application. • Declares permissions the application must have in order to operate. • Access to the internet • Access user’s contact data • Read/write to system settings. • Etc.
Android Manifest’s role • Declares the minimum level of the Android API the application requires to run. • Specify application icon and title • Describes the components of the application • Activities • Services • Broadcast receivers • Content provider
Android Manifest Documentation • http://developer.android.com/guide/topics/manifest/manifest-intro.html
What is an Android Activity • An activity is a single, focused thing that the user can do. • Activities in the system are managed as an activity stack (LIFO). • An application consists of at least 1 Activity, but can have many.
Activity • Android Apps don’t have the main() paradigm. • Unlike C, C++, Java, etc. • Instead, Android uses an Activity for the code execution starting point of an application. • Activities have an important life cycle which every Android developer should know!!! 2
Activity Lifecycle onCreate() • An activity is created when a user launches an application. • The Activity’s onCreate() is the very first method called. Think of it as main(). • Use this method for basic startup logic that should happen only once for the entire lifecycle.
onCreate()startup logic • Create User Interface components dynamically or from resources. • Initialize class scope variables.
Activity Lifecycle onStart() • Your application becomes visible
Activity Lifecycle onResume() • The activity is visible and running • Your activity will remain in this state until: • A new application starts (receive a phone call) • User navigates to a new activity (back key) • The device screen turns off • Use this method to resume anything that was stopped or paused.
Activity Lifecycle onPause() • Happens when your activity is partially hidden. (Pop-up Window) • As long as your activity is partially visible but not in focus, you’re paused. • Use this method to pause ongoing actions (video, music, animation, etc.), save state, and release system resources.
Activity Lifecycle onStop() • Happens when your activity is no longer visible. • Use this method to • release all system resources that aren’t needed while hidden • write information to a database
Activity Lifecycle onStop() • While your activity is stopped, your application is kept in memory. • If the OS runs out of memory while your application is stopped, it may be destroyed.
Activity Lifecycle onDestroy() • Called when the system destroys your activity. • By now you should have released all application resources in onPause() and onStop(), so you shouldn’t have to do much here. • This method is not guaranteed to be called, so you shouldn’t rely on it.
Activity Lifecycle • Once your activity is destroyed, all hope is not lost. • When your app is paused you have the opportunity to save temporary state into a blob. • When the system tries to resume your application, the blob is used to restore your application to the state is was in when it was paused.
Views • Views are “widgets” that appears on the screen. • Similar to HTML element • Each View has predetermined characteristics, use cases, and attributes. 3
Two Basic View Types View • Base class for widgets • Used to create interactive UI components. • Button • Textfield • Image ViewGroup • Base class for layouts. • Serve as containers that hold others Views or ViewGroups • Define layout properties. • Have unique characteristics for how they position children (grid, horizontally, vertically, list, etc)