0% found this document useful (0 votes)
34 views70 pages

Introduction To Java For Android Platform

The document provides an overview of the history and development of the Android operating system. It describes how Android was initially developed by Android Inc. and later acquired by Google in 2005. It then covers the major milestones in Android's development, including its unveiling in 2007, the first commercial device in 2008, and its growth to 2 billion monthly active users by 2017. It also briefly outlines some of the key features introduced in Android 8.0 Oreo, the most recent major version at the time.

Uploaded by

Dmytro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
34 views70 pages

Introduction To Java For Android Platform

The document provides an overview of the history and development of the Android operating system. It describes how Android was initially developed by Android Inc. and later acquired by Google in 2005. It then covers the major milestones in Android's development, including its unveiling in 2007, the first commercial device in 2008, and its growth to 2 billion monthly active users by 2017. It also briefly outlines some of the key features introduced in Android 8.0 Oreo, the most recent major version at the time.

Uploaded by

Dmytro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 70

University of Information Science and

Technology "St. Paul the Apostle"


Planet

Introduction to Java for


Android Platform
Smart

prof. Dmytro Zubov


dmytro.zubov@uist.edu.mk
Ohrid, Macedonia
Planet Outline of the course
0. Intro to Java programming language
1. History of Android OS briefly
2. Get the free software we need
3. Creating an Android app
4. Analyzing an Android App structure
Smart

5. One more Android app (simple addition)


6. Java programming language fundamentals
7. Java and OOP
8. Java concurrency utilities
Planet

0. Intro to Java programming


language
Smart
What is Java programming language?
Java is a general-purpose computer
Planet
programming language that is concurrent*, class-
based, object-oriented, and specifically designed to
have as few implementation dependencies as
possible. It is intended to let application developers "write
once, run anywhere", meaning that compiled Java code can
run on all platforms that support Java without the need for
recompilation. Java applications are typically compiled to
bytecode that can run on any Java virtual machine (JVM)
Smart

regardless of computer architecture.


*Concurrent computing is a form of computing in which several computations are
executed during overlapping time periods – concurrently – instead of sequentially
(one completing before the next starts). This is a property of a system—this may be
an individual program, a computer, or a network—and there is a separate execution
point or "thread of control" for each computation ("process"). A concurrent system
is one where a computation can advance without waiting for all other computations
to complete.
https://en.wikipedia.org/wiki/Java_(programming_language)
Planet References

1. Horstmann, Cay S. Big Java. 4th Ed., John


Wiley & Sons Publishing, 2010

2. Bruce Eckel. Thinking in Java. 4th Ed.,


Pearson Publishing, 2004
Smart

3. Jeff Friesen. Learn Java for Android


Development. 3rd Ed., Apress Publishing, 2014
Planet The Java Programming Language
In 1991, a group led by James Gosling and Patrick
Naughton at Sun Microsystems designed a programming
language, code-named “Green”, for use in consumer
devices, such as intelligent television “set-top” boxes. The
language was designed to be simple, secure, and usable for
many different processor types.
Gosling recounts that in 1994 the team realized, “We
could write a really cool browser. It was one of the few
Smart

things in the client/server mainstream that needed some of


the weird things we’d done: architecture neutral, real-time,
reliable, secure.”
Java was introduced to an enthusiastic crowd at the
SunWorld exhibition in 1995, together with a browser that
ran applets – Java-code that can be located anywhere on
the Internet.
Planet
Smart James Gosling, the creator of Java
Planet

Versions of Java
Smart
Short Description of Java
Java was designed for the Internet. It has two
Planet
attributes that make it very suitable: safety and
portability.
The safety features of the Java language make it
possible to run Java programs in a browser without fear
that they might attack our computer.
The other benefit of Java is portability. The same
Java program will run, without change, on Windows,
Smart

UNIX, Linux, or Macintosh. In order to achieve


