0% found this document useful (0 votes)
47 views81 pages

Quick Guide to Android Studio Basics

The document provides a quick overview of Android Studio, explaining its core functionalities and how to navigate the interface for app development. It also outlines the steps required to run an app on an Android phone, including enabling developer options and installing necessary drivers. Finally, it encourages users to modify a sample app to personalize it for a friend's birthday.

Uploaded by

zenebehailu34
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views81 pages

Quick Guide to Android Studio Basics

The document provides a quick overview of Android Studio, explaining its core functionalities and how to navigate the interface for app development. It also outlines the steps required to run an app on an Android phone, including enabling developer options and installing necessary drivers. Finally, it encourages users to modify a sample app to personalize it for a friend's birthday.

Uploaded by

zenebehailu34
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

3.12.

A Quick Tour of Android Studio

Video Transcript
When you started programming, you used the XML [Link] things you can do in the visualizer, you
can also do in Android Studio anda lot, lot, lot [Link] such, Android Studio might look a little bit
[Link] to start making a application,you actually only need to know a few of the core
[Link] really don't worry if you don't know what every single button does.I still look things up,
andI sure don't know what every single button in Android Studio [Link], I'm going to go down here
and click the Text [Link] at this middle part of the screen, I want you to think of this asbeing
something similar to a document editor such as Google [Link] Google Docs, you open a file, and you
edit that [Link] Android Studio, you open a project which contains many files, andyou can edit any of the
files within the [Link] we go over here to the left-hand side, we can see all the folders andfiles that
are inside of our [Link] I go over to a folder and click it, it shows me the [Link] I double-click a
file, it opens a tab for that [Link]'s where you could edit the [Link] the tabs across are for switching
back and forth between different files.I'm going to go ahead andclose this file that I just opened by
clicking the little x up [Link] you're editing things in Android Studio,it does the work of automatically
saving your [Link] if I do something really disastrous, like whoops,I accidentally select all this and
then delete it,always remember that you can undo and redo in Android Studio.I'm going to undo that by
going up here to the menu bar, andclicking Edit > [Link] you can see right below it that we have
[Link], if you don't want to use the menu bar,you can use the keyboard shortcut Ctrl+Z, or
on Macs, Cmd+[Link] to redo, it's Cmd+Shift+Z or Ctrl+Shift+[Link]'s take a closer look at the left-hand
side [Link] here combined makes up your [Link] app or an application on Android isn't a
single [Link]'s a collection of images, layouts, and other [Link] that if you go up here,you can select a
few different ways to view the files and folders in your [Link], in particular,shows you what the
folders actually look like on your computer.I'll click down a bit so you can [Link] right, not to scare you,
but when you made your app there's actually a tonof folders and files that were generated for [Link]
simplify things, I usually just stick with the Android view which is a littleeasier to [Link] back
when you made your project,you chose where the project was going to be [Link] you ever forget, you
can look up here at the menu bar,which reminds you where your project is [Link] if you're feeling
particularly brave, you should feel free to even takea peek at what this folder structure actually looks
like on your [Link] example, on a Mac machine,I could go to my home folder > Android Studio
[Link] then I see this HappyBirthday folder,and this contains all of the project files and folders for
the [Link] if I were to delete this folder or move this folder,it would really mess up things for
Android [Link], so what we're going to be working with is the activity_main.xml [Link]'s located in
the layout folder because it contains XML that hasthe design or layout of your application.I'm going to
[Link] the contents of this file should look pretty [Link]'s very, very similar to the sort of
XML that we were writing in the [Link] big difference are these two attributes [Link] xmlns
attribute says that we're working with attributes thatare specific to Android development and making
[Link] is also used with websites, storing data, and other [Link] we use this xmlns
attribute to specify that we're making thingsparticularly for Android development and particularly for
[Link] fact that we use the word Android here is also the reason that we putandroid: in front of all
the attributes that we [Link] you'd like to learn a little bit more about the xmlns attribute,you can check
out the instructor [Link] important thing to know is when you make a new application,these two
things will be automatically [Link] you should include them in any other layouts you
make,otherwise your application won't [Link] right, now down here, you're going to see two tabs, the
Design tab andthe Text [Link] the Text tab is actually the tab that you're usually going to beworking
[Link] here on the far right, you'll see that there's a button that says [Link] you click on it, it'll
remove the picture of the phone,and if you click on it again, it'll bring the picture of the phone [Link]
is a preview of what your XML looks like on a [Link]'s very similar to the XML Visualizer.I'm actually
going to go ahead and change the text attribute here to say,Goodbye [Link] if you do this on your
own machine and you zoom in here, let me do it now,you should see that the text updated to say
Goodbye World, pretty [Link] you click on the Design tab, you'll see a drag and drop interface
fordesigning phone [Link] for example, I could drag things over into the [Link] here in this
area called Component Tree,it's going to list all of the different views that I [Link] I click on a particular
view, down here in Properties, it shows meall of the different attributes or properties that I can
[Link] list can be pretty helpful for discovering new layout [Link], if you right-click on any
of these properties,such as this alignStart, it'll give you an option to see the [Link] design
view is pretty helpful if you're trying to quickly build a [Link] you get a lot more control over exactly
where different elements goif you understand and know how to write [Link] we don't really need
any of these additional options, and don't know whata lot of them do yet, it's easier to just stick with the
Text [Link] concludes a quick tour of Android [Link], we'll work on running our first app.

[Link]: Running Hello World on Your Phone


Quiz: Running Hello World on Your Phone

Lesson
Downloads

Video Transcript

Right now we see a picture of a [Link] this isn't actually on our [Link] is pretty similar to when
we were working with the XML [Link] we actually want to see our app on our [Link]
participate, you're going to need a USB cable like [Link] this USB cable should fit your Android phone,
which you will also [Link] do I mean by fit?Well, I can pick up my phone and plug this cable into
[Link] if you don't have these things,there's a slightly more complicated way you can get up and
[Link] I'm going to talk about in the next [Link], if you have both of these items,I really
encourage you to use a real device, its a lot more fun that [Link] [Link] things, [Link] phone is
not set up to let random code from your computer run on yourphone by [Link] you're going to need
to do a few steps to prep your [Link], so the first step is to become a [Link] sounds a little
funny, when would you know when you're a developer?Well, what this actually means is changing
around a couple of settings onthe phone, which will alow you to access developer [Link] your
phone's set up, you'll connect your phone to your computerusing the USB cable that I talked about
[Link] if you're using a Windows machine, the third step foryou, in Windows only, will be to install
a [Link] a driver is an extra little bit of software that will allow yourphone to communicate with your
Windows [Link] Macs, this just works by default, so you don't have to worry about [Link] finally,
you'll install your [Link], let's stop doing these [Link] newer phones, running Android 4.2 and
later,you'll need to tell your phone you're a [Link] is a pretty simple process that I'll show you
right [Link] way to do this is to navigate to the Settings [Link] you're here, scroll down to the
bottom and click on About [Link] this menu, also scroll down to the [Link] should see
something that says Build [Link], so take the build number and click it 7 [Link] now
suddenly, you're a [Link]'t that easy?Once you're done, go ahead and hit the back [Link]
because of your new fancy title as a developer, you should notice thatthe phone has provided you with a
new option, the developer [Link] ahead and click on [Link], now in developer options there's a
bunch of fancy sounding options, butwhat I want you to do is scroll down until you see USB
[Link] then go ahead andclick the little check box next to USB debugging to turn it [Link] say
[Link] checking this box,it's allowing your computer to put applications onto your [Link]
that you've enabled that,you can take your handy dandy USB cable, plug one end into the phone,
andtake the other end and plug it into your [Link] you're on a Windows device,there's one more
step to set up your phone and [Link] Windows machine needs some software called the driver
to communicatewith your phone.I'll start by downloading the [Link] most phones besides Google
Nexus phones,you can navigate to the Android OEM, which stands fororiginal equipment
manufacturers, driver website.A link is provided in the instructor notes.I'm going to scroll down to the
[Link] I have a list of drivers for different phones, andI will go ahead and download the
appropriate [Link] my case, I have a nexus phone, soI'm going to go up on the page, and click google
USB [Link] is again, for Nexus [Link], so for me, I'm going to go ahead and download the
google USB driver,Unzip it, I'm going to move this to my [Link] at this point, I've downloaded the
driver, i've unzipped it andI've put it on my [Link] you need to do next is double check that your
phone is actually plugged [Link] let's look at our task bar.I'm going to click on the start menu and go to
[Link] Windows Explorer I'm going to right-click on the word here thatsays [Link] then I'll
go to [Link] this left pane here, I'm going to select device manager, solooking through this list, go
ahead andfind the thing that says other devices and expand [Link] you should see something here that
refers to your [Link] example, NexusS, or ADB,which stands for android debug bridge, [Link].
Okay, so I'm going to right-click andselect update driver [Link] is going to launch a wizard that
looks like this, andI want to select the one that let's me browse my computer, because remember,I just
downloaded it to my desktop.I'm going to do browse computer, and I'm going to go ahead and click
[Link] I'm going to go to my desktop and hit OK and hit next.I might see some security messages
like [Link] ahead and say, [Link] right. [Link], this autoplay just popped up with my Nexus 6, so
that's a good signthat now the computer's communicating well with my phone.I'm going to go ahead
and close [Link] the final window of the wizard,says that Windows has successfully updated your
driver [Link] that sounds [Link] ahead and click [Link] at this point,your phone is
ready to accept the application from your [Link] way of saying that you can now install apps
thatyou've built on your computer, onto your [Link], were going to install our Hello World App
from the computeronto the [Link] do this, you're going to go up to the top of Android Studioto this
green play button, and click [Link] will take about ten seconds or so for the next window to pop up so be
[Link] the bottom of the screen,you should be able to see that there's some process being
[Link], if everything went as planned, your computer should detect your phone andlist it
[Link] sure you have the Choose a running device radio button clicked andnot the Launch emulator
radio button [Link] you can't see your phone, try unplugging it and re-plugging it [Link] then try
restarting the [Link] that also doesn't work andyou're on a Windows machine, you might have
installed the wrong [Link] none of those solutions seem to solve your problem, we've createda
troubleshooting document, which is linked in the instructor [Link] you can always try asking your
fellow students for help on chat orin [Link], but hopefully things are [Link] can select your
phone and click the OK [Link] about five seconds you should see your phone looks like [Link]'ve
now successfully run your first android application on a [Link] [Link] we start thinking
about our birthday card application,I want you to think about a friend who's having a birthday that's
coming [Link], my challenge to you is to change the app that we currently haveto say happy birthday
and then that person's name,and then to run that app on your [Link], you'll need to change
the main activity .xml code filethe same way we practiced changing code in the Internet [Link]
now the text says hello world, but you should be able to find the correctattribute to change it to say,
Happy Birthday and then your friend's [Link] you've done that, press the green run button
andinstall the app on your [Link] luck.

Solution Vedio

Quiz: Running Hello World on Your Phone

Lesson
Downloads

Video Transcript

Right now we see a picture of a [Link] this isn't actually on our [Link] is pretty similar to when
we were working with the XML [Link] we actually want to see our app on our [Link]
participate, you're going to need a USB cable like [Link] this USB cable should fit your Android phone,
which you will also [Link] do I mean by fit?Well, I can pick up my phone and plug this cable into
[Link] if you don't have these things,there's a slightly more complicated way you can get up and
[Link] I'm going to talk about in the next [Link], if you have both of these items,I really
encourage you to use a real device, its a lot more fun that [Link] [Link] things, [Link] phone is
not set up to let random code from your computer run on yourphone by [Link] you're going to need
to do a few steps to prep your [Link], so the first step is to become a [Link] sounds a little
funny, when would you know when you're a developer?Well, what this actually means is changing
around a couple of settings onthe phone, which will alow you to access developer [Link] your
phone's set up, you'll connect your phone to your computerusing the USB cable that I talked about
[Link] if you're using a Windows machine, the third step foryou, in Windows only, will be to install
a [Link] a driver is an extra little bit of software that will allow yourphone to communicate with your
Windows [Link] Macs, this just works by default, so you don't have to worry about [Link] finally,
you'll install your [Link], let's stop doing these [Link] newer phones, running Android 4.2 and
later,you'll need to tell your phone you're a [Link] is a pretty simple process that I'll show you
right [Link] way to do this is to navigate to the Settings [Link] you're here, scroll down to the
bottom and click on About [Link] this menu, also scroll down to the [Link] should see
something that says Build [Link], so take the build number and click it 7 [Link] now
suddenly, you're a [Link]'t that easy?Once you're done, go ahead and hit the back [Link]
because of your new fancy title as a developer, you should notice thatthe phone has provided you with a
new option, the developer [Link] ahead and click on [Link], now in developer options there's a
bunch of fancy sounding options, butwhat I want you to do is scroll down until you see USB
[Link] then go ahead andclick the little check box next to USB debugging to turn it [Link] say
[Link] checking this box,it's allowing your computer to put applications onto your [Link]
that you've enabled that,you can take your handy dandy USB cable, plug one end into the phone,
andtake the other end and plug it into your [Link] you're on a Windows device,there's one more
step to set up your phone and [Link] Windows machine needs some software called the driver
to communicatewith your phone.I'll start by downloading the [Link] most phones besides Google
Nexus phones,you can navigate to the Android OEM, which stands fororiginal equipment
manufacturers, driver website.A link is provided in the instructor notes.I'm going to scroll down to the
[Link] I have a list of drivers for different phones, andI will go ahead and download the
appropriate [Link] my case, I have a nexus phone, soI'm going to go up on the page, and click google
USB [Link] is again, for Nexus [Link], so for me, I'm going to go ahead and download the
google USB driver,Unzip it, I'm going to move this to my [Link] at this point, I've downloaded the
driver, i've unzipped it andI've put it on my [Link] you need to do next is double check that your
phone is actually plugged [Link] let's look at our task bar.I'm going to click on the start menu and go to
[Link] Windows Explorer I'm going to right-click on the word here thatsays [Link] then I'll
go to [Link] this left pane here, I'm going to select device manager, solooking through this list, go
ahead andfind the thing that says other devices and expand [Link] you should see something here that
refers to your [Link] example, NexusS, or ADB,which stands for android debug bridge, [Link].
Okay, so I'm going to right-click andselect update driver [Link] is going to launch a wizard that
looks like this, andI want to select the one that let's me browse my computer, because remember,I just
downloaded it to my desktop.I'm going to do browse computer, and I'm going to go ahead and click
[Link] I'm going to go to my desktop and hit OK and hit next.I might see some security messages
like [Link] ahead and say, [Link] right. [Link], this autoplay just popped up with my Nexus 6, so
that's a good signthat now the computer's communicating well with my phone.I'm going to go ahead
and close [Link] the final window of the wizard,says that Windows has successfully updated your
driver [Link] that sounds [Link] ahead and click [Link] at this point,your phone is
ready to accept the application from your [Link] way of saying that you can now install apps
thatyou've built on your computer, onto your [Link], were going to install our Hello World App
from the computeronto the [Link] do this, you're going to go up to the top of Android Studioto this
green play button, and click [Link] will take about ten seconds or so for the next window to pop up so be
[Link] the bottom of the screen,you should be able to see that there's some process being
[Link], if everything went as planned, your computer should detect your phone andlist it
[Link] sure you have the Choose a running device radio button clicked andnot the Launch emulator
radio button [Link] you can't see your phone, try unplugging it and re-plugging it [Link] then try
restarting the [Link] that also doesn't work andyou're on a Windows machine, you might have
installed the wrong [Link] none of those solutions seem to solve your problem, we've createda
troubleshooting document, which is linked in the instructor [Link] you can always try asking your
fellow students for help on chat orin [Link], but hopefully things are [Link] can select your
phone and click the OK [Link] about five seconds you should see your phone looks like [Link]'ve
now successfully run your first android application on a [Link] [Link] we start thinking
about our birthday card application,I want you to think about a friend who's having a birthday that's
coming [Link], my challenge to you is to change the app that we currently haveto say happy birthday
and then that person's name,and then to run that app on your [Link], you'll need to change
the main activity .xml code filethe same way we practiced changing code in the Internet [Link]
now the text says hello world, but you should be able to find the correctattribute to change it to say,
Happy Birthday and then your friend's [Link] you've done that, press the green run button
andinstall the app on your [Link] luck.

Note One additional step for some users. When you connect a newer Android phone to your computer,
the phone might show a dialog asking whether to accept an RSA key that allows debugging through this
computer. Select "Always allow from this computer" and hit OK.
OEM Drivers Website(opens in a new tab)

Troubleshooting Document(opens in a new tab)

Quiz: Running Hello World on Your Phone

Task List

Solution Video

Video Transcript

Okay, so that last practice question actually came in two [Link] first part was to go into
activity_main.xml and change aroundthe code in a very similar way to how you were changing code in
the [Link] next part was to actually run this new app on your [Link] I'm going to go ahead and
do the first part.I'll go over here, inside the res folder, layout folder,and then I see
activity_main.[Link]-click to open it [Link], now I'm looking at a TextView child viewinside of a
RelativeLayout [Link] it's the TextView that I'm interested [Link], so I should ask myself am I
changing something inthe RelativeLayout, in the TextView, or both?And the answer is I'm changing
something in the [Link] question is which attribute or attributes do I need to change?And to
change around the text of the TextView,that is the android:text [Link] I'm going to go right
here.I'm going to delete the current value but of course leave the quotes, andthen I'm going to type
Happy Birthday Ben!All right, [Link] this point, I've done the first part of what I need to do,which is
change around the activity_main.xml [Link], I need to write it on my [Link] that was a pretty
simple process.I go up to the top here andover to the green play button which will install the app on my
[Link] press play, wait about five seconds, andat that point, this Choose Device window pops [Link]
I just showed you, I'm going to select my phone,and then I'm going to hit the OK [Link] it's a little
bit hard to see, but Happy Birthday Ben appears on my phone.

3.14. Using the Emulator


Video Transcript

So, you might be wondering if you don't have a phone, what you can [Link], Android Studio has
actually provided a way foryou to simulate a phone on your [Link] simulated phone has a fancy
name called the Android [Link] start the Android Emulator, go ahead andgo up to the Run button
and press [Link]'ll see a small loading circle down [Link], whereas before I told you to select the
Choose a running device radiobutton, you're going to want to choose the Launch emulator radio
[Link] here is a drop down of all the different types of phonesyou can [Link] now you
should only have one, and it's most likely going to be a Nexus 5,which is the type of phone, and API 21
refers to the operating system [Link] x86 refers to a particular type of hardware
[Link], this is the name for your simulated [Link] I'm going to go ahead and click
OK to start the [Link] after about ten seconds, you should see a black phone appear on the
[Link]'ll also see this window here appear, which will show any sort of errormessages or activity
messages from the [Link], so here's my black phone, and I'm waiting for it to [Link] a moment
of that, I'm going to see this Android loading [Link] a little bit more time,you should see this
welcome screen appear on the [Link] go ahead and start the phone, swipe up from the [Link]
it's trying to simulate an actual phone, and the first time you load upan actual phone, you get all these
help icons and dialogues.I'm going to just click [Link], if all goes well, your app should appear on your
[Link] didn't for me, so I'm going to go ahead and try to run it [Link], since the emulator's
already running,I can see it now under my Choose running [Link] I'll go ahead and click [Link] I press
the OK [Link] right, and it loaded up my code on my emulated [Link] you should know that
sometimes these simulated phones don't always workcorrectly the first [Link] example, you might
have this issue that I had the first time when I ran mysimulated phone where it would just show the
black screen orwould just show the Android loading symbol, and it wouldn't go beyond [Link] are
actually fairly common errors when trying to start a simulated [Link] one of the reasons that we
suggest using a real [Link] said, for the most part they're solvable [Link], if you are running
into those issues, go ahead andlook at the instructor notes for how to deal with these [Link]'ve
provided a link to a troubleshooting [Link], since I have your attention, if you're having
difficulty with yoursimulated phone, sometimes you have clues in this error output over here.

3.15. Quiz: Create the Birthday Card App

Video Transcript

All right, now we're going to take everything you've learned andmake a simple birthday card [Link]
apps going to look like [Link]'s a digital card that you can show and share with a friend orfamily
[Link] at the end of the practice set,you'll be making your own custom holiday [Link] what I'm
going to do for you right now,is breakdown the process of turning a drawing like this into [Link] this
is going to take three easy [Link] when you're designing application layouts,these are good steps to
follow if you're not sure how to get [Link], so step one is to take the layout and select the correct
views to [Link] two is to then take those views and position them on the [Link] basically, you do
step two by choosing the correct view group andthen selecting the correct positioning [Link],
we add all of the stylistic touches in step [Link] is where we take the views and add things like color
orfont related [Link] thing you'll notice is that this card says Happy Birthday Ben, and>From
[Link] your name probably isn't Lyla and maybe you don't have a friend named Ben,so you can feel
free to change this text around as we go through the [Link] right, so we're going to go ahead
andjump right in, let's start with step one, which is selecting the [Link] this application, what views
do you think are included?

3.16. Quiz: The Drawable Folder

Video Transcript

Okay, so at this point we've got our drawing or [Link]'ve identified the views, and we've started our
thoughts of positioning bypicking a relative layout as our view [Link] is a great time to start
[Link], so what I have opened here is the code that we were working on before,which is
activity_main.xml located in the layout [Link] it's first creating the [Link] already have about
half of the work [Link] have a relative layout andwe have one text view that says Happy Birthday
[Link] get all of the views you need your going to make an image [Link]'re also going to make one
additional text [Link] the additional text view should be pretty [Link] image view requires a
little bit more work because you actuallyneed that Android part of your [Link] the instructor notes,
we've included a link to that picture sothat you can download [Link], so I've downloaded the picture
and I've saved it to my [Link]'s saved as [Link] you might recall before when we
added images our XML, wesaid that we'd saved them in a folder on the computer called the drawable
[Link] remember how our app has a bunch of files and folders associated with it,which we can see
in the directory structure on the [Link] include layouts, pictures, sometimes music, and so on.A lot
of these types of files are going to be housed in the res directory,which is short for [Link] res
directory contains sub-directories for all the image,text, layout, and other resources the app might
[Link] as we said, for images, there's this folder called [Link] this app, all images should be
placed inside of the drawable [Link] you've put the image into the drawable folder,you can use the
source attribute of an image view,and give it the value @drawable and then the name of the
[Link], when you're writing the image name you do not need to addthe file [Link], so
now I'm going to show you how to actually put a picture intothe drawable [Link]. This image is in
the Instructor [Link] ahead and download it to your [Link], I want you to open Android
[Link] Android Studio, you can go over here and Right-click the Drawable folder.>From there, you
should see something that says reveal in finder orreveal in [Link] I can see the Drawable
[Link] right now I'm going to move the [Link] the drawable folder and I'm just going to
do that by dragging it [Link] so make sure you're starting with the correct XML [Link] if you don't
have the correct code, you can go ahead and copy andpaste it from the instructor [Link] right, first
things first, download [Link] take [Link] and move it to the drawable
[Link] that's done, go ahead and add the TextView that says,from and then your [Link] after
that, add the ImageView with the source attribute as android [Link]'s actually a lot of default
paddingin the code that you're working [Link] you can go ahead and delete [Link] do I mean by
default padding?Well, it's this paddingBottom, paddingLeft, paddingRight, [Link] can go
ahead, And just delete [Link], click the green arrow to run the app on your [Link] you've
finished all of these steps, your app should look like [Link], it's a little bit hard to see on this screen,
butthe text views are actually [Link]'s completely normal and something that we're going to
deal with next.

