android-studio-workshop

Download Report

Transcript android-studio-workshop

Android
Dr. Vaishali D. Khairnar
IT Department
Terna Engineering College
1
What is Android?

Android is a software
stack for mobile devices
that includes an
operating system,
middleware and key
applications.
2
OHA (Open Handset Alliance)
A
business alliance consisting of 47
companies to develop open standards
for mobile devices
3
Phones
HTC G1,
Droid,
Tattoo
Suno S880
Motorola Droid (X)
Samsung Galaxy
Sony Ericsson
4
Tablets
Velocity Micro Cruz
Dawa D7
Gome FlyTouch
Toshiba Android
SmartBook
Acer beTouch
Cisco Android Tablet
5
Android Studio Installation Steps


Step 1: System Requirements
 Microsoft® Windows® 8/7/Vista/2003 (32 or 64-bit)
 Java JDK5 or later version (jdk1.8.0_101)
 Java Runtime Environment (JRE) or later version
(jre1.8.0_101)
Set-up Java Development Kit (JDK)
 set JAVA_HOME=C:\jdk1.8.0_101
 Android Studio (android-studio-bundle-143.3101438windows.exe)
 Android Studio is the official IDE for android
application development. It works based onIntelliJ
IDEA
 Link for Android Studio
developer.android.com/sdk/index.html
6

Step 2: Installation
 launch Android Studio.exe
 Machine should required
installed Java JDK.
 By clicking on next complete the
wizard for Android Studio.

Errors
 JDK error
 Install latest version of jdk and
configure it.
 HAXM installation failed.
 Download HAXM from link
https://software.intel.com/andro
id/articles/installationinstructions-for-intel-hardwareaccelerated-execution-managerwindows (haxmwindows_v6_0_3.zip)
 Extra it and run (intelhaxmandroid.exe and
haxm_check.exe)
7
8
9
10
Hello Word Example
App>res>layout>Activity_main.xml
11
Architecture
12
Android S/W Stack - Application

Android provides a set of core applications:








Email Client
SMS Program
Calendar
Maps
Browser
Contacts
Etc
All applications are written using the Java language.
13
Application components are the essential building blocks of an Android
application.
These components are loosely coupled by the application manifest file
AndroidManifest.xml that describes each component of the application and
how they interact.
Components
Description
Activities
They dictate the UI and
handle the user
interaction to the smart
phone screen
Services
They handle background
processing associated
with an application.
Broadcast Receivers
They handle
communication between
Android OS and
applications.
Content Providers
They handle data and
database management
issues.
14
Additional Components: used in the construction of above
mentioned entities, their logic, and wiring between them etc.
Components
Description
Fragments
Represents a portion of
user interface in an
Activity.
Views
UI elements that are
drawn on-screen
including buttons, lists
forms etc.
Layouts
View hierarchies that
control screen format and
appearance of the views.
Intents
Messages wiring
components together.
Resources
External elements, such
as strings, constants and
drawable pictures.
Manifest
Configuration file for the
application.
15
Android S/W Stack –
App Framework
 Enabling
and simplifying the reuse of
components


Developers have full access to the same
framework APIs used by the core applications.
Users are allowed to replace components.
16
Android S/W Stack –
App Framework (Cont)
 Features
Feature
Role
View
System
Used to build an application, including lists, grids, text
boxes, buttons, and embedded web browser
Content
Provider
Enabling applications to access data from other
applications or to share their own data
Resource
Manager
Providing access to non-code resources (localized strings,
graphics, and layout files)
Notification
Manager
Enabling all applications to display customer alerts in the
status bar
Activity
Manager
Managing the lifecycle of applications and providing
a common navigation backstack
17
Android S/W Stack - Libraries
 Including
a set of C/C++ libraries used by
components of the Android system
 Exposed to developers through the
Android application framework
18
Android S/W Stack - Runtime

Core Libraries


Providing most of the functionality available in
the core libraries of the Java language
APIs






Data Structures
Utilities
File Access
Network Access
Graphics
Etc
19
Android S/W Stack – Runtime
(Cont)

Dalvik Virtual Machine

Providing environment on which every Android
application runs



