Android
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 1
Android
4.0 International License
ZIFY JOURNEY
300,000+
1.6
Registered Users
161,000+
Trips in a Month
MILLION 150,000+ Most Awarded
Carpooling company
Total App
Successful Downloads
Trips shared on Zify till date 50,000
Tons
1960%
CO2 Emissions Saved
Growth
Companies we work with -
(YoY)
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial
zify.co | Android
4.0 International License
What is Android?
Mobile operating system based on Linux kernel
User Interface for touch screens
Used on over 80% of all smartphones
Powers devices such as watches, TVs, and cars
Over 2 Million Android apps in Google Play store
Highly customizable for devices / by vendors
Open source
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 3
Android
4.0 International License
Android Software Developer Kit (SDK)
Development tools (debugger, monitors, editors)
Libraries (maps, wearables)
Virtual devices (emulators)
Documentation (developers.android.com)
Sample code
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 4
Android
4.0 International License
Android stack
1. System and user apps
2. Android OS API in
Java framework
3. Expose native APIs;
run apps
4. Expose device
hardware capabilities
5. Linux Kernel
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 5
Android
4.0 International License
What is an Android app?
One or more interactive screens
Written using Java Programming Language and XML
Uses the Android Software Development Kit (SDK)
Uses Android libraries and Android Application
Framework
Executed by Android Runtime Virtual machine (ART)
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 6
Android
4.0 International License
App building blocks
Resources: layouts, images, strings, colors as XML and
media files
Components: activities, services, , and helper classes as
Java code
Manifest: information about app for the runtime
Build configuration: APK versions in Gradle config files
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 7
Android
4.0 International License
Component types
Activity is a single screen with a user interface
Service performs long-running tasks in background
Content provider manages shared set of data
Broadcast receiver responds to system-wide
announcements
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 8
Android
4.0 International License
Everything you see is a view
If you look at your mobile device,
every user interface element that
you see is a View.
Views
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 9
Android
4.0 International License
Examples of views
Button CheckBox
RadioButton
EditText
Switch
SeekBar
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 10
Android
4.0 International License
Creating and laying out views
Graphically within Android Studio
XML Files
Programmatically
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 11
Android
4.0 International License
Views defined in Layout Editor
Visual representation of
what's in XML file.
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 12
Android
4.0 International License
Views defined in XML
<TextView
android:id="@+id/show_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/myBackgroundColor"
android:text="@string/count_initial_value"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/count_text_size"
android:textStyle="bold"
/>
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 13
Android
4.0 International License
Create View in Java code
context
In an Activity:
TextView myText = new TextView(this);
myText.setText("Display this text!");
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 14
Android
4.0 International License
What is the context?
Context is an interface to global information about an
application environment
Get the context:
Context context = getApplicationContext();
An activity is its own context:
TextView myText = new TextView(this);
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 15
Android
4.0 International License
ViewGroup views
A ViewGroup (parent) is a type of view that can contain other
views (children)
ViewGroup is the base class for layouts and view containers
ScrollViewscrollable view that contains one child view
LinearLayoutarrange views in horizontal/vertical row
RecyclerViewscrollable "list" of views or view groups
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 16
Android
4.0 International License
Common Layout Classes
LinearLayout RelativeLayout GridLayout TableLayout
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 17
Android
4.0 International License
Events
Something that happens
In UI: Click, tap, drag
Events are "noticed" by the Android system
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 18
Android
4.0 International License
Event Handlers
Methods that do something in response to a click
A method, called an event handler, is triggered by a
specific event and does something in response to the
event
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 19
Android
4.0 International License
Handling clicks in XML & Java
Attach handler to view in Implement handler in activity:
public void showToast(View view) {
layout:
String msg = "Hello Toast!";
android:onClick="showToast"
Toast toast = Toast.makeText(
this, msg, duration);
toast.show();
}
}
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 20
Android
4.0 International License
Setting click handlers in Java
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String msg = "Hello Toast!";
Toast toast = Toast.makeText(this, msg, duration);
toast.show();
}
});
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 21
Android
4.0 International License
Resources
Separate static data from code in your layouts.
Strings, dimensions, images, menu text, colors, styles
Useful for localization
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 22
Android
4.0 International License
What is an Activity?
An Activity is an application component
Represents one window, one hierarchy of views
Typically fills the screen, but can be embedded in other
activity or a appear as floating window
Java class, typically one activity in one file
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 23
Android
4.0 International License
Examples of activities
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 24
Android
4.0 International License
Activity Life Cycle
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 25
Android
4.0 International License
Implement new activities
1.Define layout in XML
2.Define Activity Java class
extends AppCompatActivity
3.Connect Activity with Layout
Set content view in onCreate()
4.Declare Activity in the Android manifest
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 26
Android
4.0 International License
1.Define layout in XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's Shop for Food!" />
</RelativeLayout>
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 27
Android
4.0 International License
2. Define Activity Java class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 28
Android
4.0 International License
3. Connect activity with layout
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
} Resource is layout in this XML file
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 29
Android
4.0 International License
4. Declare activity in Android manifest
<activity android:name=".MainActivity">
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 30
Android
4.0 International License
4. Declare main activity in manifest
Main Activity needs to include intent to start from launcher icon
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 31
Android
4.0 International License
Intents
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 32
Android
4.0 International License
What is an intent?
An intent is a description of an operation to be performed.
An Intent is an object used to request an action from another
app component via the Android system.
Originator App component
Intent Action
Android
System
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 33
Android
4.0 International License
What can intents do?
Start activities
A button click starts a new activity for text entry
Clicking Share opens an app that allows you to post a photo
Start services
Initiate downloading a file in the background
Deliver broadcasts
The system informs everybody that the phone is now charging
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 34
Android
4.0 International License
Explicit and implicit intents
Explicit Intent
Starts a specific activity
Request tea with milk delivered by Nikita
Main activity starts the ViewShoppingCart activity
Implicit Intent
Asks system to find an activity that can handle this request
Find an open store that sells green tea
Clicking Share opens a chooser with a list of apps
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 35
Android
4.0 International License
Start an Activity with an explicit intent
To start a specific activity, use an explicit intent
1.Create an intent
Intent intent = new Intent(this, ActivityName.class);
2.Use the intent to start the activity
startActivity(intent);
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 36
Android
4.0 International License
Prepare and Publish
Your App!
This work is licensed under a Creative
Introduction to
Android Developer Fundamentals Commons Attribution-NonCommercial 37
Android
4.0 International License