Solution Vedio

Video Transcript

All [Link] during the last video, I already showed you how to [Link] and how to
move it into the drawable [Link] you're having difficulty with these two, go ahead and rewatch the
videos, orgo to the forums and ask other students for [Link] rest of these, we're going to go ahead
anddo in Android Studio right [Link], so this is my happy birthday application [Link], I'm
looking at the file [Link], which is inside ofthe layout folder, right here, so make sure you're
looking at the same [Link] one of the things that we needed to do was remove the padding, andI
showed you how to do this in the video but I'm going to do it [Link] four attributes right here,I'm
not really interested in having right [Link], so that was pretty [Link] next I want to do is go ahead
and make another [Link] should be super easy because I already have a TextView right here
thatI can model it off of.I'm in fact going to go ahead and copy this TextView and [Link] the
TextView I'm adding is supposed to say from, and then your [Link] I'm going to change that right now,
andI changed this specific attribute because it's the text [Link] so I've done two out of the four
[Link] I'm going to go ahead and add the [Link] I don't have an ImageView to copy off of,
soI'm just going to start [Link] you don't remember what an ImageView looks like you can go ahead
andcheck out the XML cheat sheet that we've provided for [Link] of the neat things that Android
Studio doesis when you start typing something it'll pop up with a little auto-complete [Link] what I
wanted was an ImageView I'm going to go ahead and click on [Link] this is super helpful, because it
gave me the android:layout_width andandroid:layout_height attributes without me needing to type
them.I'm going to go ahead right now andjust make these wrap_content just like the [Link] you
can see how I don't even need to finish typing, I can just click on [Link], now if we go over here and
look at the phone preview, I can see that itsays Happy Birthday and I can just barely see that it says from
[Link], these two TextViews seem to be displayed somewhat correctly butI'm not getting any sort of
image down [Link]'s because I still need to define the source attribute of the image [Link] all, I
haven't said what picture I actually want to [Link] I'm going to go ahead and start typing that right
now.I'll start with android, again, the auto complete pops up with src,so to save on typing I'll go ahead
and click [Link] auto complete even guesses what should come [Link] if you remember from the
lecture, I need to type @[Link] the question is, what comes after here?Well what comes after
here is the name of the picture that I want to display,and more importantly that picture has to exist in
the drawable [Link] over here, inside the rows folder, inside the drawable folder,I have
[Link], which is the image that I moved [Link] is the image that I want to [Link]'s in the
correct [Link] I need to do is type the name without the file extension,which would be
[Link], and almost immediately you should see that the picture [Link] this is looking
pretty good, so great job so [Link] before you move on, you should always,always, always, actually run
your app on your [Link] I'm going to do that by coming up here to the green run [Link] clicking
on it, it's going to think for a little bit, andI could see that it's processing down [Link], and once this
window pops up,as long as my phone is plugged into my computer and everything is set uplike we
showed before, I should be able to select the phone, and then hit [Link], so this is actually pretty
[Link] phone looks a lot like the example phone that we have down [Link] got ImageView, we
got our two Text Views up [Link] we pretty much confirmed everything's working as expected atthis
[Link] going through the checklist items I've added a TextView that says,From Lyla.I've also added
the androidparty ImageView right here.I went ahead and removed that extra default padding andI click
the green arrow and ran my [Link] at this point you're ready to move on andfix this pesky little
problem where our views are overlapping one another.

3.17. Quiz: Positioning

Video Transcript

All right, [Link] this is what we have right now andlike I said before, for the most part things are
in the right [Link] image seems to be pretty much centered in the screen,happy birthday is in the
upper-left [Link], the only thing that's an issue is that From Lyla isnot in the correct [Link]
should be in the lower-right hand [Link] can see here at our goal, we've managed to move it down
[Link]'s go ahead and fix this and turn it into [Link] start, select the attribute or attributes that you
need to positionFrom Lyla in the correct place on the [Link] the lower left-hand [Link]'s a
list of attributes and you can go ahead andcheck next to the ones that you think you [Link] you've
done that, submit your answer to see if it's [Link] you know the correct attributes to use, open up
your code andgo ahead and add those attributes to the From Lyla text view.

Solution Vedio

Video Transcript

Okay, so let's look at the [Link] you said either layout_width or layout_height,those wouldn't be bad
[Link]'s really important to choose these values carefully as as theyaffect the space that your
element takes [Link] example let's talk about layout_width.Suppose that I have a text box that says,
[Link] I set the layout_width to be wrap [Link] default,this will just stick that text box into the
upper left-hand [Link], if I use another attribute to push it over to the right,the text box will be
[Link] instead,I use match parent, by default, hi will still be in the upper left-hand [Link] if I then try
to set an attribute to push it to the right,it'll try to push this text box to the [Link] it already is as far
right as it could be because it's expandingto match the parent and fill the whole view [Link] the case
of using match parent,we can see that the hi text hasn't actually [Link] one thing that's hopefully
clear for this example is that we're going towant to set both layout_width and layout_height to wrap
[Link] it wraps exactly around the content andthen when you tell the TextView to be either to the
right, left, above,below, it'll actually move that text to that [Link], so both layout width and
layout height are [Link] is an attribute for bolding or italicizing text, andit really doesn't
involve positioning or [Link] is an attribute used specifically with linear layouts,to
determine whether they are going to be vertical or [Link] we're using a relative layout this
also doesn't make any sense.Layout_weight is really useful for spacing out elements evenly but
again,this is a linear layout [Link] leaves us with these [Link] of these attributes have to
do with relative [Link] these attributes layout_to Right Of and layout_below are
categoricallydifferent than the layout_alignParent either Bottom left or [Link] attributes take two
child views andposition them relative to one [Link] example, we could say position from Lyla to
the right of andbelow the happy birthday Ben [Link]'s see what that would like [Link], so if we
did that andwe just looked at the two TextViews, this is the code that we would [Link] the happy
birthday Ben TextView, I've created an ID [Link] then I refer to that ID from the from Lyla TextView
with the layout belowand layout right of [Link]'s a picture of what that actually looks like in the
app and unfortunately,that didn't really do what we [Link] From Lyla view is in fact to the right
ofthe Happy Birthday Ben [Link] it is also below, but it's not in the lower right-hand [Link]
these two aren't [Link], so that leaves us with these three attributes,which will take my TextView
andalign them in relation to the Parent, which happens to be the whole [Link] I want From Lyla to
be down [Link] is in the lower right-hand corner of the [Link] I'm not going to be using
layout_alignParentLeft, so in fact Iwill be using layout_alignParentRight and
layout_alignParentBottom.Let's see how this all comes together in the [Link], so first things first.I'm
going to find the From Lyla TextView and iIm going to make sure thatthe layout width and the layout
height are both set to wrap_content otherwise,even if we position our textview correctly it might not
actually [Link] I'm going to add the additional [Link] the value, I go ahead and put [Link]
is affirming that I want this TextView to be alignedto the Parent's [Link] other value I could put here
is false, but that would meanthat it is false that it aligns to the Parent Right, which is not what I [Link]
now I'm going to type the other attribute to put at the [Link] I will also set this to [Link] right, so
for those of you at home this might be a little bit hard to seebut it seems like we've positioned our view
down here [Link] said, let's see if it actually shows up correctly on the phoneby pressing the
green Run [Link], I select my phone and I click [Link], [Link] up here I have my Happy
Birthday, Ben, and down here, I have From [Link] with that,everything looks like it's pretty much in
the correct position.

3.19. Quiz: Styling

Video Transcript

You are blazing through these [Link] at this point,we've done step number one where we've selected
the correct [Link] also went ahead and did step number two andwe roughly positioned the views
correctly on the [Link] now it's time to move on to step number three, styling the views, and todo
that, we're going to have to figure out what attributes we need to [Link] right, so what I want you to
do is take a look at where we are right [Link] take a look at where we want to be at the end of the
[Link] to figure out what the difference is between these two states are andthen write those
differences down in a list in this text [Link] will help us choose what attributes we're ultimately going
to change.

Solution Vedio

Video Transcript

Okay. As I'm comparing what we have now andour final goal, I see a number of [Link] first of
these differences,is that the text of the goal is much larger than what we have right [Link] the final
font that we're using is different from the font that we'restarting out with [Link], the text here is
black and the text here is [Link] if you take a look at the image here versus the image of the goal
screen,we also see that the image has actually expanded,to fill the entire screen of the [Link] this
last one is a little bit more [Link] you look over here at the text that we have right now,it's smushed
right up against the corner of the [Link] the text of the goal has this space [Link] the
side of the screen and the text [Link] those were all the differences that I [Link] let's go ahead and
work through these one by one, adding attributes andsetting them to values, which will turn this into
this.

3.20. Quiz: Make the Text Larger

Video Transcript

Listing out those difference gives us a good starting point.>From those, I can make a to do list of what I
need to [Link] now, you're going to go through each one of these andslowly transform this into the
final [Link]'ll start with this first step, which is making the text [Link] here's the app once you're
done [Link] each step, make sure to run the app on your phone sothat you know that it looks good
on a real [Link] right, let's get [Link]'s a list of [Link] a check next to the attribute or
attributes, andthen submit your answer to see if you're [Link] you've selected the correct
attributes, go ahead andchange them so that the text is 36sp for both text [Link] you're done,
your app should look like this.

Solution Vedio

All right, so it's actually the appropriately named textSizethat will make the text [Link] maybe you
thought it was [Link] is actually an attribute that's used forchanging text to either bold or
[Link] it doesn't change the [Link] might have been looking at textAlignment,which does things like
puts the text to the left of the view orthe right of the view, or centered in the [Link] it is actually
textSize, so once we've determined this,let's go ahead and make the changes in the [Link] right, here I
am, in my code.I'm looking at activity [Link] like I always have been andI'm going to scroll down so I
can see my two [Link], So let's go ahead and add the [Link] Android, the name of the
attribute [Link], and now I have to put the [Link] if you remember from the quiz question the
value should be [Link] always, remember to have quotes around your [Link] you're going to need
android: before the value [Link] yeah, and don't forget the equals [Link] you've done this, you
should be able to go over here to the previewscreen and see that the text has actually gotten
[Link]'s go ahead and make this text larger as [Link], [Link] I've typed my textSize="36sp"
[Link] I see that it in fact, updated on the [Link], in reality, this isn't your actual [Link] you
should always, always,always run it on your phone to make sure it looks [Link] do that, I go up here
to the green run button, select my phone and hit [Link] good work, you've done [Link] no longer have
small microtext, we have big andbeautiful 36 sp text displayed on our [Link] just like that, we can
check off text is much larger on our list.

3.21. Quiz: Setting the Font


Okay, now that we have large text, let's move on to changing the [Link], start off by checking the
box next to the attribute orattributes that you need to use to change the text [Link] you think you've
got the right attribute or attributes, go ahead and,as before, hit submit to see if you're actually
[Link] then, once you know which attribute to use Go ahead to your code and changeit to set the
value of the font to sans serif light for both of the text [Link] you're finished, the screen should
look like this,with a thinner sans serif light font for both of the text views.

Solution Vedio

Video Transcript

fontFamily is the attribute that you would use to set the font fora [Link] we want the actual
font to be Sans Serif Light,sans-serif-light is the value that we use for the attribute [Link] make
that a little bit more clear, let me show you in Android [Link], I'm back here in my lovely
activity_main.xml andlet's add some more attributes.I'm going to start with the Happy Birthday, Ben
TextView, andthe attribute I am adding is android:[Link], and the fontFamily that I wanted to
use if you remember from the lastslide is sans, that's with an s, -[Link] as always, it's nestled
between two quotation [Link], this is a little bit hard to see, butthe preview view has updated to
show us the thinner, San Serif Light [Link] can see it best if you click on something that's not the
TextView, andsort of compare these two look like, since this is the old font andthis is the new font.A
couple of things to notice, or problems that you might have run [Link] things here are dashes,
they're not [Link] make sure you get that absolutely [Link] make sure you spelled sans-
serif-light completely [Link] example,the computer won't recognize sans_serif_light with
[Link] can see here it has reverted back to the old [Link] note, that when you hover over
the text, you might get this [Link] fontFamily is only used in API level 16 and higher, current
min is [Link] you remember back to when we made the project, we set the minimum SDK to 15,or Ice
Cream [Link] this is telling us is, to use this attribute, you need one higher [Link] isn't a
huge deal butbasically what it means is that on older phones you're going to see the oldchunkier font
and on newer phones you'll actually see the Sans Serif Light [Link], so I'm going to continue on and
add the attribute to my other [Link]'s scroll [Link]:[Link], and now on the
preview screen everything looks correct, butalways run it on your phone.I'm going to hit the green run
button.I'm going to wait a million years.I'm going to hit the phone, going to hit [Link] right, and when
you load it up on your phone, you should be ableto see that you have a little bit of a thinner Sans Serif
Light [Link] work, two down, three more to go.

3.22. Quiz: Setting the Color

All right, now let's go ahead and change the text to [Link] right, as before, select the attribute or
attributes,using the check boxes that you think will change the color to [Link] your answer by
hitting submit, andonce you know that you've got the correct attribute,I want you to change the value to
@android:color/[Link] go ahead and do this for both [Link] in this intermediate stage,
your app might look a little bit [Link] you've got a white background, and you're putting white
text on [Link] it might be kind of hard to see your [Link]'t worry we'll fix that in the next video.
Solution Vedio

Video Transcript

Okay, hopefully this wasn't too [Link] used the textColor [Link], just as a little bit of a
reminder of what this value is,we use @android:Color to get access to the standard Android colors,and
then /white specifies that we're looking to use Android's white [Link], there aren't a lot of
standard Android [Link] you want a specific color, it's a lot easier to use a hex code,like we talked
about in the lesson.A good resource for hex codes is the material design color web pagefrom the
Android developer [Link] web page is linked in the instructor notes,if you'd like to take a look at
[Link] enough about [Link]'re using Android's white, andlet's go over to Android Studio to take a look
at what that looks [Link] right, we're back at our trusty old activity_main.xml, and you're probablyused
to adding attributes right now, so I'm just going to go ahead andstart typing in the Happy Birthday Ben
[Link], the attribute I'm using is [Link] right, and the drop down actually gives me some
[Link]'s @Android:,then color/white, and almost immediately my preview updates,and it's white, so
it's kind of hard to [Link] again, that's what we wanted it to [Link] right, let's do the same thing to the
other [Link] to click here,android:textColor="".Quotations, important, important quotations,
@android:/[Link] once I'm finished typing this,I can see that, well, this TextView disappears as
[Link], expected, because it's a white text on a white [Link] I can see from the vew outline
here andhere that the text does in fact still [Link] for the sake of thoroughness, let's go ahead and
run it on our [Link], now those of you viewing at home might find it a little bit hard to see,but I
can just barely see my white text on my light gray [Link] it looks like we did a good [Link]'s
one more off our [Link]'s move on to the next step.

3.23. Quiz: Attributes for the ImageView

All [Link], as I mentioned, this white text here is a little bit hard to see,but that's going to be fixed in
this next step where we're going to expandthe image view so that it fills the entire screen andthen we'll
able to see our white text just [Link]. This quiz is going to be a littlebit [Link], like before, I want
you to select the attribute orattributes that you think you need to use to expand the image like [Link]
don't worry about changing them just [Link] go ahead and pick which one you think it [Link] don't
have to modify the code in this quiz.

Solution Vedoo

Quiz: Attributes for the ImageView

Lesson

Downloads

Video Transcript
All [Link], as I mentioned, this white text here is a little bit hard to see,but that's going to be fixed in
this next step where we're going to expandthe image view so that it fills the entire screen andthen we'll
able to see our white text just [Link]. This quiz is going to be a littlebit [Link], like before, I want
you to select the attribute orattributes that you think you need to use to expand the image like [Link]
don't worry about changing them just [Link] go ahead and pick which one you think it [Link] don't
have to modify the code in this quiz.

Quiz: Attributes for the ImageView

Quiz Question

Select the attribute(s) you need to change to expand the image.


Solution Video

Video Transcript

We're already displaying the correct image, which is this Android partyimage, so there's no need to
change the source [Link], the position isn't really [Link] image just needs to be
larger, soall of these layout aligned parent attributes, we're not going to use [Link] the id attribute
in andof itself doesn't actually make any changes to the [Link] just give the view a name so that you
can refer to it [Link] there's actually a way to use the background attribute to achievethe effect
we're trying to [Link] it doesn't involve an image [Link] if you chose background, you're
technically correct, butwe're going to go about it in a different [Link] way that we learned about in
the [Link] leaves us with layout_height, layout_width, and [Link] will change the size
of the image within the view box thatsurrounds [Link] layout_width andlayout_height will actually
change the size of that view [Link] this case, the image is large enough that the view box expands to
fillthe entire screen no matter if it's wrapped content or matched [Link] it doesn't matter in
this particular casewhether we put matched parent orwrapped content you don't strictly need to
change these, in this [Link] said, because we are trying to get the view to expand to the entire
screen,it would probably be good to set these explicitly as match [Link], if we end up using a
smaller image,it will still expand to fill the entire [Link] while layout height and layout width are sort
of optional in this case,scale type is the actual attribute that you're absolutely going to need to useto
make the image fill the entire [Link] question is, we've chosen the attribute, butwe don't know the
value to set scale type [Link] that's what I want you to figure out in the next question.

3.24. Quiz: How to Style the Image

Video Transcript

Alright, so at this point we know we're going to use Scale Type to expandour [Link], now what I want
you to do is actually expand the image to fillthe entire [Link] a couple of hints on how to do
[Link] number one, now there are actually a lot of different values that you canset Scale Type to, but
in the lesson we only taught you about two, solet's stick with thinking about [Link] values that we
know about are center and center [Link] what you know from the lesson,which one of these do
you think we should use?Now once you've used scale type properly to make the image expand to the
entirescreen, there's a possibility that your text will [Link] this happens to you, try changing
around the ordering of your views.

Solution Vedio
Video Transcript

Okay, if you've got it, great [Link] if you didn't, good try and keep watching for the [Link] right, so
here I am in Android [Link] I'm looking at the TextViews right [Link] I'm going to scroll up to look
at the [Link], so a little bit about what I was talking about [Link] I put my cursor
somewhere in this ImageView,it's going to give me a blue box that shows me how big the ImageView
[Link] it's pretty much the entire size of this phone [Link] I'm going to go ahead and add
scaleType, andthe two options that I'm looking at are center or [Link] center doesn't actually
scale the image,it doesn't change the size of the [Link] it makes it a pretty straightforward that I need
to use [Link], and you should see that the image now expands to fill the entireImageView
[Link] might notice here that at the bottom there's a little whitestrip where the image doesn't
expand [Link] I go up here, which let's me choose between different devices, andI choose say a tablet like
a Nexus 10,we can see that this image is not filling the entire [Link] this is bad because it means if
you're viewing this on a tablet you'regoing to get this weird boxy image with this white space around
[Link] it might look fine on a [Link] reasoning forthis is because I've put wrap_content instead
of match_parent.Let me change these to match_parent.Now when I change these to match_parent the
image expands to fillthe entire [Link] the point I am trying to make is that layout_width, layout_height,
andscaleType all work [Link] me draw you a picture to show you a little more detail about what I
[Link], so let's say you are a bit curious about what's actually happening when weuse [Link]
you go to the Android Developers website,you will see that the documentation says the
[Link] scales the image uniformly,meaning that it maintains the image's aspect [Link]
what this means is that it does this so that both dimensions, the width andthe height of the image will
be equal to orlarger than the corresponding dimension of the image view (minus padding).Okay, so one
thing I can say about scaling uniformly,if you've ever tried to scale an image, scaling uniformly means
that itkeeps the image from getting stretched out either horizontally or [Link] it does that scale,
and then it makes the image large enough sothat both the width and height are going to be equal to
orlarger than the corresponding dimensions of the image [Link]'s look at a [Link], so here's
your [Link] on our phone we have an image [Link]'s the picture for the image view, andhere's
the green box representing the actual size of the image [Link]'s centerCrop will do, will scale this
image up sothat without warping it, it fits in the [Link], so without changing the dimensions I've
scaled this image up to make it fitin this green box [Link] because the image is this long rectangle,it
actually expands over the edge of the [Link] will center this image in the view, andthen as
the name implies, it'll crop off the excess parts of the [Link], so I want to talk about one more
problem that you might have run [Link] can overlap each other much like playing cards can overlap
each [Link] the order of these view tags here determines the orderthat the views get placed on the
[Link] since view A is first,it would get placed on the screen first, followed by view B, and then view
[Link] because of the ordering that I put in this XML,I can't really see my text very well in text view
[Link]'s try changing the XML and see how it rearranges my [Link], so I've switched things around
so now I have view B, A, [Link] will then draw view B [Link] it will draw view [Link] finally it'll place
view C.I've just reordered the [Link] is placed in the same position,but the ordering of how
the views stack on top of each other has [Link] because of that, now I can see my [Link] you
might have run into a similar problem with your card ifyou put your two text views here and then you
drew your image [Link] it would draw your first text view, then your second text view, andthen
the image, which takes up the whole screen, would have covered both of [Link] just make sure that
you got the ordering [Link]'s take a look at what that looks like in [Link] so here's my working
[Link] I'm just going to show you what I mean by the ordering of [Link] now my ImageView is
placed first, then my two [Link] we can see the text easily on the [Link] if I instead, cut
this ImageView, I'm going to scroll down,I'm going to past it right [Link] right, and now I have
TextView, TextView, [Link], it draws the "Happy Birthday,Ben!" TextView, and then it draws
the "From, Lyla!" [Link] then it draws this Android party image over the other two [Link] I
select the TextViews, I can see that they're still there,they're just being covered up by the [Link]
right, I'm going to switch it [Link]'s much better, my TextViews are on top [Link], and as we
always do, let's go ahead and run this on our phone,just to make sure that everything is working with
the match_parents andthe [Link], awesome, this is looking pretty [Link] have our image
expanded to fill the entire view, we can see our text [Link]'s one tiny little problem that we still
need to fix, but great job so [Link] right, you're doing a great job. Only one more attribute to change,and
then we're going to have our final birthday card.

3.25. Quiz: Minor Adjustments

We've got one small issue that I'd like you to [Link] now all the text is smushed up in the corner of
the phone screen,both in the upper left corner and the lower right [Link] our end version we want to
give it a little bit more [Link] the same style as before, select the attribute or attributes that you need
tochange to make room between the side of the phone screen and the [Link] you've selected the
correct attribute orattributes, make this space 20 dp on all sides.

Solution Video

Video Transcript

All right, so were actually two correct answers to this question andthey were padding and
layout_margin.So you could have select either one of these but you don't need [Link] me show you
[Link] practice, a lot of times, a view that usespadding will look very similar to a view that uses
layout_margin.Let me actually draw the view borders for [Link] you draw the view borders,you could
actually see the difference between these two [Link] the case of padding,you're making the actual
view larger by adding space inside of The [Link] the case of layout_margin, you're adding space around
the view, butnot actually changing the size of the [Link], you might be wondering when does this
even come into play?Why have two different attributes?Well, if I were to do something like add a
border or a backgroundcolor to both of these views, they'd actually look pretty [Link] our case,
though, we're not adding a border or background color, soyou can pick either padding or
layout_margin.It just wouldn't make sense to pick [Link] we can choose either, I'm going to go
ahead and, a bit arbitrarily,choose to add [Link]'s see what this would look like in Android
[Link] right, here we are back in activity_main.Now the padding attribute is something that we want
to add to the TextViewsto move them farther away from the side of the [Link] and
layout_margin add space on all four [Link] right, let's go ahead and add [Link] remember, I said that I
wanted there to be 20dp on all sides, sothat's what I'm putting right here as the [Link] preview is
updated to show the added space,I'm going to go ahead and scroll [Link] right, and I'm feeling a little
bit playful, so I'm going to go ahead andadd layout_margin to this TextView instead of [Link] right,
and that also scooted the TextView [Link], though,that the TextView's size didn't increase when I
changed the layout_margin,whereas up here in this TextView, the size of the actual TextView has
[Link] what happens if I add both layout_margin and [Link]'s take a [Link], now my text
is 40dp away from the [Link]'s 20dp here and then another 20dp of padding [Link] right, but we don't
want 40dp away from the [Link] I'm going to go ahead and delete [Link], 20dp of padding in the
From, Lyla [Link] 20dp of padding and the Happy Birthday, Ben text [Link], let's run this
on our [Link] right, and may I present to you the finished Birthday Card [Link]'s get some jazz
[Link] with that, we complete the last thing in our to-do list.

