A QUICK INTRODUCTION TO ANDROID PROGRAMMING - PowerPoint PPT Presentation

About This Presentation
Title:

A QUICK INTRODUCTION TO ANDROID PROGRAMMING

Description:

Android has been with us for quite a while now. Here is the ultimate guide on android programming in a step-by-step method. – PowerPoint PPT presentation

Number of Views:57
Slides: 18
Provided by: acodez
Category:
Tags:

less

Transcript and Presenter's Notes

Title: A QUICK INTRODUCTION TO ANDROID PROGRAMMING


1
A Quick Introduction to Android Programming
acodez.in/introduction-to-android-programming/
May 17, 2021
There are a couple of reasons why you might be
reading this Youve been using the Android OS
for quite some time, and youre probably curious
if you can build an app yourself Youre a Java
programmer, and you read somewhere that one of
the things you can build with Java is an Android
app Someone told you that mobile app developers
make a ton of money (not true, by the way. Not
for everyone), and you wanna take a stab at it.
How hard can it be? Youve been building Android
applications with web technologies, and you hit a
snag on performance. You wanna go
native Whatever your reasons might be, this
article is your baby step towards Android
programming. Table of Contents Brief of
Android What you need to get started Overview of
Android What makes up an Android app? Activities
BroadcastReceivers ContentProviders
Services Intents Manifest file A simple project
in Android The Activity component Testing the app
2
Brief of Android
Android has been with us for quite a while now.
Heres a quick rundown of its history Andy Rubin
founded Android Inc in 2003. Hes got Google
backing at the time In 2005, Google bought
Android Inc Android was officially given to
Opensource in 2007. Google turned it over to Open
Handset Alliance In 2008, Android 1.0 was
released we didnt get the dessert names yet In
2009, the dessert names started with Cupcake
(Android 1.1 1.5), followed by Donut (1.6),
and then Éclair (2.0) From that point on, Android
has seen a steady cadence of releases. The
current version of Android, at the time of
writing, is Android 10 What you need to get
started You can build Android apps using Linux,
macOS, or Windows OS. It isnt too picky. Youll
also need a capable IDE to build Android apps. It
is still possible to build apps without an IDE
you can get by with just the Android SDK your
favorite editor, but I wouldnt recommend this.
Nowadays, you really need a full-featured IDE to
build non-trivial apps. Theres a couple of
choices for the IDE if youre a fan of Eclipse
or NetBeans, you can use those. Most Android
devs use Android Studio, though. Its the
de-facto IDE for Android app development. You
can get it at https//developer.android.com/studio
. Follow the link, choose your OS and then,
install it the way you would install any other
software in your platform, which usually
involves just double-clicking, following the
prompts, and accept the defaults. Android
studio states the following as hardware
requirements
3
4GB RAM 2GB available disk space 1280800 screen
resolution Either Intel, AMD, or M1 CPU Those
are the bare minimum. You dont want that. I can
tell you from experience that while the minimum
requirements technically work, its miserable.
So, heres my recommendation RAM as much as
you can afford, 32GB seems to be common among
devs now Disk space as much as you can afford.
SSD is recommended so you dont get bogged down
by I/O when building your projects Screen
resolution Full HD at a minimum. If you can
snag a 32 inch UHD screen, thats much better.
Youll need all the screen space you afford.
Android Studio has lots of tool windows CPU At
the time of writing, there is no M1 native
version of Android Studio. You can use it on an
M1 (via Rosetta), but the emulator will be
problematic. Youll have to test on an actual
Android device. Intel-based machines seem to be
best for Android development (this may change in
the future) You might be wondering why I am not
listing software development kit JDK (Java
Development Kit) as a pre-req for Android Studio.
Its because you dont need it anymore. Since
Android Studio 2.x, the installer automatically
includes OpenJDK (which is what Android Studio
uses for compilation). So, its really just
Android Studio that you need. alsoRead What is
mobile app splash screen? How is it beneficial
for your app Overview of Android The Android
operating system has the following architecture
(photo is from https//developer.android.com/guid
e/platform). Lets take a closer look.
4
(No Transcript)
5
At the lowest level of the architecture are the
Linux kernel and HAL. This part of the Android
OS is Linux. Its a very stable and secure OS. At
the most basic level, an OS does the
following Acts as a go-between for applications
and hardware it manages the hardware on behalf
of the apps Provides services to apps like
networking, memory management, security, etc.
Manages the execution of apps Next up are the
libraries like WebKit, OpenGL ES, etc. These are
not part of the Linux kernel but are still
pretty low-level. Theyre mostly written in
C/C. On the same level, youll find the ART
(Android Runtime) this is the container where
Android apps are running. Next is the Java
APIs. You build Android apps by making calls to
these libraries it takes care of marshaling the
native libraries and other calls to the rest of
the Android OS on your apps behalf. Finally,
on top is the application layer or the
application programming interfaces (system
apps). This is where all our apps reside (email,
phone app, messaging, video player, etc.) What
makes up an Android app?
An Android app may, at first, seem like a desktop
app, but its not correct to think of them that
way. Android apps are structurally different from
desktop apps. A desktop app generally contains
all the routines and subroutines it needs to run.
It is self-contained. An Android app is quite
different. Its made of loosely coupled
components that are held together by a (uniquely
Android) messaging system called Intents.
6
The diagram below shows a logical representation
of an Android app.
Its made up of components like Activities,
Services, BroadcastReceivers, ContentProviders,
and Intents. Activities This component takes
care of user interaction. Its where you put UI
elements and where you capture user-generated
events (e.g., click, long-clicks,
swipes). BroadcastReceivers BroadcastReceivers
are components that can listen for events (system
or user- generated). If you want to perform a
task in response to an event (e.g., Network went
down, a phone call, somebody sending an SMS
message, etc.), you can use BroadcastReceivers
for that. ContentProviders ContentProviders lets
you write apps that can share data with other
apps. A good example of a ContentProvider is the
Contacts app on Android. It can expose contact
data to other apps without exposing the raw data
store. It facilitates data sharing via
API. Services If you need to run code in the
background without freezing the user interface,
you can use services. You can use these
components to perform long-running tasks, e.g.,
downloading a large file, playing background
music, etc. Intents
7
Intents are used to activate components and to
pass data from one component to another if you
need to launch an Activity (a UI component) from
another Activity (usually MainActivity) as a
response to a user-event (like a button click,
you will do that by creating an Intent object
and launching it. Manifest file The Android
Manifest is an XML file that describes the
application, all its components, and
restrictions (whether or not it can use the
network/internet, use GPS, etc.). Dont worry if
youre not too handy with XML files this is
usually generated when you create a new project.
This file is also updated (automatically) as you
add components to your project. A simple
project in Android Now that we have enough
background on Android apps lets build one.
Launch Android Studio if it isnt opened yet.
Im using the Canary release (canary 15) for this
article. If you downloaded the stable release,
your welcome screen might look a bit different
than mine. Click on New project.
8
Choose Empty Activity on the project templates
(as shown in the picture above) then click
Next.
Fill in the project details like name, package
name, location, language, and minimum SDK. As
you can see, Im using Java for this project
(instead of Kotlin), and Im targetting Android
v30. Click Next to start creating the
project. It might take a while before you see
the screen that follows Android Studio will pull
some files from the repos, and Gradle will sync
up but when Android Studio done doing all that,
youll see your project opened in the main editor
window (as shown below).
9
On the left-hand side is the Project Tool Window.
This is where youll see all the files in your
project this is also where you launch them. At
the moment, MainActivity.java and
activity_main.xml are open on the main editor
window. As you can see, the contents of
MainActivity.java are shown. The Activity
component The project wizard generated one
Activity for our project. This is the main
Activity. Its the entry point for the project,
meaning, when you launch the app, the
MainActivity is the first screen that the user
sees. An Activity isnt simply an object. Its a
component thats made up of a Java class
(MainActivity.java) and an XML resource file
(activity_main.xml). The XML resource file
describes what the screen will look like and how.
It contains the definitions of all the widgets
you have on your screen. It also contains how
these widgets are arranged with respect to the
screen and other widgets (constraints). Switch
to activity_main.xml in the main editor to see
how our MainActivity looks like right now.
10
As soon as you click the tab of activity_main,
Android Studios GUI editor comes into view (as
shown above). What you see is Android Studios
rendering of the UI resource file. If you want
to see it in XML view, click the Code tab (as
shown below).
The following listing shows the contents of
activity_main.xml. lt?xml version1.0"
encodingutf-8"?gt ltandroidx.constraintlayout.widg
et.ConstraintLayout xmlnsandroidhttp//schemas
.android.com/apk/res/android xmlnsapphttp//sc
hemas.android.com/apk/res-auto
xmlnstoolshttp//schemas.android.com/tools
10/17
11
androidlayout_widthmatch_parent
androidlayout_heightmatch_parent
toolscontext.MainActivitygt ltTextView
androidlayout_widthwrap_content
androidlayout_heightwrap_content
androidtextHello World! applayout_constraintB
ottom_toBottomOfparent applayout_constraintLe
ft_toLeftOfparent applayout_constraintRight_t
oRightOfparent applayout_constraintTop_toTopO
fparent /gt lt/androidx.constraintlayout.widget.
ConstraintLayoutgt As you can see, at the moment,
it only has one TextView widget. Lets remove
that and place a button instead. Its so much
easier to work with the UI in Design mode, so
switch over to Design mode again. Then, select
the TextView by clicking on it. Now, delete
it. Next, from the Palette window, get a Button
widget and place it on our Activity resource (as
shown below).
Next, while the Button widget is still selected,
click on the Infer constraints button on the
Layout toolbar (as shown below). The infer
constraints button is depicted as a magic wand.
If you dont put a constraint, the Button will be
shown on the left-upper most portion of the
screen (coordinate 0,0) you need to define a
constraint for it to tell the Android runtime
how far is it from the top, left, right and
bottom edges of the screen.
12
Next, lets do something when the user clicks the
Button. We can tell the Android runtime what to
do when the user clicks the button by setting the
onClick property of the button to a name of a
method. While the Button widget is still
selected on the UI editor, click the Attributes
tool window (as shown below), then find the
onClick property.
Set the onClick property to greet (as shown in
the picture above) of course, the greet()
method doesnt exist yet. We still have to create
it.
13
Switch over to MainActivity.java or open it
from the Project Tool window (if youve closed
it).
Then, after the onCreate() method, add the
following method. public void greet(View view)
Toast.makeText(this, Hello,
Toast.LENGTH_SHORT).show() The greet() method
takes in a View object (android.view.View) as
parameter the Android runtime passes the
reference of the widget that was clicked to the
greet() method. In the body of the greet()
method, were simply displaying a Toast message.
A Toast message is a small message displayed on
the screen. Its similar to a tooltip or other
popup notification. Itll show for a couple of
seconds, then fade away. Testing the app Our
next step is to run and test the app. You can
connect an Android device to your PC and run the
app there, or you can use the emulator. Before
you can use the emulator, you need to install it
first. On the main menu bar of Android Studio,
click Tools, then choose AVD Manager. AVD is
short for Android Virtual Device.
14
In the screen that follows, click Create
Virtual Device.
Choose a form factor from the list of device
definitions (shown above) then, click Next.
15
Select a system image. Ive previously downloaded
API level 30 already (as you can see). So, I can
select it already.
Give your new AVD a name. You can tweak the
settings a bit more (by clicking the Show
Advanced Settings button), but I usually accept
the defaults. You can edit these settings at a
later time (if you truly need to mess around with
it). Finally, click Finish. To start the
emulator, click the green arrow on the Actions
column of the AVD Manager (shown below).
16
In the screen that follows, youll see the
Android emulator in action.
Next, go back to Android Studio and run the app.
You can run the app in two ways From Android
Studios main menu bar, click Run gt Run App
or Click the green arrow pointing to the right
(as shown below)
17
Gradle will sync, and Android Studio prepares a
build. When thats done, Android Studio will
look either for a connected Android device or a
running emulator. Since we already launched an
emulator earlier, Android Studio will deploy the
app in this emulator.
The picture above shows our small project in all
its simplicity and splendor.
Write a Comment
User Comments (0)
About PowerShow.com