Each Android application runs in its own process, with
its own instance of the Dalvik VM.
Dalvik has been written such that a device can run
multiple VMs efficiently.
Register-based virtual machine
20
Android S/W Stack – Runtime
(Cont)

Dalvik Virtual Machine (Cont)

Executing the Dalvik Executable (.dex) format



.dex format is optimized for minimal memory
footprint.
Compilation
Relying on the Linux Kernel for:


Threading
Low-level memory management
21
Android S/W Stack – Linux Kernel


Relying on Linux Kernel 2.6 for core system services

Memory and Process Management

Network Stack

Driver Model

Security
Providing an abstraction layer between the H/W and the rest
of the S/W stack
22
Create Android Application (HelloWorld)

Click on Start a new Android Studio
Project
23
Next, follow the instructions
provided and keep all other entries
as default till the final step. Once
your project is created successfully,
you will have following project
screen
24
25
Anatomy of Android Application
S.N.
Folder, File & Description
1
src This contains the .java source files for your project. By default, it
includes a MainActivity.java source file having an activity class that runs
when your app is launched using the app icon.
2
gen This contains the .R file, a compiler-generated file that references all
the resources found in your project. You should not modify this file.
3
bin This folder contains the Android package files .apk built by the ADT
during the build process and everything else needed to run an Android
application.
4
res/drawable-hdpi This is a directory for drawable objects that are
designed for high-density screens.
5
res/layout This is a directory for files that define your app's user interface.
6
res/values This is a directory for other various XML files that contain a
collection of resources, such as strings and colours definitions.
7
AndroidManifest.xml This is the manifest file which describes the
fundamental characteristics of the app and defines each of its components.
26
The Main Activity File

The main activity code is a Java file MainActivity.java. This is the actual application file which
ultimately gets converted to a Dalvik executable and runs your application. Following is the default
code generated by the application wizard for Hello World! application −

package com.example.hod_it.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder. The
onCreate() method is one of many methods that are figured when an activity is loaded.
27
The Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hod_it.helloworld">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Whatever component you develop as a part of your application, you must declare all its components in a
manifest.xml which resides at the root of the application project directory. This file works as an interface
between Android OS and your application, so if you do not declare your component in this file, then it will not
be considered by the OS.

The Strings File

The R File

The Layout File
28
Android Activities
Callback
Description
onCreate()
This is the first callback and called when the
activity is first created.
onStart()
This callback is called when the activity
becomes visible to the user.
onResume()
This is called when the user starts interacting
with the application.
onPause()
The paused activity does not receive user input
and cannot execute any code and called when
the current activity is being paused and the
previous activity is being resumed.
onStop()
This callback is called when the activity is no
longer visible.
onDestroy()
This callback is called before the activity is
destroyed by the system.
onRestart()
This callback is called when the activity restarts
after stopping it.
29
Example
30
Android Intents




Intents are asynchronous messages which allow
application components to request functionality
from other Android components.
Intents allow you to interact with components
from the same applications as well as with
components contributed by other applications.
For example, an activity can start an external
activity for taking a picture.
Intent can be used to:



Start an Activity
Start sub-activity.
Start a Service.
31
Types of Intent
Explicit Intent: An application can define
the target component directly in the intent
 Implicit Intent: Ask the Android system
to evaluate registered components based
on the intent data.

32
Explicit Intent example
33
Steps

Create "Empty Activity" project named
ExplicitIntentExample
34
35

By double-clicking on the
components on the interface, you
can set the ID and text for them:
 EditText 1: ID: text_firstName
 Properties
 layout_width: fill_parent
 EditText 2: ID: text_lastName
 Properties
 layout_width: fill_parent
 TextView: ID: text_feedback
 Text: <Feedback>
 Properties:
 layout_width: fill_parent
 gravity: center_horizontal
36



Button: ID:
button_greeting
Text: Show Greeting
Properties:

onClick:
showGreeting
37
activity_explicit_intent.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hod_it.explicitintentexample.ExplicitIntentActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="&lt;Feedback>"
android:id="@+id/text_feedback"
android:layout_alignParentBottom="true"
android:layout_marginBottom="92dp"
android:singleLine="true"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Greeting"
android:id="@+id/button_greeting"
android:onClick="showGreeting"
android:layout_above="@+id/text_feedback"
android:layout_centerHorizontal="true"
android:layout_marginBottom="68dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text_firstname"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text_lastname"
android:layout_below="@+id/text_firstname"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp" />
</RelativeLayout>
38
When users click the button, the program will call a other Activity displays a greeting.
You need to create an Activity with name GreetingActivity
.
39
40