3.26. Recap

Video Transcript

Cool coffee beans, you've made your first app, butdon't take my word for [Link] sure that it runs on
your [Link] now I've taken you through three steps to show you how to take a drawingto an
[Link]'s recap a [Link] first step was to choose your views.A great place to start with this step is to
circle the different [Link] then decide which view tags you need to [Link] Kanal taught you
about two view tags, imagery and text [Link] they also mentioned a view called a button,which we'll
be using in the next [Link] you begin to feel a little bit more ambitious,you can discover new views in
the [Link] right once you're done choosing your view, you move on to [Link] the
question you need to be asking yourself at this stage is whatview group should I [Link], view groups
have a lot of control over how andwhere you position your [Link] the views are aligned horizontally or
vertically,you probably want to use a linear [Link] if the views are aligned relative to one
another,then you probably want to use a relative [Link] once you're made this decision, if you're
using a linear layout,you're going to think about things like orientation and layout [Link] weight
in particular is a little bit complicated but pretty [Link] allows you to sort of equally distribute your
views across the screenwhich is a really great [Link] you're using a relative layout,you'll will be
thinking of how to position your views relative to theirsibling views, and to their parent [Link], this
will be done using the align parent attributes, andattributes like to left of and to right [Link] three is the
fun step, where you get to style your [Link] this step you'll look at things like the text, and the
color, andthe font, and decide what you want them to look [Link] the text have a background
color,how will the pictures fill up their space?Once you know the attributes you're going to change,then
you need to now the correct value you're going to change them [Link], you'll look at the more subtle
spacing issues between [Link] you can tweak the spacing between elements using padding
andlayout margin [Link] you're able to master these steps, you should be able to take
anysimple drawing that you come up with, and turn it into an Android app [Link] work.
3.28. Intro to Study Break: Charity Miles

## A Real-Life Case Study: Charity Miles

Congratulations on finishing the first practice set! You persisted past difficult problems and challenged
yourself beyond your comfort zone. We are very proud of you.

The reason why we created this class is because we believe that you have a unique combination of
strengths and background experiences (unrelated to programming) that no other Android developer will
have. You can use this to your advantage because a developer's effectiveness isn't based purely on
technical knowledge. Your point of view is an incredibly valuable asset as a developer because you see
the world in a different way, recognizing problems and potential solutions that others may not see.

Think of Android development as a new skill in your tool kit that you can use to solve problems in your
own community.

Coming up next, you'll hear from a developer who did just that! Gene Gurkoff will share his story of how
he used his own unique experience to recognize a problem, and built an app called Charity Miles to help
solve that problem.

3.29. Study Break: Charity Miles

Video Transcript

My name is Gene Gurkoff, and I'm the founder of Charity [Link] Miles is a free app that allows
people to earn money forcharity whenever they walk, run, or bike.I was a finance lawyer for a number
of years, and when I was in law school,before that I started running marathons to raise money for
Parkinson's research,in honor of my grandfather who has Parkinson'[Link] I figured if I got enough
people together,then collectively we would have the clout of a [Link] then companies would
want to sponsor [Link] first technological project that I worked on was actually while I wasstill a
[Link] I thought it would be really cool if we could make something where we couldcrowdsource
what we think the law should be and it totally failed [LAUGH].But it was a really good [Link]
you're going to create an app or you're going to be an entrepreneur,in any field, I think there are many
moments of [Link] you spin those failures into a learning lesson ora success that I think are the
most important.I never would have thought of Charity Miles if I hadn't had [Link] I got the
engineers I talked to a lot of [Link] ran the idea by them just to see what they [Link] I got
the developers and we worked on wireframing and designing [Link] these features that, you
know, we had in there,crammed in there, we are going to take that out, take that out, sowe ended up
with something that just did one [Link] just choose from over 30 world-class organizations
fromWounded Warrior Project to Feeding America to World Wildlife Fund, ASPCA.I'm going to choose
the Michael J Fox's Foundation for Parkinson's Research,in honor of my [Link] then we're
going to go for an outdoor walk, andnow thanks to Johnson & Johnson, we're able to earn money
forMichael J Fox Foundation for every mile that we [Link] this little mile counter down here shows us
how far we have walked andif we press this we can see more so we can again see how far we've
[Link] we can also in real time, the amount of money that we've raised forthe Michael J Fox
[Link] you know, every mile really does [Link], as you continue to walk, you'll be earning
more money to cure Parkinson'[Link] if you scroll down,you can get a little bit more information about
Johnson & Johnson andthe other things that they're doing to support great [Link] when you're
done, you just press the stop button and finish, and that's [Link] [Link] you're a social good
app or just an app, try to create value versus [Link] to create an app that makes people happy,that
makes people healthy, that improves life [Link]'re going to have to tell investors about it, you're
going to have to tellyour family about it, you're going to have to tell your friends about [Link] if it's not
something you love then it's going to be really boring.I never get tired of talking about Charity Miles
because I love it, andI feel good about being able to share this with [Link] I think it would be great if
the next generation of entrepreneurs coulduse apps to harness the power of crowds to drive major
advances in [Link] phone has so much on it from the accelerometer to the GPS that it'smonitoring
all this data about us all the [Link] that data is super critical for Alzheimer's, for asthma, forheart
disease those being able to use phones and apps to [Link] type of scientific is going to
change the game.I would tell potential developers to do something that they [Link]'s the thrill of a
lifetime, they should do it.

3.30. Learn More On Your Own

How to Learn More on Your Own

As you continue learning Android, an important skill to have is the ability to learn on your own. You
might find resources out there that feel too advanced for where you are on your journey, but we want
you to become accustomed to how developers speak and share their ideas. You don’t have to
understand every word, but you can skim for important ideas. Or you can google search for terms that
you aren’t familiar with.

Read your first Android blogpost article

Start by reading this post on the Android Developers blog(opens in a new tab). It's written by Google
Design Advocate, Roman Nurik, who was the lead designer on the Google I/O app(opens in a new tab).
Google I/O is an annual conference that Google holds for developers.

Follow official Android Development channels on social media

Aside from the blog, you can get the latest news about Android development via:

Android Developers Twitter page(opens in a new tab)

Google Developers YouTube channel(opens in a new tab)

Kirill's Favorite Resources


In addition to the official channels for Android development news, there’s a ton of content online, and a
vibrant ecosystem of Android developers who are happy to share their knowledge through blog post
articles, social media tips, and conference talks.

Here are some of Kirill’s favorite Android resources:

Chris Banes' blog(opens in a new tab): A blog that gives you a deeper look into Android support libraries.

Fragmented Podcast(opens in a new tab): A weekly podcast filled with Android development discussion.

Course 3

Use Input

Course Prerequisites

Course Prerequisites

Lesson

Downloads

1.2. Course Prerequisites

In order to be successful in this course, take a moment to make sure you have the following prerequisite
knowledge:

Beginner knowledge of Android Single-Screen App Design

Beginner knowledge of Android UI Development

Knowledge of creating user interface layouts in Android with XML

Beginner Android Studio experience

If you aren’t sure how to create user interface layouts in Android with XML, we recommend that you
first take the Android Basics: User Interface course and then return here. You should also have Android
Studio(opens in a new tab) already on your computer. If not, check out these installation
instructions(opens in a new tab).

This course is intended for people with little to no programming experience. Course topics include
variables, data types, methods, classes, and object-oriented programming.

1.3. Create a New Project

Vedio Transcript
Let's begin by creating a new project for our [Link] will be similar to the hello world app that you created
with Laila early onin practice set [Link] if you can remember how to set it up on your [Link] these
settings for app name, package name, [Link] that your code will match ours for the rest of this
[Link] it's done being [Link] sure you can run the app on your [Link] should look
something like [Link] name of the app is shown at the top in the app bar and below that,we have the
words, hello world.

IMPORTANT - The activity templates (timecode 0:48) have been updated in Android Studio Version 1.4.
If you are using this version or newer, please select the "Empty" activity template (instead of the Blank
Activity), which will generate files that match the content in this video.

If you're having trouble creating a new app, check out the Troubleshooting Document(opens in a new
tab) and following Android documentation links:

Create an Android project(opens in a new tab)

Create a project(opens in a new tab)

Both of the links above have similar instructions and vary on select few steps.

Quiz: Create a New Project

Solution Vedio

Video Transcript

Here's how you create a new project in Android [Link], open up Android [Link] click Start a
new Android Studio [Link] we mentioned in the quiz, set the App name to be Just Java,and the
Company Domain to be [Link] way your code will match our [Link]
automatically sets the Package name to be [Link] will uniquely identify our
[Link] the project location, feel free to select any spot on your computer to savethis app, then hit
[Link] the minimum SDK,we can set it to be Ice Cream Sandwich, which is API level [Link] means that
our app will run onAndroid devices running Ice Cream Sandwich release or [Link] will target 90% of
the devices that are active on Google [Link]. So this looks good so let's hit [Link] are the
different templates for creating a new [Link]'s just choose blank activity template and
[Link] we have an option of changing the default names for our main activity andour layout and
our menu, but these defaults are fine, solet's just hit [Link] now we wait while our project is being
[Link] you run into a rendering problem here regarding the action bar,you can simply change this
setting to be [Link] that the device preview here will be using API level 21This is just a temporary fix until
the bug here is [Link], when you ready,you can hit the screen play button to install your app on the
device.I had my Nexus Six device connected so I can choose to run it on this [Link], I also click use
same device for future launches sothis dialogue doesn't keep popping up, then hit [Link], this is what it
looks like running on the [Link] says Just Java across the top in the app bar, andit says Hello world!,
down here.
1.4. Plan How to Build the Layout

Video Transcript

This is what we're going to cover and in lesson [Link], we're going to show the quantity and then an
order [Link], we're going to show the price as [Link] we're going to add a plus and a minus
button sothat when you tap on either of these the quantity [Link] that, we're going to take these
buttons and rotate them horizontally sothey show up like this for the quantity [Link] lastly, we're
going to adjust the way that the price is displayed andwe'll also move the order button down to the
bottom of the [Link] is what the app is going to look like by the end of Lesson [Link] the way
we're going to learn about Java concepts like variables anddata [Link] way when we click on the
plus orthe minus button we'll be able to update the quantity or if clickon the order button we'll be able
to calculate and display the price [Link] we're going to just focus on the first stage of our app
andthen slowly build up from there.I'd like you to plan how you're going to build up this [Link] app
displays quantity 0 and then an order [Link] you're stuck try going, through the three steps
forcreating a layout that we went over in the practice [Link] the views you want, position the view,
and then style the [Link] this planning stage, I'll ask for you to write a couple words foreach of
[Link]'s generally a good habit to plan out how you're going to build something,before you actually go
and code [Link], you might get lost in all the details of the [Link] views do we need here?Do
we need text views, image views, [Link] [Link] positioning the views, which view group will be the
root view?Feel free to add other comments about how to position these [Link] for step three we
need to style the [Link] there anything we need to do here?Write your comments below, on how we
need to change the view sothat they look like this.

1.5. Build Layout

Video Transcript

Now that we've thoroughly planned this out, go ahead andbuild out this layout in your [Link]'ll need
to find and modify the activity_main.xml [Link] activity_main.xml file should already beopen when you
first create your [Link] it's not already open, you can find this file under the layout [Link] res
under app in the project view of your app,which lists all the files in your [Link] design pane is open by
default, where you can drag anddrop widgets from this palette here over onto the [Link], I want
you to use the text pane so you can write your XML [Link] any point, when you modify your XML, you
can always select this content andreformat [Link] the top of Android Studio you should see a bunch
of menu [Link] on Code, and then you'll find an option to Reformat [Link] here's the keyboard
shortcut if you're on [Link] can reformat the code and selected text is [Link] click Run andthen it
automatically reformats your code to fix the spacing and the [Link] can also select Rearrange [Link]
modifies the order of the attributes here so that it's in the preferred orderaccording to the Android code
style [Link] now the code is a lot easier to read in this [Link] you want a preview of what this
code looks like,you can click on the Preview button [Link] this shows what it would look like on a
[Link] this isn't a substitute for running it on your device, soyou should always run it on your device
[Link] you build out this layout, please also assign a view ID nameon this second TextView
here that displays a [Link] it a view ID name of @+id/quantity_text_view.Use this exact name so that it
will match our code later [Link] that we learned how to assign IDs to views in lesson [Link]
example was for relative layout, where we had a dinner party forour best friends and our
[Link], the reason why we give this view an ID butnot the others is because we will want to
later refer to this TextView sothat we can update it to increase or decrease the [Link] styling the
views, there's a couple small [Link] TextView here should have a text size of 16sp and use a black
font [Link] can use the black system font color like we showed you in lesson one oryou can use the
black hex color, which we've included [Link]'s also 16 dp of space in between each of these
views.I know that this is the first time that you are going to be adding a button toyour XML [Link] is
a really great opportunity for you to practice your Google searchskills in order to figure out how to add a
button to your [Link] you really get stuck, you can check the hint in the instructor [Link] you build
this up,periodically run the app on your device to check that it's working as [Link] you're done
with all of this, answer this [Link] happens when you click on this button?

Quiz: Build Layout

Build This Layout

Modify activity_main.xml to build this layout.

Assign the second TextView (that shows 0) a view ID name of @+id/quantity_text_view

Run the app on your device.

What happens when you click on the Button?

Video Transcript

To build this layout, I open up the activity_main.xml file.I know that I need a vertical linear layout to put
each of these viewsin a vertical [Link], I'm going to first change the relative layout into a linear
[Link], it shows an error saying that the opening tag does not matchthe closing tag, so I can
just copy this over and then paste it down [Link] the tags [Link], I can add in the [Link], I
have a quantity text [Link] I'm going to take this text view and then change the text inside to
[Link], I have a text view that says [Link] I can copy the first text view and paste it below it.I'm
going to change it so the text says zero to match the screenshot [Link] last child in this linear layout is
a [Link] haven't added a button before in our layout, so let's Google Search [Link] you open up a
browser you can search for button [Link] first link looks good, it's a [Link]
[Link] here's the documentation for button, we can ignore most of this text [Link] scroll down to
read the class [Link] talks a little bit about some Java code, which we haven't learned yet butdown
here we have some xml for a [Link] can copy this and see what it does in our [Link] our layout I'm
going to add a button below these two text views.I'm going to paste the code from the documentation
[Link] now instead of the self destruct string, I'm going to go forsomething a little friendlier like
[Link] last line we can also delete because we don't need [Link], so now you have a quantity text
view, a text view that says zero, andan order [Link]'s run it on our app, to see what it looks like,by
hitting the green play [Link] can see down here that it's still [Link], this isn't what we
[Link] appears that our linear layout is actually horizontal instead of [Link] let's make sure that
we add the orientation [Link]'re going to change this to say android;orientation="vertical".That
should fix the [Link] let's hit run [Link], that's better [Link] three children are showing up
in a vertical [Link] we just need to style these fields sothey look a little bit more like the
screenshot we were given.>From this screenshot we see that the quantity text view is in all [Link]
lesson one we learned about an attribute called android:textAllCaps,and it's showing up here in the
[Link] it shows up like this, you can just hit enter andit automatically adds it in the code here
for [Link] set that value to be true because we want it in all [Link] reason why it's better to use this
attribute to capitalize the textis because if we ever want to change the UI so that quantity is in lower
case,like this, all we need to do is just remove this attributeinstead of going and changing this [Link],
for this text here that says zero, we should use a text size of [Link] me add that now and it shows up
in the auto-complete suggestions soI just hit enter and then I choose 16sp for the [Link] also want it
to be a black font color.I'm going to use the Android system color forblack, which is referred to like
[Link], that takes care of both the quantity and the zero text [Link] order button is fine as is, it
will automatically capitalize the text foryou in a button and the height and width are wrap [Link]
do have a need to resolve this vertical space [Link] want 16dp of padding or margin in between the
quantity andthe zero text [Link] also want also want 16dp of padding in between the zero andthe
order [Link] can implement this in many different ways,I'm just going to choose to add it as layout
top margin for this [Link] I'm going to add it as a bottom margin for this quantity text [Link]
free to do it a different way as long as it looks the [Link] reason why I chose to do a bottom margin
and a top margin is because Iknow that this middle area here is going to change [Link]'re going to
change it to be a quantity picker with a plus andminus button, and I still want to have 16dp of space
between the quantitytitle and the [Link] for the [Link], let's run it again now on our
[Link], and it looks just like we [Link] quantity text view is in all caps, the zero text view is
inblack font color, size 16sp, and we have the order button [Link] also have 16dp of space between
these [Link] job. Oh, and I almost [Link] quiz also asks for what happens when you click on
this [Link], actually it does nothing, for now at [Link] watching to find out what happens next.

1.6. Do Something When the Button is Clicked


Video Transcript
All right, enough [Link] you ready to look at your very first Java file?Well you actually have already
done some work to create that [Link]'s go back in [Link] the Wizard we used to create our
project?We chose the Empty Activity template which automatically createda new activity for [Link]
prompted us to provide a name for the activity andwe just accepted the default name of
[Link] activity represents a single focus thing that the user can [Link] an activity is one
screen and an [Link] times, a single activity can have multiple [Link] an activity is how
you can display your layout andinteract with the [Link] this case we also accepted the default name for
the layout,which is activity_main, which is an XML [Link] this MainActivity is displaying this layout [Link]
the Gmail app, for example, we have a [Link] activity displays a list of
[Link] you click on this hamburger icon, it opens up the navigation door, andyou can switch to
other views like the social emails, the promotion emails, [Link] though these go to different
screens,they're all part of the [Link] all revolve around viewing [Link] also
has another activity called Compose [Link] activity is for the user to create a new [Link] you can
see how each activity is focused on a certainthing that the user can [Link]'s another [Link] is
the sunshine weather [Link] is actually the project that you can build upin the Android fundamentals
Udacity [Link] main activity is the main screenwhich shows the list of weather forecasts for the
whole [Link] detail activity shows the details for the weather forecast fora single [Link] then the
settings activity is where you can go to change the location ortemperature [Link] you can see how
each activity is focused on a certain thing thatthe user can [Link] our coffee ordering app, we're just
going to have the [Link] of our app, the main activity forour app is defined in a file called
[Link] that the file extension, or the suffix here, ends in .[Link]'s how the computer
will know that it's a java [Link] the way on your computer it'll look something like [Link] is simply a
new file extension that you probably haven't seen [Link] file extensions you may be familiar with
are .xml, forthe activity_main xml file, or png or jpeg for image [Link] can find this file in your
computer by navigating through the files in yourapp in Android studio.A lot of these files were
automatically generated for [Link] look in the app folder under Java and then
[Link] is the name of our [Link] are also other files if you expand
out the gray arrows butwe don't need to worry about them [Link]. And then we can find
[Link] double click on that to open it.I'm going to slide this view over so that I can see
MainActivity [Link] here's all the code the Android studio automatically generated forus for the
[Link] Java code for the very first time can be scary and [Link] don't worry, it's
totally normal to not understand any of this.I felt confused when I first started [Link] I still get
confused when I look at files that I have never seen [Link] you see this you may just see a bunch
of random symbols [Link] blank spaces in weird [Link] words might be recognizable in
English, while others,just don't mean anything to [Link]'t worry though, we're going to understand all
of this code [Link] we will do now is modify the code that Android Studio gave us, sothat our app
does something when the order button is [Link] new code forMainActivity is included in the link
below in the instructor [Link] want you to copy over all of this code here, andthen in Android studio
go ahead and select all of this and delete [Link] paste the new version that we gave [Link] the code
that you just copied, notice that we included some comments as well.A comment follows the following
[Link] starts with these symbols and it ends with this, and it can go on foras many lines as you
[Link] purpose of a comment is to describe the code in some [Link] you can describe it using plain
English orany language that you want to [Link] it doesn't have to follow the format orthe rules of the
programming [Link] we have a comment up here to describe the purpose of the [Link]
we have comments on these methods here to explain whatthe individual methods [Link] you want more
information on writing comments in Java,just Google search [Link]'s aimed to help the person who's
reading the code, either you orother people, better understand the intent behind why that code was
[Link]'s also really good habit to always comment your code sothat when you go back to it, it's easier
to [Link] you want to learn more, feel free to click on any of these [Link] feel free to read the
comments that we left [Link], in order to make the order button do something in our app,we also
need to make one more change to the activity_main layout [Link] need to add an extra attribute to the
button XML [Link] attribute name is [Link] the value will be [Link] this exact
name so that your code matches [Link] remember the o, because this method name is in camel case
with no [Link]'t worry about what this means for [Link]'ll learn more about it in the next
[Link], if you haven't already done it, make sure that this TextView here hasan ID of
quantity_text_view.These changes will make your layout work with the newmain activity Java code that
we gave [Link] ahead and make these changes [Link] the activity main layout file, and then
replace the [Link] file with the one that we give [Link] then also run the app on the
device to make sure that it still [Link] all of this, if there are no errors,then can you tell me what
happens when you click on the button?Write your answer here in this text box.