portability, the Java compiler does not translate Java
programs directly into CPU instructions. Instead,
compiled Java programs contain instructions for the
Java virtual machine, a program that simulates a real
CPU.
The question – Is the Java-technology
similar to .NET-technology?
Planet
Overview of a Java virtual machine (JVM) architecture. Source code
is compiled to Java bytecode, which is verified, interpreted or JIT-
compiled for the native architecture. The Java APIs and JVM
together make up the Java Runtime Environment (JRE).
Compilation in an ASP.NET web-page:
Smart
Java IDE (Integrated Development
Environment)
Planet
Eclipse, NetBeans, IntelliJ IDEA, JDeveloper,
JCreator, etc.
Smart
Java applets
Java applets are Java-programs that are embedded in
Planet
other applications, typically in a web-page displayed in a web-
browser (this Java-applet is equal to the above Java-applet in
Eclipse): HTML-file uses .class-file which is
created by javac.exe (java-compiler).
javac.exe is located in the folder
“c:\Program Files
(x86)\Java\jdk1.7.0_13\bin\”
Also, Eclipse can create .class file
from .java-file (“bin” folder of the
java-project).
Smart
Planet JavaScript is not Java
JavaScript is a scripting language that extends
HTML. Instead of having a tag that names an applet, we
have a tag <SCRIPT> that contains the script inside the
page. It was invented by Netscape to allow people to
write programs to generate pages dynamically.
JavaScript programs are placed in between
Smart

<SCRIPT> ... </SCRIPT> tags in the HTML page and they


are executed as the page is loaded by the user's
Browser. They can also be attached to buttons on a
page.
Planet
Smart JavaScript’s example using ASP.NET
Planet
Smart JavaScript’s example using HTML
Planet

1. History of Android OS Briefly


Smart
History of Android OS Briefly
Android is a mobile operating system
Planet
developed by Google, based on the Linux kernel and
designed primarily for touchscreen mobile devices
such as smartphones and tablets. Android's user
interface is mainly based on direct manipulation,
using touch gestures that loosely correspond to real-
world actions, such as swiping, tapping and pinching,
to manipulate on-screen objects, along with a virtual
Smart

keyboard for text input. In addition to touchscreen


devices, Google has further developed Android TV for
televisions, Android Auto for cars, and Android Wear
for wrist watches, each with a specialized user
interface. Variants of Android are also used on game
consoles, digital cameras, PCs and other electronics.
https://en.wikipedia.org/wiki/Android_(operating_system)
History of Android OS Briefly (cont.)
Initially developed by Android Inc., which Google bought
Planet
in 2005, Android was unveiled in 2007, along with the founding
of the Open Handset Alliance – a consortium of hardware,
software, and telecommunication companies devoted to
advancing open standards for mobile devices. Beginning with
the first commercial Android device in Sept 2008, the OS has
gone through multiple major releases, with the current version
being 8.0 "Oreo", released in Aug 2017. Android apps can be
Smart

downloaded from the Google Play store, which


features over 2.7 million apps as of Feb 2017. Android
has been the best-selling OS on tablets since 2013, and runs on
the vast majority of smartphones. As of May 2017, Android has
two billion monthly active users, and it has the largest installed
base of any OS.
https://en.wikipedia.org/wiki/Android_(operating_system)
Planet
Smart Versions of Android Platform

https://en.wikipedia.org/wiki/Android_version_history
Android 8.0 Oreo (API 26)
Android Oreo is the 8th major release of the Android
Planet
operating system. It was first released as a developer
preview on March 21, 2017, with factory images for
current Nexus and Pixel devices. The final developer
preview was released on July 24, 2017, with the stable
version released in August 2017. Features:
• Project Treble, the biggest change to the foundations of Android: a modular architecture that makes it easier and faster for
hardware makers to deliver Android updates
• Picture-in-picture support
• Support for Unicode 10.0 emoji (5.0) and replacement of all blob-shaped emojis by round ones with gradient and outline
• Redesigned Quick Settings and Settings with white background and respectively black and Accent font color
• Restructured Settings by regrouping in sections similar entries
• Adaptive icons
Smart

• Notification improvements
• Notification channels
• Notification dots (badges)
• Notification snoozing
• System-wide Autofill framework
• Sony LDAC codec support
• App-specific unknown sources
• Multi-display support
• 2 times faster boot time
• Apps background execution and location limits
• Google Play Protect
• Downloadable fonts
• Integrated printing support
• Deep color (Wider color gamut for apps)
• Wi-Fi Assistant
https://en.wikipedia.org/wiki/Android_version_history#Android_8.0_Oreo_.28API_26.29
Global Android version distribution since
Planet
December 2009
Smart