Change the attributes for
components on the
interface:



TextView ID: text_greeting
Text: <Greeting>
Properties:
 layout_width:
fill_parent
 gravity:
center_horizontal
Button: ID:
button_back

Properties
 onClick: backClicked
41
activity_greeting.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hod_it.explicitintentexample.GreetingActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="&lt;Greeting>"
android:id="@+id/text_greeting"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:textAlignment="center"
android:gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/button_back"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="165dp"
android:onClick="backClicked" />
</RelativeLayout>
42
ExplicitIntentActivity.java

package com.example.hod_it.explicitintentexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ExplicitIntentActivity extends AppCompatActivity {
private EditText textFirstName;
private EditText textLastName;
private TextView textFeedback;
public static final int MY_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explicit_intent);
this.textFirstName = (EditText)this.findViewById(R.id.text_firstname);
this.textLastName = (EditText)this.findViewById(R.id.text_lastname);
this.textFeedback = (TextView)this.findViewById(R.id.text_feedback);
}
// When 'Greeting Activity' completed, it sends back a feedback.
// (If you have started it by startActivityForResult())
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == MY_REQUEST_CODE ) {
String feedback = data.getStringExtra("feedback");
this.textFeedback.setText(feedback);
} else {
this.textFeedback.setText("!?");
}
}
}
// The method is called when the user clicks on "Show Greeting" button.
public void showGreeting(View view) {
String firstName= this.textFirstName.getText().toString();
String lastName= this.textLastName.getText().toString();
Intent intent = new Intent(this,GreetingActivity.class);
intent.putExtra("firstName", firstName);
intent.putExtra("lastName", lastName);
// Start Activity and no need feedback.
// this.startActivity(intent);
// Start Activity and get feedback.
this.startActivityForResult(intent, MY_REQUEST_CODE);
}
43
GreetingActivity.java

package com.example.hod_it.explicitintentexample;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class GreetingActivity extends AppCompatActivity {
private String firstName;
private String lastName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greeting);
// Intent is passed into
Intent intent = this.getIntent();
this.firstName= intent.getStringExtra("firstName");
this.lastName = intent.getStringExtra("lastName");
String greeting = "Hello "+ firstName+" "+ lastName;
TextView textGreeting =(TextView) this.findViewById(R.id.text_greeting);
textGreeting.setText(greeting);
}
// When completed this Activity, send feedback to the caller.
@Override
public void finish() {
// Prepare data intent
Intent data = new Intent();
data.putExtra("feedback", "I'm "+ this.firstName+", Hi!");
// Activity finished ok, return the data
this.setResult(Activity.RESULT_OK, data);
super.finish();
}
// The method is called when the user clicks the Back button.
public void backClicked(View view) {
// Calling onBackPressed().
// Gọi phương thức onBackPressed().
this.onBackPressed();
}
}
44
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hod_it.explicitintentexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ExplicitIntentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GreetingActivity"></activity>
</application>
</manifest>
45
Home-Work: Add Middle Name in First Screen
Display First Name, Middle Name and Last Name
With Message Hello How are you!!!.
46
Implicit intent

Implicit intents: These do not specify a target component, but
include enough information for the system to determine which of
the available components is best to run for that intent.

Consider an app that lists the available restaurants near you.
When you click a particular restaurant option, the application has
to ask another application to display the route to that restaurant.
To achieve this, it could either send an explicit intent directly to
the Google Maps application, or send an implicit intent, which
would be delivered to any application that provides the Maps’
functionality (e.g., Google Maps, Yahoo Maps).
47
Example with the implicit intent

In this example, you will click a Button which will
show you a website by URL, you create
an implicitly Intent, Intent sent to the Android
system to decide which components will
be opened , maybe in your equipment installs
many different browsers (Firefox, Chrome, ..),
the device will open it in your default browser or
your preferred browser

In addition examples also include: Send an email
48