IMPORTANT: Note if you copy and paste the code(opens in a new tab), you must add your package at
the top of the file (there's a comment about it). The package name can be found in the project's
[Link] file.

The package name our example uses is : package [Link];

If after you add the package and still see a line through some of the words, just ignore it for now. The
code should still work perfectly!

Here are the words we introduced in this video:

Programming language

Java

Event-driven programming

Activity

Comments

Package name

Import statements
Class name

onCreate()

You can look up their definitions in the Vocab Glossary(opens in a new tab).

Quiz: Do Something When the Button is Clicked

BUTTON CLICK

Modify activity_main.xml to add this Button XML attribute.

android:onClick = "submitOrder"

Replace entire [Link] file with the file provided in the link to the code.

Run app on device.

What happens when you click on the Button?

1.7. Modifying First Lines of Java Code

Udacity part of Accenture logo


Modifying First Lines of Java Code

Lesson

Downloads

Video Transcript

Hooray!You just made your app interactive for the [Link], when you tapped on the button, it didn't
do [Link] now, with some code changes,when you tap on the button, it actually updates
something on the [Link] job!Let's try to dig deeper into what you just [Link] you
added a new android:onClick= attribute on the button XML [Link] means when the button is
clicked on,then it will try to perform or execute as we say in Javaa set of instructions that have been
named submit [Link]. Going into the main activityfile that we gave you.I know there's a lot of weird
symbols and words on this [Link] all you need to know now is that this is a method andits name is
submit order.A method can contain multiple lines of java [Link] this translates into a set of
instructionsthat the device can understand and [Link] there are multiple lines of code in this
method,the device would perform the first line first and then the second line and soon until the bottom
of the [Link] reason why we give methods a name is so that we can easily refer to it [Link]
example, in the XML we can refer to the method submitOrder [Link]'s sort of how I have a name, you
have a [Link] you had a pet, you would also give it a name sothat you can easily refer to [Link] in code, if
we have a bunch of statements that we want to refer to,we can put it inside a method, and then give
the method a [Link] remember in lesson one how we told you that XML has very specific syntaxrules
that determine whether something is valid XML or [Link], Java has similar types of rules for
determining what is valid Java [Link] methods, the statements inside of them must be contained
within thisopening squiggly brace and this closing squiggly [Link] here, this is also a method called
[Link] statements inside this method are contained within this opening brace andthis closing
[Link], lastly, there's this method, which is called [Link] code statements inside of it are
marked off by this opening brace andthis closing [Link] also notice that there's also an opening and
closing brace forthe [Link] the functionality of the MainActivityis defined within this opening
and closing [Link] far, this MainActivity contains three methods inside [Link] after you declare a
method,which means defining the method, then you can actually use that [Link] this example, we
declare the display method, andthen we use it on this line of code, where we call it by its [Link] this is
where we declare the method and this is where we call the [Link] to summarize, when the button
is pressed in the UI,then it triggers the submitOrder method to be [Link] we execute each line of
code in the submitOrder methodfrom the top of the method to the bottom of the [Link] this case,
there's only one code statement in there, so we just run [Link], now let's look at this line of code
here that says display [Link] reason why I picked this line of code is because it seems likethe number
inside the parenthesis is what gets displayed on the screen [Link], when I click on this order button
here, in the UI,then it triggers the submit order method to be called in the main [Link] we
perform this line of code here that says display [Link] that updates the screen to show 1 [Link] if I
change this to be 2 here, what do you think will happen?Let's run the app to find [Link] now when I
click on this button, it shows [Link] on the order button triggers the submit order method to be
[Link] then we perform this line of code that says display [Link] that's what updates the screen to
show [Link] it's your [Link] you try changing this number here in parenthesis to a different number?
You can pick another small number, a big number, or even a negative [Link] you've
experimented with this line of code in the main activitythen check this box when you're done.

Here are the words we introduced in this video:

onClick

Execute

Method

Calling

Expressions
1.8. Solving Math Problems

Vedio transcript

Now that you've modified your first line of Java code,let's see what else we can [Link] can actually do
some math [Link] if you don't like that thought of math,well the good news is that Android
actually does the math for [Link] just have to come up with the [Link] you have two people
andeach of those two people wants two cups of [Link] this will display the total amount of coffee
that you need to get [Link] run it on our device and then we hit Order and then we see [Link] you
see how we can type in math expressions andit will automatically solve it for [Link] can also do even
harder math expressions likethings we wouldn't even want to calculate in our [Link] example, say
there's a whole group of Android developers andthey're really [Link] they each want to drink three
cups of coffee each,because they're not morning [Link] then we also want to throw in some extra
cups as [Link]'s a lot of [Link]'s see how much that actually turns out to [Link] hit the Order
button and it says [Link].I guess developers do write better code when they have a lot ofcaffeine
in [Link] a moment I'm going to have you type in your own math expressions [Link] a heads up, if
you run into any errors, for example,if you type a letter accidentally,then you will see a red error show
up on the right hand side [Link] error says it cannot resolve the symbol a becausea is not a valid
[Link]'s another possible error if you accidentally type the multiplicationsymbol multiple times
you would get an error [Link] this case it would be more helpful to to hit undo andthen your code
would work [Link] we mentioned in lesson one, you can try to read the error message
[Link], the error message doesn't exactly pinpoint the [Link] can also compare it to
working [Link] just showed you an example of a math expression that [Link] can also try to hit
undo to retrace your [Link], you can always ask for [Link], now it's your turn to try to come up
with a really hard coffee order forthe device to try to [Link] can experiment with different math
[Link]'s an example of [Link] you can see, you can use different symbols here or addition,
subtraction,multiplication and [Link] are what we call arithmetic [Link] you ever need to
refer to this later, you can alway search forarithmetic operators [Link] page lists the summary of all
the different operators in [Link] here are the arithmetic operators [Link] you want you can always
learn about other operators as [Link] this quiz though, we are just going to focus on the arithmetic
[Link] reason why we call this an expression is because it'sa bunch of symbols and numbers that
eventually evaluate to a single [Link] this expression is evaluated according to order of
operationswhich you might have learned in math [Link] there's anything in parentheses, it evaluates
that first to be [Link] then it does the multiplication and division parts from left to right.I don't
actually know what this evaluates to, butyou can try it in your [Link] you've become comfortable
with trying different math expressions,try this problem.I want you to display the number of coffees that
you would needIf there are 77 Android developers who drink two cups of coffee each andthen one
straggler walks in late and needs a cup of coffee, too

1.9. Add TextViews for Price

I want you to build up this layout on your [Link]'ll add one text view for the price header here
andthen another text view to show zero [Link] assign this second text view a view ID of
price_text_view.The reason why I ask you to add an ID is because we're going to need toreference this
view later so we can update the [Link] while you're at it, also make the style of these two text views
similar tothe quantity text [Link] terms of vertical spacing, we also have 16dp in between these
[Link] order for the price field to also update,we're going to need to make some changes to the main
activity Java [Link]'ll give you a little more help with [Link]'re going to provide you with a new
method that you can add in the mainactivity Java [Link] up the gist in the link provided in the
instructor [Link] the way, a gist means a snippet ora short region of reusable code that you want to
share with [Link] this case, we went to the GitHub Gist webpage, andwe created a new Gist with this
code that we wanted to give [Link] now we can just share this link with you, and you can get the
[Link] this snippet, we're going to select the whole thing, and then copy [Link] to include the
comment up here,which is a note to the reader and this closing brace down [Link] we're going to
paste it in the main activity Java file.I love free [Link] sure that the braces still line up after you
paste [Link] are a pain whether they're in your app or on your [Link] can get a ton of errors if your
braces don’t line up..For example, if I paste this code block right down here,then I get an error saying
class or interface [Link] reason is because this method is hanging out in the middle of
[Link], we want it to be inside the main activity between the opening andthe closing braces of
the main [Link], let's copy this and put it before the closing brace of the main [Link] that's
[Link] potential error is if you forget to copy the closing brace, andyou just grab this top part of
the [Link] that case,you would get another error that says that the closing brace is [Link] you
would need to add that back [Link] other potential error you might run into is if you accidentallyadd the
method right [Link] is before the closing brace of the display [Link] now this whole thing is
inside the display method,which is not what we [Link] want it to be beside the display method, right
out [Link] get the idea, there's a million [Link] just want to be careful and make sure that
your braces line [Link] good way to check is if you put your cursor right here,right after the
opening brace you can see that it highlights the closing bracethat matches with [Link] here, if I click on
this,the closing brace that matches with this is right [Link] for submitOrder method and onCreate
method and even the [Link] is the opening brace for the MainActivity, andthe closing brace is
highlighted in blue all the way down [Link] unfortunately, we still have another error [Link] I look in
the side bar here it says cannot resolve symbol number [Link] format is actually something
provided by Androidin order to help format [Link] this case we're trying to format the number
which is the price sothat it has a dollar sign, or whatever local currency you're [Link] need to add an
import statement forthis, because currently Android Studio can't recognize what this [Link] resolve this
I'm going to show you how to turn on auto [Link] the menu across the top of Android Studio,click on
Android Studio > [Link] opens up the preferences for Android [Link]'s a ton of
preferences that I've never changed, butif you ever need something I just search it using this search
[Link] I'm going to type in Auto [Link], it shows the settings forautomatically importing things in
XML and Java.I want to check this box here that says, Add Unambiguous Imports on the [Link] phrase
may not mean anything to you, but that's [Link] phrase probably sounds really [Link] it's
doing is telling Android Studio that it's okay to automatically addsome additional lines of code in our
[Link] import statements, if it knows what we're referring [Link] me show [Link] actually, it
already did it for [Link] assumed that when we said number format, we meant this one andit is [Link]
it solved the problem for us [Link] is [Link]. At this point, if you run the app,then
when you click on the Order button then the quantity changes, butnot the [Link] need to modify the
submit order method sothat it updates the quantity as well as the [Link] let's add another line of
code.I'm going to call the display price method and I'm going tosay that the price is two cups of coffee
times $5 [Link] reason why our coffee is $5 each is because we'veimported Arabica beans from
[Link] anyways, we're going to let the device do the calculation forus, because we have two cups
of coffee at $5 [Link] the way, when I refer to dollars in this lesson,feel free to swap it out for your
own local [Link]'s no dollar sign in this math [Link] gets added on later in the display
price [Link] don't worry about the details of the display price method,we're going to be going over
that in lesson [Link] now just focus on the math expression that you're going touse to calculate the
[Link] remember the semicolon at the end of each statement, otherwisethe semicolon monster is
going to come and get you and give you a red [Link] example, when you remove the semi colon, then
you get an error that says,that the semi colon is [Link], and to be extra, extra clear, the semi
colon key, is this one right [Link]'s not the colon, it's the semi [Link] right, so when you finish
everything,you should have two more text views added for the price [Link] should have modified the
main activity to includethe new method we gave [Link] you would've also modified the submitOrder
methodto add a new line of code to display the [Link] you run it on your device it should look
something like [Link] starts off at zero quantity for $[Link] when you tap on it, there's now two coffees
ordered for $[Link] free to experiment with different values for quantity and [Link] you want to
charge $10 for a cup of coffee, you can go right [Link] sure you switch it back to two cups of
coffee at $5 each sothat it matches our code going [Link] these boxes when you are done.

**Correction:**The quiz image at the end shows displayPrice(View view). It should be displayPrice(int
number).

Link to code for displayPrice method(opens in a new tab)

Make sure Auto Import is on:

For Windows, go to File > Settings > Editor > General > Auto Import

For Mac, go to Android Studio > Preferences > Editor > General > Auto Import

Check all of the following options:

Show import popup

Optimize imports on the fly


Add unambiguous imports on the fly

Insert imports on paste → All

Here are the words we introduced in this video:

Gist

Snippet

You can look up their definitions in the Vocab Glossary(opens in a new tab).

1.10. The Need for Variables

Okay Katherine, our app is coming along, but there is one major problem with it.>> What's the problem?
>> Well it only orders two cups of coffee.>> What's wrong with two cups of coffee?>> Well three is
better than two, four is better than three,sometimes I need more.>> Okay I get it, you need more
[Link] what I can do is just change the code, sothat instead of two it just says three.>> Okay, so in our
code here, we will need to changethe number of cups of coffee >> Here and [Link] what if I want ten
cups of coffee for myself and my friends later on?>> You have that many friends?Oh. Well, we need
something else, then.>> Yeah, we need a better solution.>> How about, oh, I know, we can use
variables.>> Variables?As in something that varies?>> Yeah, a variable doesn't have a fixed [Link]
can think of a variable like a box [SOUND] like this.>> Okay.>> Yeah, so this is a [Link]'s a box and
you can put a value inside of it.>> It says int.>> Yeah, int stands for integer.>> Okay, so you can put an
integer, or a number, inside of this box.>> Exactly!And you can even give it a name.>> Let's call it
number of cups of coffees ordered.>> [Link] had to pick a long [Link] Barely fits.>> Okay. >>
Okay.>> So now we can refer to this variable using this namein a code instead of putting the value
two.>> Okay.>> Later we can also update it if we [Link]'s put the value inside [Link] are we going
to do that?Well, I have some Post-its.>> Where did those come from?>> Let's call the number of cups of
coffees ordered to be 2.>> Why only 2?>> One for me and one for my friend, Katherine.>> Thank you.
[LAUGH] So now we have a variable that is called numberOfCoffeesOrdered andit holds the value 2.>>
Let's implement this in our code.>> Before we were using a literal value to representthe quantity of
coffee cups ordered.A literal means a fixed value, like the number [Link] the 5 and this 2 are also
literal values butwe're just talking about using literals for the quantity right [Link] if we say the
quantity is 2 cups of coffee ordered, thenwhen we calculate the price, we have to do 2 times 5, since
each cup is $[Link] we want to charge the customer for a paper cup because they didn't bringtheir own cup
and they like to waste trees, then we charge them $2 per [Link] that's 2 cups times $[Link] we use a
literal, you can see that it's not ideal because we have tokeep repeating the quantity number over and
over [Link] the other hand,we can use a variable to represent the quantity of coffee cups [Link]
if we say the quantity is this variable called numberOfCoffees,then when we calculate the price, we can
just use that variable name,numberOfCoffees, and multiply it by $5 a [Link] calculate the charge for the
paper cup, again we take the variable name,numberOfCoffees, and we just multiply it by 2, for $[Link] the
difference between these two is that instead of the actual literalvalue, we use a [Link] then the
value of that variablewill be substituted when we actually evaluate this [Link] when we go and
calculate the price,we just have to go check what this value is before we multiply by [Link] one
additional step that you need to do when you use a variable is,in the beginning,to set the variable,
numberOfCoffees, to have an initial [Link] this case we set it to an initial value of 2, which is also a
[Link] this time we only use the literal in one place instead of in multiple [Link] end result is still
going to be the same,because of numberOfCoffees will map to the value [Link] the quantity will still be 2,
the price will still be 10,and the paper cup charge will still be [Link] real advantage to this approach is
that you canchange the variable value at any [Link] I want to change the numberOfCoffees to be 3.I
just need to change it in one place here andthen everything else is still [Link] quantity is still the
numberOfCoffees,which now has a value of [Link] price is now numberOfCoffees times [Link] the value
is 3 times 5, andthe paper cup charge is still numberOfCoffees times [Link] is now 3 times [Link] we were
using a literal,then we would have to change the quantity to be 3 [Link] would have to update the
price calculation to be 3 times [Link] the paper cup charge to be 3 times [Link] when we use a variable,we
don't have to refer to the specific value each [Link] can just refer to the variable name andthat will
automatically get us the latest value in that [Link] can look more closely at what the actual code
would look like forcreating and using a [Link] create the variable, we say int numberOfCoffees =
[Link] creates a variable for us with this name numberOfCoffees andit puts the value 2 inside that
variable [Link] next two lines use the variable that we created on the first linein order to update this
[Link] can display the number of coffees, which involves looking at that variableand checking the
value inside, which is [Link] we show 2 [Link] display the [Link] look at the value of the variable,
numberOfCoffees, which has 2 [Link], we say 2 x 5 equals 10, and so the price is $10.I can easily
change this code, sothat the numberOfCoffees ordered is 3 instead of [Link] the box that represents our
variable, numberOfCoffees,now we put 3 as initial value inside this [Link] two lines of code
don't have to change though,because we are using the variable [Link] we called display
[Link] value in this variable is [Link] we show the quantity [Link] display price we look at
the value for the numberOfCoffees which is [Link] we multiply that by 5 so that gives us $[Link] is what
the code change would like in Android [Link] it's using the literal value of 2 for the
[Link] let's create a variable, I'll type int numberOfCoffees [Link] now I have a variable with the
value [Link] the way, this text is grayed out and it says variable number of coffees isnever used because I
never referred to it again, but I am about to use it [Link] of displaying the literal value 2,I'm going
to switch it to use the variable [Link] pops up as an autocomplete option, so you can just hit Enter
andit fills it in automatically for [Link] this variable name is no longer gray because now we've used
itin at least one place.I can do the same for [Link] of the using the literal value of 2, I'm
going to changeit to be the variable numberOfCoffees, and I just hit Enter to auto [Link] I
could also create a variable for the price value of [Link], it's only used in one place soI might as well
just leave it as a literal [Link], now let's save the code and run it on the [Link] this is what it
looks like on the [Link] you hit Order, the quantity is 2, and the price is $[Link] is exactly the
same behavior as before, but now our code is more [Link] means that the code is less prone to
errors in the [Link] example, if someone else is trying to change this code,and they were trying to
use the quantity in many other places,then they would have to change the number 2 in so many
different [Link] if we used a variable, numberOfCoffees, then we only have tochange it in one
place and then everything will automatically [Link] way someone won't mistakenly miss a line of
codethat needs to have the latest numberOfCoffees [Link] only have to change it in one single
[Link] before you go ahead and do this in Android Studio,I want you to to first practice doing it in
this [Link] ahead and type out this line of code right below [Link] line of code will create a new integer
variable, andthese two lines will use the integer [Link] reason why I want you to practice this first
is that you can payattention to every single character when you type [Link], if you miss a
character, you may get an error in Android Studio,and it may be hard to read the error [Link] go
ahead and type it exactly as you see [Link] is helping you become aware of every single character
here,whether it's a capital letter or a lower case, or if there's no spaces orif there is a [Link]'ll also
start getting used to the rules of Javain terms of creating a variable or using a variable, and as you
learned before,every Java code statement also has to end with a [Link] you type this incorrectly,
for example,the variable name, that would give you an error in [Link], other things are more
conventional and best [Link] example adding a space before and after operators like [Link] you
don't follow this spacing, it won't give you an explicit error howeverit's just better programming practice
to follow these code style [Link] way it's easier to read your code,when you're done typing
this out go ahead and continue to the next video.

1.11. Add Your First Variable

Video Transcript

This line of code's responsible for creating a [Link] that a variable is like this box, number
of coffees,with a value of two [Link]'ll talk about each part of this line of code andthen we're going
to create more variables in this [Link] may have noticed there's specific rules for how to declare a
[Link] I say declare a variable it means the same thing as defining a variable orcreating a
[Link] is the line of code that we've seen so [Link] follows a very specific format for how to declare
variables in [Link] could think of it almost like a [Link], we need to say what type of data can
go inside the [Link] this case, it's an integer, which we call int for [Link] is the variable [Link]
our case,the variable name is numberOfCoffees followed by the initial [Link] this case it's 2; for 2 cups
of coffee ordered, andthis code statement ends with a ; according to the rules of [Link]'s break down
this line one piece at a [Link] first word is Data type,this is a type of data that's allowed to go inside at
this [Link] this case we only let integer [Link] stored inside the [Link] I mentioned
earlier int is short for [Link] integer is a whole [Link] any number including zero
andnegative numbers that are not fractions or decimals.0, 5, 145, all of these are examples of
[Link] number of coffees ordered should only be a whole number like one,two, four, et [Link]
don't want the customer to order 1.5 cups of [Link] they want more than one cup, they're going to
have to buy two,then they can give the remaining half to a friend or [Link] for negative numbers,
it doesn't make as much sense in our [Link]'s no data type in Java that only allows positive
[Link] later in the next lesson, we're going to learn how to add codethat prevents negative
numbers from going inside the [Link] next part of the declaration is the Variable [Link] this case
it's [Link] that it's spelled camel [Link] starts with a lowercase letter and since there
are multiple words,it capitalizes the start of each other [Link] can think of it as putting a name tag on
the [Link] can refer to this variable with this [Link] here I've drawn it out like this as [Link]
variable box has this [Link] you want to know what this value [Link] change it, you need to use the
[Link] an appropriate variable name is actually up to you the developer,depending on what the
variable is used [Link] this case, since the variable is storing the numberOfCoffees orderedI just picked
this variable [Link] could have picked other names, like coffee count ornumber of coffees ordered or
ordered [Link] it could be anything, as long as it's easy forother people to understand what this
variable [Link] I say that the variable name can be anything,there's actually a couple of rules
that Java has about variable [Link]'s look up those rules [Link] me type in variable names
[Link] first link looks [Link] is official Java documentation from [Link] top part talks about
variables,which you can read in your infinite free [Link] here is what we're interested [Link] talks
about naming for [Link] can be any length, but a really long name is not very practical.A really short
name, on the other hand, can be good, butif it's only one character, like N, you may not know what n
stands [Link] you want to strike a balance between a descriptive name andone that isn't a pain to type
out every single [Link] are a bunch more details here that you can read if you [Link] the main
point is that if your name only consists of one wordthen spell out that word in all lower case [Link]
cadence, speed, gear, things like [Link] the variable name consists of more than one word then you
want tocapitalize the first letter of each subsequent word butit needs to start with a lower case [Link]
gear ratio is an example or current [Link] example of number of coffees also follows this [Link]
next part of the declaration is Assignment operator or the = [Link] math class, you're used to seeing
things like one plus one equals [Link] in Java, equals means a different [Link] means assigning a
value to a [Link] this case, we're assigning the value 2 to the variable [Link] our
box metaphor,we put the number 2 inside the variable that represents number of [Link] whenever
you see the equals sign,think about grabbing what's on the right-hand side of it andputting it inside the
variable on the left-hand [Link] already talked a little bit about this, butthis two here is the initial value
of the [Link] you put the 2 inside this [Link] that this initial value must be of a valid
typeaccording to this data [Link] this case only integers are allowed in this variable.2 is a valid integer
so it's okay for it to go inside this [Link] last but not least, we have this ; at the very [Link] to
summarize, when you declare a variable,you need to specify the data type, followed by the variable
name,and then an equal sign, and then an initial value, and then a [Link] this case, we're creating
a variable called [Link] data type is int, so only integers can be stored in this
[Link] we're assigning the value 2 to be the initial value of this [Link] you can change the
variable name according to the rules we talked [Link] you can change the initial value as long as it's
the same data [Link] can't change this data type, it needs to be spelled int, [Link]'re going to be
learning about other data types as well,and those have to be spelled a specific [Link] any time you
need to use a variable, just refer to it by it's name, andthen it will be swapped out forthe actual value
later when the app is actually [Link] I want you to go ahead andadd these lines of code to your
app, please don't copy and past though,because I want you to practice typing out, creating and using a
[Link] go ahead and run the app on your [Link] sure that when you hit the Order button,
this is what you [Link] that, I want you to do a little bit of [Link] to assign a new initial
value to the [Link] go ahead and try to assign a new variable [Link] of
numberOfCoffees,try something else like quantity for example.A common beginner mistake is to think
that you typed it out correctlybecause your eyes are scanning over the whole thing really [Link] if
you look individually at every single character youwould notice that there might be a [Link] will
take you some time to finish but take your time andpay close attentions to each character that you
[Link] you run into problems, try reading the error [Link] example, this error message says
cannot resolve symbol [Link] doesn't recognize what numberOCoffees isbecause the
variable name was actually [Link] you can figure out from the error or you can hit
undo andthat sometimes resolves the error as [Link] you can go back to code that you know works, like
the code that we gave [Link] you're done, check these boxes and continue.

Here are the words we introduced in this video:

Data type

int

Variable name

Assignment operator

Initializing

You can look up their definitio

1.12. Sometimes things don’t go as expected

First Things First

Awesome job on creating your first variable! We saw that creating a new variable could be done in one
line of code. If we wanted to show the value of that variable, then we were able to display it on screen
in a TextView.

As we continue modifying more Java code, it’s completely normal to try something out, and have the
app unexpectedly crash.

1.13. Debugging a Crash

Video Transcript

A crash in an app is when it unexpectedly quits andshows a dialogue message saying that the app has
just [Link] happens to Android developers many, many times a [Link] the most experienced
[Link] the problem is due to a typo in their code, or a silly [Link] times it can be due
to a fundamental misunderstandingof how to write the code for a certain [Link] your reaction
may be to panic, the voice of reason inside your headshould tell you yo go ahead and try to debug the
[Link] this video we're going to learn how to debug Android app [Link] debug means to identify
and fix bugs or errors in your [Link] it's a really obvious error like a java syntax error,then Android's DU
can catch that for you andshow a red error before it tries to install the app on your [Link] is called
the compile time error because it will fail to create any appto even install on your [Link] you want to
try creating a compile time error, go ahead and just deletethis parentheses or this semicolon and then
you'll see the error pop [Link] is like in lesson one when we tried to create an XML error
[Link] then we read the error message and then we undid the [Link] potential errors
are if I try to delete this opening brace, then I'll getan error, or if I forget this closing brace, then I also
get an [Link] sometimes the error only happens when you start using the [Link] is called a run time
error because it happens when your app is [Link]'s intentionally create a crash in the app andfigure
out the steps of how to solve it, so that when you have a real crash,you'll know how to tackle it on your
[Link] is actually based off of a real crash that I saw a student [Link] it's very likely to happen
to beginners.I'll first walk you through all the steps andthen you'll have a chance to do it [Link]
intentionally create a crash in the app,I'm going to modify the MainActivity Java file.I'll change the name
of this method to be submitOrders with a [Link] I'm going to run it on the [Link] looks fine
at first, butthen when I click on the ORDER button, it [Link] gives me the message, unfortunately, the
app named, Just Java has [Link] the only option that I have is to hit OK to exit the
[Link], we can use Android Studio to figure out what actually [Link] you run the app
on your device,you may see this Android DDMS window pop up at the bottom of the [Link] show
you information to monitor your Android [Link] the stuff that goes on behind the scenes that users
don't [Link] it's not already open, you can click on this tab down here that says Android,and that should
open up Android DDMS for [Link] can adjust the height of this window by dragging this [Link] you
drag this arrow to the right,then you'll see a list of devices that are connected to the [Link]
now I only have my Nexus 6 [Link] I also opened up the Just Java app again, so that's why it's
listed [Link] me drag this to the left again so I have more space to see the [Link] right hand side is
where the log messages are [Link] you click on this drop-down and say no filters,then it shows you
the log messages for the whole system and other [Link] talks about things like activity manager, audio,
sound,microphone, and all these other things.A system log is kind of like a diary that your phone keeps
about what'shappening on the [Link] also call it [Link] you Google forwhat is logcat, then a
definition pops up from the Android Developers [Link] talks about how logcat is the Android
logging system,where you can collect and view Debug [Link] back to the logs, the most recent
events happen at the very bottom [Link] if you want to look back in time,you may have to scroll up to
see what you're looking [Link]'s change the filter back to our app, Just [Link] significantly narrows
down the amount of log messagesthat are just relevant to our [Link] makes it a lot easier to
[Link] a crash happens on the device,the description of what the phone was trying to do gets
printed to the [Link]'s our crash in the red text [Link] is known as looking at the stack
trace,because it traces back to where the error originated [Link] know that it's our app because it's
our process,which is [Link] our app, the process name matches the package
name, butit doesn't always have to [Link] haven't really learned about processes,but, essentially,
this is just identifying that this is our [Link], there's a lot of text in this stack [Link] you don't have
to read all of [Link] just want to scan forthe information that would be relevant to help us solve our
[Link] should look for anything in the stack trace that's talking abouteither the methods or the
files that we're working [Link] we'll keep a lookout for the file name, main [Link] let's start reading
the error message.I skip over this part andjust read, IllegalStateException, Could not find the method
[Link] error message goes on to further explain why it was in an invalid [Link] the reason
is because it could not find the method submitOrder inthe activity, MainActivity, forthe onClick handler
on the view class [Link]'s a lot of words in that sentence and it may not make a lot of [Link]
there's a couple of good clues [Link] mentions button and we only have one button in our app, sowe
can assume that it's probably related to that [Link] it talks about not being able to find a
method,submitOrder, in the [Link] back at our button code, we did specify that when the
button is pressed,it should trigger a method called [Link] let's look at that now.I'm going to
drag this The DDMS window down andopen up our activity_main.xml [Link] here is the button XML
[Link] indeed we said android:onClick should trigger the method [Link] I go back into the
main activity,then I see that submitOrder is no longer a method, instead,we changed it to submitOrders,
so that's why the problem is [Link] I go ahead and erase the extra s, now there's a submitOrder
methodin the main activity, and the app should work [Link] I run the app again andI and I hit the
order button, then the app doesn't crash anymore, [Link] ahead and try to create this crash in
your appby changing the name of the submitOrder [Link] check the logs for the stack trace,
andthen read the error message [Link] go ahead and fix the error so that your app works
[Link] this box when you're done.

DDMS stands for Dalvik Debug Monitor Server, and is a tool in Android to help you debug your app. For
more info, click this link(opens in a new tab).

Here are the words we introduced in this video:

Debug

Crashes

Compile time error

Runtime error

System log

Stacktrace

You can look up their definitions in the Vocab Glossary(opens in a new tab).

1.14. Chatting with Google's Alice Yang, Part 1

Hello. You all know [Link]'ve been making the course [Link], we have a really special
guest for [Link] works for Google and has been working there for->> 11 plus years.>> 11 plus
[Link], we're going to talk about Alice's journey in terms of her first interview,how she solves bugs,
and some advice Alice wouldgive herself, from way back when, when she was wanting to learn
[Link] we begin.>> So, Alice, can you tell us about your role at Google now?>> Sure. So,
right now, I'm a TL fora new [Link] is for tech lead.>> Oh, okay.>> For a new project on [Link]'s
in the very early stages soI can't really talk about exactly what the project is.>> [LAUGH] >> Wow.>> But
I can tell you a little bit aboutwhat I've been doing the last few years.>> So, let's start at the very
beginning.>> The very beginning.>> So, you come in and what happens the first few years.>> So, the first
six years at Google, I was in AdWords, AdWords front [Link], AdWords is basically how Google makes
[Link], when you do a Google search,all those ads that pop up on the right hand [Link], I worked
on, and AdWords back then,it was like the first group at Google who was doing Java,all the other
engineers were C++ engineers, like on search.>> Oh, I didn't know that.>> Yes, so, we were the first Java
[Link] we set a lot of standards about how Java was going to,setting the Java coding style, for
example, and what tools to [Link] yeah, it was very fun.I think it was, back then, it was small.I
remember carrying the pager for [Link] weekends, all the engineers take turns carrying the
[Link], if a server goes down, you have to be prepared to log in,reboot the [Link] was before we
had [Link] means site reliability engineers, I believe.>> Okay.>> So, yeah, it was very different from-
>> I had never imagined thatwhen a server goes down there are literally pagersgoing off- >> Oh yes.>> In
the background where people are sort of hustling to try andget the machine up again.>> Yes, they can
go off like any time of the day, 4:00 AM.>> Mm. >> So,it's kind of like a doctor being on call.>> Mm-
hm.I'd imagine that as thrilling as it was foryou to get the job, some parts of the job may be
[Link] this idea of this pager is going to go off andinterrupt the fun that you were perhaps
[Link] you recall any instances where you felt like this is really stressful andhard work that we are
doing?>> So, I think when I was on Android Gmail,I think that was actually pretty stressful time of being
TL.>> Okay, so we moved on from AdWords, so you're now doing Gmail, but for Android.>> Android,
yes.>> Okay. >> So, actually, Catherine andI worked together on Android.>> Oh, nice! >> That's how we
met,that's [INAUDIBLE] [Link], yes, so, Android is very different from AdWords.I think mainly
because it's a consumer product, soit's just a different set of users, it's a lot more [Link] on Gmail
particularly, because it's one of the most used apps on Android,just the pure number of bugs that we
get is just [Link] Google we have this thing called dog [Link] means if your app is ready to
release, your new version,then you test it internally, among all the [Link] you kind of dog food
your own products, eat your own dog [Link] for Gmail, every time we dog food a new version people
will be sopassionate and found so many [Link] move one button from top right corner to the bottom
right corner, and therewill be like 20 people complaining about it changes their work flow, for
[Link] I think making those decisions was really tough,because it's hard to find a solution that
pleases everyone.>> So, it may be surprising for some of our students to understand, butthis idea of
feeling frustrated because a bug has been encountered,is not something just the novice programmers
are [Link] expert programmers like you Alice can get bogged down by [Link] you have a
strategy or tool you follow when you say come across a bug?>> So, it depends on the type of [Link],
bugs can be very different, the most Iguess important or high priority bugs are crashes, for [Link]
app is just not [Link] those are probably ones that will get looked at [Link]'ll flag that as P0s
or P1s, andalso it depends on how reproducible it is.>> P, in that stands for priority, I imagine.>> Priority,
exactly, yes.P0, P1, P2, >> An engineer who received the bug,think of a bug, andwhat would be your
strategy to going about fixing something like that.>> Bug fixing is one of the most, it's like a very easy
way, orquick way to feel [Link] fix this bug and it's very rewarding because I can mark it as fixed andit's
like I accomplished something.I think usually, first off, is trying to reproduce [Link], if it's reproducible,
then it's much easier to fix.>> Mm-hm.>> The hardest bugs are the non-producible ones,something that
on Catherine's Android device you saw, butthen on my phone I was never able to [Link], that's
much [Link], if it's reproducible, then I can easily just look at the logs,ADB logcats on Android
[Link] then see what [Link] it's a crash then there would be a very obvious stack trace.>> So,
you're like an investigator, you're looking at clues forwhy this thing might be breaking?>> Exactly.
[Link] Android bug reports are actually notorious forbeing really cryptic and hard to [Link].>> It's
not just us.[LAUGH] Oh no, yes.>> I find them to be very verbose and cryptic also.>> Yes, I think it's one
of the big challenges [Link], that's where experience comes [Link], if you're an experienced Android
programmer,you know exactly which keywords to search [Link]'s impossible to just read line by line by
line.I usually just like prep for or search for certain keywords.>> I see. So,you're scanning through the
entire log and you're looking forkeywords that you can Google.>> Yes. >> Okay,why are you Googling
these things?>> Well, I do a search locally, just like a [Link] Google comes in handy because often if
there is an error andit's something I'm not familiar with, it could be like Android Framework doingsome
illegal state exception or something that I've never seen before.>> Mm-hm. >> So, the first thing I would
do isactually Google it.>> Okay.>> And yeah, and usually on let's say, stack overflow,there's almost
always somebody else who has encountered the same.>> Is to follow similar processes.>> Yeah. >>
Where they find a bug,they try to identify a key word that they can search on Google?>> Yes.>> And try
and interpret those results, and try and fix the [Link], what's really satisfying, at least for me as a
teacher,here to learn is that the process that we're teaching our students is beingfollowed by an expert
11 years into their program.>> Yeah, yes.[LAUGH] >> And you mentioned a statement whichwas really
emphatic to me, which is, if I don't know something.>> Mm-hm.>> And that's really important to
understand, that even after 11 years ofexpertise, there are so many things that one might not know.>>
Yes, it's impossible to know [Link], yeah, I realize, there's something new to learn every day
actually,and at my job I feel like I do indeed learn something new every day.

1.15. Hook Up Two Buttons

I think you're ready for the next step of building the quantity [Link] our order form we're going to add
a plus and a minus button sothat we can increase and decrease the number of coffees [Link]
time you hit the plus button,it will increase the number of coffees by [Link] each time you hit the
minus button,it will decrease the number of coffees by [Link] when you hit the order button,it will
update the price according to the latest quantity [Link] up all of this functionality properly will
take multiple [Link] the first task we're going to add the plus andthe minus buttons to the [Link]
we'll also hook up the buttons sothey actually update something on the [Link] even later in the
lesson we're going to fix the order button sothat the price updates [Link], build this layout by
modifying the activity-main layout XML [Link]'ll add the two buttons and also please change the initial
value ofthe quantity to be 2 and the initial price to be $[Link] can make these changes in the XML as
[Link] can adjust to your local currency where [Link] reason why I'm choosing two as a
quantity is sothat when you hit the minus button, it doesn't go [Link] you add these buttons to
your layout you'll notice that they appear tobe a little bit bigger than they are showing up [Link] can
achieve this look by setting the width and the height to be 48 [Link] hook up the on click
[Link] the + button is clicked, call the increment method in the main [Link] the- button
is clicked call the decrement method in the main [Link] the main activity add an increment
[Link] that method create a quantity variable andinitialize it to the value [Link] I say initialize
it just means to set the initial value of the variable tobe [Link] go ahead and display the quantity to the
[Link] the third step in the main activity also add a decrement method andcreate a quantity
variable that's initialized to the value [Link] go ahead and display the quantity to the [Link]
you've completed all those steps this is what it should look likeon the [Link] you open the app it
should have a quantity of 2 and a price of $[Link] you hit the plus button, it increases the quantity to [Link]
you hit the minus button, then it decreases the quantity to [Link], that behavior is incorrect
because it should've gone down to [Link] for now, just make the plus button always change it to 3
andthe minus button always change it to [Link] a later coding task, we're going to fix it sothat every time
you hit the plus button, it will keep increasing to 4, 5,6, etc., and same for the minus [Link] now,
you can leave the price and the order button as [Link] now, my version just updates the quantity to 5
and the price to $25, butyou can leave it as whatever you had [Link]'re also going to fix this [Link]
a helpful tip, if you ever want to go back to how the appwas when it first launched, you can always kill
your [Link] the system navigation bar, there's a couple of buttons, back, home, andrecent [Link] you
hit this recent tasks button,it shows all the recent tasks that you are doing on your [Link] you swipe to
the left orright it kills your app, that means it stops it from [Link] you go into the all apps screen and
then click on your app tolaunch it again, then it shows the initial state of the app

Hint: To create the increment and decrement methods in MainActivity, copy the submitOrder method
and change its name. Then modify the code inside the method to do exactly what you need.

1.16. Debug Mode in Android Studio

We're making really good progress so [Link] we continue to build more challenging things forour app,
we're going to have to improve our debugging [Link] this video,we're going to learn about how to use a
tool called the [Link]'ll access the debugger using Android [Link] the debugger is meant to
help us identify and fix errors in our [Link] great thing about the debugger is that we can pause the
app at a certainpoint in time, and then we can inspect the whole state of the app more [Link],
when the app is running on our device,all the code gets executed very [Link] milliseconds, it can
respond to button clicks, update the screen,calculate the price, and [Link] with the debugger, we can
pause at a specific line of code in our app,then we can step through our code line by line as quickly or
slowly as we [Link] you want to learn more techniques on how to debug your app in Android
Studio,just search for debugging Android [Link] first link is [Link]'s a [Link] page,
andit has a bunch of techniques on how to debug your [Link], we're going to learn about how to
work with [Link] this course,you can always come back to this page to refresh your
[Link] this exercise I want to show you that your Android device does runeach line of code one at a
time from top to bottom within a [Link]'ll also verify that clicking on the plus button does trigger
the incrementmethod, and clicking on the minus button does trigger the decrement [Link] add
something known as a breakpoint.A breakpoint marks a specific line of codewhere the debugger should
pause when it reaches [Link] red circular dot indicates that the breakpoint ison this line on line 25 of
our main activity [Link] can add breakpoints to as many places as you want in your [Link] for
now,I'm just going to add one on the first line of the increment [Link] I can hit this bug icon to
run the app in debug [Link] app will only pause on these breakpoints when the app is runningin
debug [Link] you run the app normally with this green play button,then it won't pause at these
[Link] a brief moment, on the device, you may have seen a dialog that says,waiting for
[Link]'s also a button to force close the app, but don't click on [Link] wait for the debugger
to attach and the message will go away [Link] the app starts like normal and you can interact with
[Link], in debug mode, this window will pop up and show the status of the [Link] you don't see it,
you can click on this tab down here called [Link] I hit this plus button here, I expect the increment
method will be triggeredand that the app will pause at this [Link]'s see what [Link]. It
stops [Link] the debug pane, we see a list of [Link] reflects the current state of the app where
it was [Link] don't see our quantity variable yet,because it hasn't finished executing this line of
code [Link]'s a bunch of options on how to proceed with [Link] I'm going to click on this
option that says step [Link] will step over the current line of code to the next line of [Link] the
app is currently paused at line [Link] finished executing line 25,so that's why we see the quantity
variable showing up down [Link] list of variables also shows the current value of each [Link]
can also verify that the quantity variable has a value of 3,which we assigned up [Link] know that the
app is currently paused at this point,because normally when you hit the plus button,it would
automatically update the quantity to be [Link] since the quantity is still a 2,we know that it hasn't
finished executing the increment method [Link] can click step over to go to the next [Link] this point,
the method is [Link] we're not really interested in how it actually updates the screen, solet's just hit
resume [Link] we care about is that it actually does update the [Link] you're interested, you
could have clicked on these other options to lookat more details of how it actually updates the [Link]
this point, the app is running normally,but the debugger is still [Link] if I hit the plus button again,
it will stop at this [Link] I want to detach the debugger so that it doesn't always stop on this line
whenI hit the plus button, I can just click on this red stop [Link] whenever I hit the plus button, it
responds immediately andupdates the value to [Link] I want you to try it on your [Link] ahead
and add a breakpoint to the increment and decrement [Link] run the app in debug mode, and
then on the device,try to trigger each of these [Link] step through each line of code, making
sure that the quantityvariable shows up with the right value in the variables [Link] can click on this play
button to resume execution of the app sothat it runs as [Link] you can hit

1 17. Update Data

Hey.>> Hey. >> What's going on?>> Just wait for it.>> Okay. >> [Link], you know how we have a
variable called number of coffeesordered in our app.>> This box. Yeah.>> And we had the value two in
it.>> Correct. >> If I hit the plus buttonin the app now.>> Beep >> Thenwhat is the number of cups of
coffee we have?>> Well, that would be, increment by one, so the new value should be three.>> Yeah. So
I'm going tothrow out this old value and put in this new one.>> Okay. >> If I hit the plus buttonagain- >>
Beep!>> Then how many cups do I have?>> Well now we will increment that to four.>> Yep. >> So throw
this out, add four in.>> Okay. >> Now, if I hit the minus button->> Beep beep beep>> Then how many
cups ofcoffee do I have?>> But Catherine I thought you said there would be no math?>> It's [Link]
that [Link]. >> [Link] it [Link]. >> Yep.>> Let me give that to you.>> Thank [Link]'s add all
of this into our app now andwe're going to work on your math later Let's look at the pseudocode for
howclicking the plus button in the app can increase the number of coffees [Link] is a high
level description that explainsthe steps of what the app will [Link] can write it out as if we're describing
it to someone,because it's meant for people to read, not [Link] helps us put our thoughts into
a linear flow of what needs to happenone step after the [Link] writing pseudocode we don't have
to follow any specific structure orformat, we just need to include useful details that would help us
writethe code later [Link] our scenario, we want to create a quantity variable to keep trackof the
number of coffees ordered and we initialized the value to two [Link] the plus button is pressed, we
can update the quantity variable to [Link] we hit the plus button several more timeswe can increase
the quantity to four and [Link] we have a conceptual idea in our head of what the code should
do,then we can focus on writing the actual Java [Link] this point then we can worry about the syntax
of Java, the capitalization,the symbols, the spacing andother specific rules about how to write Java
[Link] real code would look like this to create a quantity variable andset it to two, we would have int
quantity = 2 followed by a ;.This is what we covered in the last [Link] update the quantity variable to
3 we just need to say quantity = 3 followedby a [Link] we update the quantity to 4 and 5, this is
what the code would look [Link] this quiz I want you to practice typing out these lines of codein the
text fields [Link] will give you practice in creating an integer variable andupdating it in [Link]
close attention to each character and symbol as you type this [Link] you're done, do you have any
observations about the format ofthe code that you just typed out?For example, do you recognize any
patterns?Enter any observations in this text field

here.

1.18. Update a Variable in the App

In this video we will apply what we learned about updating a variablewithin the context of the Just-Java
[Link] a variable is created we can update its value using assignment operator,that we learned
earlier when creating [Link] format is to have a variable name on the left, followed by the equal
sign andthen the new [Link] statement also ends with a semi-colon as [Link] line means
that,we're assigning the new value of 3 into the variable called [Link] back to our box analogy,
int quantity equals 2 creates a newvariable called quantity, and puts the value 2 [Link] we say
quantity equals 3, we set a new value of 3 inside the [Link] also take out the old value,because the
variable can only hold one value at a [Link] we say quantity equals 4,then we put in a new value of
4 inside the [Link] also remove what use to be in there, which is the [Link] when we say quantity
equals 5, we assign the variable a new [Link] that we only need to specify the data type of the
variable,which is integer in this case, [Link] subsequent lines of code where we refer to the
variable,we don't need to specify that data [Link] we did write int again on that 2nd line,then the
computer would think we want to create another quantity [Link], we're not allowed to
create two variables with the same name andthe same method, that would give an [Link] when you
declare a variable, that's when you specify the data [Link] you update it, you don't need to specify
the data [Link] I'm going to open up the main activity Java file and modifythe increment method to
take advantage of the ability to update [Link] the plus button is clicked it will trigger this
method, andI'm going to initialize the quantity variable to be [Link] I'm going to add a new line of code
that updates the quantity variable to [Link] we display the latest quantity value to the [Link] we
run it on our device,if I click on the + button, then it updates to a quantity of [Link] is because the latest
value in the quantity variable is 3 bythe time we try to display it on the [Link] do we know that the
app didn't just skip to setting the value to be 3?Well, we can use the debugging tool that we introduced
earlier,to check the value of the quantity variable at each [Link]'s add a break point in the first line of
the increment [Link] I can run the app in debug [Link], Android Studio says that the
debugger has been connected.I can click on this Debug tab to open the debug [Link] when I click
on the plus button, it triggers the increment method andthe break point at line [Link] this point the first
line has been executed, sowe've created a quantity variable with a value of [Link] we skip to the next
line,then quantity equals 3 would have already been executed, andthen we do see that the quantity
variable has been updated to [Link] this point it should display on the screen, andwe can just hit Resume,
to resume the [Link] then you see that the screen has updated to the value of 3.I'd like to show you
more examples of updating the quantity [Link] though it doesn't make much sense from a
functionality point of view ofthe app to have the plus button increase the number of coffeesby more
than one [Link] a code perspective if you wanted to, you could add additional lines ofcode to update
the variable several more [Link] run the app in debug mode [Link] I click on the + button, then
the break point will be [Link] I step to the next line then this first line has finished executing,and
we've created a quantity variable with an initial value [Link] go to the next line then quantity updates to
a value of [Link] the next step quantity variables updated to a value of 4 and then [Link] I hit resume
execution, thenthe screen will finally update to show the latest quantity value, which is [Link] on your
computer make these changes to the increment method in the mainactivity [Link] use the debugger
to step through andverify that the quantity variable is being updated as [Link] repeat the above
steps but for the decrement [Link] that method, initialize the quantity variable to 2, andthen
update it so it goes down to one cup of [Link] you run it on your device, the app should open
andstart with a quantity of [Link] you hit the minus button, then it changes down to a quantity of [Link]
you hit the plus button it changes to a quantity of [Link] quantity picker isn't working exactly like we
want yet, butwe're just learning concepts one at a time andslowly building up to the right
[Link] now you can leave whatever behavior you currently have hooked up forthe order
button, because that will eventually [Link] you're done, check this boxes to continue.

1.19. Update Quantity Variable

Video Transcript

Okay Katherine.>> Mm-hm. >> Sowhen we hit the plus button in that [Link] number of cups of coffee
goes up.>> Yeah it goes from two cups to three [Link] what happens when we press the plus button
again?>> Well it actually just stays at [Link] no matter how many times we press the plus button
in that app,the number of cups of coffee stays at three.>> Yeah. >> Why is that happening?>> Well, I
think it's because, in our code, we only update it to [Link] we never go beyond that.>> Right. That
number is hard coded right there.>> Yeah. >> You know what this remind me of?>> What is it?>> Well,
[LAUGH] my computer science professor in college used to say.>> Yeah?When you submit your final
project.>> Yeah?>> Please, no magic numbers.>> Wait, what's a magic number?>> Well, three's kind of a
magic number.>> Okay. >> It's a magic number because,we are doing the addition in our head, but then
to the computer.>> We're telling it the right answer.>> Okay, yeah, it would be better if the computer
actually calculated itself.>> [Link] you talk about calculation in math.>> Uh-huh. >> I need to start
writing things down.>> [Link]'s write some [Link]'s write the pseudocode for [Link]
that we've already created quantity variable and set it to [Link] the plus button in the app is
pressed,we're going to take the current quantity value which is two.>> Add one to it to make it three,
andthen store that as the new value inside the quantity [Link] we hit the plus button again, then
we're going to take the currentquantity value which is now three, and add one to it which makes it
[Link] then store that as the new value inside the variable,and here's the actual Java code for [Link]
initialize a variable [Link] the plus button is pressed again,then we're going to take the existing quantity
value, add a one to it,and then store this as the new value for the quantity [Link] you read
these lines of code, you can imagine the boxes in your [Link] though we're used to reading things
from left to right,I always start with what's on the right hand side of the equal sign [Link] this case, we
have the current value of [Link] is two, and then we add one to it, and that makes [Link]
we store the three inside the variable, like [Link] righthand side is an expression, which we learned
about [Link] an expression can be made up variables, operators andliteral values, and [Link] we
click on the plus button again, then we can say quantity = quantity + [Link] this point the current value of
quantity is three,then we add one to it, and that makes [Link] we store four as the new value inside
the quantity variable like [Link] we've have reduced it down to a common pattern,that the
computer can repeat each time the plus button is [Link] of having to write a new line of code
that says the explicit value ofthe number of cups of coffee, we can just do quantity = quantity + 1,and
continue to build off of the existing value of [Link] is a lot more [Link] you You wanted to
change initial quantity to 3,then the rest of the lines of code wouldn't have to change at [Link] we
would always use the latest value of quantity, whatever it is,which, in our old approach, if we wanted to
update the quantity value to 3,then after you hit the plus button, you would have to write a new line of
codethat says quantity equals 4 and update this to be quantity equals [Link] we update the quantity to
a completely new literal value each time,it's hard for the computer to know what the pattern is between
[Link] if we update quantity based on the current value of that variable andwe increment it in a
predictable way, then the computer will be able to dothis every single time that the plus button is
[Link] approach will work a lot better for our coffee ordering [Link] time we hit the plus
button,we can just increase the existing quantity value by [Link] writing out each line of code by
typing them into the boxes we'veprovided [Link] attention to each character and [Link] is the
first time you're updating a variable, using the variable itself, somake sure you type out this variable
name [Link] you're done, fill out this column that represents the current quantityvalue after
each line of code is executed.

1.20. Modify the increment() Method

Udacity part of Accenture logo


Modify the increment() Method

Lesson

Downloads

Video Transcript

Now that we understand these lines of code and have practiced typing them,go ahead and add them to
the increment method within the main [Link] that a common beginner mistake is to specify
the data type [Link] only need to specify the data type once when you declare the [Link]
that, you can just use the variable [Link] we add a breakpoint on Line 25 and then run the
debugger,we can check what the quantity variable value is at each [Link] I click on the plus button, then
the breakpoint on Line 25 is triggered.I'll click step over to go to the next line of [Link] we see that
the quantity variable has been created with the value of [Link] I click step over again,then we see that
quantity variable has been updated to [Link] is because of the quantity equals quantity plus one
[Link] we can hit play to resume the app andwe can see the latest quantity value being shown on this
screen [Link] the same thing in your version of the [Link] the increment method to update the
quantity variableusing quantity equals quantity plus [Link] use a debugger to step through andverify
the quantity variable value at each [Link] that, feel free to experiment with different expressions
forupdating the quantity [Link] could do quantity plus one, or one plus [Link] you could use
the variable name twice, or as many times as you want, oryou can do other arithmetic [Link]
can run this on your device to see what [Link] you're done experimenting, go ahead and revert
back to what you had before,so that we can continue with the [Link] these boxes when you're
done.

1.21. Modify the decrement() Method

Modify the decrement() Method

Lesson

Downloads

Video Transcript
Once you've become comfortable with how the increment method is working andhow to update a
variable based on the current value of that variable,then do the same for the decrement [Link] the
decrement method, initialize the quantity variable to two cups of coffee,then decrease it by one cup.I
bet you never thought you'd be doing so much coffee cup math, butit's helping us learn Java, so it's
worth [Link] use the debugger to step through andverify that the quantity variable is being updated as
[Link] you become more comfortable with variables,you can eventually skip this step of using
the debugger because you'll beable to keep track of all the values of the variables in your [Link] since
you're starting out, if you like seeing the concrete value in frontof you at each step in the code, then go
ahead and continue to use the [Link] these boxes when you're done.

Quiz: Modify the decrement() Method

1.22. Make the Quantity Picker Work

Okay Katherine.>> Mm-hm. >> We hit the plus sign.[SOUND] And the number of cups of coffee goes up
from two to three.>> Okay. >> But we keep hitting the plus sign.[SOUND] Again.[SOUND] And again.
[SOUND] And the number of cups of coffee just stays at two, it's not going up.>> Okay, well I think it's
because we're missing one concept, andthat is variable scope.>> Variable scope?>> Yeah.>> Care to
explain?>> Variable scope means that when a variable is declared in a method,it can be only used in the
scope of that method.>> [SOUND] >> Pay attention.>> Katherine.>> Yeah. >> We're talking in
[Link] needs to give students in this class a medal forgetting this far ahead.>> Okay. >>
Let's slow down and explain clearly.>> Sorry. Okay, I can explain.>> Okay, so we hit the plus button.>>
Mm-hm. >> And the increment method insidea code gets called.>> Yes. >> And then what happens?>>
Well, we create the quantity variable.>> Right.>> It is initialized to value two.>> Right. >> And weupdate
it to three cups of coffee.>> And then when the increment method ends, then what happens?>> Well,
then the variable gets destroyed.[SOUND] >> Okay, sowhen I press the plus button again [SOUND] the
increment method gets calledin our code- >> Andthen we create a quantity variable.>> Oh,
[Link] one magically appeared.>> Yeah, a brand new one.>> Okay. >> And it's initialized to two,
andthen we update it to three.>> And then the increment method ends again, and then what happens?
>> The variable gets destroyed.[SOUND] What we really need is a global variable.>> Oh, yeah.A variable
that can be accessed by any, and all, pieces of code inside our app.>> Exactly, like any method, the
increment method orthe decrement method.>> Mm-hm. >> So we createa global variable like [Link]'s
still the quantity variable.>> Mm-hm. >> But now it's global.>> Mm-hm. >> So after a method finishes
executing,it doesn't get destroyed.>> Okay, let's use this in our code next.>> The problem in our app is
that if I hit the + button a bunch of times,the number of cups of coffee never increases beyond [Link]
reason is because the + button triggers the increment [Link] this method, we always create a new
quantity variable, andinitialize it to [Link] when we update the quantity, it's always set to [Link] method
has no memory of the fact that I already called it beforeby clicking on the + button, and that I'm already
displaying 3 cups of [Link] has no idea that I want 4 cups [Link] the old code, we declared a local
variable for quantity in the incrementmethod and a local variable for quantity in the decrement
[Link] reason why we can declare a variable with the same name twice isbecause they are in
different variable [Link] is the scope of the quantity variable declared inthe increment
[Link] these bounds we can refer to the quantity and update the [Link] outside of these
bounds,other methods would not know about this quantity [Link] would get an unresolved
symbol error becauseother methods wouldn't know about this [Link], this is the variable
scope forthe local quantity variable declared in the decrement [Link] this method, you can
display the variable and update the variable, butbeyond the bounds of this scope, you can't reference
[Link] the updated code with a global variable,this is what it would look [Link] of declaring the
quantity variable in the increment method,we've pulled it out to be declared at the top of the main
[Link]'s declared within the opening and closing braces of the main [Link] anything inside the
main activity will know about the quantity [Link] is the variable scope for this global quantity
[Link] the methods and other variables in the main activitycan now refer to the quantity, and also
update [Link] the increment method updates the quantity variable, and also displays [Link] the
decrement method also updates the quantity and displays it, andthe quantity always refers back to the
global variable [Link] advantage of pulling it out into a single global variable is now we havea single
source of truth for how many cups of coffee the user [Link] if we've increased the number of
coffees a bunch of times,then if we decrease the number of coffees,then it'll just use the last known
value of the [Link] it was weird that we had two quantity variables, because thosetwo
variables could get out of sync, and the user ends up only seeing one [Link] it makes sense to have
one single source of truth,a global variable to keep track of the number of coffees [Link] might
wonder why we don't just make everything be a global variable andhave it solve all our [Link]
really don't want to make a variable global unless there's a real needthat other things in the main
activity need to reference that variable [Link] you only need to use a method within the context of a
single method,you should just keep it a local [Link]'s sort of like having a [Link] only tell people
on a need to know [Link], you only want to reveal variablesthat other people absolutely must
know about in order for the app to [Link] the MainActivity in the increment method,I'm going to copy
this line of code that declares the quantity variable.I just hit Cmd+X, or it's Ctrl+X to [Link] then at the
top of the main activity class, I'm going to paste [Link] will be pasted right after the opening breaks of
the main [Link] that it is outside the braces of other [Link], if it was inside the
braces of a method,it would be considered a local [Link] is now a global variable within the
MainActivity [Link]'ll learn more about classes in the next [Link] MainActivity is a class that
contains methods and [Link], any method in the MainActivity can refer to this quantity
[Link] this increment method just updates the quantity and displays [Link] that a global variable
has a purple font color for the variable [Link] it's a local variable, the font color is black for the variable
[Link] that's how you can tell the difference between local andglobal [Link] of local
variables, we want to get rid of this local variable inthe decrement method, because we want to use the
global quantity [Link] variable name in these methods must match the global variable name,
[Link] I said quantities here, it would give an error becausequantities is not defined as a local or a
global variable [Link] I'll undo that so that it works [Link] I run the app, let's see what
[Link] app starts off at a quantity of [Link] I hit the + button, it actually [Link] I hit the- button,
it actually [Link]'s pretty [Link]'ve succeeded in building our quantity [Link], I
want you to be able to build this on your phone so you can increase it ordecrease it as much as you want
to your heart's [Link]'ve included a link to the MainActivity code for you to reference.I'd rather have
you type it out yourself by following this videoinstead of copying and pasting the [Link] way, you'll
be able to create global variables on your ownin the future.I'm going to attach the debugger sowe can
see carefully what's happening with the quantity variable.I'm going to add breakpoints in the increment
and decrement methods andthen run the [Link] I hit the + button, then this breakpoint
[Link] this line of code has even executed,we see that the global quantity variable already
exists with a valueof 2 because of this line of code up [Link] I set to the next line, then this line has
finished executing andwe've increased the quantity up to 3.I can hit resume so that the app updates to a
quantity of [Link] I hit the + button again, then before I've even executed this line of code,we already have
the global quantity variable set to a value of [Link] was the last known quantity we had before, and it sill
remembers [Link] i increase the quantity by 1,then the global quantity variable has a variable of 4 now.I
can hit resume to update the screen with [Link], we can see that every time the increment method is
called ,we canactually continue to build off of the existing quantity [Link]'s see how it interacts with
the decrement [Link] I hit the- button, then this breakpoint [Link] global quantity variable has
a value of 4,which is what we last left off [Link] I decrease the quantity by 1 than it goes down to 3 cups
of [Link] I can display that on the screen and it changes to [Link] a single global variable for
quantity, the increment anddecrement methods can now increase ordecrease the number of coffees by
1 based on the latest value of [Link], it's your turn, go ahead andreplace the 2 local variables
with 1 global [Link], use a debugger to verify that the quantity variable is actuallyupdating as
[Link] your quantity picker works, go ahead and do a little happy [Link] then you can try to
experiment a little [Link] happens if you have a different initial quantity value, like 0?Or try doubling
the quantity each time the + button is [Link] halving the quantity each time the- button is
[Link] free to experiment with whatever you're curious [Link] you ever wondered, what would
happen if I do this or that?Those are sometimes some of the best learning moments,because they're
[Link] you're done check these boxes to continue.

1.23. Fix the Order Butto

Next, we'll fix the behavior of the Order [Link] I change the quantity to something like six, if I hit
the Order button,the price should update to $[Link], when I click the Order button,it always
changes the quantity back to five and the price to $[Link] this behavior is [Link] test your
understanding of everything we've learned sofar, I want you to go ahead and fix this bug on your
own.I'm confident that you can do [Link] is a very common occurrence during app [Link]
build out more functionality of your app, like this quantity picker, butthen it can have ripple effects on
the rest of your [Link] it may break something, like this Order [Link] you have to just go back and
fix that [Link] you've completed this task, this is what the app should look [Link] I increase the
quantity to something like six, andI hit the Order button, then the price changes to $30, which is
[Link] help you tackle this problem,first identify all the things that are wrong with the current state
of the [Link] can look at it on your device, and soit will be easier than looking at this [Link]
screenshot shows setting the quantity to six andthen hitting the Order button and seeing the result on
the screen.I want you to describe in words the gap between what we have now andwhat we [Link] as
detailed as possible by writing your answer in this [Link] more specific you can get about all the things
that are wrong with the app,the easier it will be to fix the problem because you'll have almost a task
liston how to get to the ideal [Link] after you've planned how you will attack the problem, go
ahead andimplement those changes in your [Link] the Order button works as expected, pat yourself
on the back, andthen check this box to continue.

1.24. Lesson Review: Making an App Interactive

Hi, [Link], [Link] are you feeling?I'm feeling a little tired. How about you?Me too. I suspect
that some of our students are perhaps feeling the same [Link] that's okay, it's a really long
lesson [Link], it [Link] this is a great point for them to at the end of this video,take a little break and
walk away from the [Link], and while you're doing that you couldactually also show someone
the app you've made so [Link], talk to your friend about the new ideas that you've learned in the
Android world,and come back and share that conversation with us on the discussion [Link].

Introduction to Making an App Interactive Part 2

Lesson Overview

In this lesson, you'll learn more about views, layout, and variables to finish the Just Java App.

You'll work with Nested ViewGroups

You'll update the Quantity Picker Layout

You'll learn more about string variables and add more to the interface.

2.2. Plan Layout for Quantity Picker

Thank you for taking the time to reflect on what you've done so [Link] able to teach others about
what you've built feels rewarding, butit's also a core skill in being able to communicate with teammates
that you maywork with on a software [Link] that in mind, let's keep moving forward soyou can go
back to those people soon and share your [Link] far the functionality of the quantity picker in our
app is working [Link] now we can spend a little bit of time making it look a little better,a little [Link]
want to move closer towards our end [Link] is what we have now, and this is what we [Link]
buttons in our app currently work, but they're not in the ideal [Link] layout leads to a better user
experience,because we're going to be adding more fields to the order form in Lesson [Link] this layout
of the buttons,it's easier to scan through the list of fields in the order form [Link] the current layout,
if you want to scan through the form quickly,you would read quantity plus two minus price and so [Link]
it's faster to scan through the form if the buttons are laid out like [Link] updated layout also takes
advantage of more of the horizontal space onthe screen, instead of squishing everything into a very
narrow,vertical [Link] challenge becomes identifying the gap between what we have now andwhat
we [Link] we repurpose the views that are already on screen and move them intothe new position
for the new layout, or do we have to add views or remove views?Before we get lost in the details of
changing the XML,I want you to first plan out how you're going to approach building this [Link] your
own words, describe your thoughts on each step that we've taughtyou for how to build [Link]
don't need to write XML at this [Link] can just plan out your [Link] notes are going to help
you actually write the code at each step, sobe as detailed as you [Link] first step is to select the
[Link] views are you going to need in terms of text view, image view,button, et cetera?The
second step is to position the [Link] you describe how these views are positioned on the screen?
Which view groups are relevant to building this layout?Step 3 is to style the [Link] there anything we
need to do to change the visual look andfeel of each of these views?

2.3. Nested ViewGroups

When discussing how to position views in the last quiz,you may have mentioned using a relative layout.I
know we [Link] I want to introduce you to another way that you can build the layout forour app that
would be more ideal, using nested [Link] viewgroups means putting viewgroups inside
other [Link] way you can build more interesting and complex layouts like [Link]'s see how
you would build up one of these [Link], for example, you have a vertical linear layout with three
views,an image view and two text [Link] if you wanted to overlay some text on top of this image?
Well, with a linear layout, you can't overlap [Link] with a relative layout, you can, sowe can replace
this child with a relative layout [Link], we can put the image view inside this relative [Link]
image view is now aligned to each edge of the parent relative [Link], there you see an example of a
relative layout that's contained withina larger linear [Link] can have many combinations of view
groups within other view [Link] instance, you can have a relative layout as a root view for an
[Link] can contain a couple of views like an image view and two text [Link] can also add a linear
layout as a child of this broader relative [Link] linear layout itself can contain other children like
two image [Link] can nest as many view groups inside other view groups as you [Link], be
careful because the more nesting that you do,the more expensive it will be for your app to lay out on
the [Link] relative layout, in particular, is a very flexible layout but it can be verycomplex to
calculate the position of each of these views relative to [Link] it's recommended to not have too
many layers of nestingwithin a relative layout.I pulled some examples from real life Google apps to show
you some cases whereit would be useful to use nested view [Link] Google Now app shows you
cards of relevant informationbased on your current situation, like the current time or current [Link]
you like a certain team like the Red Sox,good choice, then it can show you the score for the latest
[Link] build out the layout for this card, you can imagine using a vertical linearlayout because the
information is displayed as [Link], the first item in this vertical linear layout is not just a single
textview, it's made up of multiple [Link] fact, you would build this first item as a horizontal linear
layout,made up of an image view and two text [Link] for the second item in this vertical linear
[Link] would build it with a horizontal linear layout containing an image viewand two text
[Link] card in the Google Now app shows relevant stocks that youmay be interested in.I wish
the Google stock was still at that [Link], you can also build out this layout as a vertical linear
layout,because you can imagine the rows [Link] these middle entries aren't just a single text
view,they're made up of multiple text [Link] this line item relating to the Google stock price,you
could use a horizontal linear layout and fill it with four text [Link] for the other stock prices as
[Link] horizontal linear layouts are children of this broadervertical linear [Link] the Google Play
music app, you can, of course, listen to [Link] this looks like a complicated layout to build,you can
actually break it down into smaller view groups that you [Link] pattern feels like a vertical linear
layoutwith three different [Link] first child can be built using a relative layout,because the views
are relative to each [Link] second child can also be built using a relative layout,because the views are
overlapping each [Link] this third child here looks like a horizontal linear layoutmade up of five
different [Link], we can use nested view groups to build out this [Link] is just one way to
build out the [Link] can build it in many other ways as [Link] the Google Maps app, earlier we saw
that there are detailed pages forindividual locations, like [Link] can't build out this screen with
just a single linear layout ora single relative [Link] need to nest a bunch of view groups [Link]
a broad scale,we can see that the information is almost organized into [Link], we can use a vertical
linear layout as the parent [Link] child could be this whole layout [Link] child could be this
row of [Link] child would be this description and so [Link] within each child is not just a
single [Link] have multiple [Link] child in the broader linear layout is made up of three individual
[Link] you can use a horizontal linear layoutwith equal weights to each view so that they get spread
out [Link] child in the broader vertical linear layout can also be madeup of a horizontal linear
layout with an image view and a text [Link] you keep scrolling this page, you see more information on
the [Link], I can imagine the individual rows of information [Link] a vertical linear layout
for all this information still works,and, again, some rows are more complex than just a single text
[Link] row shows a bunch of available times where you can make a reservation,and it's made up of
three different buttons that you can arrange usinga horizontal linear [Link] row of pictures can be
built using a horizontal linear layout as [Link] actually, within this horizontal linear layout,this first
child is not just a single image [Link]'s an image view with a text [Link] you could use a relative layout
to build out this first [Link] you could see how there can be view groups, within view groups,within
view [Link]'s actually more view groups beyond just relative layout,and linear layout, there's also
frame layout and grid [Link]'re not going to have time to cover those in this course, but I
definitelyrecommend that you go out and learn about them on your own because sometimesthose
would be better than just using a relative or a linear layout.

2.4. View Hierarchy Diagrams

Let's look at how nested ViewGroups would apply to our [Link] information in our app looks like it's
arranged in a vertical [Link] we can use a vertical LinearLayout for [Link] looks good sofar
because we have one view per row in the vertical [Link] when we get to this row, it's actually
made up of multiple [Link] can use a horizontal LinearLayout to build out this row composed of a
button,a TextView, and a [Link] this LinearLayout would be the second child inthe bigger vertical
[Link] a moment, I'm going to show you three different XML layout files, andI want to ask you
which one you think accurately builds upthe layout that we [Link] catch is I don't want you to copy
these files andrun them in Android Studio.I want you to make an educated guess based off of reading
andunderstanding this [Link] can find this link in instructor [Link] has three options listed, Option
A, Option B, and Option [Link] of these options correctly builds out the layout that we [Link] is going
to be challenging because we haven't previously encounteredXML with nested [Link]'re going
to split this challenge across two quizzes, soI won't ask you to make a decision [Link] this first quiz, I
want you to sketch out the view hierarchy diagramfor each of the XML [Link]'s a quick spiel on what
view hierarchy [Link]'ll need paper andsomething to write with in order to sketch out these
[Link] way that Android organizes all these views on screen is in a view hierarchy,or a tree
structure of [Link] is simply a diagram that represents the view [Link] shows the parent and
child relationships among the [Link] root view of this whole layout is a [Link] has three
children, a RelativeLayout, a TextView, and another [Link] then this RelativeLayout in turn has its
own children, an ImageView anda [Link] our Puddles the Pig app,this is the view hierarchy that
gets created by [Link] have a RelativeLayout as a root view, and it has four children, an
ImageView,a couple TextViews, as well as a [Link] this LinearLayout, in turn, has two
ImageViews as its [Link] you understand what a view hierarchy diagram [Link] sketching out the
diagram for each of these XML layouts,you'll better understand which view is the parent view, who the
children are,and whether those children have children views or [Link] you still need a hint, here are
some examples of how to go from XML layoutsto drawing a view hierarchy [Link] this layout, we
have a root view of a [Link] the way, I added thesedots here because I couldn't fit all the code
[Link] a root view as a LinearLayout, you draw that at the top of the [Link] then it contains two
children TextViews, soyou draw those as children underneath that, and that's [Link] is a more
complicated [Link] have a root view that's a LinearLayout, andit contains three direct children, two
TextViews, and a RelativeLayout, andthen inside that RelativeLayout, we have an ImageView and a
TextView as [Link] you're done drawing out the view hierarchy diagram foreach option, then go
ahead and check these boxes to [Link] the following quiz, I'll ask you to make a selectionon which
option is best, but for now, you don't have to make a decision.

2.5. Choosing the Right XML Layout

With the pen and paper still in your hand, go ahead andsketch out what each option would look like on a
[Link] can refer to the view hierarchy diagrams that we created in the lastquiz, if that helps
[Link], as you're reading through each XML file,make sure that you read each attribute carefully, so
you don't miss [Link] you're done with those three sketches, I want you to decide on
whichoption gives you the desired layout that we [Link] are radio buttons instead of check boxes,
so you can only pick [Link] luck.

2.6. String Data Type

So, far we've built a quantity picker.>> Right. Andthe next place to go in the app is the price.>> [Link]
price is [Link], price is an integer so far, but we want to move beyond that anduse strings.>> You
mean, this type of string?>> No I mean like this type of string.>> Huh.>> So a string is just a sequence of
characters.>> So wait. How does this work with the price then?>> So we're going to change the screen
so that it says [Link] then $[Link] it's not just a number, now it has some text with it as well.>>
Numbers and text.>> Yeah. Isn't that awesome?This is from the party that you may or may not have
[LAUGH] been invited [Link] the next few videos we'll talk about using stringsin our Just Java
[Link], we'll learn how to create and update string variables in the [Link] we'll change the
app so that under the price header it says total, $15 orwhatever the price is based on the quantity
[Link]'ll also show the text,Thank you, because we're excited about having any customers at
[Link] might be wondering why don't we just put all of this textinside the XML layout?Well, we could,
but the problem is that the quantity can change andwhen we hit the order button, we want the price to
be updated [Link] this text needs to accurately reflect the price based on the [Link] using a string
variable, we can change the value of the text whilethe app is running without having to hard code it into
the whole XML [Link]'s an example from real apps that you may [Link] you receive an
incoming call you see the person's name at the topof the [Link] can't put this name directly into the
XML layoutbecause it will change depending on who calls you, unless all your family andfriends are
called Britta [Link] best thing to do is to store this name in a string [Link] way you can update
it depending on who calls [Link] when we show this screen we can just display the latest namethat is
stored in that [Link] the News and Weather app, we see a list of the top news [Link] we
build the XML layout, we can't just specifically hard codethese specific news stories into the XML
[Link] much as I love reading about the Winter Olympics,I do want to see other news articles as
[Link] we can store the information for each news story,like the article title, as a string variable,
because that can [Link] the next task I'm going to provide you with some code andI'm going to
have you make those changes in your [Link] you'll run the app and then observe what happens.I'll
ask you some questions to see if you can try to figure outwhat's going [Link] first change is to modify
the submitOrder [Link] can remove the code that's already in between the opening andclosing
braces and then replace it with these two lines of [Link] this code will give you an error, so I want
you to alsoadd the displayMessage method, which we've provided in the instructor [Link] the
instructor notes, you'll find this Gist of [Link] ahead and select the whole thing,from the comment all
the way to the last closing [Link] can treat this method like a black box,you don't have to worry
about what's going on Inside of the method, you justneed to know that you want to add it to the
MainActivity so that you can use [Link] in Android Studio,in the [Link] file, I'm going to paste
in this new method.I can paste it anywhere in the MainActivity, butI'm just going to put it right after the
displayPrice [Link] I'm going to put it afterthe closing brace of the displayPrice [Link]
that it's also inside of the closing brace of the [Link] MainActivity has an opening brace right
here,and everything inside of it is contained within the [Link] then you have a closing brace
for the MainActivity [Link] we've added the displayMessage method in,we can now modify the
submitOrder method.I'm going to erase the code that we already have insidethe submitOrder
[Link] according to the quiz image, I'm going to add the new code [Link] I'm not
giving you too many details on what I just did.I want you to try it out in your version of the app, run it,
andsee what [Link] that I want you to answer these [Link] happens when you click on
the order button in the app now?If you take a look at the submit order code more closely,we actually
created a string [Link] though we haven't gone over the code for string variables, I want you
tomake your best guess as to, what do you think the variable name is?And what do you think the
variable value is?When you are done go ahead and continue.

2.7. Create the String Variable

Earlier in the lesson we learned about how to declare orcreate an integer [Link]'re going to follow
the same format to create a variable, butthis time the variable holds text because it's a [Link] is the
code we added to our [Link] creates a new string variable called [Link] the value that it
holds is the text [Link] follows a specific format according to the syntax rules of [Link], we specify
the data [Link] this case, it's a [Link] that a string is just a sequence of [Link] it's
followed by the variable name, which is priceMessage in this [Link] it's followed by an equal sign,
initial value,and a semicolon to indicate the end of the [Link] that the data type is a string
limits what type of information can goinside this [Link] one string can be stored in it at a time.A
string can be made up of letters, numbers, or [Link] may recall that a variable name is the name
that we give this variable, sowe can refer to it later, to get the value out or to update the [Link]'s like
giving the variable a name [Link] you're picking a variable name, it's important to choose
somethingthat's descriptive, that describes what type of information it will be holding.I could call this
variable name Catherine, butit really does not reflect the information inside of [Link] makes my code
harder to read as well,because no one knows why there's a string variable called [Link] the other
hand,a variable name like priceMessage is a little more helpful, because you knowthere's a message
inside of it that's related to the price [Link] looked at this earlier, but there are naming
conventions for variable [Link] you forget what those are, you can always look them up.I just search
for variable name Java in Google Search.I can scroll down here to where it talks about [Link] says
that variable names are case-sensitive, andyou usually start with a lowercase [Link] then you use
camel case if there's multiple [Link] we have the assignment operator, or the equal sign [Link]
operator is in charge of assigning a value to a [Link] you take this value on the right-hand side
andyou assign it into the variable [Link] specify the initial value of this string to be some text
anddouble [Link] is called a string literal, because a literal has a fixed [Link] first heard about
literals when we were learning about integer variables.A fixed value like two or five or ten are all integer
literalvalues because they don't change, as opposed to a variable which does [Link] back to our
box analogy, the value,Free, would go inside this [Link] last but not least, we also have a
semicolon at the end of the [Link] that we understand this line of code a little better, go ahead
andtry to experiment with different [Link] can try to change the initial value here or change the
variable [Link] example, you can use a short string like Free orchange it to be a longer [Link]
could make it a completely empty string as well,which leaves no characters in between the two quote
[Link] could add a space, or you could add numbers as well, oryou could add [Link] you're
creating your string literal, just be carefulthat there are certain characters that you need to deal with
[Link] example, if you use the quote sign in your string,then you're going to run into an error that
says,can't resolve symbol dollar and a semicolon is [Link] is because the compiler doesn't know
that the whole string is supposedto be this whole [Link] solve this problem, let's do a Google [Link]
Google, search for Java escape [Link] scroll down to a section called escape [Link] Java,
certain characters mean special [Link] start and end quote symbols are to denote a string in Java,
butif you want to actually put a quote inside your string,you'll need to use an escape [Link]
escape sequence is a character preceded by a backslash, andthe Java compiler will interpret it in a
special [Link] we use a backslash with a double quote, that indicates that we actuallywant to use the
quote symbol, not the quote that means it's a text [Link] me show you what I [Link] Android
Studio I can add a backslash before eachquote mark that I actually want to appear in the [Link], the
whole initial value of this variable will be this whole string,because it's marked off by the certain end
[Link] inside that text string, I also want to use quotes, butI mark those by using a backslash in
front of each [Link] I run on my device, I see the quotes around one dollar, butI don't see the quotes
around the full thing ,which is what we [Link] useful escape sequence to know is this backslash
n,which inserts a new line so that the text will continue on to the next [Link] can try this one out in
your code as [Link] that the semicolon should always be at the end of the [Link]
though in English it's grammatically correct to put thepunctuation inside the quote, you want to put it
on the outside of the [Link] in between here is part of the text [Link] you need to put the
semicolon on the outside for the Java [Link] can also modify the variable [Link] I change this to
something like message, then this gives me an errorhere, because it can't resolve the symbol price
[Link] I just need to copy it and put it down here as [Link] this should run on your [Link]
can't change the data type, [Link] must be spelled string exactly like [Link] is capitalized here
because it's an [Link]'ll learn about objects in Lesson 3, but fornow just type string with a capital
[Link] we were working with integers earlier,we would declare integers with a lowercase i for [Link]'s
lowercase because it's a primitive data type,where primitive means it's one of the basic Java data
[Link] with different variable names andinitial values until you're comfortable with creating
string [Link] I want you to tell me what's incorrect about each of these stringdeclarations listed
below.I want you to point out the error in each of these lines of code.

2.8. Create the String Variable

Video Transcript

Earlier in the lesson we learned about how to declare orcreate an integer [Link]'re going to follow
the same format to create a variable, butthis time the variable holds text because it's a [Link] is the
code we added to our [Link] creates a new string variable called [Link] the value that it
holds is the text [Link] follows a specific format according to the syntax rules of [Link], we specify
the data [Link] this case, it's a [Link] that a string is just a sequence of [Link] it's
followed by the variable name, which is priceMessage in this [Link] it's followed by an equal sign,
initial value,and a semicolon to indicate the end of the [Link] that the data type is a string
limits what type of information can goinside this [Link] one string can be stored in it at a time.A
string can be made up of letters, numbers, or [Link] may recall that a variable name is the name
that we give this variable, sowe can refer to it later, to get the value out or to update the [Link]'s like
giving the variable a name [Link] you're picking a variable name, it's important to choose
somethingthat's descriptive, that describes what type of information it will be holding.I could call this
variable name Catherine, butit really does not reflect the information inside of [Link] makes my code
harder to read as well,because no one knows why there's a string variable called [Link] the other
hand,a variable name like priceMessage is a little more helpful, because you knowthere's a message
inside of it that's related to the price [Link] looked at this earlier, but there are naming
conventions for variable [Link] you forget what those are, you can always look them up.I just search
for variable name Java in Google Search.I can scroll down here to where it talks about [Link] says
that variable names are case-sensitive, andyou usually start with a lowercase [Link] then you use
camel case if there's multiple [Link] we have the assignment operator, or the equal sign [Link]
operator is in charge of assigning a value to a [Link] you take this value on the right-hand side
andyou assign it into the variable [Link] specify the initial value of this string to be some text
anddouble [Link] is called a string literal, because a literal has a fixed [Link] first heard about
literals when we were learning about integer variables.A fixed value like two or five or ten are all integer
literalvalues because they don't change, as opposed to a variable which does [Link] back to our
box analogy, the value,Free, would go inside this [Link] last but not least, we also have a
semicolon at the end of the [Link] that we understand this line of code a little better, go ahead
andtry to experiment with different [Link] can try to change the initial value here or change the
variable [Link] example, you can use a short string like Free orchange it to be a longer [Link]
could make it a completely empty string as well,which leaves no characters in between the two quote
[Link] could add a space, or you could add numbers as well, oryou could add [Link] you're
creating your string literal, just be carefulthat there are certain characters that you need to deal with
[Link] example, if you use the quote sign in your string,then you're going to run into an error that
says,can't resolve symbol dollar and a semicolon is [Link] is because the compiler doesn't know
that the whole string is supposedto be this whole [Link] solve this problem, let's do a Google [Link]
Google, search for Java escape [Link] scroll down to a section called escape [Link] Java,
certain characters mean special [Link] start and end quote symbols are to denote a string in Java,
butif you want to actually put a quote inside your string,you'll need to use an escape [Link]
escape sequence is a character preceded by a backslash, andthe Java compiler will interpret it in a
special [Link] we use a backslash with a double quote, that indicates that we actuallywant to use the
quote symbol, not the quote that means it's a text [Link] me show you what I [Link] Android
Studio I can add a backslash before eachquote mark that I actually want to appear in the [Link], the
whole initial value of this variable will be this whole string,because it's marked off by the certain end
[Link] inside that text string, I also want to use quotes, butI mark those by using a backslash in
front of each [Link] I run on my device, I see the quotes around one dollar, butI don't see the quotes
around the full thing ,which is what we [Link] useful escape sequence to know is this backslash
n,which inserts a new line so that the text will continue on to the next [Link] can try this one out in
your code as [Link] that the semicolon should always be at the end of the [Link]
though in English it's grammatically correct to put thepunctuation inside the quote, you want to put it
on the outside of the [Link] in between here is part of the text [Link] you need to put the
semicolon on the outside for the Java [Link] can also modify the variable [Link] I change this to
something like message, then this gives me an errorhere, because it can't resolve the symbol price
[Link] I just need to copy it and put it down here as [Link] this should run on your [Link]
can't change the data type, [Link] must be spelled string exactly like [Link] is capitalized here
because it's an [Link]'ll learn about objects in Lesson 3, but fornow just type string with a capital
[Link] we were working with integers earlier,we would declare integers with a lowercase i for [Link]'s
lowercase because it's a primitive data type,where primitive means it's one of the basic Java data
[Link] with different variable names andinitial values until you're comfortable with creating
string [Link] I want you to tell me what's incorrect about each of these stringdeclarations listed
below.I want you to point out the error in each of these lines

2.9. Combining Strings Together

When dealing with strings in Java,an important concept to understand is string [Link]
that's a really big word, butit just means we're joining character strings together end to [Link] this is a
string and this is a string,you can combine them by concatenating them to make an even longer
[Link] concatenate these strings together, we use the plus [Link] is the same addition symbol
that we know from [Link] like you can add numbers together, you can concatenate strings
[Link]'s look at an [Link] I have three different strings, one string literal says I need,another
string literal says 2 cups of coffee,and another string literal says on Monday.I can use the plus symbol to
concatenate all these strings [Link] forms a ginormous string that says I need2 cups of coffeeon
[Link] I see something like this, I imagine the plus symbols are gone, andI imagine the
quotes are gone, andI just imagine literally squishing all of these things [Link] when I say
squished, we're really squishing them [Link]'s even no extra space in between this string and
this [Link] you want to add a space here,you would have to explicitly add a space in this string literal at
the end of it,or you add a space at the beginning of this string [Link] with coffeeon Monday.I want
a space here, so I'd have to either add it at the end of this stringor the beginning of this string.I added a
space here and a space here, sowhen I concatenate all of this together,I squish them together, and the
sentence comes out correct like [Link]'s a space here and a space [Link] spaces in the right
place is a little bit tricky because you havethe quotation marks everywhere and the plus symbols,
andthere's even spaces around the plus [Link] these spaces around the plus symbol don't
contributeto the overall display [Link] space must be inside the double [Link]'s an example of
string concatenation in our app.I'm going to change the text sothat it says "Amount due " + "$10".I'm
concatenating this string literal with this string [Link] I run it on my device, andthen I hit the order
button, then I see Amount Due $[Link] can also concatenate strings with integers like I have
[Link], the ten was in quotes sothat was a string representation of the number [Link] here I just
have 100 without quotes, so this is the integer value for [Link] I concatenate a string with an
integer,then it immediately turns this whole thing into a [Link] I hit the Order button, then I see $100
showing up on the [Link] a moment, I'll have you play around with string concatenation to
trydifferent [Link] could get compile errors, so be careful of [Link] I forget a closing quote, I could
get an [Link] a moment, I'll have you play around with string concatenation andtry different
[Link] to Android code style guidelines,we should have a space before and after each
[Link] this string concatenation operator counts as an [Link] it's your turn to practice in
your [Link] with combining different strings using the plus [Link] can also combine it
with integer literal values as [Link] you feel comfortable with string concatenation,I want you to
answer these questions.

2.10. Chatting with Google's Alice Yang, Part 2

Video Transcript

So our students have been learning about variables and data types in this [Link] about
integers and [Link] there examples in the apps that you've worked on of these things?>> Yes, there
are [Link], strings and integers are [Link] Gmail for example, when you first open your email
it tells you how manyunread messages that you have, so that's a very, very important [Link] we
actually have these very subtle bugs,that the number might not match exactly how many unread
messages you [Link]. [CROSSTALK]>> I've never had encounteredthat, have you?>> That's good. >>
[LAUGH].>> That you've never encountered [Link], yeah, but these are, you know, it's an interesting
problem to solve,because for Gmail, we actually sync that number [Link] sync means like,I'm fetching
my data from the Cloud to my Android [Link], that data is actually,that number, is synced separately
from your actual [Link], they could potentially go out of [Link] is one of the very trickier issues.>>
Okay, so we were established that the number of unread emails is a [Link]'s data type would be.>>
It would be an integer.>> It would be an [Link]'s value is being calculated across [Link] it's
value is changing based on the use case of the scenario.I use it as experience.>> Yes, [Link]
another use of integers, sothat's an example of a variable integer, basically it could [Link]
example is ID'[Link] for ID's it's actually very important that the integer stays the same.>> So my email ID,
[Link]. >> [Link] the email ID on the server that represents thatemail needs to be exactly
the same as the ID that's on the [Link] that's the way we uniquely identifythis email everywhere
across all your devices.>> So this is for our conversation email, not like an email address.>> Yes, for our
conversation, so for in Gmail for example the whole [Link] the email thread which has multiple
messages in it, the whole thread we calla conversation, it has a conversation >> Oh, okay, I see.>> And
then each message itself also has its own unique ID.>> Okay, and each one of these ID's is stored in a
variable who's data type is?>> Integer again.>> Okay.>> Yes >> How manyemails are going out in a
[Link] you know?This is a lot of numbers.>> Yes, I do, I do not know on top of my head how many
emails.>> I'd imagine that if each message in a conversation is unique andeach email is unique across all
emails that are going.>> Yes.>> That should be a very large number, right?>> So that's a very good
[Link] instead of int actually for these we use logs.>> Okay. >> Because it's a 64 bit.>> So then we
will never.[CROSSTALK] >> So long just gives you more space tohoard a larger number, right?That's really
the difference between Inter Long?Okay. >> How about any strings?>> Strings.>> I'd imagine it's
subjective.A string, or how is that stored?>> Yeah, [Link], subject is a string itself.>> Okay.>>
And obviously email addresses are [Link]'s very [Link] almost everything that you see in
any app,like virtually everything that has text will be a [Link] I think a good engineer doesn't have to
know [Link] you really,you have to be good at like figuring out like, you know where the answer
[Link] can probably look for, there's a lot of good tutorials out there for,just have to know how to look
for the right key [Link] for the right key words.>> If you also ask people for help.>> Oh yes, that
is actually very important.I think almost always I regret not asking people [Link] know I could like
try to fix something, for like a whole day, andat the end of the day,said okay, maybe I should have asked
whoever wrote this [Link] yeah, I think reaching out to people early is definitely valuable,to
save very valuable [Link] also just being more aware of what other projects are going on at Google.I
think the, or at your own, around you.I think the really good engineers, they're not only focus on what
they'redoing, but they we have these things called tech talks at work.>> Which are available publicly
forpeople to view, at least some of them are.>> [Link] of them are, [Link] their often know what the
latest technologies are, from these tech talksor announcements that are sent to the entire engineering
[Link], just being on top of.>> And let's go back to the time when you were actually learning to
[Link] you could talk to the Alice at that time,what advice would you give her today?>> Wow. That's
a very interesting question.I feel like I should have had more fun with [Link], I think the way I
learned to program, andI think it's very different now, because I grew up in, originally I'm from [Link]
I think it was very academically [Link], I went to these extracurricular classes that tlike taught via
teacher,and then we'd get assignments like you learn to solve them, andlearn to solve this [Link]
I think, looking back,I wish I had like done more fun-type projects.>> Would you make a birthday app?A
birthday app? >> [LAUGH]>> Yeah exactly,like, these type of apps I think I could learn a lot
[Link] it's not just about there's one right [Link] in like doing an app like that I can learn
aboutdesign if its a birthday app.I learn about different UI components its not just about,okay you need
to send this data from here to [Link] then solve it more efficiently.I think in real life that is what, you
know, engineering is more [Link]'s about not just solving a technical challenge, butalso creating a
great product.>> Sort of like being driven by your own curiosity.>> Yes, [Link].>> Well, Cathy and I
want to thank you for your time.>> You're very welcome.>> I think our students will benefit a lot from
your perspective.>> Yeah, okay.

2.11. Combining String and Integer Variables

When dealing with strings in Java,an important concept to understand is string [Link]
that's a really big word, butit just means we're joining character strings together end to [Link] this is a
string and this is a string,you can combine them by concatenating them to make an even longer
[Link] concatenate these strings together, we use the plus [Link] is the same addition symbol
that we know from [Link] like you can add numbers together, you can concatenate strings
[Link]'s look at an [Link] I have three different strings, one string literal says I need,another
string literal says 2 cups of coffee,and another string literal says on Monday.I can use the plus symbol to
concatenate all these strings [Link] forms a ginormous string that says I need2 cups of coffeeon
[Link] I see something like this, I imagine the plus symbols are gone, andI imagine the
quotes are gone, andI just imagine literally squishing all of these things [Link] when I say
squished, we're really squishing them [Link]'s even no extra space in between this string and
this [Link] you want to add a space here,you would have to explicitly add a space in this string literal at
the end of it,or you add a space at the beginning of this string [Link] with coffeeon Monday.I want
a space here, so I'd have to either add it at the end of this stringor the beginning of this string.I added a
space here and a space here, sowhen I concatenate all of this together,I squish them together, and the
sentence comes out correct like [Link]'s a space here and a space [Link] spaces in the right
place is a little bit tricky because you havethe quotation marks everywhere and the plus symbols,
andthere's even spaces around the plus [Link] these spaces around the plus symbol don't
contributeto the overall display [Link] space must be inside the double [Link]'s an example of
string concatenation in our app.I'm going to change the text sothat it says "Amount due " + "$10".I'm
concatenating this string literal with this string [Link] I run it on my device, andthen I hit the order
button, then I see Amount Due $[Link] can also concatenate strings with integers like I have
[Link], the ten was in quotes sothat was a string representation of the number [Link] here I just
have 100 without quotes, so this is the integer value for [Link] I concatenate a string with an
integer,then it immediately turns this whole thing into a [Link] I hit the Order button, then I see $100
showing up on the [Link] a moment, I'll have you play around with string concatenation to
trydifferent [Link] could get compile errors, so be careful of [Link] I forget a closing quote, I could
get an [Link] a moment, I'll have you play around with string concatenation andtry different
[Link] to Android code style guidelines,we should have a space before and after each
[Link] this string concatenation operator counts as an [Link] it's your turn to practice in
your [Link] with combining different strings using the plus [Link] can also combine it
with integer literal values as [Link] you feel comfortable with string concatenation,I want you to
answer these questions.

Note: At 0:19 in the intro video, there should be a space before " cups of coffee" so that the phrase
correctly prints "I need 2 cups of coffee on Monday".

2.12. Update the String Variable

Just like you can change the value of an integer variable after you'vecreated it, you can also change the
value of a string [Link] can update it to a completely new [Link] our Just Java app, say we
wanted to display a special drink of the [Link] we could use a string variable to store the name of
that [Link] we declare a string variable called drinkOfTheDay andwe initially set it to the value
[Link] our box analogy this is what it would look [Link] have the Latte value stored inside this
[Link] another line of code we can say drinkOfTheDay equals a new string [Link] the word
Espresso is stored in this [Link] don't need to specify the data type string again,because it was
already declared as a string up [Link] the next line of code if we say drinkOfTheDay equals Green
Tea,then we store Green Tea into this variable [Link]'s another example where we might want to
display the store hourswithin our [Link] can create a string variable to store the text thatshould be
displayed on the [Link] initial value is Open today from 8AM to [Link] a different day we can
update the string variable to say Open today from8AM to 2PM, because we want to leave early to go to
the [Link] on another day we can update the store hours to say Closed today,because we don't work
on [Link] our app had a running list of all the drinks that the user wantedthen we could create a
string variable called [Link] could start it off with one drink, like 1 [Link] the variable we
would store the text 1 [Link] then if the user wants another drink,then we can just concatenate the
new drink onto the existing [Link] gets stored inside the variable drinksOrdered like [Link] I
want to add another drink,I can just concatenate that onto the existing string [Link]
expression evaluates to one long string that says 1 mocha, 1 cappuccino,1 [Link] of that gets
stored into this variable like [Link] might look a little funny to have a comma anda space at the beginning
of this string,but that just makes the list look nice with commas in between each [Link] reason why I
didn't put a comma at the end of cappuccino is becauseI can't be sure if there's going to be another
drink coming after that or [Link] there's only two drinks, then I will say 1 mocha,1 cappuccino and that
looks fine as [Link] there's another drink coming, like a macchiato,then I can just add a comma because I
know another drink is [Link] ahead and try this out in Android [Link] another line of code that
updates the string variable to a new value,or you can update it to the current value plus some more
[Link] you're comfortable with knowing how to update string variables inAndroid Studio, then go
ahead andimplement this final behavior in the app for lesson [Link] you change the quantity and hit
the Order button, it should say Total andthe price, and then Thank you!Technically you don't need to
update the string variable in order to build outthis [Link], I'd still like you to do so if you
[Link] the next lesson you're going to need to update string variables more often,specifically because
there's going to be more fields in this order form andwe're going to need to build up the order
[Link] you're done, check these boxes to [Link] is the last coding task of the lesson, so
good luck.
COURSE 4

INTRODUCTION TO KOTLON

1. Introduction

1.1. Well Come

Video Transcript

Welcome to the Kotlin Bootcamp for Programmers, where, surprise!you will learn [Link] there. I'm
Sean McQuillan and I'm a Kotlin expert at Google.I love Kotlin because it let's me build great features
with less [Link] I'm not working with Kotlin,you can find me playing the piano and [Link],
here, invited me to join him,and I'm really excited to share Kotlin with [Link], I'm Alex.I'm a training and
software developer at [Link] main job is to learn stuff,and then explain it to [Link] year, I'm
learning to become a Kotlin [Link] like you I had many questions while writing this course and
byanswering them in these lessons with lots of expertise from Sean,we hope you have a smooth journey
and some fun along the [Link] you have a fun fact?We'll get to that. Why don't you look around the
room?Before we start, let's make sure you are in the right [Link] course is for students who know a
modern object-oriented,statically typed programming [Link]'s best if you have been programming
for some time,and are comfortable with an IDE,such as IntelliJ or Android [Link] in, you should
know the basics of programming in an object-oriented [Link] should be familiar with classes,
methods,and exception handling in at least one [Link] don't need to be a Java [Link]
in any modern object-oriented language is [Link] you do know what functional programming and
lambdas are,that's definitely a [Link], we want to provide a course that let's an experienced
developer learnKotlin without having to read another book about the concepts of structured
[Link] is an expressive programming language witha strong type system, featuring type
[Link] has lambdas, co-routines, properties,and many other features that let you write less code
with fewer [Link]'s really started to take off recently,but it's actually been around since 2011, open-
sourced in [Link] by JetBrains, it reached version 1.0 in [Link] 2017, Kotlin is also an
officially supported language for building Android apps,and it's included with IntelliJ,and also with
Android Studio 3.0,and later, out of the box.I'd been asked all week what is it I liked by Kotlin,and I
basically have to stop and say, "How much time do you have?"Because I could go on for an hour,and I
have with some people on the team just to convince them to switch to Kotlin.A lot of us started writing
code in Kotlin,and found it was just a beautiful programming [Link]'s so concise and so
[Link]'s very hard to take a favor toFischer because there are so many of them, and I love them
[Link] is a lot of excitement about Kotlin,and here are some big reasons [Link]'s talk about null
pointer [Link] know null pointer exceptions are bad becausethey have been responsible for a
huge financial losses,spectacular computer crashes and uncountable hours of debugging [Link]
make our life a lot more enjoyable and safer,Kotlin distinguishes between nullable and none non-nullible
data types,and this eliminates many errors at compile [Link]? All [Link] there is [Link]
code is very, very [Link] example, boilerplate code is code that we have to type,and that doesn't
actually affect the way our app [Link], Kotlin takes care of the boilerplate code for [Link]
does remove a lot of boilerplate from [Link] was also designed for readability,and that means code
always stands on its own without any hidden side [Link] boilerplate and improving
readability are sometimes at odds with each [Link] when the Kotlin designers were faced with
decisions like this,they always chose readability.I like to call this just enough [Link] has both
object and functional [Link] of classes, how many trivial getters and setters have you
written?Well, never more. In Kotlin,the compiler does it for you and without [Link] compiles so
that you can use Java in Kotlin side-by-side,which means you can continue to use your favorite Java
[Link] also means that you can start using Kotlin inan existing Java project without having to migrate
everything,and that's a big [Link] if you do choose to migrate,Android Studio and IntelliJ provide a
handy [Link], if you don't know how to program using the Java programming language, don't
[Link] don't need it to start learning [Link] course is designed for programmers from any
language [Link] this course, we are using IntelliJ from JetBrains,the makers of [Link]
you need to work with Kotlin is included with the [Link] add some color,instead of building a usual
weather,shopping list, or chat app,we are going to build an [Link] [Link] [Link] ahead and
install the IDE following the steps in the next text [Link]'ll see you back when you're ready to write
some code.

1.2. Why Kotlin?

1.3. Installing the JDK

Installing the Java Development Kit (JDK)


If you don't have the latest JDK already installed on your computer, follow the steps below. You will
need to have the JDK installed to run Kotlin programs.

The JDK is freely available, and you can download it here:


[Link] in a new tab).

