In the previous video we created a layout
that we're going to be using for our
button counter app, to show how to make a
button to do something in Android.
So open the project if you haven't already
and then we're going to have a look at
the code in the MainActivity class.
Now Android Studio often opens up with most
of the screen blank, with the project
panes shrunk away to the left. Now I
showed you how to get back by clicking
them on the tab over to the left, here.
You can also go to the View menu and
choose Tool Windows and select Project
from the list. Now obviously, because the
project was selected, that's then
disabled it. So I can go back again
and select Project and bring it back on
the screen again. So if you don't see it
that's how to activate it, and you saw
that there was a shortcut there as well.
In my case, on a Mac its command 1, and that
would be control 1 on a PC running
Windows, and also the same for Linux. But
it is actually worth checking, anytime
you see these menu options, just to have
a look at what the relevant
keyboard shortcut is for your operating
system, because you will find that it's a
lot more efficient, as you go through
learning Android app development, to be
using those keyboard shortcuts. But the
quickest way to open the project pane is
just to click on the tab, up here, at the
top left hand corner. But I wanted to
show you how to do it from the menu and
also show you that the menus have that
reminder of the keyboard shortcuts.
Alright, so now that the project pane is
open, to get to our code, we need to
expand the app menu which, it's now
expanded, but if it wasn't expanded it
would look like that. Click on that and
then also come down to here and click on
Java, and you can see that the package name
that we created using the reverse domain,
in this case, is academy.learn
programming.buttoncounterapp. This
is where all our classes that contain
the code for the app will actually
eventually live, and I'll just expand
that out so we can see the main activity
there. Now there's two other directories
in Java as well, and these ones down here,
androidTest and test - you can see them
in parentheses when I hover the mouse
over them. And they're actually for our
unit testing, both locally on the
computer and also on an Android device
or emulator. Now we're not going to be
using those two directories in this
course so you can safely ignore them and
just focus on the first one - the one that
hasn't got anything in parentheses. So
make sure you've opened the correct
folder and work in that, rather than
those other two, and also don't be too
concerned that the directory or the
folder is called Java. You actually are
in the right course! Now if you remember,
Kotlin code is compiled to bytecode that
runs in the java virtual machine. So it's
pretty much the same bytecode that an
equivalent Java program would produce.
Now historically, this folder has been
called Java. Now of course, Google may
well change that in the future, so if you
don't see MainActivity in a Java folder,
then expand your Kotlin folder instead.
But here we've got a Kotlin class
called MainActivity that extends app
compat activity, and you can see the code
relating to this MainActivity class,
over here in the right-hand side. If you
don't see that you can just double click
it to open it up. So again, here we've got
a class, a Kotlin class called Main
Activity, and you can see by this code up
here, that it actually extends AppCompat
Activity. Now remember, that I said that
although we're using the latest version
of the Android SDK, we can still create
apps that run on older versions. Well
it's the AppCompat library that makes
that possible.
Now when I first recorded this video
before updating this course, there
weren't any devices being used that knew
what to do with a constraint layout,
because Android N wasn't released then.
When it was released, Google sent an
updated AppCompat to all other
supported devices, to enable code using
the new constraint layout, to work on
those devices as well. So all supported
devices this time, means everything
coming back to Gingerbread, and that goes
all the way back to API 9. So what that
means is that to us as developers, we can
take advantage of the latest features, while
still allowing our apps to run on older
devices, and that, I'm sure you would
agree, is seriously cool. Alright, so in the
import section here, I'm just going to
click on the plus and do that if
you can't see the list. We can see here
that AppCompatActivity is imported
from the android.support.v7.app
library, and that's the code that Google
updated back in 2016. Now this particular
class, this MainActivity, is really
simple. There's just a simple function here
called onCreate - you can see that on line 8. Now
when it's called by the Android
framework, it gets passed something
called a bundle, and you can see the
reference to the Bundle here, again on
line 8. And that's a Java class that's
part of Android for passing data around.
Now don't worry about it just yet. We're
going to be looking at bundles in detail
a little later, and that's because our
app is going to be doing something
strange when we rotate it. We're
going to cover this bundle thing, though,
when we look at how to fix that. But
notice that I said that a bundle is a
Java class. Now at the moment, most of the
Android framework is actually written in
Java, so we're going to be using a lot of
Java classes in this course, even though
the code we write will be Kotlin. When we
look at the Android source code from
time to time, it's going to be Java that
we're going to be looking at. But don't
worry, you certainly don't have to be an
expert Java programmer. You just need to
understand some very basic Java syntax
relating to method declarations, and
that's because functions are called
methods in Java. Okay, so the first line of
the function calls the super method - you
can see this on line 9 - and it's passed
to it the bundle, passing to it the
bundle that this onCreate method received,
the savedInstanceState, or saved
InstanceState. And that means that if
our onCreate method here, doesn't do
anything with the bundle, the super
method still gets a chance to do
whatever it has to do with it. Again,
we'll be seeing that a little bit later.
And the last thing that this particular
method does is called a setContentView
method, telling it which layout we want
to use for this activity. Now we saw that
briefly in a previous video, when we
changed the layout that the playing app
displayed. Now one of the things that
setContentView does is inflate the
layout. We're going to be talking about
layouts being inflated at various points
in the course, so what does it actually mean
to inflate a layout. Well it just really
means that Android takes our xml
definition for the layout, then creates
all the widgets that we've defined in it.
It then positions them on the screen,
taking note of all the constraints,
margins and other settings that we've
defined. So as you can probably
imagine, there's some pretty complex
calculations going on while it does all
this. But from our perspective, as far as
we're concerned, inflating a layout just
means creating the screen display from
the XML in the layout file. So in our
code here, we've told it to use the layout,
R.layout.activity_
main. Now that means it's going to look
in the layout folder for a file called
activity_main.xml and
then use that to create the screen
display. Alright, so we're only a few
minutes into this video and I've already
thrown a whole lot of jargon at you.
Kotlin class, activity, support libraries
super methods - it's probably all a bit
confusing so I'd better explain some of it.
Now one of the great things about
Android programming, is that the
framework does a great deal of work for
us. There's still more than enough for us
to do, though, so don't worry that you're
going to have lots of time on your hands.
But loads of fiddly things that have to
be done, are actually done for us by
Android. So a good analogy is to think of
the Android framework, a bit like a
pancake mix. So making good pancakes
takes some skill, but before you can
start you have to mix up eggs, flour,
water and probably some other stuff that
I don't know about. But it's tedious and
hard work getting it all mixed just
right, and that's before you start the
important business of actually cooking
the pancakes. So it's much easier to buy
a pancake mix and use that. All the
weighing and mixing has been done for us,
so that we can concentrate on getting
our pancakes light and fluffy and cooked
on both sides. So the android framework
actually does a similar job. Now in the
old days, programmers had to do
everything themselves, so they'd spend
hours writing code to do basic
things like, for example, checking if a
key had been typed on the keyboard and
working out where to display lines on
the screen and where text should be
printed, so it fits nicely in the boxes.
So all of that sort of stuff had to
be done before they could get around to
creating the application that did the
useful stuff. And in fact, if you go back
a bit further it was even worse, and they'd
even have to write code to do things
like debouncing the keyboard. Now
keyboards are mechanical switches, and
when a key is pressed the mechanical
contacts don't immediately connect. They
sort of make him break quite a few times
until the contact is fully made, and the
mechanical parts are just pressing
firmly against each other. So it happens
with light switches in a house as well,
and if you've ever operated a badly worn
light switch, it can spark when the
connection has been made - of course, the
voltages are much higher and you don't get sparks
from a keyboard, but it's really the same
type of thing. So all that happens too
quickly for us to notice the light
flicker as it's turned on, but fast to a
human is excruciatingly slow to a
computer, and the computer can detect all
those contacts while the mechanical
keyboard switch settles down. Now if the
keyboard wasn't debounced, you'd press
a key and you'd get 8 or 12
characters, perhaps, appearing on the
screen. Now nowadays, with USB keyboards,
it's all done on the keyboard itself, of
course. So the Android framework is
running all the time, doing a great deal
of work for us, again, all the time. So, as
a result, we don't have to write code to
detect when a character was typed or a
button is pressed. We actually leave that
to Android and we just respond when
Android tells our program that something
interesting has happened. Now another
example is orientation change. If we had
to include code to monitor the
motion detectors and include it in all
your apps, you'd get a little bit bored.
Instead, Android deals with orientation
changes and we don't have to worry too
much about them. And the way that
Android does deal with a change from
portrait to landscape is pretty cool,
and we're going to be looking at that a
little bit later on, and that, in fact, is
why we need to use Bundles. Alright, so
the Android documentation is pretty good
for most things. So you can check out
more information on the framework and
I'm going to just go into a browser here,
and there'll be a link in the resources
section of this video and anytime I
mention a link, almost certainly it
will be in the resources section on the
video. And here's the Android website, as
you can see there. Basically, Google's
site for all things Android, so you can
consider anything on there, on this site,
as the definitive word on how
Android works. There's a link on this
page to activities. Come down here you
can see Learn more Activities, I'm gonna click
on that, and that gives us a bit of an
overview of what an activity is. Al
right so what is an activity? Well
firstly, an application is made up of
activities, which can launch other
activities to perform different tasks.
Now they are normally launched when the
user clicks a button, or activates an
option on a menu, for example.
So when you launch your phone's dialer to
make a phone call, that's an activity.
When you're, say, viewing your contacts
list, that's also an activity, and when
you go on to edit a contact, that
could be another activity, although
Android has something called Fragments,
that can also be used to display a user
interface, but we're going to be looking
at those later in the course. Now as you
perform different actions in an
app, you'll generally move from one activity
to another. And Android keeps track of
which activities you've used, and when
you tap the back button, it closes the
current activity and displays the
previous one on the screen. Now we're
going to be creating lots of activities,
so you'll get plenty of practise at
creating and using them for various
things. And if you've never programmed in
Kotlin before, you might be wondering
what a Kotlin class is. Now we covered
this in the Kotlin tutorial, in the
previous section. Now that'll give you
an introduction to the basics of
programming in the Kotlin language, and
will explain what things like classes
are and how to create them. At the moment,
though, think of them as building blocks
that you can use to perform a function,
or a group of related functions, when
creating your programs. Now classes can
be extended to add more functionality to
change their behaviors. So, for a non-
programming example, you could take a
wooden pallet and extend it to make a
packing crate. Now it's still got the
properties of a pallet, and you can pile
cartons in it and lift it with
a forklift truck, but it's been extended.
So now we could fill it with footballs
and lift it without all the balls
rolling off. Now trying to transport
footballs on a wooden pallet wouldn't
work out too well, but by extending the
pallet by adding sides, we can use it
for more than a basic pallet allows. And
extending it further by perhaps adding a
lid, then we could use it for transporting
chickens, without them all jumping out.
Alright, so the same principle applies
to classes and in our simple code, as you
go back to our Android Studio and into
our class, the simple code here, the basic
activity class has been extended and
some additional code has been created, in
this case, one line that displays our
layout. So a new class called Main
Activity is now an activity, that
displays a particular layout.
Now it's AppCompatActivity that's
being extended, not activity as I
mentioned earlier. So the principle is
exactly the same, but the thing that's
been extended can support other devices.
Now all that code was generated by
Android Studio when we chose the
template. So what I'm going to do is end
the video here, and in the next video
we're going to start by making our code do
a little bit more. So let's work on that
in the next video.