https://en.wikipedia.org/wiki/Android_version_history
Global Android version distribution as of
September 2017
Planet
Smart

https://en.wikipedia.org/wiki/Android_version_history
How can we start Java code on Android
smartphone?
Planet
A Java program (such as an Android app) undergoes
several translation steps between the time we write the
program and the time a processor runs the program:
1. Compiler creates Java bytecode from our Java
source files. The source filenames have the .java extension;
the Java bytecode filenames have the .class extension.
2. Another compilation creates Dalvik bytecode from
Smart

our Java bytecode files. Dalvik bytecode file names have the
.dex extension. Android Runtime (ART) replaces Dalvik nowadays.
3. Android app has XML files, image files, and possibly
other elements. Before we install an app on a device,
Android Studio combines all these elements into a single file
– one with the .apk extension.
How can we start Java code on Android
smartphone?
Planet

To perform the compilation from source code to


Java bytecode, Android Studio uses a program named
javac, also known as the Java compiler.

To perform the compilation from Java bytecode to


Smart

Dalvik code, Android Studio uses a program named dx


(known affectionately as “the dx tool”).

To combine all our app’s files into one .apk file,


Android Studio uses a program named apkbuilder.
How can we start Java code on Android
smartphone?
Planet
Smart
What is Dalvik software?
Dalvik is a discontinued process virtual machine
Planet
(VM) in Google's Android operating system that executes
applications written for Android. Dalvik is an integral part of the
Android software stack in Android versions 4.4 "KitKat" and earlier, which is
typically used on mobile devices such as mobile phones and tablet computers,
and more recently on devices such as smart TVs and wearables. Dalvik is open-
source software, originally written by Dan Bornstein, who named it after the
fishing village of Dalvík in Eyjafjörður, Iceland.
Programs for Android are commonly written in Java
and compiled to bytecode for the Java virtual machine,
Smart

which is then translated to Dalvik bytecode and stored in


.dex (Dalvik EXecutable) and .odex (Optimized Dalvik
EXecutable) files; related terms odex and de-odex are
associated with respective bytecode conversions. The
compact Dalvik Executable format is designed for systems that are constrained
in terms of memory and processor speed.
https://en.wikipedia.org/wiki/Dalvik_(software)
Planet What is ART?
Android Runtime (ART) is an application runtime
environment used by the Android operating system.
Replacing Dalvik, which is the process virtual machine
originally used by Android, ART performs the translation of
the application's bytecode into native instructions that are
later executed by the device's runtime environment.
Smart

Unlike Dalvik, ART introduces the use of ahead-of-time (AOT)


compilation by compiling entire applications into native machine code upon
their installation. By eliminating Dalvik's interpretation and trace-based JIT
compilation, ART improves the overall execution efficiency and reduces power
consumption, which results in improved battery autonomy on mobile devices.
At the same time, ART brings faster execution of applications, improved
memory allocation and garbage collection (GC) mechanisms, new applications
debugging features, and more accurate high-level profiling of applications.
https://en.wikipedia.org/wiki/Android_Runtime
Planet

2. Get the Free Software We Need


Smart
Get the Free Software We Need
1. Visit www.oracle.com/technetwork/java/javase/downloads
Planet
and download the Java Standard Edition JDK (if we don’t
already have a recent version of Java on our computer).
Choose a version of the software that matches the
operating system (Windows, Macintosh, or whatever) and
the operating system’s word length (32-bit or 64-bit).
Smart

JDK (Java SE
Development Kit)
includes a complete JRE
plus tools for
developing, debugging,
and monitoring Java
applications.
Planet Get the Free Software We Need
2. Visit http://developer.android.com/studio and
download the Android Studio IDE along with the Android
Software Development Kit (SDK).
Smart
Planet
Smart Install Android Studio

https://developer.android.com/studio/install.html
Planet Android Studio: Welcome window
We see the welcome window when we start
Android Studio:
Smart
Planet Android Studio: Existing project
For instance, we choose to open existing Android
Studio project:

The areas
that you
see on
the
computer
Smart

screen
may be
different
from
these
areas…
Planet Android Studio: The main window
• The top of the main window – the topmost area
contains the toolbar and the navigation bar:
- the toolbar contains action buttons such as Open, Save
All, Cut, Copy, and Paste;
- the navigation bar displays the path to one of the files in
the Android project.
• The Project tool window.
Below the main menu and the
Smart