The JDK or the JRE?

The JRE (Java Runtime Environment) is needed for running Java and Kotlin programs. The JDK (Java
Development Kit), on the other hand, includes the JRE, plus the development tools you'll need for
writing and running Java programs. You need the JDK for writing Kotlin Programs.

Steps to install the JDK

1. Uninstall any older versions of the JDK/JRE

We recommend that you install only the latest JDK.

2. Download the JDK

You can download the JDK for free here:


[Link] in a new tab).

Click the "Download" button under the JDK for the latest Java SE version.

Check "Accept License Agreement".

Choose the JDK for your operating system.

3. Install the JDK (for Mac)

From either the Downloads window of the browser, or from the file browser, double-click the .dmg file
to launch the install file.

A Finder window appears with an icon of an open box and the name of the .pkg file.

Double-click the package icon to launch the Install app, and follow the prompts as they appear.

You might need to enter the administrator password to continue.

Feel free to delete the .dmg file to save space after the installation is complete.

3. Install the JDK (for Windows)


Run the downloaded installer (e.g., jdk-10.0.x_windows-x64_bin.exe), which installs both the JDK and
the JRE.

By default, the JDK will be installed in the directory "C:\Program Files\Java\jdk-10.0.x", where x denotes
the version number; and the JRE in "C:\Program Files\Java\jre-10.0.x".

