Ad

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. 

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

Android Code Patterns

findViewById(resource_id)

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 
    • https://developer.android.com/studio/releases/
    • https://en.wikipedia.org/wiki/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. 
  • https://developer.android.com/distribute/best-practices/
  • https://developers.google.com/training/android/
  • https://developers.google.com/training/courses/android-fundamentals
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

https://docs.oracle.com/javase/tutorial/java/concepts/class.html

Declare a Java Method quickly https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html (Oracle)

Java keywords. Reserved symbols that has existing meaning / purpose in Java.  programming https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

Basic Android Development Cheat sheet on Medium
https://medium.com/@ryanstewartalex/basic-android-development-cheat-sheet-58d501dabb3f

Common Android Views Cheat Sheet by Udacity
http://labs.udacity.com/images/Common-Android-Views-Cheat-Sheet.pdf

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
https://plus.google.com/+KatherineKuanPlus/posts/GyrcP6fEo2v
https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
https://stackoverflow.com/questions/44902651/no-need-to-cast-the-result-of-findviewbyid

Android Log

Example: Log.i("name_of_class_where_error_is_from.java","Your Message Here");
https://developer.android.com/studio/command-line/logcat


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

https://developer.android.com/guide/components/intents-common
https://developer.android.com/guide/components/intents-filters#Resolution
public void capturePhoto(String targetFilename) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            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
https://plus.google.com/+AndroidDevelopers/posts/1EyxkFnPjmD

You can also click on the view source link by each class name to access the source code in git.
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/TextView.java
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ImageView.java

Udacity simplified TextView and ImageView class (teaching aid)
https://gist.github.com/udacityandroid/bd550cc8fd37190d85a6
https://gist.github.com/udacityandroid/47592c621d32450d7dbc


Read more here http://www.techotopia.com/index.php/A_Tour_of_the_Android_Studio_User_Interface

No comments:

Post a Comment

Natural Language Processing NLP - Useful libraries, tools and code samples

Basic Concepts Stop words removal Stop words are words that may not carry valuable information In some cases stop words matter . For e...