toolbars we see two different areas.


The area on the left contains the
Project tool window. We use the
Project tool window to navigate
from one file to another within our
Android app.
Android Studio: The main window (cont.)
• The Editor area is the area to the right of the Project
Planet
tool window.
- when we edit a Java program file, the editor displays the
file’s text;
- when we edit a layout file, the Editor area displays the
Designer tool.
• The lower area
is below the
Project tool
Smart

window and the


editor. It contains
several tool
windows. For
instance, the tool
window “Android
Monitor”:
Android Studio: The main window (cont.)
• The status bar is at the very bottom of Android
Planet
Studio’s window. The status bar tells us what’s happening now.
For example, when we tell Android Studio to run our app, we see
Gradle: Executing Tasks on the status line. When Android Studio
has finished executing Gradle tasks, we see Gradle Build Finished
on the status line. Messages like these are helpful because they
confirm that Android Studio is doing what we want it to do.
• The kitchen sink. In addition to the above areas, other areas
might pop up as the need arises. We can dismiss an area by
Smart

clicking the area’s Hide icon:


Installing new (older) versions of Android
The Android SDK Manager lists the versions of
Planet
Android and helps us download and install the versions
that we need on our development computer:
Smart
Creating an Android virtual device
To run our code, we must have something (a
Planet
physical device or an emulated device) that can run an
Android program.
Smart
Planet
Smart Creating an Android virtual device (cont.)
Planet

3. Creating an Android App