Accept the defaults, and follow the screen instructions to install the JDK.

4. Add the JDK installation path to PATH (Windows only)

Windows searches the current directory and the directories listed in the PATH environment variable
(system variable) for executable programs.

Open "Control Panel" -> "System" -> "Advanced system settings" -> "Environment Variables".

Under "System variables", scroll down to select "Path" and click "Edit...".

Append to the existing Path value a semi-colon ";" then the JDK's "bin" directory (e.g. ";C:\Program
Files\Java\jdk-10.0.0\bin").

1.4. Installing IntelliJ

Installing IntelliJ

Lesson

Downloads

Download and Install IntelliJ IDEA

Download IntelliJ IDEA(opens in a new tab) for your operating system.

Do the following, depending on your operating system:

Windows:

Run the [Link] or the [Link] file you have downloaded.

Follow the instructions in the installation wizard.

Mac:

Double-click the [Link] or [Link] file you have downloaded to mount the macOS disk image.

Copy IntelliJ IDEA to the Applications folder.

More information

Check out this link(opens in a new tab) for more information on how to install and set up IntelliJ.
1.5. Task: Verify IntelliJ Installation

Verify IntelliJ Installation

Check that your IntelliJ is installed and up-to-date by following these steps below:

To check whether you need to install JDK, in a terminal window, type:

