Intro to Android Development
with
by
Parthiban Loganathan


Am I ready to become one with the Android?
(aka prereqs)
- You know Java (1004, 1007 is fine)
- You own an Android device
- You want to build cool apps!
What are we going to do today?
- Learn the basic components of a simple Android app
- Go over some app code and see the results
- Learn about amazing libraries you should use to build apps
- Learn how to learn - I'll point you to the best resources for Android
But first...
Let's understand the pros and cons of the Android OS because other mobile platforms do exist... sort of.

Android is fragmented



Different screen sizes
Different OS versions
... leading to frustrated Android developers

Then why Android?
-
52% of US and 85% of the global mobile market + it's growing
-
Growing ecosystem - Android Wear, Google Glass

No review process from an evil overlord (*cough* Apple *cough*)

Build awesome apps and help the world be a better place!


Dev Environment
You will need:
- Android Studio with SDK+AVD Manager (https://developer.android.com/sdk/installing/studio.html)
- Android SDK (https://developer.android.com/sdk/installing/index.html?pkg=tools)
- USB drivers for your device if applicable
Setup can sometimes be complicated and often machine specific.
Come to Cookies & Code for help.
Main components of an app
-
Activities (stuff you can see)
-
Services (happen in background)
-
Broadcast receivers (react to system broadcasted messages)
-
Content providers (accessing data)
We will only use Activities today
We will look at
- Building a simple UI in XML
- Reacting to interactions UI in our Activity
- How to store static resources as XML
- How to start activities and use external features of the Android OS
- A very brief overview of using 3rd party libraries, APIs and networking
Architecture Choices?
Doesn't matter at this stage.
Pick your favorite:
- MVC
- MVVM
- VIPER
- JKALDGSDB *
* result of banging head on keyboard
How do I make UIs?
(aka shiny stuff)
- Layouts (View Groups): Organize UI elements in XML
- You can add Views and widgets to these Layouts to organize UI
- Reminiscent of HTML in web development
- Android Studio has a GUI builder (but it stinks)

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10px"
android:paddingLeft="10px"
<Button
android:id="@+id/sample_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:background="#50AD21"
android:text="Click me!" />
</LinearLayout>
A LinearLayout with a Button

Activities
- Loosely translates to a single thing you can do. For our purposes, it's a screen.
- Many methods like onCreate(Bundle), onPause()
- Can load your UI layout
- Controllers to react to user interactions
- and much more that we're not going to cover here

"Controllers"
- React to button presses
- Get text from text boxes
- Perform API requests
Plain old Java + Android specific methods via interfaces to start new activities, access SharedPreferences, etc.
// Get an object that represents the button view declared in the XML
mButton = (Button) findViewById(R.id.sample_button);
// Set an onClickListener to react to it
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do stuff here on click
}
});
Context

- An instance of context lets you access information about the application environment - global parameters, resources like text, images.
- Need it to write to disk, get GPS data, access volume, handle intents and more
- WARNING: Mishandling context is the biggest source of memory leaks!
Files we will touch
- app/src/main/java/com/adi/awesomeapp/MainActivity.java
- app/src/main/res/layout/activity_main.xml
- app/src/main/res/values/strings.xml
- app/src/main/AndroidManifest.xml
* default project structure varies with different versions of Android Studio (it's still in beta)
Let's look at some real code

Step 1
- We added a blank MainActivity.java which just loads the UI activity_main.xml
- activity_main.xml contains a simple RelativeLayout with a TextView saying "Hello world!" using the resource from strings.xml
Step 2
- We modified activity_main.xml to use a ScrollView with an ImageView that loads a picture of a puppy.
- Changed the message in the TextView by pointing to a different string resource.
Step 3
- We modified activity_main.xml to also contain a Button
- In MainActivity.java, we loaded objects referring to the UI components mImage and mButton. We added an onClickListener to the button which creates a camera intent and starts a new activity.
- The result of the intent is parsed and if the camera succeeded in taking a picture, we read it from disk and set it to the image view.
- We added <uses-feature> to the manifest so that only devices with cameras can download our app on Google Play.
Step 4
I went crazy and added 2 libraries and a bunch of other stuff to let us tweet pictures.
But seriously, the concepts to know are:
- Using threads for network requests to not block the main UI thread
- How to use 3rd party libraries with the gradle build tool.
- How to use features that need permissions (like internet, GPS)
Pro Tips
Remember to handle edge cases - there are a ton of them. Eg - storing state on screen rotation, exiting and resuming app, starting duplicate services by accident, etc.

Learn the Activity Life Cycle

...also applies to some stuff I didn't cover like Fragments
Rule of thumb to avoid memory leaks
-
Don't use getBaseContext(). Avoid using activity context to prevent memory leaks. Be wary of passing the context around since it won't be garbage collected.
-
Try getApplicationContext() when possible.

Great libraries to use

RxJava by Netflix

Flow
Mortar
Dagger
ButterKnife
Retrofit
Full tutorial from ADI
Do Android Developers Dream of Electric Sheep?
github.com/parthibanloganathan/android-tutorial

adicu.com/resources > Android
Further learning
-
(bit.ly/1hqDdmv)
-
Programming Mobile Applications for Android Handheld Systems (coursera.org/course/android)
-
Docs (developer.android.com)



What did we learn today?
- Why Android development
- A rough idea of what comprises an app
- A few good tips and tools to use
- How to learn more about Android on your own
- I use too many memes


Hopefully you're not too confused...
Contact me at
parthi (at) adicu.com
@parthiban_logan
OR
come to Cookies and Code on Wednesdays at 10 PM in
Satow Room, Lerner 5th Floor



Intro to Android Development
By Parthiban Loganathan
Intro to Android Development
- 3,413