Smart
Creating an Android App
1. Launch Android Studio.
Planet
2. If we see a project’s main window, go to the
window’s main menu bar and select File ➪ New ➪ New
Project. If we see the Welcome screen, select Start a
New Android Studio Project.
Smart
Creating an Android App (cont.)
3. Choose the Target Android Device. This window
Planet
has check boxes for Phone and Tablet, Wear, TV, and
Android Auto (sometimes, Glass). The window also has a
number of Minimum SDK drop-down lists.
Smart
Creating an Android App (cont.)
4. Select the Activity option. In this example, the
Planet
Fullscreen Activity is chosen.
Smart
Creating an Android App (cont.)
5. Customize the Activity (if necessary) and click
Planet
Finish:
Smart
Creating an Android App (cont.)
6. We see in the status bar that the project is
Planet
created:
Smart
Creating an Android App (cont.)
7. We can see how our project looks like (Run
Planet
‘app’ or Shift+F10):
Smart
Creating an Android App (cont.)
8. We can see our project in the emulator:
Planet
Smart
Creating an Android App (cont.)
9. If we want to build an .apk file to install app on
Planet
the Android phone:
Smart
Creating an Android App (cont.)
10. Copy .apk file onto the Android phone and
Planet
install the app:
Smart
Creating different app with the same
project in Android Studio
Planet
1. Copy the project files to another directory and
rename folder
2. Open it in Android studio
3. Right click on package name and refactor ->
rename
4. Sometimes, it doesn’t work :( Do you have
other ideas how to create different app with the same
project in Android Studio?
Smart
Creating different app with the same
project in Android Studio (cont.)
Planet
Smart
Creating different app with the same
project in Android Studio (cont.)
Planet
Smart
Planet

4. Analyzing an Android App Structure


Smart
Planet Analyzing an Android App Structure

Android project contains over 1,000


files in nearly 500 folders. If we
expand some of the branches in
Android Studio’s Project tool
Smart

window, we see some of them.


However, we use only few of them…
Planet The app/manifests branch
The app/manifests branch contains the
AndroidManifest.xml file. This file provides information
that a device needs in order to run the app.
For example, an app may contain several
activities. The AndroidManifest.xml file tells Android
which of these activities to run when the user launches
the app.
Smart
The app/java branch
The app/java branch contains our app’s Java
Planet
code. In fact, the branch contains several versions of our
app’s Java code. Also, this branch contains the extra
code that can be used to test the app on our
development computer (we’ll not emphasize too much
this feature :)
Smart
Planet The app/res branches
The word res stands for
resources. The res branch
contains extra items – items that
our app uses other than its own
Java code:

• app/res/drawable branch
Smart

• app/res/layout branch

• app/res/mipmap branch

• app/res/values branch
The app/res branches: app/res/drawable
The app/res/drawable branch contains any regular-size
Planet
images that our app uses.
The app/res branches: app/res/layout
The app/res/layout branch contains files that describe the
look of our app’s activities (activity_fullscreen.xml in our case).
The app/res branches: app/res/mipmap
This branch contains some additional images – the images
of our app’s icons.
Smart

The app/res branches: app/res/values


This branch contains other kinds of information that an
app needs when it runs. For example, the branch’s strings.xml file
may contain strings of characters that our app displays. When we
first create an app, the strings.xml file may contain the line
<string name="app_name">My Application</string>
Planet The Gradle scripts branch
Gradle is a software tool. When the tool runs, it
takes a whole bunch of files and combines them to form
a complete application — a single file that we can post
on Google Play. Of course, Gradle can combine files in
many different ways, so to get Gradle to do things
properly, someone has to provide it with a script of
Smart

some kind.
The heart of that script is in the build.gradle
(Module: app) branch of the Project tool window. That
branch describes our app’s version number, minimum
SDK, and other things.
Planet

5. One more Android app


(simple addition)
Smart
Simple Addition Android App
1. Create a skeletal Android app using Fullscreen
Planet
activity template. Name the project “Simple addition”.
Also, change the content of the strings
“dummy_button” and “dummy_content”:
Smart
Simple Addition Android App (cont.)
2. In the res/layout branch, double-click the
Planet
activity_fullscreen.xml item:
Smart
Simple Addition Android App (cont.)
3. Double-click strings.xml in res/values branch. Add
Planet
strings variables textView1 (“Result = ”), text1 (“0.123”), and
text2 (“2.0”), as well as change the string variable
“dummy_button” as follows:
Smart
Planet Simple Addition Android App (cont.)
4. Correct Text property of
fullscreen_content(Textview) to empty.
Smart
Simple Addition Android App (cont.)
5.1 Drag and drop the “Plain Text” element (i.e.
Planet
editText) from the “Text” palette to FullscreenActivity graphical
layout twice. Drag and drop the “TextView” element from the
“Widgets” palette to FullscreenActivity graphical layout.
Select editText element in the Component Tree and
change the properties layout_marginLeft and
layout_marginTop to 40dp. Change the property text to
@string/text1
Select editText2 element in the Component Tree and
Smart

change the properties layout_marginLeft and


layout_marginTop to 40dp and 90dp, respectively. Change the
property text to @string/text2
Select textView element in the Component Tree and
change the properties layout_marginLeft and
layout_marginTop to 40dp and 170dp, respectively. Change
the property text to @string/textView1
Simple Addition Android App (cont.)
5.2 Finally, we’ll have the following xml code:
Planet
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/text1" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
Smart

android:layout_marginTop="90dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/text2" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="170dp"
android:text="@string/textView1" />
Planet Simple Addition Android App (cont.)
6. On the preview screen or in the component
tree, select the dummy_button.
7. In the Properties pane, type onButtonClick in
the onClick field.
Smart
Simple Addition Android App (cont.)
8. Inside the app/java branch of the Project tool
Planet
window, double-click FullscreenActivity.java
9. Modify the activity’s code as follows:
package com.example.zubov.myapp1;

import android.view.View;

import android.widget.EditText;
import android.widget.TextView;

public class FullscreenActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
TextView textView;

@Override
Smart

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
textView = (TextView) findViewById(R.id.textView);

findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
}

public void onButtonClick(View view) {


Double x,y,z;
x=Double.parseDouble(editText.getText().toString());
y=Double.parseDouble(editText2.getText().toString());
z = x + y;
textView.setText("Result = " + Double.toString(z));
}

Simple Addition Android App (cont.)
Planet
Smart
Planet Simple Addition Android App (cont.)
package com.example.zubov.myapp1;
// This is the package declaration
// I.e. the following code is inside this package

import android.widget.EditText;
import android.widget.TextView;
// We tell Java that, in this program, we’ll be using,
// i.e. importing, some code that’s already defined
// in the Android library

public class FullscreenActivity extends AppCompatActivity {


// We create a class named FullscreenActivity
Smart

// This line is a class header


// The following lines is the class body

@Override
protected void onCreate(Bundle savedInstanceState) {
// Inside the FullscreenActivity class, we make a list of
// instructions (i.e. method) named onCreate

You might also like