In the previous videos,
we created our first Hello World Android application
and then I showed you how to set up
and configure a virtual device or emulator.
So now it's time to run the application on the AVD.
Now you'll find I talk about virtual devices,
emulators and AVD's throughout the course
and use the terms interchangeably.
Strictly speaking, emulator refers to the software
that the virtual device is running inside,
but we're not really interested in that level of detail.
Using an emulator to run a virtual device
is a way to the get the Android operating system
running on your computer so that you can run programmes
on it as though it were a real device.
And it's very handy if you haven't got a physical device,
and also let's you test on different Android versions
just by creating a new AVD.
So we're going to run the Hello World application
on an Android virtual device.
Now you should have Android Studio running as I've got
and the Hello World application open.
Android Studio keeps a track of the last few projects
that you've worked on so you can just click on Hello World
on the left hand side of the Welcome screen
to load the project if it's not already opened.
Now, we saw one way to start an emulator
in the previous video.
From the Tools menu, I chose Android and then AVD manager,
and now from the AVD manager,
click the green triangle next to the virtual device
that you want to start.
So I'm gonna click on that now.
And if you've got a slow computer,
it's probably a good idea to start the virtual device
before you actually need it,
you can leave it loading to get on with your work.
And you can also close down the Android virtual device
manager before the emulator has fully loaded,
you don't need to wait for it to fully load.
Now when the emulator has fully started,
you're ready to use it to run your app.
And you can see that it's started now.
So I'm gonna switch back to Android Studio,
now to run our app, there's several ways.
The obvious way to run is to go to the Run menu
and select Run app there.
Before I click that though,
notice the green arrow up here,
and there's also another green arrow up on the toolbar,
so we can choose that as well
and get that to run that way instead.
So I'm gonna click on this one, either one would work.
So the next thing it needs to know is
what do we want to run this on?
Now because I've already started an emulator,
it appears at the top here under Connected Devices.
Any emulators that are running,
as well as any physical devices that are
plugged into the computer should appear here
and you can just choose the one you want to use.
Now, this also integrates quite nicely
with the AVD managers, so that system images
that you've set up from there will appear
in the available emulator section,
which will show up if there are extra ones.
And you can create a new emulator here if you like,
using the button at the bottom,
and that just launches the AVD manager.
So it's probably not often that you want to use this button,
more useful is that you can choose an emulator
that isn't running and start it from here,
and I'm gonna be doing that next.
For now though, we've already got an emulator started
so I'm just going to select that one or leave them.
So I currently selected at the top and click on OK.
And I'm going to Tab over to my emulator
and you can see down the bottom,
we've got some things happening there,
Gradle Build Running, eventually we should see the app
start in the emulator.
Now in previous versions of Android Studio,
before version 2.2, you used to open up the run pane
automatically then switched to the Android monitor pane
once the app started running.
But the version of Android Studio
that I'm using to record this video doesn't do that,
but they may have put that feature back in again
in the future.
The Android monitor pane has also been renamed to Logcat,
I suppose that's not surprising
because everyone referred to it as the Logcat anyway.
Now, you can see that the app has now started.
And it is worth having a look at these panes
when you run an app.
So I'm gonna open up the Run pane, over here, click on that.
And you can see the commands that it used to deploy the app
to the phone and start it running.
And if you've played around with ADB
to root your device and instal apps on it,
then this may all look very familiar,
but if it doesn't make much sense then don't worry about it,
it's not something you need to know how to do
because Android Studio runs the ADB commands for you.
Now ADB, by the way, stands for Android Debug Bridge
and that's the method used to communicate
with an Android device when developing,
and we're gonna be looking at these commands
in a later section and we'll set up ADB
and have a look at some of the things
that you can do with it.
The only reason you'd be interested in this pane
at the moment though is if something goes wrong.
You can then either google the error message
or ask a question in the Q&A section of this course,
and then paste the contents of the screen
into your question.
Now the Logcat screen is a little bit more interesting,
so I'm going to click on that,
but it's also scary because a lot seems to be happening.
Now I'm not gonna talk about it right at this moment
because we've missed most of the excitement,
so I'm gonna come back to the Logcat shortly.
So firstly, if we go back to our emulator,
Tab over, we can see the Hello World programme app
running on this emulator,
and it looks just like a physical Android device
with the home button, etc.
at the bottom and our app running in the window.
Our simple app doesn't do a great deal
but that's just how you can test you app using an emulator.
So I can close the app using the back button
and just like on a physical device
when you instal apps on the Play Store,
there's an icon to launch Hello World
from the device's Apps menu.
So if I click on that,
you can see we've got our Hello Word app there,
I can click on that to start it.
Now you may have noticed some activity down here
in this Logcat pane while all that was going on.
So let's actually have a look at that
and so that we can see that all from the start,
I'm gonna shut down this emulator
and we'll start it back up using the other method
I mentioned.
You can stop the emulator by either
holding down this power button here,
holding down the button and powering off,
but you can also close it
by clicking on this little X button down here.
Either way will actually close down the emulator.
Alright, so now instead of starting an emulator first,
I'm just going to run the programme.
Now if you've got a fast computer with loads of RAM,
there's no need to start an emulator first.
In the button bar at the top of the screen,
there's a green arrow that you can use to run your app
like I clicked earlier, and do that again.
And when I've clicked that,
it asks for an emulator or device to run on
and that's the same screen we saw earlier
but now there's no running devices,
connected devices is none,
we've got available virtual devices
showing the Nexus 5X API 26 virtual device.
So I can choose one from there and click on OK.
And we should find the emulator starts up for us
automatically and eventually once that's loaded,
the application will automatically start.
And I've got the Logcat showing on the screen here
as you can see, so we should be able to see things
happening here as that does start.
Now I say should be able to,
the Logcat often hangs on the emulator
that you've just closed so it might be showing
as disconnected or just generally not updating.
It doesn't seem to be doing much in the way of updating
here now, as you can see.
And this is the disconnected here
that I'm referring to in the dropdown window.
I'm gonna change that to select a new emulator,
click on that, select the new emulator
and then we'll be good to go.
And as we get new message and as we do things
on the computer, that will then be updated
and show in the Logcat.
And you can see that as I'm doing things
we're getting entries in there.
Start Hello World again,
there's generally a lot going on in the Logcat,
and what we might do is just to see that in full glory,
we'll close this down, power it off,
and then we'll start it again and you'll be able to see
the emulator and all the various messages.
Alright so at the moment you can see it says "disconnected",
so I'm gonna start that again
and I'm gonna come down here straight away,
give it a second to start, and select no filters over here,
and you can see we've got absolutely everything.
Quite a bit of information as you can see there.
And this was what I was talking about before,
you can see some of the messages that have got
an I and a slash at the beginning, information messages,
and you can see others, we've got a W/ for warning
and E/ for error, and it's also color-coded as well.
Now the Logcat clears itself as our app launches each time,
so if I go back and actually select our app,
show only selected application, there's currently nothing,
so there will be little to nothing showing,
and this is because this happens to be
a bit too simple to log much of interest.
And in fact, as you can see in my case,
it's absolutely blank at the moment.
Now, we're gonna be looking at Logcat
in more detail a bit later
when we've actually got some more log entries to see.
For now though, I just want you to be aware that it's there,
if you get problems with your app, if they crash,
you'll find a crash report in the Logcat
and you can copy that in the same way as you copy text
in the edit window and paste them into the Q&A section
of this course along with your question.
Now it may not mean much to you at first
but it will be very useful to us
when working out why your app crashes.
So the emulator overall does a pretty good job
of showing what your application will look like
and you'll be seeing that in operation a lot more
as we go though the course, and that's because
it's one of the core things that I recommend you do
to be testing your applications as much as possible
and seeing what they look like on the screen.
Now I certainly recommend that you test the final version
of an application on a physical device
before you upload it to the Play Store,
but for testing purposes, the emulators are great.
They also allow you to test on the different versions
of Android that your app supports
without you having to go out
and buying loads of Android phones and tablets.
So it's a great way to get a feel for what your application
is going to look like before it goes into production
and before you test it on a physical device.
So that's testing on an emulator,
if you can run them, they are a great help.
If you do have problems running emulators
because your machine doesn't have enough memory,
then you can run all the apps in this course
on a real Android device though.
So don't be too put off if you can't run an emulator,
Android devices are also fairly cheap these days
so it can be a very inexpensive way
to test on a real Android device
if you have problems using emulators.
Alright, so I'm gonna end the video now.
In the next video, we're going to fire up
one of my Android devices, my Nexus 5X,
and run the app on the physical machine
just to show you what it looks like on that
and how to use it on a real device.
So I'll see in the next video.