java -version

javac -version

If you do not have it, or do not have the latest version, download and install JDK.

Install IntelliJ. This course uses IntelliJ and we recommend you use the same IDE.

Start IntelliJ.

Install any updates and additional content you are prompted for.

Select IntelliJ IDEA -> Check for updates… until there are no more updates.

1.6. Hello, Kotlin

Video Transcript

There are a few steps to get a project [Link] you don't already have a JDK,or Java Development Kit
installed,take a moment to install a JDK before you start [Link] you first start an IntelliJ,you'll
have to click through a setup [Link] default options are mostly spot on.I prefer dark blue theme,but
choose the option that you [Link] you run through the setup wizard,you'll be presented with a
welcome screen like [Link]'ll need to create a Kotlin project,so IntelliJ knows that we're working in
[Link] do that, create a project,select Kotlin in the left-hand navigation,select Kotlin/JVM in the right
panel,name your project "Hello Kotlin," click [Link] you're done, you'll have created a project set
up to run [Link] that you're in a project,you can access the Kotlin REPL or read-eval-print [Link]
contrast to the compiler,a REPL evaluates the code you're typing in right after you hit Command
[Link]'ll find it in Tools, Kotlin, [Link] good tradition, we start with Hello [Link] or paste the
following printHello function code intothe REPL and press Control Enter or Command Enter under
[Link] code is also in the notes below the [Link] execute the code,type printHello, and Control or
Command [Link] you see the result? There you have [Link] have just written and executedyour first
piece of Kotlin code. Let's take a look at [Link] fun keyword designates a function followed by the name
of the function right [Link] is followed by a set of parenthesis where the arguments [Link] function
doesn't have any [Link], we have the curly braces and they frame the function [Link] a
function, we can make [Link] call is for println,which prints one line of [Link] are printing the string,
"Hello World".You do not have to specify a return type because we're not returning anything inthis
function and you may notice that thereis no punctuation at the end of your [Link] semicolons.
Code to enter into REPL:

