Intro to Android Development
with
by
Parthiban Loganathan
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/856705/adi-logo.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/856780/Android-Happy2-e1357315066858.png)
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.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862447/DE13-26-011-1024x984.png)
Android is fragmented
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/861966/android-fragmented.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/856896/ifhbo8gx7zm0sjf6irmu.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/856897/sxgyaux3rruhjzmytssh.png)
Different screen sizes
Different OS versions
... leading to frustrated Android developers
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/856888/Untitled-1.jpg)
Then why Android?
-
52% of US and 85% of the global mobile market + it's growing
-
Growing ecosystem - Android Wear, Google Glass
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862463/android-growth-rate.png)
No review process from an evil overlord (*cough* Apple *cough*)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/861989/litigate-different-600.jpg)
Build awesome apps and help the world be a better place!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862004/Screen_Shot_2014-11-23_at_8.36.29_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862015/Screen_Shot_2014-11-23_at_8.39.55_PM.jpg)
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)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/861683/Screen_Shot_2014-11-23_at_6.31.05_PM.png)
<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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/861895/Screen_Shot_2014-11-23_at_7.54.26_PM.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/861746/Screen_Shot_2014-11-23_at_6.54.07_PM.png)
"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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/861229/its-all-about-context-thumb.jpg)
- 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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862513/bqpzgj.jpg)
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.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862568/56515283.jpg)
Learn the Activity Life Cycle
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862580/Android-activity-Life-cycle.png)
...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.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/861957/ei5iw.jpg)
Great libraries to use
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862100/6407041.png)
RxJava by Netflix
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862109/Square-logo.png)
Flow
Mortar
Dagger
ButterKnife
Retrofit
Full tutorial from ADI
Do Android Developers Dream of Electric Sheep?
github.com/parthibanloganathan/android-tutorial
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862225/Mareep.jpg)
adicu.com/resources > Android
Further learning
-
(bit.ly/1hqDdmv)
-
Programming Mobile Applications for Android Handheld Systems (coursera.org/course/android)
-
Docs (developer.android.com)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862305/logo_coursera.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/866243/android.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/866244/vogella.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/864131/55504273.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862390/659798ea7ef4d49c0fb13816bbd995ca58d173fa70b8705a3838b40c87369eb4.jpg)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862396/cookiemonster.jpeg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862302/ltnlfz.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/parthibanloganathan/images/862387/wsyb2z.jpg)
Intro to Android Development
By Parthiban Loganathan
Intro to Android Development
- 3,333