Sunday, March 25, 2018

Android Basics 101

Android Basics and Keywords

Android Activity: a layout template MainActivity, DetailActivity, SettingsActivity

How to write and track a todo task in Android Studio:

To note a todo task and have it show up in the TODO Android Tools later use this format: 
// TODO : Implement this method
Note that the task is specified with a capitalized "todo" in the comment section, and the name of the task follows immediately after.

Styling in style.xml and Choose a Theme

Specify styling, layout and themes in style.xml. 

<item name="colorPrimary">#hex_color</item>
<item name="colorButtonNormal">#hex_color</item>

Android Code Patterns


Tricks Android Studio can do

  • Search through all of Android Studio Cmd+Shift+A. 
  • Automatically reformat code with one click
  • Take a screenshot from device Android Monitor > Capture mode
  • Record screen for up to  3 minutes and output as a video of the hardware device Android Monitor > Screen Record
  • Presentation Mode: hide the toolbars
  • Android clip board tracks the copy-paste done in the past. 

Android Best Practice

  • Check for Updates frequently to get the latest Android Studio
  • Read Release Notes 
  • Mobile device has limited memories
  • Large images buildup can cause mobile applications to slow and even crash
  • Use LinearLayout Orientation Horizontal to position buttons or views in a row or in a column
  • Name conventions prefix state variables with small "m" as in "mContext".  "m" stands for member variable.
  • context is a special parameter that helps methods access state information in an instance of a class. Can be passed into TextView as a parameter. Helps access application resources and environment. 
90% of your time as an Android developer will be spent in Android studio.

Java Coding for Android

The convention is constants are written in all CAPS. 

Declare a Java Class

Declare a Java Method quickly (Oracle)

Java keywords. Reserved symbols that has existing meaning / purpose in Java.  programming

Basic Android Development Cheat sheet on Medium

Common Android Views Cheat Sheet by Udacity

OOP in Java Android

Use @overide to declare overriding of parent methods. 
Use extend to inherit parent methods. 

Java strings are immutable, means they cannot be edited despite that indexing is allowed. The entire variable has to be reassigned.  StringBuilder is mutable and is used for that purpose.

Pro Tip turn on auto import to automatically import modules.

Variable scope matters. Each function variable will get destroyed if the function returned.

Android studio has left, right (aka side panes) and bottom panes - lots of easy access menu.

Android Getter and Setter Methods

Getter methods are traditionally prefixed with get. E.g. getText
Setter methods are traditional prefixed with set. E.g. setText()

Android Casting and Inheritance

casting inheritance

Android Log

Example: Log.i("","Your Message Here");

Commonly used Android tools

Android Monitor - Android debugging system, output of log, breaks and code step through
Gradle - Gradle tasks that help project build and run correctly. Gradle is the system that is responsible ultimately for building and running Android projects.
Project -
Terminal - provides command line interface
TODO - track todo list in the source file.
Capture - tracks bugs found in connected devices
Event log - display messages related to events and activities like a notification system
Favorites - a favorite list for files
Gradle Console - display information about the Gradle system, display errors, warnings or success message
Message - Display outputs from the Gradle Build System. Display if any problem compiling the application
Structure - Shows high level view of the source file, classes, variables and methods. It's "hyperlinked" : can click and jump to source file.

A temporary message such as a notification is called a Toast in Android.

Android can use intent to activate functionality of another app: for example in web browser, you click on share, wanting to send an article using the Gmail app instead.

Common intents in Android
public void capturePhoto(String targetFilename) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            Uri.withAppendedPath(mLocationForPhotos, targetFilename));
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
See the code above. if intent.resolveActivity(getPackageManager()) == null is the exception handling.

Android Intent Exception Handling

Run an intent only if it is found in the system and can be resolved.

Android Intent is useful when you want to launch existing Android functionalities such as Camera, Send an Email, and more. 

Special formatted data can be written in (Uniform Resource Identifier) URI - structure data in a specific way so that it can be processed by the receiving end. For example tel:415-xxx-xxxx where x is some number between 0-9 is considered a phone number on iPhones. When user tap the URI, a phone call will be made. Similarly on android, tapping an URI linked email address will open up the Gmail app.

Commonly Used Android Methods and Attributes

findViewById returns a View
TextView.setText: change text of a TextView
ImageView.setImageResource: change image url source
onClick attribute: an XML field and an event listener on the android view

Common Android Errors

Android incompatible type error: usually fix by casting e.g. trying to store an int returning function result into a string variable

Android Developer Tools

Android SDK Search Chrome Extension: use this chrome extension to view Android source code

You can also click on the view source link by each class name to access the source code in git.

Udacity simplified TextView and ImageView class (teaching aid)

Read more here

No comments:

Post a Comment

React UI, UI UX, Reactstrap React Bootstrap

React UI MATERIAL  Install yarn add @material-ui/icons Reactstrap FORMS. Controlled Forms. Uncontrolled Forms.  Columns, grid