fun printHello () {

println ("Hello World")

printHello()

1.7. Summary

Udacity part of Accenture logo

Summary

Lesson

Downloads

Video Transcript

And these are the tools you need to work with [Link] the next lesson,you will use the Kotlin
Interpreter to learn about basic language features.
Kotlin Koans

The official Kotlin documentation(opens in a new tab) includes rich tutorials called Kotlin Koans(opens in
a new tab), a web-based interpreter and a complete set of reference documentation with examples. If
you want more information on any topic in this course, or if you get stuck, [Link]
in a new tab) is your best starting point.

Go to the official Kotlin language documentation(opens in a new tab) and find the code for Hello World.

If you prefer to work offline, you can download the entire reference documentation as a single PDF
file(opens in a new tab).

Go to the Kotlin Koans(opens in a new tab). Find and run the program to print Hello World.

Aquarium fun (Optional)

When you are building an aquarium, it is good to have some resources in case you have questions. Open
a Browser and search for "aquarium tutorial" or "how to set up an aquarium". There are many resources
available, check out some of them!

2.1. Introduction

Welcome back. In this lesson,you're going to use the Kotlin interpreterto get comfortable with the basic
language [Link] is best if you just follow along with us,as we type out and explain the [Link]
make this a bit more entertaining,we will go at a good pace,and we're going to wrap all of this into an
[Link], I've had aquarium since my aunt gave me [Link] and all when I was about
[Link]'s everything in an aquarium,to teach your [Link], we couldn't really set up a big fancy
aquarium here at the [Link], we hope you enjoy our small project.I'm not really sure how Kotlin is
like an aquarium,but I bet we'll all find out by the end of this [Link]'s get started with the basics.

2.2. Kotlin Documentation

Kotlin Documentation

Throughout this course you might find it useful to check the official Kotlin documentation every time
you learn something new:

2.3. Operators

Video Transcript
So, for this screencast,I'm going to type the code into the interpreter and you can seeexactly what it
does and then I'll explain it as we [Link]'ll start [Link] along if you want and pause the video if you
get out of [Link], you can just wait and watch,and then practice in the practice [Link] don't
have to memorize all of this on the gobecause at the end we'll give you a Kotlin cheat [Link] first, let's
get operators out of the way,plus, minus, times and [Link] your typewriter, typeone plus one
command enter for one fish plus fun fish to the [Link] you get 53 [Link], three of the baby
fish are eaten by [Link] the fish keep fighting over food,you want to divide the fish into batches
of [Link] an integer always returns an [Link], one divided by two return zero,and 1.0 divided by
2.0 returns to [Link] your fish keep multiplying and after six months you have a lot of [Link]'ll cover it
a bit later,but Kotlin let's you override the basic [Link]'re not just stuck adding one's and
two'[Link] can define your own fish,and give them the ability to [Link] edition about
addition,Kotlin keeps numbers as primitives,the same number types your CPU [Link], it'll let you call
methods on them like they're [Link] means you can call methods on the number one like one
times [Link]'s a few things to note about basic [Link] you can use numbers as if they were
objects,Kotlin also supports object wrappers for [Link] the numerical types in Kotlin have a
supertype called [Link] if you try to store the value one in a variable of type number,it'll need to
be placed in an object wrapper,this is called [Link] it will be implicitly done whenever it is needed
in your [Link], as a developer,you can avoid creating these objects wrappers by not
storing numbers in [Link] write useful code you need to be able to store [Link] make your fish
more permanent,you can store them in [Link] are two types of variables in Kotlin; changeable
and [Link] val, you can assign a value [Link] you try to assign something again, you get an
[Link] var you can assign a value,and then you can change [Link] that in all of these the type is
inferred,which means the compiler can figure out the type from the [Link] so the type is
inferred,it becomes fixed at compile time,and you can not change the type of a variable inKotlin once it's
type has been [Link] course, you can use variables in operations and there is no punctuation at
the [Link] the topic of [Link] types won't implicitly convert to other types so you can't
assigna short value to a long variable or a byte to an [Link] does this becauseimplicit numerical
conversion is a common source of errors in [Link] can always assign them by casting like [Link]
may have seen syntax like this before,Kotlin supports underscores in [Link] you can specify long
constants in a format that makes sense to [Link] type is inferred by [Link]. Let's do something
that's really [Link]. Earlier, we talked about how Kotlin helps avoid null pointer
[Link] you declare a variables type explicitly,by default, it's value can not be [Link] the
question mark operator to indicate that a variable can be [Link] you have complex data types such
as a list,you can allow for the list to be null,but if it's not null,it's elements cannot be null,or you can
allow both the list or the elements to be [Link] you really love no pointer exceptions,Kotlin will let you
keep [Link] not know assertion operator can force your way past a nullable type in [Link], it
will still throw an exception when it's [Link] programming slang, the exclamation mark is often called,
[Link] the Wikipedia article in the [Link]'s a lot easier to say double bang then not null [Link]'s
generally a bad idea to use the double bang operator andthat's why they've made you type two
exclamation marks instead of [Link] can do some cool null testing with the question mark
operator,saving you the pain of many if else [Link] example, you can check whether an object
orvariable is non null before accessing one of its [Link] can and will only give fish treats if they
are tweets [Link] can also chain null tests in an expression,like this return statement,if fish food
treats is not null,use a treat and return a new value and otherwise,return the value after the colon,
which is [Link], in this expression,we are checking whether fish fruit treats is [Link] it is not, we remove
a treat and return a new [Link] fish food treats is null,we stop evaluation, so that the decrease method
is not called on a null [Link] we return the alternative value after the colon,which in this case is
[Link] at the question mark colon [Link]'s called the Elvis operator,because it's like a smiley on
its side with the way Elvis Presley style his hair.

Kotlin Language Documentation(opens in a new tab)

Kotlin Koans(opens in a new tab)

Exclamation Mark in Computing(opens in a new tab)

2.4. Building the Aquarium

That was a lot of [Link] we move on to Strings,let's take a break and look at what's happening
with our aquarium.

2.7. Strings

Of course, we have strings in Kotlin,and they were pretty much like strings in any other [Link]
can concatenate strings using plus,but even better you can use string templates tobuild strings by
combining them with [Link] dollar variable name is replaced by text representing its [Link] as a
preview of things to come,you can also do [Link] two numbers get added first,and the result is
[Link] other languages, Kotlin has a Boolean data type and [Link] equal sign equals a by
value comparison,and of course, you have not equal, greater,smaller or equal operators just like in other
[Link] that we can get to the truth of matters,we can test for [Link] two basic ones in
Kotlin are if-else and [Link]-else is for comparing [Link] Kotlin thing is that you can be more
flexible aboutyour conditions by using ranges, like [Link]'ll see more ranges in a moment,and when,
which is Kotlin's way of doing [Link] can of course nest ifs and whens to your heart's delight,and
again, you can use ranges.

2.10. Arrays and Loops

Video Transcript

Now, here's the thing about [Link] your val variable value is a reference,then you cannot assign it a
different reference [Link] if you're referencing something that's not immutable,it can still [Link]
only applies to the reference,and it doesn't make the object it points to [Link] this example, we
cannot assign a different list in my list,but we can manipulate the elements of the lists,such as removing
an [Link] for loops, we need something to loop [Link], here are lists and arrays as examples of
[Link] work pretty much as you'd expect with some cool [Link] can create an array
with [Link] can create typed arrays,for example integers, but if you add another type element it
gives you an [Link] you can mix types in untyped [Link] print the array,you can convert its
contents to a string with [Link]'ll probably see an error in the [Link] for the tool tip,and
then press options enter to import arrays from the standard [Link] can nest [Link] you put
an array within an array,you have an array of arrays,not a flattened array of the contents of the two.A
really cool feature of arrays in Kotlin isthe ability to initialize them with dynamic [Link]'t worry too
much about the syntax for [Link] important point is to see how much boilerplate code this cut-
[Link] can read this as initialize an array of five elements,assign each item to its index times [Link]
you have arrays,you want to do something with their elements and that often happens in a for
[Link] is your basic loop over an [Link] are a few other things you can do with for [Link] can
loop over the index along with the elements,which saves you from having to usethe index to get the
element inside the body of the [Link]'s much more readable this [Link]'ve seen ranges and here is
[Link] can do ranges in an [Link] can do ranges of [Link] can do a range going
downwards,and you can do a range advancing in multiple steps.

3 . Function

3.1. Introduction

Now, that we've flown through the very basics,you are ready to write some Kotlin code that actually
does something [Link] aquarium could really use some [Link] importantly, it needs an
AMS,an aquarium management [Link] AMS will feed the fish and change the water for [Link] of
course, the AMS needs a Kotlin program to run [Link], in this lesson,you're going to write parts of a
Kotlin program withfunctions and functionality to manage an aquarium.

3.2. Main Function

Video Transcript

This function has all the basic parts of a Kotlin [Link] starts with the keyword "fun."Then, comes the
name of the function,printHello, followed by parentheses for [Link] we don't have any
arguments andthe function body where things happen inside the curly [Link] we execute the
function by invoking its name followed by the [Link] instead of using the interpreter,we want to
write and save a program in IntelliJ,we have the following [Link] the left-hand navigation,there is a list
of all our project files and [Link] and right-click the source [Link] new Kotlin File Class,name
the file "AMS" and click ''OK''.The file is now in your source folder,and it's extension [Link] can now
put our code into this file and eventually execute [Link]'ll also use the repl and switch back and forth as
it's [Link], to run an actual Kotlin program,we need a main [Link] is always the entry
point for [Link] Kotlin, it looks like [Link]'s called [Link] takes an array of strings asits arguments
followed by the function body in curly braces,and we print "Hello World."This function has no return
statement,but every function in Kotlin returns something even when nothing is explicitly [Link], a
function like main returns a type unit which is Kotlin's way of saying no [Link] when a function
returns unit,you don't have to specify it [Link] is different from other languages where you have
toexplicitly say you are returning [Link] run this function,click on the green triangle next to the
main function and select Run [Link] will take a moment to build,and execute the code,and then
you can see the result in the log window that
3.4. Main Function

To demonstrate how to run main with arguments,we can pass to maine whom to say hello [Link]
Run, Edit Configurations,type Kotlin for the program arguments,and close the configurations
[Link], change your code to use the [Link] world with a dollar sign to start a string
[Link] a string template,instead of a string,you put instructions for how to get a string,either from
a variable,or from an [Link] current value of the expression after the dollar sign will be inserted
for [Link] this example, it should be the value of the zeros argument to main,args[0], which is
[Link], we get the value of args[0],and put it inside curly [Link] need to put args[0] inside curly
braces,because it's not a [Link] the first element in the array is an [Link] other
languages have statements,which are lines of code that don't have a [Link] Kotlin, almost everything
has a value,even if that value is [Link] programming language lingo,we say that everything in Kotlin is an
[Link] is really cool, because it means for example,you can use the value of an 'if' expression
right [Link] need to make an extra variable to figure out which branch was [Link], for example we
can assign an 'if' statement to a variable,or even use it as a string [Link], we're checking the
temperature to say a fish isfried or safe based on the temperature,and if we print that out, we'll see the
fish is safe.

3.5. Greetings, Kotlin

Udacity part of Accenture logo


Greetings, Kotlin

Lesson

Downloads

Free Response

Exercise: Greetings, Kotlin

Create a main() function that takes an argument representing the time in 24-hour format (values
between and including 0 -> 23).

In the main() function, check if the time is before midday (<12), then print "Good morning, Kotlin";
otherwise, print "Good night, Kotlin".

Notes:

Remember that all main() function arguments are Strings, so you will have to convert this argument to
an Int before you can apply the check.
Advanced

Try to use Kotlin's string templates to do this in 1 line.

3.6. Random Day

Now, that you've made a file with a main function,it's time to define some more [Link]'s add a
function for our AMS that feeds the fish different foods on different [Link], let's define a function,feeds
the fish that will hold our AMS logic and call it from [Link]'s make feed the fish to [Link] goal
is to pick a random day of the week and output a different food for each [Link]'ll build it one step at a
[Link], let's put the basic structure [Link] the body of the feed the fish function,add a
variable called "day" and assign it to "Tuesday".To start with, our AMS will only know about
[Link] a second variable called food that is pellet'[Link]'s what fish eat on [Link], add a
print statement using a string [Link] will print out today and [Link]'s run our program again
and you'll see the output,today is Tuesday and the fish eat pellet'[Link] our functionality, we want
to selecta random day of the week each time this program is [Link] feed the fish,create a function
called random day with no arguments and it returns a [Link] are using strings for the weekdays in
this example because it'smore readable and the final destination is to print the [Link] the function,
create a list of weekdays as [Link] that, returned a value at a randomly chosen index inside the
[Link] gives us an error because we have to import the [Link] are some things to notice
about this finished [Link] are importing a Java library for random because under the hood,Kotlin
works seamlessly with Java [Link] the return statement,we get a random number between zero and
[Link] is the boundary and [Link] then, we return the associated [Link] feed the
fish,replace Tuesday with a call to random [Link] we run the program again a couple of times,we see
a random day of the week in the output.

3.8. Fish Food

Now, let's serve the fish a different food each day of the [Link]'ll make another function for that,and
call it fish [Link]'s call fish food from feed the [Link] fish food will return a different food for each
day of the week,we need to parse the day as a [Link], let's define fish food.A parameter is
specified by its name,a colon followed by the [Link] we have the colon and the return [Link], fish
food takes a day as a parameter,and it will return a [Link] is the first version of the function
body,where we choose and return the [Link] when statement looks at the day variable,and picks the
correct [Link] that you don't have a break at the end of the when [Link] works a lot like case
statements,but it automatically breaks,saving you from writing boilerplate code or writing accidental
[Link] we can do much [Link] everything in Kotlin has a value,the when expression also has a
[Link] value will be the last expression of the branch that was [Link], we can return the when
valuedirectly without needing to assign the result to a [Link] we do that, we must have an else
branch to cover all [Link] else case is the catch-all,if none of the other branches [Link] make
sure the else case happens sometimes,let's remove Tuesday and [Link], we can run the code
and see how it outputs the different days,and foods, and fasting for the missing days.

You might also like