Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Hello, everyone, and welcome to the complete Android app development masterclass. My name
is Mason. I'm a professional Android app developer. And I'm going to be your instructor for this
course. Before starting the course, let's talk about it for a few minutes. This course
is about 15 hours. And by watching it, you're going to learn a lot about Android app development.
But it is impossible to cover everything in just 15 hours. For that I recorded an extended
version of this course, which is about 60 hours. And in that you're going to learn everything
that you need for a successful career in Android app development. You can find the extended
course by clicking on the link down below this video. Let's see what we are going to
cover in this video. In this 15 hours, you will start from the very beginning. That means
that even if you don't have any prior experience in programming, you're going to do just fine.
We will cover everything in the course in the first section of the course he will set
up the environment that you need for Android app development. That includes installing
Java Development Kit, Android Studio software development kit for Android app development,
and emulator for testing your applications. Besides setting up the environment. In the
first section, you will create a small application to get familiar with Android app development,
and each piece in an Android application. In the second section of this course you will
learn Java language Java is the most popular language for making Android applications and
in this section, we will take a deep look at that you will learn about different variables
and operators, loops and conditional statements, different collections in Java concurrency
and trading, handling exceptions Singleton pattern and a lot more. You will also learn
about object oriented programming in Java. That includes things like classes and interfaces,
inner classes and abstract classes, inheritance and polymorphism. And a lot more, we will
end this section with a challenge. So to make sure that you have learned everything we have
talked about. In the next section, you will learn about user interfaces, you will learn
about how to design modern layouts for your applications. In this section, we will talk
about things like buttons and text views, different layouts, working with images and
fonts showing a list of items with ListView and recycler view. We will talk about snack
bars and card views, adding animations to your applications. And last but not least,
we will talk about material design in Android. Once again, we will end this section with
a challenge so to make sure that you have learned everything. After learning Java and
designing user interfaces, you have the ability to create a lot of applications. In the final
section of this course we will create one real world application from scratch. We will
combine all of our knowledge about Android app development and we will create this application
together in this section beside practicing everything we have learned so far, you will
learn about a lot more topics. For instance, you will learn about persisting your data
with shared preferences. And also you will learn how to show different websites in your
application. And of course a lot more. You can also include this application in your
portfolio if you want. We are going to set up the environment that we need in order to
create Android applications. We are going to install few things. First of all, we are
going to install Java Development Kit or as some might call it JDK. After that we are
going to install Android Studio. And after everything we are going to create a virtual
device in order to test our Android applications. As you can see, I have already downloaded
JDK and Android Studio. Let's also see where we can get them from. For JDK, you can simply
search for Java JDK, and a link from Oracle website should pop up. The first link from
the Oracle web page is probably our needed link. As you can see, we are going to download
and install JDK version eight, it's going to be enough for our purposes in this course,
down in here. First of all we need to agree to the license agreement. And after that,
depending on your operating system, you can download one of these versions. I'm on Windows,
so I'm going to download and install this windows x 64 version. If you're not sure about
this x86 or x 64, you can right click on your my computer on your desktop in properties.
In here you can see the architecture of your CPU minus 64 bit for you it might be x86.
Depending on that select one of these two files, just click on this download and the
download should be started. Sometimes like here, it might ask you to log into your Oracle
account. Let's do that creating an Oracle account is free and you won't be charged.
Okay, let's select Sign in. And as you can see the download has begun. I'm going to select
the canceling here because I have downloaded the JDK previously. Let's select that. It's
going to be a really simple installation process. Let's select Next.
Once again next. Once again, let's click on Next. And as you can see, we have successfully
installed JDK. Let's click on this plus, if you want to make sure that you have installed
Java JDK successfully, you can always go to your command prompt by going to start menu
and typing cmd. In here, you can type Java dash version. As you can see, we are seeing
some information about the version of Java installed on our system. As long as it's 1.8.
and higher, we are good to go. Let's close our command prompt. Next, we are going to
download Android Studio. For that we can go to developer.android.com. Let's see developer.android.com.
During the course, we will come back to this website a lot because this is the official
website for Android developers. Okay, for downloading Android Studio, just go to this
Andrew the studio tab. And from here, you can download one of the versions. If you don't
see your version of operating system, you can click on this download options. But in
here, I'm just going to click on the download, let's say download Android Studio, we need
to agree to the terms and conditions. And as you can see, the download has started.
Once again, I'm going to select to cancel in here because I have already downloaded
Android Studio. After the download is finished, just click on that. Once again, this is going
to be a simple installation process. Let's select Next in here, make sure to check this
Android virtual device. Let's select Next in here you need to select a path to install
Android Studio. Notice that installing Android Studio and all of its SDKs. And also emulators
sometimes might take something like 20 or even 40 gigabytes. So make sure to have at
least 20 gigabytes on the drive that you are going to install your Android Studio. After
that. Let's select Next, and install. After the installation is finished, just click on
this next. And let's start our Android Studio. Don't worry about these three projects that
I have in here. Basically, these are the recent projects that I was working on. I have Android
Studio installed on my system previously, and I uninstalled it for this video. But for
you this list would be empty. Also along the way of installing Android Studio you might
see some windows is about selecting a theme or other preferences. I didn't see them because
I have installed Android Studio previously on my system. Those are not important depending
on your preferences, select one just if you see something like installing SDK on check
that because we are going to install everything here together. In order to install SDK, you
can come to this configuring here. And you can select this SDK Manager SDK stands for
software development kit. And for developing Android applications, you need to install
at least one SDK. Once again, as you can see, I have installed two SDKs in here and return
an Android nine API's both for you this list should be empty and none of these should be
checked. First of all, let's check this show package details in order to see the package
details for every SDK. By the time I'm recording this video, the latest version of Android
is Android 10 or aka level 29. To be precise, I suggest that you always install the latest
version. But sometimes there might be some preview versions in here, I don't suggest
installing those because those can be buggy. And those are not yet released on a stable
channel. So make sure to install the latest stable version. If you want, you can install
the complete SDK. But I don't suggest that because these can be memory and hard drive
consuming. And these can occupy a lot of the storage on your device. But the necessary
things in here is first of all Android SDK versions, whatever the version of your API
is. So the first one, also the sources for that version of SDK. The second option, also
I suggest that depending on the architecture of your system, select one of these two version
of atom mine was 64 architecture. So I'm going to select this one. If you are going to work
with different Google API's. For example, if you are going to work with Google map or
YouTube API's, check one of these, for example, this Google API's for atom 64 and also check
one of these two Google API versions. This is going to install at least one SDK on my
system, and return or API level 29. But if you want, you can select and install multiple
SDK versions. Basically, this SDK versions and API's means that in what version of Android
Are you going to run and test your applications later on. We will see that when we are going
to run our application On a virtual device, you're going to need at least one SDK, installing
multiple SDKs can be memory consuming and also harddrive consuming. But if you have
the resources, I suggest that you at least install the latest version after that, install
this API level 29. Because there were a lot of changes specifically in the data storage
structure also installed API level 28, down in here installed API level 26. And after
that, level, 21, and also 19. Because in this version of SDKs, there were a lot of changes.
And I'm just saying this because of my experience in Android app development. But if you don't
have the proper resources, selecting one of these is going to be just fine. After selecting
what you need in here, just click on this apply, make sure to have a stable internet
connection because it's going to download some stuff. Let's click on this apply. In
here you can see a dialog that will show you some information about the stuff that are
going to be downloaded and also the size of the download file. Next, select OK in here,
and our download should be started. I'm going to skip the video in here until the download
is finished. After the downloading and installing all of the components that you have selected.
Before it's finished, just click on this finish. And let's see what else do we have in here.
Okay, we have successfully installed at least one SDK, let's switch to this SDK tools. And
let's see what we need from here. First of all, once again, check this show package details,
install one of these Android SDK build tools, I suggest that installed the latest version.
As you can see, mine is installed in here 29.0 point two at the time is the latest version.
Also down in here, you need to install Android emulator mine is not updated. So I'm going
to update that. The next two things that are mandatory are these SDK platform tools and
SDK tools, make sure to select those two. Also down in here we have this documentation
for Android SDK. This option is not necessary, but it's going to help us a lot during our
journey in Android app development. Later on, we will see that these documentations
will help us a lot when we want to debug something or when we faced some errors. So I strongly
suggest to install these documentations as well. The other thing that is not necessary,
but I suggest to install is this hexam installer, it is going to be an accelerator for the speed
of your emulator. The native emulator in Android Studio can be various low, especially if you
have a low memory Ram. For that reason, I suggest you to install this hex installer.
But there is one thing that I need to mention in here. I've seen a lot of students having
problem with this hexam installer, I've seen them that they cannot install these hacks
them successfully. This hex m cannot be installed on all of the CPUs. And depending on the structure
of your CPU, this XM might not be installed. But there are some CPUs, especially Intel
CPUs that they have a special option in the devices bias is specifically for this emulator
accelerator. If that's not enabled, you may want to enable that in the BIOS settings as
well. And also if for any reason you couldn't install this handsome installer, this emulator
accelerator. And also for that matter, the Android emulator itself, there is no need
to worry there is another way as well. Besides the native Android emulator, you can install
external emulators as well for example, the one that I know that is working very well
is called genymotion. In here, I'm not going to install that but I will put a link to download
genymotion in the description of this video. And also besides all of these, you can always
install and test your applications on the real device that you have. Later on in the
course we will see that how we can install and test our applications on our mobile phones.
Okay, after selecting everything from here, just click on Apply once again. Once again
we are seeing the download the information like Select OK and that the download begin.
Once again. I'm going to skip the video in here until the download is finished. Once
again When the download and installing is finished, just click on this finish. And we
have successfully installed SDK platforms and SDK tools just before I close this window,
let's switch to this appearance in here. From here you can change all of the preferences
that you want. For example, you can change the theme Dracula is the dark theme. Also
we have this high contrast theme. The intelligent theme is the light theme. I'm going to stay
with that because it's good for recording purposes. If you want you can use custom fonts
for the fonts of your Windows is from here. You can also change the size of your font.
I'm not going to do that. Also then in here, you can change The font size. Also for the
presentation mode. If you want to change the size of your codes, you can search for fonts
in here, I believe it's in this editor tab in here in this font, you can see that mine
is 18, you can increase or decrease that if you want. Let's delete this one. And also,
let's minimize this editor. I have another suggestion for you in here as well, in this
appearance inside the system settings inside the updates, make sure that your updates are
being checked from a stable channel. We have other channels like developer channels and
betha channels as well make sure that you have the stable channel. Others can be buggy.
Okay, that's it for installing SDK, we will be coming back to this setting menu later
on. Let's select OK. OK, now it's time to install at least one emulator. For that we
can once again select this configure this time, we can click on this AV manager AV D
stands for Android virtual device. As you can see, once again, I have two devices installed
in here pixel two and pixel three, this list should be empty for you. But because I had
Android Studio installed on my system, I can see them let's select this create visual device
in order to create a new virtual device. First of all, you can create your own and customize
the hardware by selecting this new hardware profile. From here you can name your device,
you can select that if it's a phone or tablet, you can set the screen size and resolution.
Also then in here you can specify some ram to it and all sorts of sensors and hardwares.
I'm not going to create my customized device. Instead, I'm going to install one of the predefined
devices. As you can see, I have pixel two and pixel three in here. Let's select another
device from here. Also from this left panel, you can see all sorts of devices like TV viewers
and tablets. Let's select one device from here. I don't even know all of these devices.
Let's just select this pixel Excel, select Next. Okay, from here, you need to specify
the Android version that is going to be installed on this device. I'm going to install Android
q or API level 29 on this device, so we need to download that first, let's select Download
once again, I think it's going to be a large five. So I'm going to skip the downloading
here until the download is finished. Yes, it's 1.1 gigabytes. Okay, the download and
installing is finished just click on Finish. Now we can select the downloaded API, sometimes
you might not see the version of API that you're looking for. For that, first of all,
you can always refer to this list. After that, you can check the second tab and also the
third tab. But we are going to install Android q on our device. Let's select Next in here
we can customize our device. For example, we can change the name of our device, we can
also change the default orientation. For example, the landscape and portrait I'm going to save
its portrait. In this Show advanced settings, we can do some more customization, for example,
you can change the camera, the network. Also down in here, you can change the size of RAM,
and SD card and all sorts of customization. I'm not going to change any of these. I'm
just going to click on this finish. And as simple as that we have installed a new emulator
on our device. Okay, let's close this window. Okay, I think it's a good point. To finish
off this video. In this video, we have installed JDK eight we have installed Android Studio.
After that we have installed SDK and we have done some customization on the appearance.
And after everything we have installed a new virtual device.
We are going to create our first application we can do so by opening Android Studio and
selecting this start a new Android Studio project. First of all, in this window, we
can see some templates. These templates are just some small applications that have some
codes written to some level. For example, in this basic activity, you can see that we
have a small button and also a menu above in here. We can see other templates in here,
for example, a template for Google Maps, we have a template for navigation drawer. Don't
worry about all of these because we will learn how to create all of these elements ourselves
later on. Even we select this ad no activity template. But the Android team are nice guys,
and they have provided us these templates. In a lot of these templates. You can see the
word activity Don't worry about this activity yet. If you remember from the content videos,
I said that we are going to have an entire session which we will talk about activities
and fragments. But for now you can think of activities as different parts of your app.
application. For example, if your application has different pages and activity is one of
those pages, this definition is not accurate, it might not even be correct. But for now
we can think of an activity as a page of our application. In our first application, I'm
going to save it this empty activity, which has no element, it's just going to create
some files for us. But if you want to save it, at no activity, you can do so as well.
Once again, later on, we will see that how we can create all of these templates ourselves.
Okay, let's select empty activity. And let's hit next. In here, we need to provide some
information about our application. For example, the name of our application, I'm going to
change the name of my application to let's say, Hello world. As it's a cost among developers
to name their first project, I level just for no reason. The second part, you will define
the package name for your application. This package name should be unique to every application
that is being released into the world. later on. When you want to publish your application
to play store, your application will be identified with this package name. The convention in
here is to provide the name of your website in backwards. For example, My website is called
maker.org. So my package name is or dot maker dot the name of my project. Don't worry if
you don't have any website, if you don't, you can use something like conduct example.
Let's say comm dot example dot the name of my project. But since I have a website, I'm
going to use that in here, I'm going to say port that. Once again, this package name should
be unique to every application. After that, you need to define the path that you want
to save your project. If you want, you can change this path by selecting this folder
in here and select the path but in here, I'm not going to change that. After that you can
select the language that you're going to work with. Android applications can be written
with Java language, and also kotlin language. In this course, we are going to stay with
Java. But if you want, I am currently recording a new course about developing Android applications
with kotlin. Feel free to check that if you want, but let's select Java in here. After
that, you need to define a minimum API level. This minimum API level in here means that
how many older Android devices can install your application. For example, if you click
on this drop down in here, you can see the list of different API levels. Right now, the
minimum API level that I can select these 14, but I can go off to API level 29, or Android
10. To be precise, there is a balance in here. If you select your minimum API level very
high, you can work with a lot of newer functionalities in Android, but fewer devices will be able
to install your Android applications. For example, if I select API level 29, which is
the latest version, only less than 1% of the devices can install my application. If I select
something like 19, we can see that 95% of devices by this time can install my application,
you as the developer should decide this balance for this course, probably, we are going to
stay with API level 19 for most of our projects, because I think it's a good balance, and 95%
is a good number. Leave this check empty for now. Also, using Android x artifacts is mandatory
since few months ago, we will talk about it later on in the course Let's hit finish. And
let's create our first project. If it's the first time that you are creating your Android
applications, this process can be very slow because Android This video is going to download
some stuff, for example, the gradle file from internet so for that reason, this process
can be slow if it's the first time that you are creating your application. The first thing
in here that is noticeable is that Android Studio have created two files for us this
activity main dot XML file, and this main activity dot java file. These have been created
because we have selected that empty activity template when we have created our project.
Inside each of these files. There are some codes. Once again, we can create all of these
codes if we don't select that empty activity. But if you select that, you can have some
codes written to some level. The other thing in here is this view pane down in here. This
build painting here will show the progress of building your Android applications whenever
you want to run your Android application, whether that is in the debug mode, which we
will be working in our entire course. Whether it's in the production level, this building
here will show the progress of building your Android applications. I'm going to minimize
it for now. And let's talk about all the other stuff that we are seeing in here. First of
all, let's talk about this project thing here, I can minimize and maximize that. Also, talking
here, we have multiple options. For example, we have this project option. This project
option view exactly shows the structure of files on your system. For example, you can
maximize every folder. If you want to have access to this main activity dot java file,
you can go to App folder inside the sources. Inside the main folder inside the Java folder,
you can see that we have a folder in here, which contains the main activity java file.
This is the exact hierarchy of files on our system. If you want to make sure of that,
you can right click on the name of your project and select Show in Explorer. This option in
here, it will open the Explorer on your Windows, you can see that we have a Hello World project,
don't worry about the others inside this project. As you can see, in the hierarchy, we have
this app folder. Inside this app folder, we have a source folder, main folder, Java folder,
org, may code HelloWorld. And this is the Java file that we are seeing here. So this
project view will show the exact hierarchy of different files on your system. There is
another view in here and that's called Android view, we are going to stay with Android view
for the most of this course, because it's much more easy for eyes to follow. As you
can see, we have this app folder. And inside that we have this Java folder, we have this
package name. And inside that we have this main activity dot java file, it's much more
easy for eyes to follow different files. Also, besides this app folder, we have these Gradle
scripts down in here as well. Before I talk about these Gradle scripts, I need to talk
about the structure of every Android application. So in most Android applications, there are
at least two different kinds of files, the layout files, which will define the looks
of your application. The layout files in Android are called XML, you can see this activity
main dot XML file. If you click on that, you can see the layout of your application. Right
now it has only one simple text. The other part of every Android application is the Java
file. If you are developing in kotlin language, there are kotlin files. The Java or kotlin
file in most cases will define the logic of your application. For example, what happens
if you click on a button, or what to do with the user's input, all of those will be handled
in the Java or kotlin files. Beside the layout files and Java files. In every Android applications.
There might be some other files, for example, you may have some images, and also some audio
files as well. Those will be inside this Resources folder, for example, inside the meat map folders.
Right now we have an icon for our application, which is this simple Android icon. Either
images or other files can be put inside this Resources folder as well. Basically, this
Resources folder is for the static variables or static phones on your application. So these
are the three main components of every Android application. We have layouts, we have the
Java file, and also we have the static files. Beside these we have this manifest file inside
the manifest folder. This manifest file Vila is specify some general properties for your
application. For example, you can see the icon of your application in here also, you
can see the name of your application, which is HelloWorld, in this case, and some other
properties or features about your application. They will talk about this manifest file later
on in the course. But for now, let's close it. We have all of these different files in
every Android application. But for example, when you are going to download some application
from the Play Store, you will see that we have only one file, one apk file. That's the
final application that will be released, how are we going to generate that apk file? Well,
in that case, this Gradle tooling here is going to be useful. gradle is a build tool
that will combine all of these files and other files, and we will create an APK file. No
matter if you want to release your application or if you want to test your application on
an emulator, you are going to need Gradle for that reason we are going to work extensively
with The other use of Gradle is that if you want to use other people's code in your project
as well, for example, if you want to use YouTube player in your project, you will add its dependency
inside this built up gradle file down in here, just by writing one line of code Gradle will
download the code for YouTube video player, as simple as that Gradle will add the YouTube
player code to your project. And after that, you can use it. So Gradle is a build tool
that will handle the combination of every file, and also other codes and we will generate
an APK file, we will talk about different Gradle scripts later on in the course. Okay,
let's close all of these extra files. Okay, this was the project when we can minimize
or maximize this pain from here. Let's minimize that. And let's talk about other pains available
in Android Studio. Then in here, you have some useful pains as well, for example this
to do is very helpful when you are creating or when you're developing Android applications
to those are some works that you don't want to do at the time, but you don't want to forget
those works. For example, inside this main activity, I can add a to do as simple as that
I can say to do, let's say complete
this. It's just for the developers to remind themselves or other colleagues to do something
later on. As you can see this to do has been added inside this to do pain as well. If you
want, you can check that it says that it's inside the main activity. Later on, we will
talk about how we can define to dues and also other kinds of comments. So this is one of
the other pains. The other one is this terminal dining here. This terminal is like the command
prompt in Windows or let's say terminal in Linux or Mac. And it's very helpful if you're
working with something like ADB or Android debug bridge. Later on, we will see the use
of ADB in our projects. Also, it's very useful for working with SQL lite databases, we have
worked extensively with this terminal in the database session. After that we have this
new tool here we have seen it when we have created our project, you can see the progress
of every application when it's going to be built. In a lot of cases, there might be some
problem when we build our project. And from here, you can exactly locate the problem.
After that we have this logcat in here, it's going to be very helpful when we are going
to debug our Android applications. In fact, we are going to work a lot with this look
at Okay, let's minimize all of these for now. Above. In here we have some menus, we have
File, Edit view and all of these, we will work with most of them, I believe in the course
but it's impossible to work with all of them. Also, there is one thing that I need to say
in here. When you create your first application, you may get some warnings, for example, a
rendering warning or some other kind of warning, if you get those probably there isn't something
wrong with your application, you just need to rebuild your project. For that you can
come to this build option in here and make your project once again, in case if you get
some errors at the first time that you create your application. Sometimes newer versions
of Android Studio can be buggy. And creating your project once again simply can solve your
problem. Also, there is another option from here from five, you can close your project
and reopen it that can be useful or in some cases, you may want to invalidate your cache
and restart Android Studio. So if you get those warnings or errors, we can try these
three different ways in order to overcome the problem. There is one very useful tool
that has been added to Android Studio recently in the past few months. And that's called
Android profiler. Let's add that to this pane. down in here as well. We can add that by going
to this view to Windows and profiler, it's going to be helpful for monitoring the resources
that your application is using, for example, the amount of RAM the CPU that your application
is using, or even the amount of network that your application is using. If you're on our
application, we can track the live amount of different resources that our application
is going to use. Let's run our application and see for ourselves. You can run your application
by clicking on this green triangle from here, but before that, make sure to select the current
app that you're working. And also beside that make sure to select the device. In the previous
video we have created a virtual device and we can see the list of different virtual devices
available. I'm going to run my application on these So through API level 29, for no reason,
let's just run. If we click on this building here, you can see the build progress, I said
that even if you are going to debug your application, or even if you are going to run your application
at the production process, you can check the progress of build as well. As you can see,
this emulator has been opened. And we will see our application in a few minutes. Also,
from down in here, you can see the current status of running your application. Right
now it has created or built my application successfully, and it's waiting for the device
to turn on. Also, if you check this logcat in here, you can see that there are a lot
of things going on in here. We will be using this logcat extensively later on in the course,
for different debugging purposes. Let's minimize that. Also, if you open this profiler, you
will see the live tracks of different resources that your application is using. Let's open
our emulator. Right now we have this simple application in which has a predefined text
called hello world. And if you want, you can check the amount of CPU or other kinds of
resources that your application is
using, it's going to be helpful when you're not sure how much resources your application
is using. Let's minimize that for now. If you want, you can close your application with
this red button here, you can stop it. And also if you want, you can turn off your device.
By long pressing this turn off button, you can say power off and your device will be
shut down. As you can see, we are getting some warnings in here right now, these are
not important. But if you want to check them, you can click on this event log. And everything
that happens since we click on this green triangle, we will be logged in here. Okay,
let's minimize that. And let's switch back to our activity main dot XML file. And we
have a lot of options in here as well. Right now you can see two different views of your
application. This white one is called the design view. And this one in here is the blueprint
view. There are some slight differences between these two, for example, if you have some invisible
item inside your activity that might not be visible inside this design view. But you can
keep track of that inside the blueprint view. For now, I'm going to disable this blueprint
view because we are not going to work with that just for now. In order to disable that,
you can click on this stack icon. And you can see just design, we can also minimize
and maximize that by pressing this plus button. At the left panel in here, you can drag different
elements different user interface or UI elements. For example, if you want to add a button to
your application, you can simply drag it like this. And this button will be added to your
application. As you can see right now because this element is clicked, you can see different
attributes. At the right panel, we have a lot of attributes for every user interface
element. The most important one is this ID above in here. This ID is the unique ID of
your element in which we will use it in order to have access to this button, for example,
from inside of Java file. Later on, we will talk about that if we click on our text view,
you can see that we have different attributes as well. These are just a few attributes.
If you want to check the list of all of the available attributes, you can click on this
all attributes. And you can see that there are a lot of them. We are going to get familiar
with different UI elements in the UI session we have that I think in two sessions from
now, after the Java session we will be talking about user interface in a specific details.
Also, when you are inside your XML file, you have two views design view and text view design
view is this view that we are working right now. And if we switch back to text view, you
can see the exact value of every UI element in XML code. xml is a markup language. If
you are a web developer, you know the concept of a markup language. HTML is also a markup
language. markup languages are just for defining some elements that user is going to see, for
example, a button or text view, you can see that there are a lot of attributes in here
but that the don't know anything about them yet. We will be talking about them later on
in the force. So there are two ways of defining UI elements. One is that You drag simple items,
for example, a button to your design view, or from the text view, you can type it, you
can see that we have a button in here, we can select the whole button. And we can simply
press Delete, and that button will be deleted, you can see that we no longer can see that.
Also, if you don't have this preview enabled, you can enable it from here, let's just press
Ctrl Z in order to get back our button. And let's switch back to our design view. I'm
going to define an ID for this Hello text because later on inside my java file, I'm
going to identify that and I'm going to work with that. So by clicking on HelloWorld, you
can see the list of attributes. At the top, we have this ID, we can set an ID for our
TextView. Also, if you don't know the difference between a text view, edit text, and all of
these don't be very TextView is just a fancy way of saying a simple text, you can define
the ID whatever you want. For example, in here, I'm going to say txt message. But notice
that there are some conventions in here, you can't use a spaces when you are going to define
an ID also beside that you can't use some weird characters like $1 sign or a hashtag
sign. The other convention in here is that you type the first letter in lower cases.
For example, this team here is learn cases. And also after the first word, you type the
first letter of the second word. in capital letters. For example, we have this m here,
there are some just basic conventions among programmers by pressing Enter, we now have
an ID for this hello world. If we switch back to our TextView from down in here, we can
see that for this TextView, an attribute has been added the ID attributes. So no matter
what you do inside your TextView or design view, they are basically the same. But as
you can see, right now we have some warning for this button. The warning says that this
button is not constrained to anywhere. That's because we are using this constraint layout
for this whole XML file. Don't worry about this constraint layout yet we have other layouts
like relative layout and linear layouts as well. But for now, just know that when you
are using constraint layout, which is the default case, when you are creating an Android
application, you need to concentrate or chain your different elements to software. For example,
if I don't constrain this button to anywhere, it's going to be floating at the wrong time.
Because of the different screen sizes, we cannot know the exact position or exact place
of this button. And right now if we run our application, probably This button will be
moved to the place zero and zero, which is exactly this corner in our layout. In order
to overcome this warning, we can simply add some constraints by clicking and dragging.
As simple as that I'm constraining this button to the both edges of my screen. Also for the
top and the bottom, I can constraint that as well. For the top, I'm going to constrain
that to this TextView. For the button, I'm going to constrain that to the bottom of my
screen. As you can see whenever I add a constraint, and attributes will be added to this TextView
in here as well. Now we don't have that red one. Also, it's worth mentioning that by default,
when we have created this button, when we have dragged that into our design view, an
ID has been added to it as well. The idea of this button is just button, we can change
that if you want. For example, I can say btn let's say hello, or whatever you want. But
in an XML file, the ID should be a specific to every element.
Right now this button doesn't do anything. In this video, we are going to make our application
a little bit more interactive. For example, we are going to change the text of this TextView
by clicking on this button. For that we need to define this TextView in our Java file so
that we could have access to its attributes. But before I go further to my java file, I'm
going to switch to full screen so that you won't be distracted with all of my icons down
in here. If you go to this view option in here, down here at the bottom, you can see
that we have few options. I'm going to go to full screen. For the rest of the course
i'm going to stay with full screen. Okay, let's switch to our main activity dot java
file. As I said in the previous video, you can see that we have some code written in
here. And that's because when we created our project we have selected the empty activity
project for that now we have an empty activity which has an activity main dot XML file and
also main activity dot java file. In future we will see that we can create All of these
folds if we select no activity at the time of creating our project, but in here we have
these codes returned to this level. Also, we have this plus sign in here beside this
import. If you click on that, you can see that we have imported some packages to this
class. If you want, you can always minimize and maximize that input. And for that matter,
you can minimize and maximize all of your methods. For now, I'm going to leave these
imports like this so that we can see what we are importing into our Java file. Don't
be worried about all of the new keywords that you're seeing in here, for example, this probably
class extends protected void. We will talk about all of them later on in the course.
But for now, just know that this onCreate method is going to be the start point of our
application, it means that when we run our application, the code inside these two curly
braces is going to be the code that will run first, once again, don't worry about the term
method that we used. We will talk about that later on in the course. So inside this onCreate
method, two things are happening, we have this super.on create, we are not going to
talk about that right now. And after that we are setting some content views by setting
content views we are accessing to the activity main file, you can see that we have this activity
main in here. Basically it means that this Java class is somehow related to this activity
main layout file. We will talk in more depth about this later on in the course. But for
now just know that up until this point in our code, we have said that this java file
is related to this layout file. After that, we can write the quotes that we need. For
example, in here, I'm going to access to that TextView that we have created in the previous
video for that I can say text view. As you can see, when I type something intelligent,
the idea behind the Android Studio is suggesting some options. There are a lot of options in
here, but the one that I need is going to come from Android dot widget package. So in
Java, we have classes equivalent to the UI elements or user interface elements in our
layout file. If we are going to define a button we have that class in java file as well. In
here we are going to use text view. So we can import that in our project as well. As
you can see, a new line of code has been added to our imports. This line of code is importing
the TextView from the widget package into our Java file. Next, I need to name my TextView
The name is optional, and you can name it whatever you want. For example, in here, I'm
going to say txt Hello, this is just the basic convention you can name your class, whatever
you want, but we will talk about conventions later on in the course, after naming your
TextView you need to locate the TextView from inside the layout file. And the way to do
that is like this, you can say is equal to there is a helpful method in every activity
that is useful for finding different views by their IDs. And that method is named find
view by ID. Once again, this method is useful to find different views different user interface
elements from your layout files by date ID inside the parentheses of this method, we
need to pass the ID of our TextView from inside our layout file. And the way to do that is
like this, we can type r r is a special class in Java, which will give us the access to
all of our resources, our static files in our project. Once again, it stands for resources,
let's select that, you can say r.id dot after that, we need to add the ID of our tapes we
have which was txt message, I believe this ID in here should be the exact ID that you
have put inside your layout file for this TextView you can see that the ID is txt message.
And we are using that to have access to our text view. So this way by using this fine
view by ID method, and after that passing the ID in this shape. Now we have access to
our text view. But you can see that there is one more error in here. And that's because
in Java, every time you write a sentence, you need to finish your sentences with a semicolon.
That's just syntactical and it really doesn't matter some languages does not require the
semicolon, but in Java, you need to put a semicolon at the end of every sentence. Okay,
now up to this point, we have our TextView in our Java file. Now we can use it for example,
if we want to change the text of our TextView we can say something like this, we can type
the name of our TextView which is txt Hello. After that we can say dot Don't worry about
this dot operator. We will talk about it in the next section in the Java section. But
for now just know that when you are typing.on an object, you have access to all of its methods
and attributes. For example in here I can see set text once again you can see that we
have multiple options. Let's import the first one inside the parentheses of this method.
You need to pass a text. And whenever you're going to pass a text statically, whenever
you're going to hard code the text, you need to pass it inside double quotations. Let's
pass a double quotation inside this double quotation, I'm going to say hello, this way.
Now we have changed the text of our TextView. Let's run the application and see if it's
going to work. Remember that inside our activity main layout file, the text is HelloWorld.
Inside our Java file, we are changing it to Hello, let's run the application. As you can
see, in here, the text of our TextView has been changed to Hello. Still, our application
is not interactive, we want to change the text when we click on this button, not when
we run the application. For that we need to define an event listener to be precise, an
onClick listener for this button so that when we click on this button to take off our TextView
changes, there are multiple ways of defining an onclicklistener. For our button, the simplest
one is to do that inside your main activity layout file. If you come down in here inside
your button element inside the anchor brackets of your button, if you type on click, you
can see that we have an option in here. Let's pass a name in here, for example, I'm going
to say on btn clique, this name in here is going to be the name of a method in our Java
file. As you can see, right now we are getting a red warning, it says that there is no such
method in your Java file. Let's copy the name from here. And let's go to our main activity
and create that method. So in order to create a method, we need to do that outside of the
scope of this onCreate method. And inside the curly braces of this class and talking
about this, we need to define our method inside this class. And the way to do that is like
this, we can say public void after that the name of our methods, which is on btn. Click
a pair of parentheses, because it's going to be the onclicklistener. For our button,
we need to accept a viewing here, don't worry about this viewing here, we will talk about
all of these in Java session. Let's name this view. After that, we need a pair of curly
braces for our method. And in here, we need to put the code that we need to execute when
the user clicks on our button. If you switch back to your activity main dot XML file. Now
you can see that the red error has been disappeared. Okay, let's go to main activity dot java file,
I'm going to change the text of our TextView to Hello when the user clicks on our button.
So for that method, I'm going to move these two line of code to inside this method, you
can probably guess the behavior of our application right now. Let's try it once again and see
how is it going to work. You can see that the text is still says hello world. If we
click on our button, the text will change to Hello, this is okay. But this is not entirely
what we want. It's just saying hello right now we want to say hello to the exact user.
For example, we are going to get the name of our user. And by clicking a button we are
going to say for example Hello may sound Hello Tom and something like that we are going to
get the user's input, the way to get the user's input is by using some element in our UI called
edit text. Let's switch to design view. You can add an edit text into your project by
going to this text option in here, you can see that we have multiple options. The first
one is for text view, we have seen the text view it's this hello world text. After that
we have other options, plaintext, password, email, and all of these, these are all helpful
for when you want to get different kinds of users inputs. But for our purpose in here,
we are going to use plain text. Plain Text is just a simple text that we are going to
get from the user. Let's drag that into our view. If you take a look at the attributes
in here, you can see that it's ID is edit text. Let's quickly change that. Let's say
EDT. txt name. Because we are inside the constraint layout, we also need to constraint this edit
text as well. But before that, I'm going to change the top constraint of this button,
I am going to change its constraint from the bottom of this TextView to the bottom of these
editors. For that I am going to direct the top constraint of this button to the bottom
of this edit text. Now let's add some other constraints for this edit text as well. For
example, for the top, I'm going to constrain it to the bottom of my text view. And also
for the edges. I'm going to go to the edges of the screen. Right now it's too close to
our text view. If you want we can add some margins. For example in here, I'm going to
say 70. That seems to be better. The other attribute that every edit text has is this
text in here. I'm talking about this attribute in here. You can see that right now it says
name if you want you can change that but in here, I'm going to completely delete that,
instead, I'm going to define some hints. You can see this hint attribute in here. The difference
between hint and text is that when you click on your edit text, the hint will be deleted.
It's just a hint for the user to know that what he or she is going to enter inside this
edit text. For example, in here, I'm going to say me, you can see that it's somehow grayed
out. Okay, now we have an edit text inside our layout file inside our Java file, we need
to have access to this edit text so that we could get the user's input once again, the
start point of our activity. And our application is this onCreate method. So we can do that
inside this onCreate method, we can have access to our edit text like we did for this TextView.
We can say Edit Text. Once again, you can see that we have an equivalent class for this
edit text as well. Also, if you take a look at in here, you can see that this line of
code has been added to our class as well. Let's say Edit Text, edit GTX. The name, like
before, we can say is equal to find view by ID. And once again, we need to pass the ID
of our edit text which I can say r.id dot EDTA. txt name. To this point, we have defined
our edit text. Now, we need to get the text of our edit text for that we can say Edit
Text name,
dot get text, you can see that we have this option. But this is not enough, you also need
to do another level of conversion. And we do that by saying.to string. Don't worry about
this two string method yet, we will talk about that later on in the course. It's just for
converting whatever the text is inside our edit text to a text that we can use in our
Java file. Don't worry about that. We will talk about it later on. But there is a problem
with our code in here, we are going to get the user's name and we are going to say hello
to the user. When we click on the button. For that we need to have access to this edit
text inside our on button click method. But if you try to do that, for example, if you
say Edit Text name, you can see that everything is in red, it means that you don't have access
to that edit text inside this method. And that's because something called a scope in
Java. The scope of this edit text is this onCreate method within the curly braces of
this onCreate method, we can have access to these elements from inside are on button click
method. In order to solve that problem, we need to move these two line of code to inside
our on button click method. Let's quickly do that. Inside our on button click method,
let's paste them. But right now we are not doing anything with the users input we are
getting the user's input, but you're not doing anything with that, we need to pass this line
of code to the set text method. Let's quickly delete this line of code. And inside the parentheses
of the set text method. I'm going to pass that I'm going to say Edit Text name, dot
get text.to string. This way, we are setting the text of our TextView to whatever the text
is inside our edit text. Let's quickly run the application and see if it's going to work.
This is how our application and this edit text looks right now. If we click on that,
you can see that we can type something for example, let's type my name. And if we click
on our button, the text of our text view changes to Mesa, this is not entirely what we wanted,
we want to say hello to myself. In order to do that we need to make some changes to our
code. Also notice that I didn't put any double quotation in here. And that's because we are
passing this text dynamically to our set text method. If we wanted to pass our text statically
we need to put that inside double quotation but in here we don't need to double quotation
if we want to say hello to the user, we can say something like this, we can say double
quotation, let's add a plus in here. And inside these level quotations, let's say hello. Also
let's add a space so that we could see everything better. This in here is called concatenation
in programming, adding the two texts together, let's run the application once again. And
let's type something in here. Let's say Mesa by pressing button. This time, you can see
that the text has been changed to allow Mesa our application is now much more interactive.
Okay, this is our challenge, we are going to create a registration form like this, we
are going to get the user's first name, last name and email. And by pressing this register
button, we are going to show them inside these text views. Let me quickly show what I'm talking
about. So if we have a first name, last name, and also an email. If we click on this register
button, we are going to show them inside these text views to the user. It's just a simple
application so that we can practice everything that we have learned so far. Okay, I want
you to pause the video in here and create the simple application After you have created
it, come back to the video and we are going to create this exact application together.
I hope you solve the challenge but if not, that's totally okay, we are going to create
that in here together. Also notice that whenever you run Android Studio, you can see a list
of recent projects in the left panel in here. Okay, let's move on. And let's create our
project we can say start a new Android Studio project. Once again, we are going to select
this empty activity. Let's name our project in here. I'm going to name it the first challenge
or Let's name it registration for you need to specify a package name, mine is fine. Change
the Save location if you want. And also I'm going to stay with API level 19. Okay, let's
create our project. Let's start by creating our layout file. I'm going to close these
jellified for now. And let's also minimize this project name. First of all, let's delete
this HelloWorld text, you can click on it and by pressing Delete, you can delete that
in our registration form, we needed to edit texts, let's add them quickly. In text, we
need to add three plain texts in here. Let's quickly add them, we will constrain them later
on. Don't worry about that right now through edit texts. And also we need a button. Let's
add that as well. We also needed three text views. For the Edit texts.
Let's also change the IDs. For the first one, I'm going to say Edit Text first name. Let's
also delete this text and also add something. That's the first thing. Let's do the same
thing for the other two edit texts. Sometimes when you're defining a hint, or maybe a text
attribute for your elements, you can see some annoying dialogue like this in here, you can
avoid that by pressing the Escape key on your keyboard. Okay for the button. First of all,
let's change its ID to let's say btn register. You can name it whatever you want. Let's also
delete this text. And let's say register. Also, you can see that we have this onClick
attribute in here, we can define an onClick attribute for our button from here or like
we did in the previous video in our TextView. I'm going to define that right now in here,
let's say on Register btn. Click later on, we will create this method for the text views.
First of all, let's change their IDs. For example, for the first one, let's say txt
first name. Also, let's change the text to Firstly, similarly for the other two. Okay,
and let's greatly constraint all of these elements, we can click on them. For the first
one, I'm going to constraint it to the top edge of my screen. And also both sides of
my screen. Let's also add a margin top of maybe 100. That seems to be better. For the
next one, I'm going to constraint it to the both edges of the first name edit text, and
also to the bottom of my first name edit text, add a margin top of 24. I'm going to do the
same thing for this email. For the button, I'm going to do the same thing, but I will
probably change the top constraint. Also, if you did a mistake, when you're constraining
your elements, you can click on the constraint by pressing Delete key you can delete the
constraint, let's change the top constraint to maybe something like 90 db. For the first
name, I'm going to constraint it to the both edges of my button. Sometimes it may not work
just try once again. And also the top constraint to the bottom of my button. For the top constraint
once again, let's say 90, I think 90 is fine. Let's delete its top constraint because right
now we are not seeing our last name TextView. And let's add it once again. For the last
name, I'm going to do the same thing. Let's change the top constraint margin to let's
say 24. Same thing for the email. Okay, this is going to be the look of our application.
And now that we have defined our layout file, let's switch to our Java file. And to find
the behavior of our application, I'm going to close this activity main dot XML just before
everything, let's just copy this onClick attribute so that we don't make any mistake when we
define this method in our Java file. In our projects being inside Java folder inside our
package folder inside our main activity dot java file, I need to create my onClick method.
Once again, I can say public void the name of my method, which I've copied from my layout
file, inside the parentheses, I need to say view with capital V after that view with the
Lord v. And now I have created my own button click listener. Once again, don't be worried
about the syntax of this method. We will talk about it later on when we have talked about
Java in here. First of all I'm going to define my text views. If you remember we can do something
like this. We can say TextView for the first one. Let's say txt first name is Call to find
view by ID r.id dot txt. Firstly, I'm going to do the same thing for email. And lastly,
after you have defined your text views, it's time to define your edit texts. You can do
that like this, we can say Edit Text. For the first one, let's name it edit text first
name is equal to find view by ID r.id dot edit text first name. Once again, I'm going
to do the same thing for edit text, email and also edit text last name. Okay, now we
have access to all of the UI elements that we wanted. Now let's change the text of our
text views. For example, for the txt first name, I can say something like this, let's
say txt first name. That's a text. We have seen this in the previous video. Nothing new
in here. Let's say first name, plus the text of our edit text. Firstly, we can say Edit
Text first name, dot get text.to string. As simple as that. Now, we are setting the text
of our text view to the text of our edit text. Let's do the same thing for last name and
email. Edit Text last name, that's a text. Let's say last name, first of all, was edit
text last name dot get text.to string, edit text email. That's a text. Sorry, I have put
the wrong element in here. This should be an txt. Last name, not edit text last name.
Same thing for the email, txt email.
That's it text. Let's say email plus edit text email dot get text.to string. That's
all we need from our application. Let's just run it. Okay, it seems like we have done a
good job with the layout of our application. I think it's just better to decrease the margin
top of this register button and also these first name edit text so that we could see
all of them better. Let's also test its behavior for the first name, let's say Emma. For the
last name, let's say Watson for the email, Mr. watson@gmail.com. Let's click on this
register button. And as you can see, we are seeing the text that we wanted. It seems like
our application is working perfectly. In this section of the course we are going to talk
about Java for writing Java applications, we are going to need an ID ID stands for integrated
development environment. Android Studio has been built upon an ID called IntelliJ. Until
few months ago, you could have right and drawn Java applications on Android Studio bought
I think it was around July 2019, that you can no longer do that. And that was because
of a new release in Gradle. I have recorded a video on that exact problem at the time.
And it's on my YouTube channel. If you want to take a look at that just search for makeup
on YouTube and you will probably find a video let me quickly show that this is the video
that I'm talking about. Andrew the sudo does not support plain Java code anymore What to
do now. And you can also see my channel name, so make sure to check that if you need. But
basically what I was talking about in that video was that instead of Android Studio,
you need to use another ID. The most famous IDs that are available for writing Java applications
are Eclipse and IntelliJ. In this course, we are going to use intelligent because it's
a lot like Android Studio. And also it's free. In that video I have talked about how to install
intelligent Id make sure to check that if you need but it's a lot like installing Android
Studio beside intelligent, you're going to need JDK Java Development Kit, which we have
already installed on our system, you can get intelligent ID from JetBrains website, let's
say intelligent ID. The first link from the JetBrains website probably is our needed link.
Just one thing that was mentioning here is that there are two versions, the community
version and also an ultimate version, the community version is free. And also it's enough
for our needs in this course. So make sure to download that and after you have downloaded
it and also installed it come back to this video to write Java applications. I have intelligent
installed on my system and I'm going to open it right now you can create a new project
in here by saying create new project. As you can see, the process is a lot like Android
Studio, I have the ultimate version of intelligence ID and for that I can see all of these options.
These might not be available for you if you're using community version, but no matter what
version you're using, you should see this Java option in here select that in here you
need to specify the path to your JDK. If you have installed JDK with me at the beginning
of this course you should be good in here you can just select the version of your JDK
my version in here is 13. But we are not going to use All of the features of Java 13. Because
Oracle does not support this version of Java for long term, we are going to stay with all
of the features of Java eight. Okay, After you have selected the Java and your JDK, let's
click next, we are not going to create our project from template, so make sure to uncheck
this option. After that, click Next. In here, we need to name our project and also specify
a location for the name of this project. I'm going to say hello world. Also, if you don't
see these settings in here, you have this more Settings option down in here, make sure
that your module name is exactly your project name. Also feel free to change the project
location if you need, I'm not going to change that in here. Let's just click Finish. And
let's create our project. As you can see, intelligent ID is a lot like Android Studio.
And the reason for that once again is that Android Studio has been built upon this ID.
Let's just close this tip dialog. And let's switch to full screen mode. And let's go to
full screen mode. Once again, in IntelliJ ID, you can see this project pane you can
minimize and maximize that you can also move through the folders right now because we didn't
use any template our folders are empty, we can create our project like this by clicking
on the source folder by right clicking on that selecting new package, you can specify
a package for our application. In this case, I'm going to say for the maker
dot let's say hello world. And inside this package, we can create our Java classes Don't
worry about the term class that are used. We are going to talk about that in object
oriented session. But for now just know that we are going to need at least one Java class
in order to run our Java applications. By right clicking on our package and selecting
new Java class we can create our class Let's name this class. Hello. As you can see, a
new Java class has been created in our package. And also we have few lines of code. Let's
minimize our project pane. Let's focus on this Hello dot java class. Basically what
we have in here is the package that we are currently in which is orb dot maker dot HelloWorld.
And also we have the declaration of a class once again, we are going to talk about classes
later on in future videos. In order to have a start point for our application, we are
going to need at least one method and that method is called the main method. In order
to create that method we can type p SVM. By pressing tab or enter, we can have that method.
As you can see, this green triangle has been added in here it means that now we can run
our application from this point, we will talk about the meaning of all of these new keywords
later on in the course. But for now, just know that in order to run your Java applications,
you need at least one Java class in which has one method called main this main method
is like the onCreate method of our activities. Now we can write our codes inside this main
method. For example, in order to print something to the console, we can say something like
this we can say s o UT or south and by pressing tab or enter you can see this line of code
System dot out dot print line. Or alternatively, you can type this manually. Instead of using
the shortcut, you can say system, notice that the s should be in other cases, make sure
to use the upper case for us. Let's say system dot out. Basically this out means output in
here it means that we are going to add 14 something in this case we are going to print
something later on we will see that we have an in as well in which we will get the user's
input. Let's say System dot out dot print line, you can see that we have print and print
line. For now we are going to use the print one we will talk about the difference later
on. In order to print something to the console, you need to pass it inside the double quotation,
you can say something like this. For example, let's say hello world. Now if you have the
simplest Java application that can be written, let's quickly run our application. In order
to run our Java application for the first time, we need to right click inside the curly
braces of our main method and select Run hello.me. Let's do that. As you can see, we are printing
hello world into our console. Sometimes if it's the first time that you're running Java
applications, you may get some errors in here. In the most cases those error would be gone
by rebuilding your project, you can come to this built up above in here and rebuild your
project. After the rebuild is finished, you can run your application once again by right
clicking and selecting run. Hello, that's me. In most cases, the error should be gone.
But if you had any other problem, please ask them in the q&a section of the course also
notice that for the rest of this section, we are not going to have any user interface
it is possible to have a graphical user interface GUI for your Java applications but because
this is an Android course and later We've talked about user interface in Android applications,
we are not going to use any user interface for our Java applications, we are just going
to understand the language. Okay, now let's minimize our round pain from here. And let's
talk about the difference between print and print line. The difference between print and
print line is that when you are using print line, after printing your text, the cursor
will move to the next line. That is not the case with print. In order to see the difference,
let's say System dot out dot print. Once again, let's pass our hello world text. And let's
run our application to see the difference. Also, if it's the first time that you're going
to run your application, you can right click inside the main method and click on this run
Hello dot main. Or if it's not the first time, you can click on this green triangle at the
left or the green triangle. On the top of your screen, you can also see the shortcut
for Windows is shift plus fn. If you're using Mac or Linux, we can hover over this green
triangle and see the shortcut. Let's run the application and see the difference between
print and print line, you can see that the text has been printed, but the cursor never
moved to the next line. If we switch these two lines of code, we can see the difference.
But
let's switch now. Now print line is the first line and print is the second line. Let's run
the application once again. This time, you can see that the two text has been printed
in two separate lines. Okay, now that we know the difference between print and print line,
let's talk about variables in Java. But before I do that, I'm going to comment these two
lines of code, you can comment them by pressing Ctrl plus slash when you comment some code
in Java, it means that you are telling the compiler to ignore these two line of code.
The use of comment is for developers to remind themselves or other colleagues of something
important. For example, in here I can say slash slash, let's say do something, this
line of code will be completely ignored. The other kind of comments in here is like this,
we can say slash slash to do after that we can say do something. If we use this kind
of comment with to do at the beginning of your comment, you can see that first of all
the color will be changed to blue. And also if you click on the to do tab in here, you
can see that you have one to do in your file. It's very helpful when you're working with
multiple programmers. By clicking on this to do everyone can see the list of different
tools. There are more to comments, we will talk about them later on in the course. But
for now just know that when you're commenting something that line will be completely ignored.
Okay, now let's talk about variables. Variables are useful for when you want to store simple
data in your job application. For example, if you want to store a number or a text variables
are useful for when you want to store some small data in your Java application. For example,
if you want to store a number or a text, if you want to store a number, you will do that
like this. You can say int int stands for integer basically inside an integer, you can
store whole numbers for example, negative and positive numbers plus zero. And the way
to define them is like this. First of all, you type int after that, you need to name
your variable. For example, in here, I can say number this name in here is optional.
And you can use whatever you want. After that you can assign a value to your variable. For
example, I can say five in here, this is the data type of my variable, this number is the
name of my variable. And this five is the value of my variable beside positive numbers,
you can also have negative numbers. After you have stored your variables, you can do
some operation on them, for example, you can print them and the way to do that is like
this, you can say System dot out dot print line. And after that you can pass your number
or whatever is the name of your variable. Notice that in here I haven't used double
quotation. Basically, whenever you are passing some text explicitly, you need to pass the
devil quotation but whenever you're passing your variable, you shouldn't pass any double
quotation, let's run the application and see if we can see our number. You can see that
the value of our number is negative five in this case, but inside integers, you cannot
have decimal values. For example, in here, I can say 5.3 previoulsy, other kinds of variables
in which you can use in order to store decimal numbers, but more on that in a few minutes.
The other kind of variable that you can use in order to store whole numbers is long. Let's
see that as well. We can see a long, you can name it my long is equal to once again, let's
say five. Let's also print that, let's say System dot out dot print line and let's pass
our log. If we run the application, you can see five down in here. The difference between
long and integer is that you can store larger numbers inside a lung. And also a long variable
will store more spaces in your device's memory ram to be exact inside an integer you can't
store numbers as large as the number of two to the power of 31. But inside a long, you
can store numbers as long as the number of two to the power of 63. That's the difference
between long and integer. But what if you want to store decimal numbers floating point
numbers for that you have other kinds of variables, the first one is a double, let's see that
we can say double, let's name it my double. Let's assign it the value of 4.5. Like before
we can print it,
let's pass our data. If we run the application, we should see the number of 4.5 printed, you
can see that down in here. So double is one kind of variable in which you can use in order
to store floating point numbers. The other option is to use float. Let's see that as
well. We can say float, let's name it my float. Once again, the names are optional, you can
name your variables, whatever you want, we can say 4.5 in here once again. But right
now you can see that we are getting a read error in here. And that's because floating
point numbers by default are double in Java, it means that if you want to define a float
number, you need to cast your number to float, the way to cast your number is like this.
After the equal, we can open and close a parentheses and inside the parentheses you can say float
by casting, you will explicitly tell the ID that this is going to be a float number and
not a double number. Okay, let's quickly print this as well. Let's pass our float. And let's
run the application, you can see that once again 4.5 has been printed. Once again, the
difference between float and double is that inside a float, you can store smaller numbers.
And also the other difference is in the space that each one of these are going to occupy
in the device's memory. Basically double view occupying more space, we won't be using floats
that much in this course, I just wanted to show you that float does exist in Java. Okay,
now we have seen numbers. But what if we want to store different characters in Java, for
that we have a special kind of variable which is char, for example, we can see char, my
char is equal to, let's say M. Notice that when I've assigned this character to my char
variable, I have used this single quotation that wasn't the case when we have worked with
different numbers. When you're assigning a character, you need to use the single quotation
like before we can print our character. Let's pass our character. And let's run the application.
You can see em printed in here. When you're using char variable, you can't have multiple
characters. For example, in here, I can't have ma you can see that we are getting an
error. In order to store multiple characters, we have another kind of variable in which
we will talk about that in a minute. But before I do that, I'm going to show you just one
more thing. You can also store something like this in your character, you can say backslash
u 00. A, this is a special Unicode value for registered symbol. Let's run the application
and see what would be the result, you can see that the Registered Trademark has been
printed into our console. When you're using a char variable. You can also assign Unicode
values as well. You can search for different Unicode values. For example, if we want to
show the copyright symbol, we can say something like this, we can say backslash use 00 a nine.
If you've run the application, we should see the copyright symbol Feel free to search for
other Unicode values on the internet. Okay, now let's talk about the string. If we want
to restore multiple characters, we can use a string, we can say a string with capital
S. After that, like before, we can name our string, let's say my string or Let's name
it name is equal to Mesa meaning like before, if we want, we can print our string. Let's
fascinate and let's run the application, we can see that Mesa has been printed into our
console. But you may have noticed that there is a difference in here, the color of this
string is not blue, like the other kinds of variables. That's because of a very important
difference between a string and other kinds of variables. The other kind of variables
that we have talked about so far are primitive data types. But a string is a class in Java,
do we have talked about classes and objects in Java, but for now, just know that when
you are using a class, you can have some operations on that. For example, if you type name, you
can say dot and you can see a list of different available methods. We will talk about few
of them later on in the course. But you can see that there are a lot of them. So the difference
in here is that the string is a class, but others are primitive data types. There is
one more kind of variables that I'm going to talk about in here, and that's called Boolean.
Let's see that as well. We can say Boolean, my Boolean, let's say is equal to true when
you're using a Boolean, that Boolean can have only true or false values. These are the only
two values that a Boolean can have. Once again, like before, we can print our Boolean. Let's
pass my Boolean in here. And let's run our application, you can see that true has been
printed. As I said, Our Boolean can have false values as well, let's run the application.
False has been printed Boolean is very helpful when you are working with conditional statements.
We will talk about conditional statements in the next video. Okay, this was our talk
about different variables in Java, there are a few more of these in which you can check
out the Oracle documentation, but probably you're not going to need the others. These
are all that we are going to use in this course. Okay, now that we have talked about variables,
let's quickly talk about arithmetic operators as well. But before that, I'm going to comment
all of these lines of code, because these can be distracting. The reason that I'm commenting
these lines of code and not deleting them is that I'm going to upload the source code
so that if you need, you can check the source code. Okay, let's move on down in here, let's
quickly define two new integers, I can say int a is equal to six. After that, let's say
int b is equal to let's say two. You can use operators in Java, in order to perform some
kind of operation on your variable. We have different kinds of operators. The first one
that we're going to talk about in here is called arithmetic operators. Let's quickly
see them. For example, here I can say, answer is equal to a plus b. This plus is the first
arithmetic operator that we are going to talk about thinking, this is going to add the value
of B to A,
let's quickly print our answer. And let's run our application, you can see that the
answer is eight, it has added B to A. The other kind of arithmetic operator that I'm
going to talk about in here is minus which you can guess its value, it's going to deduct
B from a, the answer in here should be four, we can see that four has been printed. The
other one is the star, which is used for multiplying the two values together in here, the answer
should be six times two, which is going to be 12. Let's run the application. And you
can see 12 in here, the other one is slash, which is used for division, we are going to
divide six by two, let's run the application, we can see that the answer is three. The thing
that you need to be extra careful with this a slash operator in here is that the second
number V in this case should never be zero. Let's quickly change the value of b to zero.
And let's see what will be the result. Let's run the application, you can see that we are
getting a read error in here we are getting an arithmetic exception. We will talk about
exceptions later on in the course. But for now just know that whenever an exception occurs,
our application crashes. Certainly we don't want that behavior in our application. So
be careful about the second number. Or alternatively, you need to catch the exception that you think
that might occur. We will talk about exceptions later on in the course we have one more kind
of arithmetic operator in Java, and that's called remainder. Let's see that as well.
It's the person so the remainder operator is going to give us the remainder of a divided
by b. In this case, if we divide six by two, the answer is three. And its remainder is
zero. So right now if I run the application, I should see zero. You can see that zero has
been printed down in here. But if I change the value of a to, let's say five. Now the
remainder of five divided by two is one. Let's see that if we can see one. Yes, it has been
printed from time to time this remainder operator can be useful as well. There is one more thing
that I need to talk about the slash operator the division operator right now we can expect
that if we divide five by two, we should see 2.5. Let's run the application and see if
we can see that we can see that we are getting two as the answer. And that's because we have
saved the answer inside an integer. If you remember at the beginning of this video, I
said that integers can only contain whole numbers. If you want to have floating point
numbers, you can use something like double. But this in here is not going to work as well,
because the answer itself is an integer. If you hover over the highlight, you can see
that a divided by b is an integer. If you want to cast your number to a double after
the equal sign, you can open and close parentheses and inside the parentheses you can say Java,
this way you have cast an integer to a double. Now if we run our application, you can see
that we are seeing 2.5 so if you want to see the exact value of division use doubles. Let's
just talk a bit more about the plus operator. After that finish off this video. So right
now if I use plus in here, you can guess the answer, five plus two is seven. Let's run
the application. We can see seven down in here, we can also use this plus with different
texts. For example, if I have to text in here, let's say string first name is equal to, let's
say, Emma. Let's also define another text. Let's say last name is equal to Watson, we
can use the plus operator to add these two text together. For example, let's say a string.
Full Name is the call to first name, plus our last name. If we print our full name,
you can guess what would be the result. In here we can see Mr. Watson. Let's also add
a spacing here in order to see everything better. Mr. Watson has been printed. This
in here is called concatenating in Java, adding two text together. So you can see that this
plus operator have two usages, it can be used to concatenate two text together or whenever
we use it with different numbers, it will act as the addition operator. We are going
to talk about more operators in Java. Specifically, we will talk about relational and logical
operators. Before doing that, let's quickly create our project by selecting create new
project by selecting Java and clicking on next. And next once again, we can name our
project. Let's name this project operators. Let's create our project. And inside the source
folder, let's create our package. Let's say new package. Let's name it orc. That's my
code dot operators. Inside our package, let's create our main class. Let's name this class
main. Inside our main class, we are going to need our main method. Let's type PS VM.
By pressing tab, we have our main method. Before I talk about relational and logical
operators. Let's quickly talk a bit more about arithmetic operators. If you remember from
the previous video, when we have to find a new integer, for example, a and we assign
a value to that we could have say something like this, we could have said in answer
is equal to a plus two. Or instead of defining a new integer, we can say a is equal to a
plus two, it means that increase the value of i by two, let's print a and see what would
be its result. Let's run our application because it's the first time I'm going to say run main
dot Hello, you can see that the value of a is seven, we can simplify this line of code,
we can say something like this, I'm going to comment that, and in here, I can say a
plus equal to let's also add some spaces, these two lines of code are exactly the same,
you can use plus equal in order to simplify the line six, let's run our application. Once
again, you can see that the result is the same. Also, if we want to increase a by one,
for example, if you want to say a is equal to a plus one, we can do something like this,
we can say a plus equal one. Or alternatively, we can say a plus plus, that is possible as
well, this a plus plus is going to increase the value of A by one. If we run our application,
we should see six as the value of a you can see six down in here. Alternatively, we can
use minus as well, we can say a minus minus it will deduct one from the value of a, if
you're on our application, we can see four in the console. Similar to that we can say
a minus equal to let's say two. For example. In this case, if we run our application, we
should see three as the value of a like minus equal You can also have multiplied equal and
also divided by equal it means that divide a by two and store the value or a store the
answer inside a this sentence in here is the simplified version of this line of code that
I'm going to write we can say a is equal to A divided by two, both of these lines are
the same. If you want to make sure of that you can delete this line and you can run your
application. five divided by two is 2.5. Because we are saving that inside an integer, the
answer should be two. So we should see two as the value of our a and you can see two
down in here. Before talking about logical and relational operator. I just wanted to
talk about these simplifications. Okay, now let's talk about relational and to be specific
comparison operators in Java comparison operators are useful for when you want to compare two
things. For example, two numbers. Before that, let's quickly define two numbers. I'm going
to say int a is equal to five After that, let's say int b is equal to, let's say three.
After that we can say Boolean answer is equal to the first comparison operator that I'm
going to talk about in here is greater than operator. And the way to define that is like
this, we can say, a greater than B, I have saved the result of this comparison inside
a Boolean, this left anchor bracket in here means greater than operator, it means that
if a is greater than B, assigned the value of true to this answer, right now, if we print
our answer we should see through, let's quickly do that. Let's see answer. And let's run our
application, you can see that answer is true, because A is five and five is greater than
three, the other kind of comparison operator is less than, and that's this right angle
bracket, right now, a is not less than b, so the answer should be false. If we run our
application, we should see false in the console. And we can see that the other kind of comparison
operator that I'm going to talk about in here is useful for when you want to compare the
equality between two integers. And that operator is these two equal signs, right now, a is
not equal to b. So if we run our application, we should see falls into our console. Let's
run our application. And here we can see false. Remember that when we are using two equal
signs, it means that we are checking for the equality of two number. But when we are using
a single equal sign like in here and above, in here, we are assigning a value to our variable.
Let's change the value of b to five. To see that if this is going to be true,
let's run the application. Now you can see that the answer is true. We have other kinds
of comparison operators as well. One of them is greater than equal. When we use this left
anchor bracket. And also this equal sign, it means that check if a is greater than B,
or if a is equal to b, right now, a is equal to b. So this answer should be true. If we
run our application we should see through, you can see that down here we see true. Also,
if A is five, and B would be something like three, if you run the application, we should
see through once again, we can see through. But if we change the value of a from five
to something like two, because none of these condition is true, right now, we should see
false, let's run our application. And we can see false, the other comparison operator is
less than equal. And we use that like this, we can use this right angle bracket and equal
sign this in here means that check if a is less than b, or if a is equal to b, right
now, a is less than b. So the answer should be true if we run our application we should
see through. But if we change the value of a to something like five, we should see false.
And we can see false. Beside these, we also have another kind of comparison operator.
And that's this explanation Mark plus the equal sign this in here means that check if
A is not equal to b, this exclamation mark in here as a negative meaning to our equal
operator right now, a is not equal to b. So the answer should be true. If we run our application
we should see through and through has been presented. Okay, these were the comparison
operators that I was going to talk about, feel free to practice them because we are
going to use them a lot in our course. Next I'm going to talk about logical operators
in Java. But before that, let's quickly comment these two line of code down in here, let's
define a new Boolean, let's say Boolean answer is equal to let me write the syntax and I
will talk about it, we can say a is equal to five, or let's say B is equal to something
like two. This is our first logical operator this in here means or you can add these two
pipelines by using shift plus backslash backslash is somewhere above the enter in English keyboard.
So in here, this OR operator means that if a is equal to five, or if b is equal to two,
assign through to this answer, if none of this condition is true, so assigned false
to this answer, right now, a is equal to five at least one of our conditions are true. So
the answer should be true. So if we print our answer, we should see through, let's run
the application. And you can see through down in here, if we change the value of a for example
to six, because none of these conditions are true, we should see false. Let's run the application.
And then here we can see false. The other kind of logical operator that I'm going to
talk about is to add science. Let's quickly see that as well. This in here is an operator
it means that assign through to answer if both of these conditions are true, if A is
equal to five and also if B is equal to two if one of these conditions is not true, so
assign false the answer right now, none of these conditions is true. So we should see
false as our answer. Let's run the application. And as you can guess, we see false. If we
change the value of a to five, and run our application, once again, we should see false,
because the second condition is not true. But if we change the value of v2, and run
our application, this time we should see through and we can see through them here. And these
were the two kinds of logical operators that I was going to talk about or and and now that
we have talked about operators that we wanted, let's talk about conditional statements, I'm
going to comment these two lines of code. The first kind of conditional statements that
I'm going to talk about is called if statements, we can type something like that we can say
if a pair of parentheses inside the parentheses we need to provide our condition. For example,
we can say if a is greater than three, after the parentheses we need a pair of curly braces
inside the curly braces, we can put our code for example, let's say a is greater than three.
So in here, we are saying that if our condition is true, execute the code inside the curly
braces. If it's not true, jump to after the curly braces. For example, in here, let's
say continue. Right now a is five, and it's greater than three. So we should see this
text printed, let's run our application, you can see that a is greater than three. But
if we change the value of a to something like two
and run our application, you can see that we are no longer seeing this text in if statements
we can also have multiple conditions. For example, in here, I can say else, I can provide
another pair of curly braces inside these curly braces, I can say something like this,
I can say a is less than three. Right now a is two, a is less than two. So we should
see only this text printed, let's run our application, you can see that a is less than
three. As for the condition, you can provide your condition like this directly inside your
if statement parentheses or you can define your Boolean before your if statement. For
example, in here, if I say Boolean answer is equal to a greater than three. Now I can
pass my answer. inside these parentheses, I can say if answer is equal to true, let's
minimize this ramping for now. This is going to work the same path. If you hover over the
highlighting here, the intelligent ID is warning me about something the first one is that answer
equals equals to true can be simplified to answer it means that I can delete this part,
this is going to work exactly the same. If we run our application, we should see the
same result a is less than three. Let's run our application. And you can see a is less
than three. If we want to negate this answer, we can add an exclamation mark before our
variable. For example, in here we can say if answer is not equal to true, if we run
our application, we should see the first line, we can see that a is greater than three. This
is not true because A is true. But in here we have changed the meaning of this brilliant,
we have added a negative meaning. Let's delete this for now. And let's run our application
once again. Besides else, you can also have an else if as well. Let's briefly comment
this if statement. And let's write another one. down in here I'm going to see if inside
this if statement. Let's check that if a is positive or negative, I can say if a is greater
than zero, inside the curly braces, let's print as positive. In here instead of else,
I can use something like this, I can say else if now we need to put another condition in
here we can say if a is less than zero. Once again, we need a pair of curly braces. Inside
the curly braces, we can say a is negative. You can have as many elsif cases as you want.
For example, I can add another one in here. But of course in here, I don't think this
if statement would mean anything. But you can have as many as you want. But in every
if statement you can have at most one else case, let's delete all of these. For example,
I can have only one l space this else is going to be the default case. In this case if we
use this else, it means that a is not greater than zero. And also it's not less than zero.
So it is zero. In here we can safely say a is zero. This is going to be the default case
for our if statement right now A is true. So we should see a is positive. Let's run
our application. And as you can see a is positive. Let's change a to minus two. If we run our
application, you can guess it, we should see a is negative. And we can see that down in
here. If we change the value of oil to zero, we should see a zero. And as you can see,
we can see that down in here. So if statements are one kind of conditional statements, we
have another one as well, let's comment all of these, the other one is called switch statements.
Let's see that as well, we can say switch inside the parentheses of the switch statement,
we need to provide the variable that we want to switch on. For example, I can pass a in
here, after the parentheses, I can provide a pair of curly braces inside the curly braces,
I can define different cases. For example, I can say in case a is equal to one after
that we need a column. And in case a is one, we can print something for example, we can
see a is one. Also, it's very important to provide a break after every case, let's do
that. I will talk about this break in a minute. But for now, just know that after every case,
you are going to need a break, we can define as many case as we want. For example, in here,
I can say in case a is to screen something, let's say a is two. After that we need a break,
we can define another case. For example, for three, let's say a is three. In every switch
statement, we are going to need a default case as well. And we can pass that like this
we can say default. And we can print something like a is not one two, or three. In the default
case, we need the break as well. Let's quickly add that, right now the value of a is zero.
Let's change that to two. And you can guess it we should see a is two, let's run the application.
And you can see a is two, if we change the value of a two once again zero and run our
application. You can see a is not one, two or three. Let's also talk about this break
in here. But before that, let's quickly change the value of eight to one. This break in here
will cause our switch statement to break. For example, if we don't have this break in
here at the first days, which is a is equal to one. And if we run our application,
you can see that even though a is one we can see both sentences, we can see a is one plus
a is two in here. Because we don't have any break, we didn't break out of the switch statement.
And we have jumped to the second statement. So don't forget to have a break after each
case in your switch statement. We are going to talk about loops in Java. But before that,
let me just say that I have created my project, I have added this package inside my source
folder. Inside this package we have only one main class in which has this one main method,
I hope that you can come to this point by yourself. If not, please watch the previous
videos on how to create a project and create your main class and your main method. In programming
loops are useful for when you want to do a repetitive job. For example, if you want to
print something 10 times like this in here. If you want to print this Hello 10 times you
can just copy and paste it 10 times what in general, it's a bad idea that you copy and
paste yourself whenever in programming you're copying and pasting know that you're doing
something wrong. Instead of copy and pasting we are going to use loops in this video. The
first kind of loops that I'm going to talk about are for loops. Let me write it and we
will talk about its syntax. This is the general syntax of a for loop first of all we write
for. After that we have a pair of parentheses inside the parentheses we have three statements
in the first statement we are defining a new integer called oil you can name your integer
whatever you want, and also we are initializing our AI to have a value of zero. The second
statement is the condition of our for loop we are going to continue this loop until this
condition is not met. And the third statement is just a statement for changing the value
of i every time that we look PVC inaction what all of these mean after the parentheses
we have a pair of curly braces in which we can contain our code inside them. For example,
if we want to print something like hello here, we can include that inside this pair of curly
braces. So what this for loop is going to do is that first of all we are defining a
new integer called AI and we are assigning the value of zero to that integer. After that
we are checking our condition we are checking that if i is less than 10 in the first time
that we are going to look i is Hello So this condition is met. If this condition is true,
we are going to execute the code inside the curly braces. In this case we are going to
print hello. After printing Hello, we are going to execute the third statement. In this
case, we are going to increase the value of i by one. For the next time that we are going
to loop first of all we are going to check our condition we are checking that if I is
still less than 10 or not. For the second time I is one, I still is less than 10. So,
we are going to execute our code. This circling here is going to continue and continue until
our condition is false. When it's false, we are going to break out of the photo. So in
this case, we are going to print hello 10 times, let's run our application. And let's
see if we can see Hello 10 times, you can see that we have printed Hello 10 times it
seems to be perfect. Instead of 10. You can put any number that you want in here, for
example, you can say five, if you execute your program, we will see that Hello would
be printed five times, instead of this five in here, you can pass a variable for example,
if you had a variable before this for loop, if we had int a is equal to five in here,
later on, we can pass a instead of five to this photo. For example in here instead of
five, we can say if you run the application, we should see the same result. Inside our
for loop we can also have access to the eye itself. For example, instead of printing Hello,
we can print by, let's quickly paint that as well. And let's run the application, you
can see that we are printing the numbers zero to four. Okay for loop is one kind of loops
in Java. The other point is my loop. Let's briefly see that as well. Once again, I'm
going to write the syntax of while loop and we will talk about it after that.
This is the general syntax of a wild loop. First of all, we type void. After that we
have a pair of parentheses inside the parentheses we have our condition, in this case, a less
than 10. After that we have a pair of curly braces and inside the curly braces we have
our code. In this case we are going to print hello, notice that we are not changing the
value of a so this condition is always true. And if I run my application right now, we
are going to stuck in an infinite loop. Let me quickly run the application and see what
an infinite loop looks like. But before that, let me comment this for loop, you can see
that no matter what happens, we are inside an infinite loop we are printing Hello indefinitely.
In some cases, this might be our desired behavior. But in most cases, we probably do not want
that we can exit our application by pressing on this red button, we can also check our
exit code which is negative one in a normal application, we would get zero. Okay, but
how can we exit a wild? There are multiple ways for that the simplest way is to change
the value of a inside the void loop. For example, after printing Hello, I can say a plus plus,
in order to increase the value of a so that it won't be the same every time that we are
going to loop through this while loop. So for example, for the first time a is five,
so our condition is true. So we are going to execute these two line of code, we are
going to loop through the cycle until a is 10. when that's the case, this condition is
no longer met. And we are going to break out of the Void loop. Let's run the application
one more time. This time you can see that we have only printed Hello five time. And
also this exit code is zero which means that we have exited our application successfully
without any error. The other way that break out of avoid loop is to add a break. Let me
quickly add that this time after the first loop we are going to break out of the while
loop it means that we are going to print hello only once. Let's run the application. And
in here you can see that we have printed Hello only once inside while loops we have another
keyword called continue. Let's quickly see that as well. I'm going to delete this break
and before this print statement, I'm going to put an if statement. Let's say if A is
equal to eight. Then let's continue. This continuing here means that go to the next
record of your while loop go to the next round of looping. It means that no matter what is
the rest of the code inside the viable break out of the while loop and go to the next record,
so for example in this case, if A is five, six or seven we are going to print hello.
But if a is eight you're not going to print hello. After that we are going to print hello
in case if a is nice. If you don't want to stuck in an infinite loop, you also need to
change the value of a software inside the while loop. For example before this if statement,
I can say a plus was right now if I run my application, I should say hello only four
times, because if a is eight, we are going to continue the loop, it means that we are
going to jump to the next record and we are not going to print hello, let's run the application
and see if we can see Hello four times. And as you can see, we are seeing Hello four times.
So continue is a useful keyword for when you want to jump to the next record. Okay, now
that we have talked about why loop let's also talk about another kind of loops in Java,
the value of A in here is five. If we change our condition to let's say a less than five,
we know that this condition is no longer true. So we are not going to go inside the violin,
it means that we are not going to run the code inside the void loop. Let's run the application,
we shouldn't see any Hello printed into our console. And as you can see, we can't see
any Hello. But in some cases, you may want to go inside the while loop at least once
no matter what the condition is. For those situations, you can use do while loops. Let
me comment this while loop and let's create a divide loop. It's a lot like via loops,
you need to type do inside the curly braces, you need to put your court for example, we
can say System dot out dot print line. And you can print hello, after the curly brace,
we need to say why. And inside the parentheses you need to specify your condition for example,
we need to say a less than five, after the parenthesis you need a semicolon, of course,
in this case, because A is equal to five, this condition here is not true. But even
with that condition, we should see this Hello at least once because we are using two wild
loops. Let's run the application. And let's see if we can see Hello. As you can see, we
can see Hello only once. So do while loops are useful for when you want to execute your
code, at least one no matter what the condition is. We also have another kind of loops in
Java called for each loops. But I'm not going to talk about that in here. Because I think
that if we know about different arrays and collections, and in regard to that if we know
about object oriented programming, we will understand it much better. So I'm going to
talk about for each loops after we have learned about all those stuff. Okay, I think that's
enough for loops. Now we are going to have a quick challenge. But before we have our
challenge, we need to know about two more things, we need to know how to get the user's
input. And also we need to know how to generate a random number. Later on, we will see that
how these two are going to be useful. In order to get the users input, you can use something
called a scanner. The syntax in here might not be familiar to you because we haven't
talked about object oriented programming. But we are going to need that in our challenge.
Because we want to have an interactive challenge. If you don't understand the syntax, just copy
and paste the code that I'm writing in here. In order to define a scanner, you can type
something like this, you can see the scanner with capital S, notice that it's coming from
Java dot util package. After that, you need to name your scanner, which I'm going to say
a scanner, after that you can say is equal to new scanner. Once again, with capital S
inside the parentheses you need to say system.in we have seen system dot out which was useful
for outputting something system that is useful for when you want to get the user's input.
Now you have a scanner, you can get a number from user like this. You can say int answer
is equal to a scanner, dot next int this one down here by saying a scanner dot next int
our scanner is going to wait for the user's input until the user enter some number. We
will see that in action in a minute. But before that, let's quickly output something. For
example, let's say answer was plus the value of answer. Also, let's type something before
our scanner. Let's say please enter a number. Let's run our application. And let's see what
these few lines of code are going to do. You can see that we are saying please enter a
number text. And in here console is waiting for the user to enter a number. For example,
I can say five by pressing Enter. You can see that answer was five. And we have exited
our application successfully. So by using a scanner, you can get the user's input. Besides
integers beside number, you can also get a text from user if you want. Let's quickly
see that as well. First of all, let's go Something, let's say enter your name. After that, I can
say something like this, I can say the string name
is equal to scanner dot next, you can see that this next method is going to return a
string. Don't worry about all of these new stuff, method, scanner classes, object oriented
programming. We will talk about all of these later on. But for now, because we are going
to need the user's input, we are going to figure out a way to receive that. So by using
scanner dot, Next, you're going to get the user's name. And after that, let's create
something. Let's say hello, close the name of the user. Let's run our application once
again. In here, first of all, we need to enter a number. Let's add four. After that the scanner
is waiting for my name, let's say Mesa. And we can say hello Mesa message. So this way
we can get the user's input. Let's also see how we can generate a random number. I'm going
to comment these lines of code for now, in order to generate a random number, you can
say something like this, you can say random with capital R, let's name it random, is equal
to new random. And we don't need to provide anything inside the parentheses. After that,
in order to generate a random number, we can say something like int number is equal to
random dot next int, this method in here is going to generate a random number. let's print
that number. That's the number plus the number. Let's run our application. Once again, you
can see that we have this random number. It's also useful to specify a domain for your random
number. Right now, our random number is this weird negative number, it might not be useful,
it's good to define a domain. In order to define that domain, you can pass a number
to this next int method. For example, if I put 20 in here, and if I run my application,
once again, the number should be somewhere between zero and 19. If you want your random
number to be somewhere between one to 20, you can simply add one in here, we can say
plus, but let's run the application. Once again, we can see mine this time. This numbering
here is completely random. It means that every time that we were on our application, we should
see a different number, you can see that we are generating different numbers. Okay, now
that we know how to get a random number and also how to get the user's input, it's time
for our challenge. Here is our challenge we are going to create again, the concept behind
this game is to generate a random number and ask the user to guess that number. continue
asking the user until you receive the correct number. Also, to make everything more fun.
After five times of guessing gronke show a game over message to the user. Here is how
our game is going to work. First of all, we are going to show a welcome message. After
that we are going to ask for the user's name. And we are going to say hello to the user.
After that, we are going to ask for the user's permission to start the game. If you received
a positive answer, we are going to generate a random number and we are going to ask for
the users guests. If the guest is correct, we are going to show a congratulation message
and quit the game. If the guest is wrong, we are going to ask again until we receive
the correct number. Also, as a hint to the user beside the first time, every time that
you're asking for a number tell the user to guess higher or lower. For example, if the
random number is 10. If the user has guessed seven for the first time, tell him to guess
higher if the user failed after five times, show a game over message and quit the game.
This challenging here is the combination of everything that we have learned so far. So
make sure to practice before watching the next video, it's going to help you a lot.
Okay, go solve the challenge. And after that, come back to the next video so that you see
my solution for this challenge. See you in the next video. I hope you solve the challenge
but if not, that's totally okay. After all, it's your first encounter with Java. And it's
the first application that you're writing. In this video, we are going to solve the challenge
together. Let's start by creating a new project. Like before, I'm going to select Java next.
Once again next, let's name this project gets me going. Let's create our project. And inside
our source folder, let's create our package and Java file. Let's name this package or
that may code
that gives me a new Java class. Let's name it main. Inside our main class, let's type
p SVM and now we have our main method. Okay, first of all, we are going to show some welcome
message to the user. So let's say System dot out dot print line, let's say welcome to Wonderland.
After that, we are going to ask for the user's name. So let's say Please enter your name,
or marry have your name. After that, we need to receive the user's name, in which we need
a scanner for that. Let's create our scanner. Let's say scanner is equal to new scanner
system.me. After that, let's say string name is the call to the scanner dot next, and let's
say hello to the user. Let's say hello, plus the name. After that, let's ask for the user's
permission to start the game. Let's say Shall we start? in here? I'm going to give the user
two option. And here are my two options. Let's say one, yes. And the other, let's say no.
But in order to format my text a little bit better, I'm going to add a special syntax
in here in which you haven't seen so far. By adding backslash T. I'm adding a tab before
this. Yes. So this will be formatted with a tab. Let's add one in here as well. Nothing
is special Just for formatting my text a little bit better. After that, I'm going to save
the user's answer. Let's say eight, begin answer is equal to the scanner dot next int.
And after that, I'm going to create a while loop. Let's say vine begin answer is not equal
to one, we are going to continue asking our question. So let's copy these three line of
code and paste them inside our while loop. After that inside the void loop, we also need
to store the new users answer for that we can say begin answer is equal to the scanner
dot next int. This way, if you are not going to break out of this while loop until the
users answer is valid, when it's fun, we have the permission to start the game. After the
void loop, it's time to generate a random number, let's say random, random is equal
to a new random. After death, let's say int x is equal to random dot next int for the
domain, I'm going to say 20. Because I want my number to be from one to 20. So I need
to add a plus one in here. After that, it's time to ask the user to guess a number. So
let's say
please get a number. And in here, we need to save the user's answer.
Let's say int, user input is equal to a scanner dot next int. In here, I'm going to define
three new variables and their usage as will be clear in a minute. Let's quickly define
them. First of all, I'm going to save the times that user has tried the game. So let's
say eight times tried is equal to zero initially, after that, let's define a Boolean, let's
say Boolean. As well, initially, it's false. And after that, a Boolean indicating if we
should finish the game. So let's say should finish. Once again, it's going to be false.
Initially, After defining these three new variables, I'm going to create another vial
of let's say, why should finish is not equal to two.
Then,
inside the while loop, first of all, I'm going to increase the times that user has right.
So let's say times three plus plus, after that, I'm going to create an if statement.
Let's say if times three is less than five, then we have the permission to continue. If
it's more than five, in the else case, we need to break out of this while loop. For
that, I'm just going to say short finish is equal to true. This is going to be the last
line of our while loop. So if we look once again, because this condition is no longer
met, we are going to break out of the while loop. But in case the time stride is less
than five, we are going to check that if the users input is equal to the randomly generated
number for that I can say if user input is equal to x, then we need to change the value
of husband to true because user has won the game. And also we need to break out of this
while loop let's say short finish is equal to true. Let's add an else if case to this
if statement. Let's say else. if let's say if users input is greater than r x, which
is our randomly generated number, we are going to tell the user to guess lower for that we
can say, guessed lower. And of course, we need to wait for the new users inputs for
that we can say user input is equal to a scanner dot next int. We also need another case in
here in case the number is less than x. If that's the case, we are going to tell the
user to guess higher. And also if you're going to wait for the new user input, let's say
user input is equal to scanner dot next int. We are done with this void loop, we will review
what we are doing in here. But after the void loop, we need another if statement, we can
say if the user has won the game. If that's true, then we are going to show a message,
let's say congratulation. And also let's say to the user, in which try he or she has won
the game. Let's say you have guessed in your plus times tried plus guess. But in the else
case, first level, we are going to show a game over message. Let's say game over. And
after that, we are going to say what was the number,
let's say the number was plus x. This is our entire application. Let's review what we are
doing in here. First of all, we are showing a welcome message to the user. After that
we are asking for the user's name, we are creating a scanner and after that we are waiting
for the user to enter his or her name. After we received the name, we are waiting for the
user's permission to start the game, we are saving the user's input in here, inside the
while loop we are making sure that we have the permission. If we don't have the permission,
we are going to ask again and again until we have the permission. After that, we made
sure that we have the permission, we are creating a random number, the random number is between
one to 20. And after that, we are asking the user to guess a number. We are saving the
user's input in here, we have defined three new variables in here times through it has
one and short finish. Next, we are creating this viral loop. It starts from here and ends
in here. The condition of this viral loop is vile, we shouldn't finish the game continue
the loop. First thing inside this while loop we are increasing the time stride. So if it's
the first time because the time stride is initially zero, we are changing that to Vaughn.
After that we are checking that if times three is less than five, we are comparing the user's
input with the randomly generated number. If that's equal to a random number, we are
changing the value of this husband to true and also we need to break out of this loop.
So we are changing the value of this short finish to true. But in case the user input
is greater than our random number, we are telling the user to guess lower and also we
are waiting for the user's input. But in case the user's input is less than our random number,
we are telling the user to guess higher once again, we are waiting for the users input
in case the time strike is greater than five, we are just waiting out of the loop. If that's
the case, the husband is still false. So we are checking that in here. If has one is equal
to true it means that user has won the game. So we are showing this congratulation message.
Also we are showing that English try the user has guessed the number correctly. But in case
if the user didn't want the game, we are showing this game over message and also we are showing
the random number. Let's run the game and see if everything is working fine. We are
seeing the welcome message. After that it's asking for our name. Let's put Mesa we are
seeing Hello Mesa after that we are seeing the message for our permission, shall we start
or it was better to say Shall we begin? Let's say to know once again, we are seeing the
message. This is our first wide look. Also this formatting here is because of that backslash
t that I have used when I have created this text I'm talking about this one, this backslash
t in here. Also beside one and two. If you enter any other number, we should see this
message once again. So let's say three for example. You can see that we are seeing the
message. Okay, let's say Yes to continue. The console is waiting for us to get a number.
Let's say 10 it says gets lower, let's say five. It seems like I have won the game in
my second Troy. Okay, let's run the game once again. May I have your name? Let's say Mason.
Yes. Let's begin. That's guess another number. It says that gets lower, I'm going to guess
higher in order to debug all the features of the game, let's say 1112 1315. You can
see that after five Troy, I can see the gameover message. And I can see that the number was
nine. Okay, it seems like our application is working perfect. Just before I finish off
this video, let's change this message in here too. Shall we begin? I think that's better.
Okay, this was our challenge. I hope you solved it if you did do celebrate, but if not, that's
okay. Later on in the course, we are going to practice all of these many more times so
that we make sure that we have learned everything that we see in the course. Okay, I think that's
a good point to stop the video in here. Just before I do that, I'm going to say that in
the next video, we are going to talk about arrays in Java.
See you in the next video. In this video, we are going to talk about arrays in Java.
Up until now if we wanted to store some simple data, for example, a number or maybe a text,
we could have used variables. For example, we could have said int a is equal to five.
But what if we want to store a list of different variables for example, a list of names or
a list of students. for storing a list of variables, we have multiple options in Java.
The first one that we are going to talk about in this video is called array, let's see how
we can have an array of for example strings. For that we can say for string. After that,
we need a pair of square brackets. After that, we need the name of our array. For example,
I can say students, next I can say new string once again with a pair of square brackets.
This new keyboard in here is simple English, it means that we are defining a new array
of strings inside this square bracket, we can define the size of our array. For example,
if we want to serve five different student names, I can say five in here, as simple as
that now we have an array of size five. After defining your array, you can put some values
into it. For example, I can say the students with an index of let's say, Vaughn is equal
to let's put my name in here It may sound this way, we are assigning this value to the
element in which has the index of one in our array, there is a very important point in
here and that indexes in Java starts from zero. So in here, when I said the students
with an index of one, it means that we are referring to the second element in our array.
If you want to assign some value to your first element in your array, you can say students
with an index of zero. Let's see that as well, let's say is equal to, for example, so we
can keep doing this for all of our elements inside our array. Besides assigning values
to your array elements, you can also get the value of those elements as well. For example,
in here, we can say print the value of a student with an index of
two. You can
guess it in this case, it should be Tom. Let's run the application and see if we can see
Tom. As you can see, Tom has been printed. This is one way of assigning different values
to your array elements. The other way is like this, you can assign different values at the
time of creating your area. For example, in here I can see a string array, let's name
it employees is equal to instead of saying new string array, I can pass a pair of curly
brace. And inside that curly brace, I can pass my values. For example, I can say Mesa,
or every other value that I want. So these are the two ways of assigning values to your
array elements, like a string array, you can also have other kinds of arrays as well. For
example, if I want to put some numbers inside an array, I can say int with a pair of square
brackets, let's say numbers is equal to, let's say a pair of curly braces. And inside those
curly braces, I can pass my values, let's say one, two, through six. When you're working
with different arrays, for loops can be very helpful to access all of the elements inside
your array. For example, if I want to print all of the values inside this numbers array,
I can say something like this, I can create a folder. Let's say for int i is equal to
zero, i less than in this case, the size of my numbers array is six. So let's say six.
After that, let's say i plus plus. Inside the for loop, I can say print the value of
numbers with an index of I. This way, I am going to print all of the numbers inside my
numbers array before running the application. I'm going to comment this line of code so
that we Have a clean console. As you can see numbers from one to six have been printed
into our console. So for loops are very helpful when you're working with different kinds of
arrays. In here, we have hard coded these six. But what if we don't know the size of
our array? For example, if we get some values from our database or from our server in that
case, it might be possible that we don't know the size of our array. How can we use for
loops with arrays, if you don't know the size of our array for that, we can use a helpful
property of different areas. For example, in this case, I can say numbers, dot length.
This way, we don't need to be worried about the size of our array, we can just use it
like this. Let's run the application once again. And as before, we have printed numbers
from one to six. Later on, we will see that beside primitive data types and strings, we
can also have different kinds of arrays for different objects, for example, for different
classes in Java, but more on that later on. So using arrays is one way of having a list
of different items. But there are some limitations to arrays. For example, the size of our array
after the definition is immutable. Let me show you what I'm talking about. In here.
When we define our students array, we have set the size to be five. But what if we want
to change the size of our array, for example, what if we want to define a sixth element?
For example, if you say the students with an index of five, let's say Brian, if you
hover over these five, you can see that array index is out of bounds, it means that these
elements in here cannot fit in our array. For example, if we try to reach to that element,
you should see a problem, let's say print the students with an index of five. Before
that, let's comment this line of code. Let's run our application. As you can see, we are
getting an exception in here. Once again, when we get exceptions, it means that our
application crashes. And the exception type in here is array index out of bounds exception,
which means that the sixth element does not exist in our array. So arrays are immutable
after you define them, you cannot change their size. This is one of the limitations of simple
arrays in Java, there are others as well. For example, you cannot do that much with
your arrays. later on. When we use other kinds of collections in Java, we will see that we
have all kinds of operations on our array elements. But I'm not going to talk about
other kinds of collections in here because I think that it's better to talk about them
after we know about object oriented programming. Later on in the course, we will talk about
other kinds of collections like array lists and maps. Just before I finish off this video,
I'm going to write a simple application in which will help us to practice what we have
learned about arrays. Before that, let me comment all of these lines of codes. And let's
start creating our application down in here. In this application, I'm going to simulate
the contacts application on the phone, I'm going to show a list of different contacts
names on a phone for to the user. And after that by typing the contacts name, we are going
to give the user that contacts number. In order to simulate that first of all, we are
going to need a string array, let's say a string array names is equal to let's pass
our names directly. Let's say Mesa. After that, we need a list of different numbers
for that I'm going to create that integer array. Let's see numbers is equal to and let's
initialize our array. After that, I'm going to show the name of all of the available contacts
on the phone. So let's create a for loop. For int i is equal to zero, i less than names
that length, and i plus plus. let's print the names inside our names array. Let's say
print names with an index of I. After the for loop, I'm going to ask the user to enter
a name. So let's say print. Let's say please enter a name. After that we need our scanner.
So let's create that is equal to new scanner system that mean after that, let's get the
contacts name. Let's say
the string name
is equal to a scanner dot next. Then I'm going to create a for loop and inside that loop
I'm going to find the index of the contact. So let's say for int i is equal to zero. i
less than names, dot length, i plus plus. Let's write an if statement in here. Let's
say if name is equal to names with an index of joy. Then inside this if statement we are
going to print the contacts number for that I can see print numbers with an index of pi.
But right now there is a problem with my code, I'm not sure that if it's going to work fine
or not, and that's because of these two equal signs in here. Let's run the application and
see if it's going to work. Let's enter our name in here. Let's say Sarah. And as you
can see, nothing has been printed. Let's talk about these two equal signs in here. If you
hover over the warning in here, it says that the string values are compared using two equal
signs not equal with a pair of parentheses. The problem in here is that when you're using
two equal signs, you are checking for the equality of two strings by a reference, I'm
talking about the reference inside the devices memory Ram. Whenever you are storing a variable,
that variable allocates some space inside the device's memory RAM, and that allocated
a space has some address. And that allocated the space has some reference in which we are
comparing those two references in here together. So even if the value of two strings in here
can be the same, but the reference might not be for that reason, it's not a good way to
compare two strings by using these two equal signs. Instead of using these two equal signs,
there is a better option for comparing two strings, and that's the equals method. Let's
see that as well. In here, I can say name, dot equals, and inside the parentheses, I
can pass the other string. For example, I can say names with an index of I. If you remember
from the previous videos, I said that the strings are not primitive data type, and they
are objects in Java. And because they are objects, we have some properties on our strings.
For example, this equals method. Once again, we are going to talk about methods later on
in the course when we talk about object oriented programming. But for now, just know that there
is a method called equals in our string class, in which we can use to check the equality
of two strings by the values. So now, if you're on our application, we should get Sarah's
number. Let's run the application. In here, once again, let's see Sarah. And as you can
see, we are getting the number. Let's also check that if you're getting the correct number,
Sarah is the second contact in our array. And the second element in our numbers array
is this number in which we have printed into our console, it seems like our application
is working fine. Okay, I think that's enough talking about arrays in Java. In the next
video. Finally, we are going to start talking about object oriented programming. That's
a very important topic, and it's going to help you a lot to understand Java, so make
sure to Don't miss the next two or three videos. See you in the next video.
As I said at the end of the previous video, in this video, finally we are going to start
talking about object oriented programming. Up until this point with the help of variables,
we could have defined simple data types. For example, if we wanted to have numbers, we
could have say int number is equal to five, four. Similarly, we could have defined characters,
texts, and all sorts of variables that we have talked about in previous videos. But
what if we want to have a more complex data? For example, how can we define a car in Java
or a phone or every other object that you can think of? Well, that is possible with
the concept of object oriented programming. In object oriented programming, you define
your own objects, the core component of object oriented programming our classes. Right now
you can see that we are inside the class, this class is named main. But if we want,
we can create our own class as well. Let me quickly delete this line of code. And let's
create a new class. For example, inside my source folder, inside my package on the package,
I can right click and by selecting new Java class, I can create my class. In here I need
to name my class. For example, I can say phone, notice that there is a convention, you need
to use upper cases for the first letter of the name of your class. Also, you cannot have
spaces or some strange characters as the name of your classes. So in here, I'm just going
to simply name my class form. Notice that there are other options in here as well for
example interface and you know, later on we will talk about them but for now, let's just
create a class the one that has the CI con. By pressing Enter, we can create our class.
This is how a class looks like it lives inside the package right now its package is altered.
maker.org is section one. Also if you take a look at the left pane here you can see that
a new class has been added into our package. Every class has the keyword class in its declaration.
Also every class has a name and besides This too has an access modifier. We will talk about
access modifiers in few minutes. But for now let's continue on. Every class has a pair
of curly braces in which we can put our code inside them. A class is going to represent
an object. In this case, we are going to represent a phone. If you think of different objects,
objects can have different properties. For example, in the case of a phone, a phone can
have some amount of memory RAM, it can have a screen size. Beside that it has a name,
and all sorts of other properties that you can think of. When you define a class, you
can have properties for your classes as well. For example, in this case, if we want to give
some properties to our phone class, we can say something like this, we can say the string
name. Now we define a property called name for our phone. Notice that I'm not initializing
the property of my class in here, I'm not giving a name to my phone. And that's because
when we declare a class, we can reuse that class, we can create multiple instance of
that class with different properties. For example, if we define a new iPhone, the name
would be iPhone, if we define another phone, the name of that phone might be different.
So for that reason, I'm not initializing the properties of my class, let's add a few more
properties to our phone object, let's say in screen size. Beside that, let's say in
memory Ram. After that, let's say int camera. So now that we have created this simple class,
we can instantiate it, we know for sure that the starting point of our application is this
main method inside our main class. So if I need to instantiate my class, I need to do
it in here. And here is how you can create an instance of your class you type the name
of your class, you can see that when I typed the name of my class, I'm getting some suggestions
from IntelliJ. Sometimes you may get multiple options. And that's because different classes
with different names can live in different packages. For that reason, always take a look
at the package that your object lives in. In this case, I am going to just import this
class. After that you need to name your instance for example, I can say iPhone, next I can
say is equal to new phone. This way with the help of this new keyword, we can create a
new instance of our object. Now that we have created an instance of our class, we can have
access to the properties of our objects our class, for example, if you want to define
some property for our class, we can say something like this, we can say iPhone dot, let's say
name is equal to let's name it iPhone 11.
With the help of this dot operator, you can have access to all of the properties or to
be precise all of the fields of your class. Similar to assigning a value to your classes,
properties or fields, you can also get those properties or fields. And here is how you
can get them. For example, if you want to print the name of our iPhone, we can say something
like this, we can say print, iPhone dot name. If you run our application, we should see
iPhone 11 printed into our console. And here is iPhone 11. Similar to that you can have
access to all of the fields of your class. For example, I can say iPhone, let's say memory
RAM is equal to eight. Beside properties, your classes can also have some behaviors.
For example, in the case of a phone, you can play some music with your phone, or you can
call somebody. If you want to define some behavior for your class, you can use methods,
let's see them as built. Let's switch back to our phone class. Here is how you can define
a method I'm going to write the syntax and after that we will talk about it. First of
all, our method has a name in this case its name is play music. After that a method can
have an access modifier like we did for the declaration of our class, we will talk about
this public in a minute. After that every method can have some inputs. For example,
in this case, we are getting a string called trackman. Later on, when we use this method,
we need to pass some input to this method. If you don't want to have any inputs for your
method, you can just pass a pair of empty parentheses. But if you want, you can have
one or more inputs. If you want to have multiple inputs for your method, you can separate those
inputs with a comma. For example in here I can say is drink album name. But I'm not going
to do that in here. But you can have as many inputs as you want. beside a name, an input
and also an access modifier. Every method can return something in this case, it's void,
but if you want you can change it. For example, we can say a string as the return type of
your method. You can also have integers or other kinds of primitive data types. Even
you can return another class with your method. But more on that later on. If you don't want
to return any From your method, you can use the keyword void. Like we did before, we will
talk more about the return type of every method later on. But for now, we are not going to
return anything from this method. So we are going to pass void inside the curly braces
of my method, I can put my code for example, in this case, I'm going to play some music,
or at least I'm going to print something. In this case, I'm just going to say, playing
plus the track me.
Now that I have created this method, I can use it in my main class where I have instantiated
my object. For example, in here I can see iPhone dot play music. Once again, you can
see that by typing dots, I can have access to my methods as well. Right now, I'm getting
error error in here. And that's because when I have created my method, I have set an input
for it. So when I'm going to use my method, I need to pass a tracking. For example, let's
say our beings are bearing the name of a track. Let's run the application and see what would
happen, you can see that we are playing some music. So by defining methods, you can have
behaviors for your classes, there are a few reasons for using methods. The first one is
that you can reuse your method. For example, in here, after this line of code, I can say
iPhone dot play music, and I can pass a different track name, let's pass lamenting case this
time. If we run our application, you can see that our methods are being executed one at
a time. So the first benefit of using methods is that you can reuse them. The next one is
for organizing our code. For example, imagine that you want to have some sort of calculation,
you can pass all the codes for that calculation into a method. And by calling that method,
you can pass all of those calculations to a method and later on, you can just call that
method in order to do the calculation, it's going to help us a lot for organizing our
code. Also, by using methods, you can encapsulate things we will talk about encapsulation in
later videos. But in here inside your class, you can define your methods as private, and
that method will be accessible only from your class and not from outside of your class.
If you go back to main class, you can see that we are getting red warning. If you hover
over there, it says that this method has private access. So by using private methods, you can
encapsulate different behaviors for your own class. Okay, now let's talk about this access
modifier. You can have access modifiers, for your methods for your classes. And also for
the fields of your classes, I can make this name private, if I want as well. Basically,
we have three kind of access modifiers, we have private we have public. And also we have
protected if you don't use anything as the access modifier. It's equal to when you use
public. So there is no difference between public string name or just a string name.
When you use public as the access modifier. For example, if I use public in here, it means
that this field is going to be accessible from other classes as well. But if I use private
in here, let's say private, if I go back to my main class, you can see that we are getting
a red warning, it means that we can no longer use this property. So by setting the access
modifier of our field or method, that field or method is only accessible from inside our
class, beside public and private we also have projects that we've been talking about protected
later on in the course both private and public are the most access modifiers that we are
going to use in the course for the access modifier of your class. You can also have
private admin group inside there. But usually you wouldn't do that right now you can see
that we are getting a red warning in here. And the warning says that modifier private
not allowed in here. Do you use private mostly when you use inner classes we will talk about
inner classes later on in future videos. But basically, they are some nested classes inside
other classes. When you define them as private classes, you can have access to those nested
classes from the parent class. We will talk about that later on when we talk about inner
classes. For now let's just change this one to public. There are a few reasons for using
private as the access modifier of your fields or methods. The first one is that you may
want to limit the access of your fields or methods to the class itself and not other
classes. For example, imagine that you are writing some code that you are going to publish
for other developers. In that case, you may want to protect your classes against changes.
For that reason, you may want to change the access modifiers of your fields and also methods
to private but now that we have defined the access modifier of this field, as Private,
how can we have access to this name from outside of this class, there is a way to that, let's
quickly talk about it, we can create public methods. For example, down in here, I can
say public as the return type of this method, I'm going to say void. For the name of this
method, I'm going to say set name. As you can see, when I use the keyword set, I am
getting some suggestions from IntelliJ. Basically, there is a convention among programmers to
set the name of the setter methods like this, an intelligent is smart enough to know the
convention. But of course, you can name them whatever you want. In this case, I'm going
to follow the convention. So I'm going to say certainly, inside the parentheses as the
input of this method, I'm going to say,
the string, let's say name. Inside this method, I'm going to change the value of this name
to whatever the name that I'm going to receive via this method. For that I can say this dot
name is equal to name, the keyword, this is referring to the current object that we are
in. For example, right now we are inside our phone object, our phone class. So by using
this dot name, we are accessing the property of this class called name. The second naming
here is the input of our method. So we are setting the value of this name to whatever
we receive, we are the input of this method. Now because the access modifier of this method
is public, we can use it inside other classes. For example, from inside the main class, if
we want to set the name of our iPhone, we can say something like this before that, let's
delete this line of code. We can say iPhone dot, let's say set name. Inside the parentheses
of this method, I need to pass the name of my phone, let's say iPhone 11. So this way,
I don't have access to the field directly. But by using the method, I can change the
value of that field. But what if we want to get the value of this field? Well, we can
write another method. And here is how we can create that method. We can say public as the
return type of this method, I'm not going to say void because I'm going to return this
name. So I'm going to say a string. Once again, there is a convention in here, we can say
get name, we don't need anything as the input. Inside this method, we are going to return
something. And here is how you can return something from a method you type return. After
that, whatever you are going to return. In this case, I am going to return this name.
So I'm going to say this dot name. Once again, this refers to the current object, this dot
names means the name inside our phone object. Now that we have created this method, inside
our main class, instead of saying iPhone dot name, I can say something like this, I can
say, iPhone dot get name. If we run our application, once again, we should see the name of our
form. Let's run the application. You can see that iPhone 11 has been printed. Beside creating
these kinds of methods ourselves, we have another option. If we come down in here, we
can use the help of our ID, we can right click in here, we can go to this generate method.
Similarly, we can press ALT plus insert, you can see that in here we have a list of options.
We have getter, we have surfer, and also we have getter and setter, if we want to create
getters and setters, we can select that. After that we can create the field that we want
to create getters and setters for for example, if we select a camera, and press OK, you can
see that Id has created some codes for us, you can see that these methods are almost
identical to the methods that we created above in here. Because creating getters and setter
methods are very usual, Id can help us with the creating of them. But why are we using
getters and setters? Well, there are a few reasons for that. The first reason is encapsulation.
Let's quickly talk about that a bit. For example, if you don't want to give others the permission
to change the value of our fields, we can delete the setters. In that case, we only
give them the permission to get the value of objects but not set the value of objects
or vice versa, we can create setter methods for our class. In that case, we can set the
value of our properties from other classes, but we cannot get the value of this properties.
The first reason is encapsulation. The other reason is that sometimes in your setter and
getter methods, you may want to do some other operations as well. For example, if you're
setting a name for your phone, you may want to validate that name before setting it. In
that case, you can have some validation before assigning the value to your property. Okay,
that's enough talking about getters and setters. That's greatly changed the access modifiers
of have these two private, and let's create getters and setters for the ones that we didn't
create any. down in here after everything, I'm going to press ALT plus insert, let's
say getters and setters. And let's select both of them, you can see that IntelliJ has
created these codes for us. But because we have changed the access modifier of this memory
ram to private, now we have an error inside our main class in here, instead of saying
iPhone dot memory is equal to eight, I can say something like this, I can say iPhone
dot cert memory. And I can pass eight. What we did in here is one way of instantiating
objects, we have defined our object. After that we have set different values for our
object, there is another way of instantiating your objects, let's quickly see that as well.
So if I switch back to my phone class, above the play music method, I can create something
called constructor. Let's use the help of ID for creating that constructor. Once again,
I'm going to go to generate menu. The first item in here is constructor in here, I'm going
to select all of the fields. And I'm going to press OK. And as you can see, this code
has been generated for us this is called a constructor in object oriented programming.
Our constructor needs to have public as its access modifier, it cannot be private. After
that the return type of this constructor is a phone the exact item that we are in our
constructor does not have any name. And as the input of this constructor we are receiving
all of the fields of our class we are receiving name a screen size, memory and camera inside
our constructor we are assigning different values that we have get for your the input
to the fields of our class. Now that we have created a constructor if we switch back to
our main class, we can see that we are getting a red warning in here. The warning says that
expected four arguments both found zero, it means that now that we have created the constructor,
we need to pass the values directly to the parentheses of this new form. And here is
how you can pass them. The first one was the name of our phone,
let's say iPhone 11. I don't remember the order of things. But the next one was a screen
size, let's say five, I believe the next one was memory RAM, let's say eight, and the other
one was camera, let's say eight as well. Now that we have instantiated our object like
this, we don't need to directly set fields of our class, we can safely delete this also,
let's delete this as well. In this line, we are printing the name of our iPhone. Let's
run the application and see if we can print the name correctly. As you can see, iPhone
11 has been printed. So when you are using constructors, you need to pass the values
of different properties at the time of instantiating your objects your class can have multiple
constructors as well. For example, if I come down in here, and once again, right click
and go to the Generate menu. If I select konstruktor, this time, I can select different items. For
example, in this case, I'm going to select only name and memory. Now if I press OK, you
can see that we have another constructor which will only receive name and memory run. But
if you hover over this phone, you can see that this constructor is never used. If we
switch back to our main class, we can create another phone and we can use the second constructor.
Let's quickly do that. I'm going to do that after these two line of code. Let's say phone.
And this is the beauty of object oriented programming. When you create a class, you
can reuse that class as many times as you want. For example, in here, I'm going to define
an entirely new font. Let's name this one pizza, let's say is equal to new phone. This
time I'm going to use the second constructor for the name I'm going to save pixel three,
and four the memory, I'm going to say 16 you can see that the ID is happy with this way
of defining a new phone. Now we have two different instances of our phone class having two different
kinds of constructors for a class is called polymorphism in Java polymorphism has other
types as well. We will talk about it later on. Okay, I think that's a good point to stop
the video in here. In the next video we are going to continue our talk about object oriented
programming. There are many more things left that we need to talk about. See you in the
next video. In the previous video, we have talked briefly about classes what those are,
how can we create them and also what are the most usages of a class in this video we are
going to talk about some important concepts regarding to object oriented programming.
The first of those concepts is inheritance. I'm going to talk about inheritance with an
example because I think that would make everything easier to understand First of all, I'm going
to create a class called doc. For this doc class, I'm going to define some properties.
For example, I'm going to define a name, let's say private string name. After that, I'm going
to define a color, let's say private string color. Let's also define the number of legs,
let's say private int, legs. And also I'm going to define a Boolean indicating that
if a dog has a tail or not, so let's say private Boolean, let's name it as tail. Let's also
create a constructor for our class by pressing ALT plus insert, you can see the dialog in
here. Let's select all of the fields. Let's also create some getters and setters for our
class as well, once again, by pressing ALT plus insert. By selecting this getter and
setter, we can create getters and setters. Let's also define a method for our class as
well. I'm going to say public void, let's name this method eat, I'm going to indicate
the eating behavior of a dog. As the input of this method, I'm going to receive a food.
So let's say let's drink food. And inside this method, I'm just going to print something,
let's say eating class our food. Okay, nothing special is in this class. First of all, we
have created these four fields. After that we have created the constructor, we have created
our getters and setters. And we have created this eat method, or we have talked about why
I'm doing all of this, let's also create another class called bird. Let's say bird. For this
bird class, let's also have a name. Also, let's have a color. Let's define two more
fields, one for the number of legs, private int legs, and also another one indicating
that if our bird has a tail or not, so let's say private Boolean,
let's say has, for this bird class, let's also create a constructor, and some getters
and setters. I'm going to create my eath method for this bird class as well, let's say public
void,
eat.
Let's get the name of our food. And inside this method, let's create something. Let's
say eating that food. You probably can see the pattern in here, there are a lot of similarities
between our bird and dog class. Both of those classes have four fields, they have a constructor,
they have all of these getters and setters. And both of them have this eat method. In
programming, this is not efficient, you basically are copying and pasting yourself. Instead
of defining our bird and dot class like this, we are going to use another concept in object
oriented programming called inheritance. For that, first of all, I'm going to create an
animal class. And after that, I will extend the animal class to the spirit and dog. Let's
see how we can do that inside my package, I'm going to create my animal class, let's
say animal. First of all, let's add some fields, I'm going to use the four previous fields.
Let's also copy them from here. And after that, let's create a constructor and getters
and setters and our eat method. Don't worry, we will reduce the amount of copying and pasting
in a minute, let's say public, void eat string food as the input. And let's print the name
of our foot. Now that we have created this animal class, creating bird and dot class
is going to be much simpler. For that I'm going to delete all of the quotes inside this
bird class. Let's delete all of them. This time, instead of defining all of the fields
and constructors and methods, I'm going to say extends animal. This way we are saying
that our bird is an animal, we are extending the animal class and later on if we want,
we can customize our birth class as well. But right now you can see that we are getting
a red warning. And the warning in here it says that there is no default constructor
available in this package. Basically, it says that you need the constructor, let's press
ALT plus insert in here. And let's select constructor. And as you can see, the ID has
generated this constructor you can also see that even though we don't have any fields
in our birth class, we are receiving some arguments in the constructor. But this constructor
has rather a strange syntax. We haven't seen this super keyword so far. Basically, this
super keyword means that do whatever you're doing with these parameters in the parent
class. In this case, our parent class is animal. So this super is passing all of these arguments
to the constructor of our animal class. It means that It setting all of the values to
the fields of our animal. Now that I have created my bird class like this, if I want
to instantiate it, for example, inside the main class, I can do as before, let's say
bird. Let's name it Phoenix, let's say is equal to new bird. And as you can see, the
constructor is waiting for our arguments. First of all, we need a name. That's past
Nina, we need the color, let's say golden. After that, we need the number of legs, which
I'm going to pass to, and a Boolean indicating if our bird has a tail or not, that's passed
through. Once again, remember that in our bird class, we didn't have any extra methods.
But in my main class, if I want, I can use some of the getter methods of our parent class,
in this case, animal. For example, if I want to get the name of my bird, I can say something
like this, I can say Phoenix dot get name, you can see that we have all of the getter
methods. Let's run the application. And let's see if we can print the name of our birth,
you can see that the name has been printed successfully. Beside getter and setter methods,
you can also have access to all of the other methods that we have created. For example,
if you remember inside the animal class, we have created this each method, let's see if
we can use it in our bird instance. For example, in here, if I say
Pheonix, that it, I don't know what Phoenix is, its birth, let's just pass meat. And let's
run our application. And let's see if we can successfully execute the eat method. And as
you can see in here, Nina is eating meat. Now that you have created your birth class,
you can also change the dog class as well. For example, in here, I'm going to delete
all of these codes once again. Instead, I'm going to extend the animal plus, once again,
let's say extends animal, you can see the red warning. And that's because we need the
constructor, you can see that the amount of similar codes that you were copying and pasting
is a lot less right now, the bird and dog class are basically the same. But we know
for sure that a dog and a bird are not the same, they may have other fields. And also
other behaviors as well. If you want, you can customize your child classes as well.
For example, inside my birth class, I want to add another field, I'm going to say private,
let's say ain't veins, we know that dogs do not have wings. In here. I'm going to delete
this constructor and I'm going to create it once again, because I'm going to use the help
of my ID, let's press ALT plus insert. In here, I'm going to select Winx. And this time,
as you can see, the constructor has been changed a little bit, the super statement is as the
same it means that do whatever you're doing with all of these four fields. But because
inside our parent class inside the animal class, we do not have a wings field, these
wings hasn't been passed to this super statement. So we are setting the value of wings directly
inside the constructor of this bird class. This way we can customize our child classes.
Right now the duck does not have any wings field, but our bird does what's right now
if I switch back to my main class, I should get a warning. And the warning in here says
that you need to pass a wink that's also fascinating here as well. For example, I'm going to say
to we can also create getter and setter methods for these wings as well. For example inside
our bird class down in here, let's press up plus insert and add select getter and setters.
Now that we have created the getters and setters, let's see if we can print the number of wings.
For example, down in here, I'm going to say Phoenix dot get wings. Let's run the application.
And you can see that two has been printed as the number of means beside getter and setter
methods. You can also have other methods inside your child class as well. For example, inside
the spirit class, if I want, I can define a fly method as well. Let's quickly see that
let's say public void
fly.
I'm not going to receive anything as the input let's just print something. Let's say this
dot get name. Also if you remember from the previous video, I said that this keyword refers
to the current object that you are in. In this case inside the bird class, we don't
have any getname method. But because we are extending the animal class, we can use the
start getting, let's say is flying. So besides using fields, you can also customize your
child classes with different methods. Let's see if we can call this method from inside
our main class, let's say Phoenix dot fly. As you can see, Nina is flying. Also beside
the finding new methods inside your child class. You can also change the behavior of
methods of your parent class. For example, if you were Remember, inside the animal class,
we had this eat method, I'm talking about this one, which inside this method we are
saying eating food inside our bird class. If we want, we can change that. For example
right in here, I'm going to press Ctrl plus Oh, you can see that we are seeing a list
of different methods, I'm going to override this eat method. So by overriding, you're
going to say that I'm going to change the behavior of this eat method from what is happening
inside the parent class. Once again, we are seeing this super keyword, it means that do
whatever you're doing with this parameter inside the parent class. If you want, you
can delete this super statement completely, but let it be for now we will delete that
later on. After this super keyword. I'm going to print another statement in here. Let's
just say eating finished. So this way, we have changed the eat method
inside our child class from what it was inside the animal class. Right now inside our main
class, we have executed this line of code for Unix that eat let's comment the lines
after that, so that we can see what's going to be the behavior of this eat method. Let's
run the application, you can see that we are saying eating meats. And after that eating
finished, if you want to completely change the behavior of your method inside the child
class, you can simply delete this super that eat method. For example, in here, I'm going
to say chewing food instead of eating fruit. So in both child and parent class, we have
this eighth method, but their behavior is different. Let's run the application and see
if we can see the difference, we can see that we are chewing meat. So what we did so far
is called inheritance in object oriented programming. We have inherited the birth class from the
animal class. You can also continue this inheritance multiple times. For example, if you want,
you can inherit the dog object. Let's quickly create another class in here. For example,
let's say Shephard. Now in here, I can say extends the dog class, you can see that once
again, we are getting this red warning. Let's create our constructor in order to overcome
that problem, we can see that even though inside the doc field, we do not have any field.
But inside our Shephard class in the constructor, we need all of those arguments, because the
parent of this Shephard class, which is dog is the child of another class, which is animal.
So you can do this inheritance as many times as you want, you can also create another class
and extend the shefford class. But I don't think that mean anything in here, I just wanted
to say that you have that option. Okay, I think that's enough talking about inheritance.
The other concept that I'm going to talk about is called polymorphism. We have talked a bit
about polymorphism in the previous video, but let's talk more about it in here. So in
previous video, I said that one kind of polymorphism is that for a class to have multiple constructors,
for example, inside our animal class, if we had another constructor in here, which will
accept different number of fields. For example, the first three now that we have two constructor,
this is one kind of polymorphism. Now we can create this animal class in two different
ways. Basically, polymorphism means having multiple forms. But there are two more kinds
of polymorphism. But before we talk about that, let's delete this constructor, I'm not
sure that if you're going to need that the other kind of polymorphism that we have used
so far is about this eat method. So inside this animal class, we had this eight method,
but also we had it inside the child class, which is birth. But these two eight methods
have different forms, it means that we are doing different things inside each one of
these methods. This is another kind of polymorphism two methods inside a parent and child class
that are doing the same job differently. We also have another kind of polymorphism. And
that's with the methods themselves. We don't have this following method inside our animal
class. But in here, we can also have another method called fly as well, let's quickly see
how we can create another method called flight. I'm going to say public void
fly. But this
time inside the parentheses, I'm going to receive some arguments for example, I'm going
to receive a speed as you can see, the compiler is happy with this way of defining two different
methods with the same name. Now, if I switch back to my main class, you can see that we
are not getting an error but before that, I think I should uncomment this line of code.
Even though we are not getting error warning this in here it means that we are using the
first flow method Let me close all of the unnecessary classes. Inside the bird class
we are using the first method, but if we want to use the second method, we just need to
pass some arguments in here. For example, let's pass 100 we can have methods with the
same name when we have different number of arguments, or alternatively, when we have
different kinds of arguments, for example, in this case, we are passing an integer. But
if we had another method, for example, in which we pass a string, that method can have
the fly name as well. Let's see that briefly, let's say public void fly. And inside the
parentheses, I don't know, let's pass a string. Let's just say name, you can see that the
compiler is happy with this way of defining a method with the same name. So besides the
difference in the number of parameters, the kind of parameter is also important. This
in here is also called polymorphism. using methods with the same name in different ways,
okay, let's delete these two, I don't think we are going to need them. And I think it's
a good point to stop the video in here. In the next video, first thing we are going to
start talking about composition in object oriented programming. See you in the next
video. In the previous video, we have talked about inheritance and polymorphism. In this
video, we are going to talk about composition in object oriented programming. In previous
videos, I said that classes are useful for when you want to define your customized data
type. For example, imagine that you are going to create a car in that car you have multiple
systems, for example, you have a system for a stereotype, your car also may have some
engine, it can also have a fuel system. If you want to simulate a car. In programming,
you can create different classes, for example, for engine for a stereo system, and also for
field system. And after that inside your car class, you can compose all of those classes
to have a car, let's see how we can use composition in Java. In my package, I'm going to create
a class called engine. For this engine, I'm going to have two fields. First of all, I'm
going to define a model. So let's say private string model. And after that, let's have an
integer, private int. I'm going to name this integer, rpm, rpm stands for round per minute,
it's just a property of different engines. I'm going to keep it really simple. So I think
these two would be enough. After that, let's quickly create our constructor and getters
and setters. Let's create a car class. And let's see how we can use this engine inside
that, let's say new Java class. Let's name it car. For this car class, I'm going to define
four fields. First of all, let's pass a name,
private string name. After that, let's have an integer for the number of doors. After
that, let's define the color of our car private string color. And after that, I'm going to
include or compose an engine in here. For that I can simply say private engine with
capital E. And in here, you can see that in my package, I have an engine class, I can
import that into this class. Let's name it engine. For the sake of simplicity, I have
composed only one class inside this class. But if you want, you can create another class
for a stereo system and your system and everything else that you want. But I think you will get
my point. If I use only one class after death like other classes, I can create my constructor,
you can see that this engine has been passed to our constructor the same way that we have
passed other data types like integers and strings, we can also have getters and setters
the same way that we had for previous classes. Now that we have created this car, we can
instantiate it. For example, inside our main class. For example, if I want to have a car
object in here, I can say car, let's name it Mercedes, is equal to new car. As you can
see, the constructor in here requires four fields and name doors color, and also an engine
for the name. I'm going to pass Mercedes AMG for the number of doors that's passed to for
the color, let's say silver. What What should we pass as the engine here? Well, we can pass
our engine in two ways. First of all, we can create our engine before this court class.
For example, in here I can say engine, let's name it engine is equal to new engine. And
I can pass a model and RPM for this engine to instantiated for example, I'm going to
say three note for the RPM, let's say 8000. After creating our engine objects, we can
pass it to the constructor of our car for example, in here I can say engine and like
that we can create our car but the other way is to pass your engine directly to the constructor
of your car. For example, I can see a new engine in here. And I can't instantiate my
engine objects directly inside the constructor. Let's say Renault Let's fast 1000. Now that
we have passed it directly, we don't need the first line. Of course, we know that Mercedes,
we don't use Renault engine, but I think it doesn't matter in here. Now that we have instantiated
our car objects, we can have access to all of the fields like we did before. For example,
if you want to print the name of our car, we can say something like this, we can say
Mercedes dot name, or that gets named to riversides. But what if we want to get the model of the
engine of this car? Well, for that, we can say something like this, let's print engine
model. Plus, we can say Mercedes dot
get engine, this method in here. After that, we can once again say dot get model. So this
get engine method will return an engine object in which we can perform another dot operator
on that, let's run the application and see what would be the result. First of all, we
can see the name of our car, and after that, you can see the engine model, which is renamed.
Also instead of using the methods of your engine, you can get the engine object directly.
For example, in here before the printer statement, I'm going to say engine, let's name it engine
is equal to let's say Mercedes dot get engine. You can see the return type in here. But whenever
you are not sure about the return type, you can press down the control key. And by hover
over in your method, you can see the return type, you can see the declaration of your
method which says public engine get engine, it means that this method is going to return
an engine object. Now that you have your instance of engine, you can perform all kinds of operations
on that, instead of pressing down the control key and hover over your method, there is another
option to get the return type and for that matter the declaration of your method. And
here is how you can do that, you can click on the name of your method, and you can press
down the control key. This way, you can see some information about the declaration of
your method. These kinds of documentation are especially useful for when you are using
a third party library or some built in methods. For example, if we click on this print line
method and press Ctrl Q, you can see that the documentation is giving us much more information.
For example, in this case, it says that this method is printing a string and then terminate
the line, you can see some information about the method itself. Also, if you want, you
can have this kind of information on the methods that you yourself create. For example, on
this get engine method. If we switch to our car class, we can create some sort of documentation.
Let's find that method. And here is how you can do that you need to add a comment in here.
In previous videos, we have seen how to create a new comment by using two slashes we could
have create our comment. But these kinds of comments are not useful in here. Instead,
we can use a single slash. And after that, we can have to start by pressing and enter.
You can see that we have these sorts of comments as well. These kinds of comments are useful
for when you have multiple lines. And in here you can have some sort of information in which
later on will be shown when you press Ctrl q on the name of your method. For example,
in here, let's say returns the engine of our car. Now that we have provided this information
in here, if we switch back to our main class on the declaration of our method on the main,
if we press Ctrl plus Q, you can see that in here we can see that information. Sometimes
these kinds of commenting can be useful as well. But there are more to these kinds of
comments. For example, we can provide some links, but more on that later on in future
videos. So this way of using different classes inside other classes is called composition
in object oriented programming. Once again, if you have created some other classes, for
example, a stereo system class and also a fuel system class, you could have add them
one by one in here as well. But for the sake of simplicity, I've just included this engine,
this way we can compose different objects inside one class. Okay, I think that's enough
talking about composition. Let's talk about a keyword in Java called nom. Before that,
let's close this car class. And let's comment all of these lines of codes. There are some
times that you don't want to instantiate your objects immediately. For example, down in
here I can say car Mercedes is equal to I don't want to instantiate my car object in
here like we did before. Instead, I want to post one that too few lines after this for
that you can pass null values for your objects. Let's see now in here as well. This null keyword
in Java means nothing. It means that this car Mercedes is nothing when the value of
an object is now You cannot perform any kind of operation on that. For example, in here,
I cannot say Mercedes dot get name, for example, we are not getting a compile time error, it
means that our application is going to be created. But if you take a look at the highlighting
here, it says that this method is going to produce no pointer exception. If you remember
from previous videos, I said that whenever nullpointerexception happens, your application
is going to crash. So the error in here is going to be a runtime error and not a compile
time error. Let's run our application. And let's see what would happen. You can see that
in here we are getting a red warning in our console says Java dot Lang dot nullpointerexception.
In general, we should always avoid exceptions in our application. Later on, we will talk
about how can we do that, but for now, let's just assume that we know whenever an object
is now we cannot perform any operation on that, for example, we cannot use the methods
inside that object. So whenever you're not sure about null values, for example, whenever
you are retrieving some data from a web server, or from your local database, first of all,
you need to check that if your object is null or not. And here is how you can do that. For
example, you can create an if statement before this Mercedes dot get name, I can say if let's
say Mercedes,
is not equal to Now, I'm going to put an if statement in here in case our object is not
now we are going to call this method but in the else case, I'm going to print something.
Let's say the car was now let's run the application once again, this time, you can see that we
are not seeing the exception, which is good. But right now, we are not doing anything helpful.
With our value being now we are just printing something, sometimes this is the desired behavior.
But sometimes you may want to check that what was the cause of your objects to be no, so
not means nothing in Java? Okay, let's comment all of these. And let's talk about another
keyword in Java called final. So up to this point when we want to instantiate our object
or variable, because I've said something like this, because I said int a is equal to five.
For example, later on, we could have changed this a for example, because I have said, A
is equal to five plus five for a plus fun. This way, we could have changed the value
of our variable. But there are some times that you need to be sure that the value of
your variable wouldn't change. In those cases, you can use the final keyword. And here is
how you can do that before the type of your variable you can say final final int a is
equal to five. Now if I try to change the value of my variable, for example, like before,
if I say a is equal to a plus one, you can see that we are getting a warning in here
the warning says that cannot assign a value to a final variable. So whenever you are declaring
your variables and classes as final, you cannot change the value of that variable or class.
Let's also check the case when we define our classes as final, I'm going to comment these
two line of code. And then in here, I'm going to say final, let's say engine, let's name
it engine is equal to new engine. Let's pass a model and rpm. Now if I try to change the
value of this engine, for example, if I say engine is equal to new engine, with other
parameters, for example, let's change the RPM, let's say 7000. Once again, you can see
the error, the error says that cannot assign a value to final variable. But there is a
point in here. When you declare your objects as final, you cannot change the whole instance
of that object. But you can change the properties of your object. For example, in here, if you
remember, inside our engine class, we had two setter methods which with the help of
them, we could have changed the value of two properties. For example, if we wanted to change
the RPM, because I said something like engine dot set RPM to let's say 10,000. You can see
that even though we declared our engine as final, we can change its properties, we just
cannot change the whole instance we can change its properties using Final keywords can be
useful from time to time. Okay, I think that's enough for this video. In the next video,
we are going to have a quick challenge to make sure that we have understand the concepts
of object oriented programming. See you in the next video. This is our challenge. Imagine
that you are a doctor and you want to check on your patient. In this application. First
of all, you're showing the name and age of the user to the doctor. And after that you
can see a list of different organs in which A doctor can select the first organ, his left
eye. Let's select that. In here, you can see that we are seeing some details about the
organ, for example, the name, the medical condition, and also the color of the oil.
After that, we gave the doctor the option to close the oil. And also if the eye is closed,
we are going to open that let's select one in here to close the eyes, you can see that
left eye closed. Once again, we are seeing the least of our organs. Let's select two
in this case. This time, if I don't want to close or open the eye, I can put any number
beside one for example, if I put two in here, you can see that once again, we are seeing
the list of different organs. Let's select three to see the heart. Once again, you can
see that we are seeing some details about the heart. After that we have the ability
to change the heart rate. In a normal doctor patient situation, this wouldn't be the available
option. But in here, we just want to demonstrate some behavior for our heart organ. So if we
select Vani, here, the scanner is asking for our new heart rate, which I can put 75. And
you can see that heart rate change to 75.
Let's select a stomach. Once again, you can see the details of the stomach, we have one
behavior in here, which is digest. If we select that, we can see that digesting has begun.
Once again, this is a hypothetical situation. And in a normal situation, you cannot order
the patient to digest the food. Let's also select five in here to see the scheme. This
time for the skin, you can see that we are not seeing any behavior instead, you're just
seeing some details. After that, once again, we have our list, in which if we put six or
for that matter any other number, we will close the application. Let's put six in here.
And as you can see application has been exited successfully. by solving this challenge, I
want you to practice the object oriented programming concepts that we have talked about in the
past three videos. So make sure to use them. Okay, pause the video in here and go solve
the challenge. After that, come back to the video. And let's see my solution for the challenge
as well. Here is my solution for the challenge. First of all, I am going to create a package
inside my source folder. Let's name it or dot maker dot o p challenge. Inside this package,
I am going to create my organ class. Inside this organ class, I'm going to have two fields,
let's say private string, name and also private string medical condition. I'm going to keep
it simple. So I think these two would be fine. After that, let's create the constructor.
And after that, let's create all of the getters and setters. I'm not sure that we are going
to use all of these getters and setters. later on. If we didn't use them, we just simply
delete them. Beside these, I'm going to have another method inside this class. Named get
details. Let's see that as well. Let's say public void, get details. And let's print
some details about this Oregon. First of all, let's say name. Plus, this stuff gets named.
After that, let's print a medical condition of this organ as well. medical condition plus
these that get medical condition. That's all we need inside this organ class. Let's quickly
create our organs one by one. First of all, I'm going to create an organ called I. Let's
say I inside this iclass I'm going to have a string called color. And I after that let's
have a Boolean indicating if the eye is open or not. So let's say private Boolean is opened.
After that, it's time to extend the Oregon class, let's say extends Oregon. In here we
are using inheritance. Now it's the time to create our constructor. Let's select both
of these two fields. And after that, let's create all of the getters and setters. Once
again, I'm not sure that we are going to use all of these getters and setters. later on.
If we didn't use them, we just simply delete them. Okay, let's override the get details
method. I can do that by pressing Ctrl plus or by selecting get details method. Now we
can change Use this method. For example, in this case, let's also print the color of the
eye. Let's say color. Plus this dot get color. Inside this iclass, I'm going to have two
other methods as well, let's see them, let's say public void, open. Inside this open method,
I'm going to change the value of this Boolean to true. So let's say this dot set opened.
And let's fast through. After that, let's bring something indicating the AI has been
opened. Let's say this that gets name, you can see that we don't have any getname method.
Inside this class, we are using the getname from the parent class, which is Oregon, let's
say plus opened. Let's have another method called close. I'm going to save public void,
close. Once again, first of all, I'm going to change the value of the Boolean to false.
Let's say this dot set opened, and let's pass false. After that, let's print something.
Let's say this dot get name,
plus close. That's all we need inside this iclass. Let's quickly create another class
for the heart. Let's say heart. For this heart class, I'm going to add another field called
rate, let's say private int rate. It's time to extend the Oregon class extends Oregon.
And after that, we need to create our constructor. After that, we need to create our getters
and setters. I'm pressing ALT plus insert to get that dialog. In case that's confusing
for you. After that, it's time to override the get details method by pressing Ctrl plus
Oh, and by selecting get details method, we have that in here, I'm just going to print
the heart rate, let's say heart rate. Plus this dot get heart rate, or get rate. That's
all we need to do inside this heart class. Let's quickly create a stomach class. Let's
say stomache. Inside this Islamic class, I'm going to have a Boolean that's a private Boolean,
these empty indicating if the Islamic is empty or not. After that, it's time to extend the
Oregon class extends Oregon. And after that, we need to create our constructor. Let's create
our getters and setters. And after that, let's override the get details method. Inside this
get details method. First of all, I'm going to check that if the stomach is empty or not.
So let's say if this.is empty, I'm going to use the method in here. If the stomach is
empty, I'm going to print something I'm going to say need to be fat. But in the else case,
let's just print the stomach is full. Let's also create another method for this class
called digest. Let's say public void digest. Let's just print something let's say digesting
begin. Okay, we are done with our stomach class. Let's quickly create the skin class
as well. For this Scan Class, I'm going to define two more fields. First of all, I'm
going to define a string for the color of the skin. After that, I'm going to define
an integer for the softness of their skin, let's say private int softness. This integer
is going to be some number from zero to 100, indicating the softness of the skin. After
that it's time to extend the organic class. After that, we need to create our constructor.
Next, we need to create our getters and setters. And after that, let's override the get details
method. Inside this method, let's also print a skin color. Let's say skin color. Plus this
dot get color. That's all of our organs. Now we can create our patient class. Inside my
package. Let's create a patient class. For this patient, first of all, I'm going to have
a string for its name. After that, we are going to have an integer for the age of the
patient. After that we can create our organs. For example, I'm going To save private eye,
let's say left eye. Let's do the same for the right eye. Let's also have a heart. After
that may be a stomach and a skin. Okay, that's all of our fields for the patient. Let's create
the constructor.
And let's create all of the getters and setters. We don't need any special method inside this
patient class, inside our main class, which we need to create, we can instantiate it.
Let's create our main class. First of all in here, we are going to create our main method.
After that, we can create our patient object. But before that, let's minimize this project
main. Let's say patient, let's name the patient is equal to new patient. First of all, we
need a naming here, let's say Brad. After that, we need the age, let's say 28. After
that, we need the left eye which I can say new I. For this, I first of all, we need a
name, let's say left eye. After that, we need the medical condition, which I'm going to
say short sighted. For the color, let's say blue. For the is open Boolean, let's say true,
we need the same thing for the right I knew I let's just change the name to right i. Also,
let's change the medical condition to normal. After that, we need the heart, let's say new
heart. For the medical condition, let's say normal. And for the rate of the heart, let's
just say 65. I think the next organ was a stomach. So let's say new stomach, the main
would be Islamic. The medical condition, I'm just going to enter a name of some medical
condition, I'm going to say p UD public services in case if you're wondering. And for the ease
empty, let's say false. After that, we need a skin let's say new skin for the color of
their skin. Or First of all, we need the name, let's say a skin for the medical condition,
let's say burnt. After that, what do we need, we need the color and also softness for the
color, let's say right? For the softness, let's say 40 out of 100. Okay, that's our
patient object. Now we need to create the logic for our application. First of all, I'm
going to print the name and age of the patient. So let's say print name. Plus patients that
gets me after that do the same thing for the age. We are going to receive the user's input.
So we need a scanner. Let's say a scanner scanner is equal to new scanner. Let's fast
system that. Also, now that we know about object oriented programming, we know how this
scanner is working. This is just the class with this new keyword we are instantiating
it inside the constructor of this scanner, we need this system.in whatever that is. So
this way we have created an instance of this scanner. After that I'm going to define a
Boolean, let's say Boolean. Let's name it short finish. And initially I'm going to set
it to false. Later on, we will see that how it This one is going to be useful. After that
I'm going to create a while loop let's say while should finish is not equal to true,
continue looping. Inside the while loop, I'm going to show a list of different organs.
For that I can say choose an organ. In here I have two options. I can use this system
dot out dot print lines line after line. Or the better option is to use backslash n. Let's
see that as well. First of all, let's go to the next line in order to have a cleaner code.
In here I can say backslash n, this backslash n moves the cursor to the next line. The other
one that I'm going to use in here is called backslash t. This backslash T will add a tab
to our text. Later on we will see exactly what these two are doing. Okay, the first
organ is left eye
The next one is right I After showing this list to the user, I'm going
to receive the user's input. So I'm going to say int choice is equal to a scanner dot
next int. We have seen this in previous videos. So I'm not going to explain in here, you're
just going to receive the user's input. After that, depending on this choice, I'm going
to create a switch statement, let's say switch on choice. And let's create our different
cases. In case the user's choice is one, first of all, we are going to print the details
about the left eye. So let's say patient that gets left I don't get details. Next, I'm going
to give the user the option to open or close the eye. For that, I need to check that if
the eye is opened or closed. So let's say if patient that get left eye that is opened,
we need to close it, or at least we need to give the option to close by, let's bring something
in here. Let's say backslash, D, backslash D in order to have two tabs. After that, let's
say close or close the next we need to get the user's input in case if it's one, we are
going to close the eyes. So let's say if a scanner dot next int is equal to one, then
we need to say patient dot get left eye dot, close the eyes or close. But in the else case,
we are just going to continue to the next record. In the else case of the first if statement,
this finding here in case the eye is not opened, we are going to show the option to open the
eyes. So let's say else, we need the same logic, we can print something backslash t
backslash t Vaughn opened it. Next, we need to get the user's input. Let's say if scanner
dot next int is equal to one, we are going to save patients that get left eye dot open.
In the else case, we are just going to continue. By continue I mean, once again, we are going
to show the list of different organs. Okay, that's our first case. We also need a break
in here. But let's write the second case. The second case is the right eye. We basically
can copy and paste all of these logic. But we just need to change the left eye to right.
So for example, in here, let's say get right. Okay, we are done with our second case. Let's
also write the case for the heart. Let's say case three. First of all, let's show the details
of the heart, let's say patient that get heart dot get details. After that, we are going
to give the user to change the heart rate. for that. Let's say backslash t backslash
T one, change the heart rate. Once again, we need to listen for the user's input. So
let's say if the scanner dot next int is equal to one, we need to ask for the new heart rate.
So let's say enter a new heart rate. After that, we need to save that heart rate. So
let's say int is equal to a scanner dot next. After that, we can say patients that get heart
dot, let's say set rate. And let's pass our new heart rate. But in the else case, like
before, we are going to continue. We also need a break in here. Let's quickly add that
and also after changing the heart rate, it's better to show some message. Let's say heart
rate change to plus patients that get heart dot get rate. Okay, that's our third case.
The fourth one was for the stomach. So let's say case four. First of all, we need to print
the details of the stomach patients that get the stomach that get details. For the stomach,
we had the digest option. So in here, let's show that option. backslash D, backslash D.
Let's see digest. Let's see if the scanner dot next int is equal to one.
If that's the case, let's say patient dot get stomach dot digest. But in the else case,
let's just continue. We also need a break in here. Let's quickly add that the fifth
case is for the scheme. Let's say case five For the scheme, we didn't have any option,
we just need to print the details, let's say patient dot get scheme, dot get details. Of
course after that we need to add a break. In here, I'm going to add a default case.
And if the user enters any other number than these five, we are going to create the application.
For that we are going to change the value of this short finish Boolean to true. After
that, we need a break. Okay, I think our application is ready to test let's see how did we do.
In here, we can see the name and age of the patient which is good, we can also see the
list of our organs. Once again, it's good. Let's select left eye, we can see the details
of the left eye. And also we have this close the eye option. Let's put another number beside
this one, let's say to once again, we can see the list of our organs. Let's see if we
can close the left eye. Let's put one in here. And as you can see, the left eye has been
closed. Okay, let's check the right eye. It's working the same. Let's enter two in here
in order to show the list of organs. And let's select our heart, we can see the hearts details,
which is good. Let's see if we can change the heart rate. Let's select one in here,
the console is waiting for the new heart rates, let's say 75. And as you can see, heart rate
has been changed to 75. Let's check it once again, in order to make sure of that, let's
say three. And as you can see, the heart rate is 75. Let's go to in order to show the list
of organs. And let's select four in order to see their stomach, we can see all of the
details of the stomach. Let's digest some food. And as you can see digesting has begun.
Let's check the skin which is five, you can see that we are getting the details of the
skin. But we don't have any option. That seems to be good. And if you enter any other number,
then these five we should create the application. Let's enter seven in here, for example. And
as you can see application has been finished successfully. Okay, it seems like our application
is working fine. Let's just delete the getter and setter methods that we said that we will
delete if we didn't use them. Let's check the classes one by one. First of all inside
the iclass. Everything that is grayed out It means that we never used that, for example,
we never used this set color method. Okay, it seems to be good. Inside the heart class.
I think we have used all of them. Okay, that's fine. Inside the organ class, we didn't need
this set medical condition method. And also we didn't need this setname method inside
the patient class. Let's see what do we have in there? And I think we didn't use any of
the setters of this patient class. Let's just delete them. Okay, that seems to be good.
Let's see, what do you have inside a scheme class. And in here, we never use these setters
and this get softness method. Let's delete them inside the stomach class. Let's delete
this set empty method. And in order to make sure of everything, let's just run our application
one more time. Okay, it seems like we don't have any compile time error, which seems to
be fine. Okay, that was our challenge about object oriented programming concepts. I will
upload the source code at the links that you can see on the screen, feel free to check
them and also, I'm more than happy to see your feedback about the code. In the next
video, we will talk about collections in Java. See you in the next video. In previous videos,
we have seen simple arrays in Java. For example, if we wanted to store a list of different
names, we could have said something like this because I've set a string with a pair of a
square bracket. After that, we could have named our array. And after that we could have
initialized our string array like this inside a square bracket in here, we needed to pass
the size of our array for example five. The other way was to pass our elements at the
time of instantiating our array for example, we could have said a pair of curly braces
and inside those curly braces because I've passed our elements. Let's pass few names
in here.
After that, if we wanted to have access to these elements, we could have say something
like this, we could have said print, let's say names with an index of let's say two for
the third element, which is Brad in this case, if you're on our application right now we
should see Brad. Let's run it. You can see that Brad has been printed but if you remember
I said that using these kinds of simple arrays in Java has a lot of limitations. For example,
the first limitation is that their size is immutable, it means that their size cannot
be changed. Right now we have five elements inside our names array. If we want to add
another item inside this array, we cannot do that. For example, we cannot say something
like names with an index of five, which indicates the sixth element, we cannot assign a value
to that, you can see that in here we have a red warning. And if we try to run our application,
we get an exception. Let's see the exception. The exception in here is array index out of
bounds exception. Also, when working with simple arrays in Java, you don't have that
much options. Later on, we will see that when we use different kinds of collections, we
can operate all sorts of operations on our collections in which we do not have them in
simple areas. If we want to add a sixth element to our array, we can create a new array, and
after that, we can copy all of the elements from inside this array. Right now for the
situation that we have in here. If we want to add another item to our array, we can create
another array with the size of six. After that, we can copy all of these elements to
that array. And after everything, we can add this new item. For example, in here, I can
say a string array. Let's name it new names, is equal to new string array with the size
of six. After that I can create a for loop let's say for int i is equal to zero i less
than names dot length, and i plus plus. And inside that for loop, first of all, I need
to add all of the names to this new names array, I can say new names with an index of
i is equal to names with an index of oil. This way, we can copy all of the elements
from inside this names array to this new names array. After copying all of the elements.
After the for loop, I can say new names with an index of five is equal to Jerry. This way
we can get around this problem. For example, if I comment this line and run my application,
maybe after that, print the six element inside our new names, we have a solution. But as
you can see, this solution is not that much effective. Let's quickly print this. You can
see Jerry has been printed in here. But as you can see, there are a lot of codes involved.
And beside that if your array has a lot of elements, for example, a million elements,
this process can be really time consuming. Besides that it can be resource consuming
if you are downloading this array from the internet. So this solution in here is not
effective. Instead of copy and pasting the whole array, we are going to use collections.
Let's delete all of these codes. In Java, we have multiple kinds of collections. The
first of which I'm going to talk about in here is called ArrayList. Let's begin to see
that so we type array list with capsule a. After that inside a pair of anchor bracket,
or as some might call them diamonds, we need to define the type of our ArrayList object
for example, I can say is string. After that we need to name our ArrayList. For example,
I can say names, is equal to new ArrayList. As simple as that we have an ArrayList This
is one way of defining your ArrayList. There is also another way, let's quickly see that
so instead of saying ArrayList string, we can say list, you can see that the icon in
here is different. It's an interface. We will talk about interfaces later on in the course.
But for now just know that there are two ways of defining your ArrayList. Let's see how
can we use this list to create an ArrayList we can say list of strings for example. Once
again, let's name our list. For example, let's see students is equal to new ArrayList. There
is a slight difference in here. But I just wanted to show you both ways of defining your
ArrayList. Okay, let's delete this line for now. Right now our ArrayList is empty. And
if we want to add some elements to this ArrayList, we can simply say names.ad,
we can use this add method in order to add elements into our ArrayList. For example,
let's add one. You can use this add method as many times as you want. If I want to add
another element, I can simply do that. So the first benefit of using array list is clear.
Now the size of array list is mutable, it means that we can change the size of array
list. After adding some elements to your array list. You can get those elements by using
the get method. Let's quickly say that, for example, let's print the first element I can
see names that get you can see that this get method requires an index. Once again, indexes
in Java start from zero so if I pass zero in here, it means that I'm going to get the
first element. Right now if I run my application, I should see my Sam printed into the console,
you can see that Mason has been printed. Also when using array lists, you can also get the
size of your array list. For example, if I want to print the size of my ArrayList, I
can say something like this, I can say names dot size. Let's run the application and see
what would be the result, you can see that the size of our ArrayList is two, you have
all sorts of options when using array lists, you can also clear all of the elements inside
your ArrayList. For example, I can say names dot clear. This clear method removes all of
the elements inside your ArrayList. After clearing, if I once again get the size of
my ArrayList this time the size should be zero. And as you can see down in here, it's
zero. If you want, you can also remove one element from your ArrayList. Let's quickly
see that I can say names dot remove this remove method requires an object, it means that we
need to pass the whole element. So let's say may sound for example. But before that, let
me quickly comment this line because we don't want to clear our array list. And after removing
my son, let's print the first element inside our array list. Let's say names that get and
let's pass zero as the index. As you can see, the first element right now is Sarah, it means
that my Sam has been removed successfully. Let's see what other options do we have on
our ArrayList. Let's like names by typing dots, we can see the list of options, we can
also check that if an option exists in our ArrayList. For example, if I use this contains
method, once again, it requires an object, let's say Mesa, let's bring this whole line
of code. This contains method returns a Boolean. So right now if I run my application, because
we have removed my sample we should see false, we can see that that's good. But if we change
this to Sarah, we should see through. And through this, you can also check that if your
ArrayList is empty or not. For that you can print something for example, you can say names.is
empty, we have this method once again, it's returning a Boolean. Right now we have zero
in our ArrayList. So it shouldn't be empty. And as you can see we are receiving false.
But if we remove Sara before this line of code we should see through, let's quickly
do that names dot remove. And let's pass this time you can see that through has been printed,
you can also get the index of some element from inside your ArrayList as well. Let's
quickly see that before everything I'm going to comment all of these lines of code because
I want to have a clear console.
down in here. Let's say print names that index of and in here I can pass my object for example,
I can pass Mesa. Right now Mason is the first element so we should see zero printed, you
can see that it's zero. But if we don't have that element inside our ArrayList. For example,
if we pass Brad in here, we should see negative one. And as you can see negative one has been
printed. When you're using ArrayList you can only have objects as the type of elements
inside your ArrayList. For example, in here, I cannot say integer, you cannot use primitive
data types when you are creating a new ArrayList. As you can see in the error in here, the error
says that type argument cannot be primitive type. If you want to have an ArrayList of
different numbers, you can use the equivalence Java class. For example, in here I can say
integer with a capital I. And now I have an ArrayList of integer. Right now I am getting
an error because I'm trying to add some strings to this integer array list. But you get the
idea of we can use this integer class in order to have an ArrayList. So in Java equivalent
to every primitive data type, you have a class as well, we have seen this integer. Let's
also check others, we can say Boolean similar to that we can say long, we can also say double.
And you can check the others yourself. But let's change this one to a string right now.
The other option that you have when you're using ArrayList is sorting different items.
For example, if you want to sort different elements inside your array list alphabetically,
you have that option. And the way to do that is like this. Let's see that down in here
we can see names that sort you can see that this sort method requires some comparateur
later on in the course we will see this comparateur In fact, we will use this sort method in order
to source different elements in our array list. But for now, I'm not going to talk about
this I just wanted to say that you have this option. Okay, let's delete this and let's
move on from this part. Similar to simple arrays in Java. If you want to eat
To read through all of the elements in your array list, you can use for loops. Let's quickly
see that as well. You can say for int i is equal to zero, i less than named that size,
this time, i plus plus. Inside this for loop, we can say, print names that get with the
index of AI. This way, we can print all of the elements in our array list. But before
that, let's comment this line. And let's run our application, you can see that we can see
both of our elements printed. Okay, that's enough talking about array lists. Let's also
talk about another kind of collection in Java called maps. maps are useful for when you
have some key value pair data. Let's define a map and we will talk about what I mean by
key value pairs. You can define a map like this, you can say map you can see that map
is an interface. We will talk about interfaces later on in the course I believe in the next
video. But for now, let's move on. You can also see that inside these anchor brackets
inside these diamonds, we have these K and V these two stands for key and values. So
in here, if we say anchor bracket, for the first element, we need to pass the kind of
key the data type of our keys. For example, I can say string. And after that, we need
to provide the data type of our values. For example, I can say a string once again, for
the data type of my values. Right now I'm getting a red warning. And that's because
it seems like the ID hasn't imported maps into my class, I can press ALT plus Enter.
And if we take a look at above, in here, it seems like we have imported maps into our
class successfully. After defining the data type of your keys and values inside your map,
you need to name your map. For example, I can say contacts. In here, I can say new maps.
But if I do that, you will see that there are a lot of methods that I need to be worried
about, there is a better way of defining your maps, let's press Ctrl Z in order to undo
the change, I can say new hashmap. This time, you can see that this hash map is a class.
And if you want we can instantiate our map this way, this time, we do not have all of
those extra methods. I don't intend to talk about hash maps in here, the topic is a bit
technical, it's about the way that we are going to iterate through our maps elements.
It's way above the talk that we have in here, I just wanted to say that you can instantiate
your maps as different hash maps. Okay, now you have an empty map, which the data type
of different keys is a string. And the data type of different values is a string as well.
If you want to put some elements to this map, you can say something like this, you can say
contacts that put this is the method that we are going to use in order to add some elements
to our map, you can see that right now this port method is waiting for a key and also
a value for the key. For example, I can pass a name, let's say me, Sam, and for the value,
let's pass his email, let's say me sam@maker.org. This way we can add elements to our map, I
think if I change the name of my contacts map to let's say email list, that would be
much more better. In order to change the name of some variable or class in Java, you have
an option, you can select the whole name, you can right click on the name of your variable,
you can go to this refactor in here, and you can use this rename or alternatively, you
can use the shortcut, which is shift plus f6. Let's select that. And in here, I'm going
to delete this whole name, you can see that once I'm deleting this, it's deleting the
name in both places. So let's say email list. And let's press enter. I think this name is
much more suitable. Let's add another element to our map. Let's say email list dot put,
let's say Brad. And let's say brad@gmail.com. Now that I have some elements in my map, if
I want to have access to those elements, I can say something like this. For example,
let's print this statement. Let's say email list dot. And once again, you can see that
we have this get method which requires an object for the object in here we need to pass
the key. For example, if I pass Mesa, as the result, I should see the value of Mesa. In
this case, I should see the email. Let's run the application. And let's see if we can print
the email of maysa. You can see that the email has been printed successfully, like array
lists. When you are using maps, you have all sorts of options. For example, if you want
to get the size of your map, you can say something like this you can say email list dot size.
Let's run the application and let's see if we can get to two has been printed. If you
want to remove an item from your map you can say email list that remove once again this
remove required As an object, in this case, we need to pass the key. Let's pass Brad.
And let's print the size of our map once again. This time after removing Brad, you can see
that the size is one. You can also check that if some key exists in your map or not. The
way to do that is like this. Let's bring that first of all, we can say email lists that
contain keys. Similarly, we have contains value, which you can guess it will check that
if some value exists in your map. Let's try the first one contains key. And let's pass
Brad in here, because we have removed breath, we should see faults. And as you can see,
false has been printed. But if I change this to Mesa, we should see through. Okay, that
seems to work fine. We can do the same thing for the values.
Let's say email list dot contains value. Let's pass the May Sam's email may sam@miko.org.
Because it does exist in our map we should see through and through it is you can check
other options. yourself. If you want. We can say email list dot once again, you can see
that we have this clear option, it will clear all of the elements of your map you have this
is empty method, it will check that if your map is empty or not. The other option that
you have in here is this values, you can see that this values method will return the collection.
If you want to copy all of the values of your map, you can use these values and you can
save it inside a collection. But I'm not going to do that in here. But to just show you I'm
going to say that there is an interface in Java called collection this morning here in
which is the generic type of all of the collections available in Java. But more on that later
on when we source different items inside our ArrayList like ArrayList, you cannot have
a primitive data type as the data type of your key or your values when you are using
maps. For example, in here, I cannot say int if you take a look at the warning, it says
that type argument cannot be of primitive data type. Similarly for the values, let's
say int, once again, you can use the equivalent Java class for these primitive data types.
But you cannot just use primitive data types. Okay, let's roll back everything. Besides
maps and array lists, you have other kinds of collections in Java as well, I don't want
to talk about them in here, because I think that for the Android course, these two are
enough and you don't need to know about others. But if you're curious, you can always check
this Oracle webpage, you can see that we have sets, lists and other kinds of collections.
But honestly, these are not going to be helpful for our Android course maps. And array lists
are just fine. As the data type of key values inside maps, and also values inside the ArrayList.
You can also have different classes. Let's quickly see that as well. In my source folder
inside the package, I'm going to create a new class called student. Let's quickly see
that inside this student class, I'm going to define two new fields, let's say private
string name, and private integer
ID.
Let's have a constructor and some getters and setters. Now that I have created this
class, I can pass it as the data type of my ArrayList. Let's quickly create another array
list down in here. I can say ArrayList of different students This time, let's name this
ArrayList students, let's say is equal to new ArrayList. Now you can put as many students
as you want inside this ArrayList. For example, let's say students that add let's say a new
student, let's say may sound with the idea of 15, maybe. So beside the strings, other
kind of customized datatype is agreeable with the collections in Java. Okay, just before
I finish off this video I'm going to talk about for each loops in here. If you remember
when we talked about loops in Java, I said that there is another kind of loops called
for each loop, I said that we are going to talk about for each loops when we know about
object oriented programming and also collections in Java. So I think now it's a good time to
talk about that you can create a for each loop like this, you can say for instead of
saying int i is equal to zero, I'm going to pass the datatype of the list that I'm going
to iterate over. For example, in this case, I'm going to say a student. After net I need
a name. For example, I'm going to say s and after the name, I need a column. After that
I need the list or ArrayList that I'm going to iterate. For example, in this case, I'm
going to say students, let's review everything once again. In here. First of all we have
passed the data type of the object that we are going to look into the name of that specific
object is as you can name it, whatever Do you want after the name, you should have a
colon. And after that, you need to pass the collection that you're going to look into
inside this for loop, you can use this s in order to have some sort of operation on your
object. For example, if I want to print the name of my students, I can say, print s, that
gets me right now our ArrayList has only one student, let's quickly add another, let's
say students that add a new student. Let's say Sarah, with the idea of maybe 18. Let's
run the application and see what would be the result. As you can see down in here, Mason,
and Sarah has been preempted. So this is in here is like I in a for loop. But instead
of being the index of the item, it's the item itself from time to times for each loops can
be useful and this is how you can create them. Okay, I think that's a good point to stop
the video in here. In the next video we are going to talk about some small concepts. For
example, we will talk about this static keyword that we have seen in the declaration of our
main method. Besides that, if we had time, we will talk about inner classes, we will
talk about interfaces and abstract classes See you in the next video. In this video,
we are going to talk about some small concepts that we need to know about. The first of those
concepts is this static keyword that we have seen so far in the declaration of our main
method. I'm going to talk about that in another class. So let's create another class in my
package. Let's name this class test class. You have seen inside the main class that we
can have this static keyword for our methods. But besides that, we can also declare our
variables as a static as well. For example, in here, I can say public, let's say static.
After that I can say a string. Let's say name, you can see that it's working fine as well.
I'm not sure if you have noticed or not. But as soon as I declare this a string as a static,
the style of this name has changed to italic. If I remove that you can see the difference.
But what does it mean to have a static variable or a static method? Well, when you define
your variables and methods as static, those methods and variables belong to the object
itself and not to the instances of that object, we will see what I'm talking about. But before
that, let's quickly have other variables in here. For example, I can say public
int, age, maybe another one public, let's say a string, a skin color. These variable
doesn't mean anything, I just want to have some variables. Let's also create a constructor
in here. Notice that when I use the intelligent generator to create my constructor, I do not
see my static variable in this list. Once again, that's because the static variables
do not belong to the instances of the object ID belongs to the object itself. Let's select
these two. And let's create getters and setters. But as you can see, we do have a getter and
setter for this static variable. Okay, now that we have this class, if we want to create
an instance of that class, for example, from inside our main class, if you say test class,
let's name it test class is equal to new test class. The constructor in here is waiting
for an age and also a skin color, let's say 25. Right? This way, we can create our instance
of the class but how do we assign a value to the name variable farming site artists
class? Well, you can use the setter methods. For example, you can say test class dot set
name, this method and you can pass a name for example, Mason, but there is a warning
in here this highlighted warning. What does that mean is that you can set a value for
this name variable even without instantiating your class. For example, in here, I can say
test class with a capital T dot, let's say name. Or alternatively, because the setter
method for this name is static as well, I can say set name. Let's use the first one,
I can say test class dot name is equal to for example, Mesa, we can see that even without
instantiating this object, we can assign a value to this variable. In order to make sure
of that let's move these two line of code to after assigning a value to the name variable.
I can also delete this setname method safely. Now, if I want to have access to the name
variable of this test class, I can say something like this. I can say name plus test class
this time with flirty. That let's say getname. Let's run the application and let's see what
would be the result. You can see that message has been printed even though we have set the
value of this name variable before instantiating our object so whenever you use a static variables
that variable will be the same in all instances of your class. For example, if I have another
instance of this test class, for example, if I say test class,
second test class is equal to new test class, maybe another age and another skin color,
let's say 30. Black. If I print the name variable of the second test class, the result would
be the same. Let's say second test class dot get name, or for that matter, we can say name.
Let's run our application, you can see that my Sam has been printed. Besides variables,
methods can be as static as well, let's quickly define a static method inside our test class,
let's say public static void. Let's name it print something. And inside this method, let's
just print something. This static method like a static variable belong to the object itself
and not to the instances of this object. For example, in here, once again, I can say test
class with a capital T dot print something, you can see that we do not need to instantiate
our object. If you're on our application, you can see that something is printing. Okay,
now we know that static variables and methods belong to the object itself and not to the
instances of that object. But why would we want to use a static variables and methods
there are a few reasons for that. First of all, you may want to have a constant variable
in all instances of your class. For example, imagine that you are creating a class for
simulating different employees of a company inside that class you may have some information
about the employee, for example, the age, the name, email, and everything. But besides
all of those, you may have a field called company name, you know that in all instances
of your employee class, the company name should be the same for that matter, you may want
to declare your company name field as a static also, some say that using a static variables
and methods is very memory friendly, because there can be only one instance of that variable
or method in all of your application. So no matter if you have 1000 employees in your
application, the company name field in all of those instances will occupy only some spaces
as a string can so using a static variables and methods is very memory friendly. But there
is a downside when you're working with this static keyword. Let's quickly see that inside
our test class as well. Let's quickly create another method in here, let's say public void.
Let's name it print, notice that this print method is not a static and because this is
not a static now, we cannot have access to that method from inside this print something
method for example, in here I cannot say print, you can see we have a red warning in here.
It says that non static method print cannot be referenced from a static context similar
to non static methods, you cannot have access to non static fields inside the static method
as well. For example, in here if I try to print the age of this test class, you can
see that we are getting a red warning in here. Once again, the warning says that non static
field cannot be referenced from a static context. So you cannot use non static fields and methods
inside a static method. Okay, I think that's enough talking about this static keyword.
Let's quickly delete this class. Also, we need to delete all of these lines of code.
Okay, now we know everything about this main method. First of all, its access modifier
is public. It's a static method, its return type is void, its name is main. And also as
the input of this method, we are receiving a string array called arguments. Because this
main method is a static, we know that there can only be one instance of this main method
in our entire application. We are not concerned about the voice in here, but we know that
in order to run Java applications, we need a main method, which is a static with this
specific syntax. Okay, that's enough. Let's move on. The next topic that I'm going to
talk about in here is inner class. Let's quickly see that in another class called test class.
Inside our classes, besides variables and methods, we can also have another class and
here is how we can define that class. We can say private, let's say class, I'm going to
name this class test inner class. And here we have this inner class like before, inside
this in our class, we can have other methods and variables. For example, I can say private
string name in here. I can have a constructor for this class as well. Let's quickly see
that you can see we can create the constructor we can create all sorts of getters and setters.
And we can treat this class as any other class in Java. But there are a few differences between
a class and an inner class. For example, if we have some fields in our parent class, that's
Let's define some, let's say private int, age, and private string color. No matter what
the access modifier of these fields are, we can have access to these inside our tests
in our class. But we need to do that inside another method, let's say private, void, print
the page. Inside this method, I can say print, let's say age plus age, you can see that it's
working fine. Even though the access modifier of this age is private, we can have access
to that from inside our inner class. The same is for different methods of your parents.
Plus, no matter what the access modifier of those methods are, you can have access to
them from inside your inner class, there are some times that you may want to use inner
classes for example, when you use inner classes, your code might be much more easier for us
to follow. The other reason for using inner classes is that you can have this private
access modifier at the declaration of your class if you remember, we cannot change this
public access modifier when we create our parent class for example, in here I cannot
say private you can see an error in here and if you hover over that you can see that modifier
privates not allowed in here beside private access modifier You can also have a static
classes. That is another benefit of using inner classes you didn't have this option
when you created your parent class let's quickly see that if I say static in here once again
modifier static not allowed in here. So within our classes, you can have private and static
classes in this course, we are not going to use inner classes that much. In fact, the
first time that we are going to use it is when we know about async tasks. In most cases,
we are going to define our async tasks as inner classes. We will talk about that later
on in the course. Okay, I think that's a good point to stop the video in here. I did want
to talk about interfaces and abstract classes in this video. But I think that's better to
have that discussion in the next video. So see you in the next video.
As I said at the end of the previous video, In this video, we are going to talk about
interfaces in Java interfaces are like a contract between two parts of your application. Let's
see what I mean by that you can create an interface like you have created a Java class,
you can right click on your package by selecting new Java class. And then in here, by selecting
interface, you can have an interface Let's name this interface card interface. If you
take a look at your package, you can see that we have created this interface. And if you
take a closer look, you can see that the icon for this interface is somehow different from
the icon of classes in Java. This is the general schematic of an interface in Java, like classes,
interfaces, lives inside the package, they have this public access modifier, we can name
our interfaces, whatever we want. And also they have this interface keyword in their
declaration. Inside interfaces, we can define our abstract methods, let's quickly see an
abstract method. I can say public, let's say void. After that, let's say print name. As
the input of this method, let's just receive a name, let's say a string name. And that's
it, you cannot have a body for your abstract methods. For example, in here, if I put a
pair of curly braces, you can see that I'm getting a red warning. And the warning says
that interface abstract methods cannot have body. So I can delete these two curly braces
and I can finish my sentence in here. By using abstract methods you are hiding the functionality
of your methods. For example, in this case, we don't know what this print name method
does. Instead, we are just defining some sort of contract. The contract in here is the signature
of our method, we know that the name of this method is print name, we know that we are
receiving a string called name. And also we know that the return type of this method is
void. The signature of this method is a contract in which ferrovie implements this interface,
we are going to use this signature. Also if you take a look at this public keyword, you
can see that it's somehow grayed out. If you hover over that keyword, you can see that
modifier public is redundant for interface methods, it means that it's not necessary
in here. And the reason for that is because all of the abstract methods are public inside
an interface. It means that you cannot have private or protected abstract methods. Inside
your interface. You can have as many abstract methods as you want. For example, if I want
to define another one in here, I can say void. Let's name this one to start. And I'm not
going to receive anything as the input of this method. Let's define another one. Let's
name the spawn move. And also let's receive an integer call. Speed, I created the first
method so that I can show you how you can create abstract methods. And I'm going to
delete that in here. So this car interface has two methods start and move. But how can
we use this interface? Let's quickly see that as well. Inside my package, I'm going to create
a Java class called electric car. And inside this electric car, I'm going to implement
that interface that I just created. In order to implement that interface, I am going to
say implements at the declaration of my class. And after that, I need to provide the name
of my interface. In this case, it's called interface. But once we do that, you can see
that I'm getting a red warning in here. And for this red warning, we need to implement
the two methods that we have created inside our interface. In order to implement those
two methods, we have multiple options. First of all, we can click on this red light bar.
By selecting implement methods, we can implement the necessary methods. Or alternatively, if
you don't see this red light, well, you can click on the error and by pressing ALT plus
enter, you can see the same dialog or the other option is to come inside the declaration
of your class. And by pressing Ctrl plus i, you can see the list of necessary methods.
In this case, we have a start and move methods. Also, if you press Ctrl plus or you can see
the list of all of the methods that you can override. In here, you need to be a little
bit careful because all of these methods are none necessary. The methods that are necessary
are these two methods. If you take a closer look at the icon of these two methods, you
can see that the icon is somehow not completed. These are the two methods that we need to
implement or override. Let's select them. And in here we have a body for this start
and move method, it means that we can define the functionality for these two methods in
here. For example, in the start case, I'm just going to print something. Because it's
an electrical car, I'm going to say electricity flow started. Let's also define a field for
this class. I'm going to save private string name. We can also have a constructor by pressing
ALT plus insert, let's create that constructor. And let's also create getters and setters.
down in here, let's also define some functionality for this move method. Once again, I'm going
to print something, let's say this that getname plus moves at some speed.
Okay, now that we have created this class in which implements our interface, how can
we instantiate our interface for that we can switch to our main class. And inside this
main class, we can say car interface. Let's name it car interface is equal to in here
I have two options. First of all, I can say new car interface like this for Java classes.
But if I do that, you can see that we are overriding these two methods. Sometimes this
might be our desired behavior. But in here, I'm not going to use this way. Instead, I'm
going to say is equal to new electric car. This one, let's also pass a name, let's say
Tesla, you can see that we are assigning a class to an interface. But how is it working?
Well, because when we have created our class, we implemented this interface. Now Java compiler
is happy with assigning a class to this interface. Now that we have instantiated this interface,
we can use its methods. For example, I can say car interface dots, let's say start. And
also let's say car interface dot move, we also need to pass a speed, let's pass 60.
Let's run the application, you can see that electricity flow started and Tesla is moving
at 60 miles per hour. You can see that even though inside our interface, we didn't have
any body. But we are printing some functionality. When we call the start method on our interface.
If you want to make sure that this start method is happening inside your class. You can also
print something else in here for example, inside this start method, let's print the
name of this class. I'm going to pass this if you remember I said that this refers to
the current object. In this case, it's referring to this class and right now if I print this,
it will print the address of our class. Let's run the application. You can see the address
of our electric car class. It means that this start method is happening inside our electric
car class and not inside the interface. If you remember when we have talked about collections,
we have defined array lists in two ways. One of those ways was to define our array list
as list for example, because I've said list which once again you can see that is an interface.
For example, the stuff a string, let's name it names is equal to new ArrayList Probably
now you understand this syntax in here, it means that we have this ArrayList class in
which implements this list interface. If you want to make sure of that and see it for yourself,
you can press down the CTRL key on Windows, I believe it's Command key on Mac. And by
clicking on this ArrayList, you can see the documentation for your ArrayList class. Let's
scroll a bit above in here inside the declaration of our ArrayList class, you can see that we
are implementing this list. Don't worry about this in here, it means that we are going to
accept any kind of object but as you can see if you are implementing this Okay, let's move
on from here. Now that we have our interface, we can also have another class in which implements
this interface. Let's quickly create another class, let's say new Java class. Let's name
this phone fossil fuel car. Once again, in here, I'm going to say implements. Let's say
car interface, we need to implement two methods. Let's press Ctrl I, and let's select our two
methods. Once again, let's define some functionalities for these two methods. For example, let's
bring something in here. Let's say explosion in cylinder cause the engine to start. Before
I define some functionality for this move method, let's quickly add a field above in
here like before, I'm going to say private, the string name. Let's create the constructor
and also getters and setters. After that insight, the move method, I'm going to say this car
is moving at some speed. Now that we have created this class, we can instantiate our
interface inside our main class, like this, let's delete this line of code. In here I
can say car interface. Once again, let's name this one fossil car interface.
Like before, I can say new fossil fuel car. Let's pass Mercedes in here, for example.
Once again, you can see that Java compiler is happy with this way of instantiating our
interface because we have implemented this car interface inside both electric car class
and also fossil fuel car class, we can instantiate our car interface in both of these ways. And
after that, I can say fosu car interface dot start. And let's see fossil car interface
dot move. Let's also pass some speed in here and let's run our application. You can see
that both of these methods have been called in here, I have a point by defining two classes
and implement the same interface in both of these classes. My point is that when you create
an interface, that interface is a contract between different parts of your application.
For example, in this case, I'm defining multiple points of cars. No matter what kind the car
is, the contract says that discourse should have a start and also a move method. So probably
by now you can see the usage of interfaces. But there are a lot more two interfaces. For
example, we can use something called callback interfaces. callback interfaces are extremely
useful for when you create event listeners like clique listeners. And also they are useful
for when you want to create some sort of connection between two different threads. We will talk
about threads in the next video, don't worry about that, we will see the use of interfaces
when we want to create a communication between two different threads. Also, it's worth noticing
that you can implement multiple interfaces at the declaration of your class. For example,
let's quickly create another interface in here, I'm going to name this one test interface.
For this, let's just have a method in here. Let's say sorry, for private, the abstract
methods cannot be private, let's say void print name.
And let's receive a name.
After I have created this interface inside, for example, my fossil fuel car, after implementing
the first interface, I can add a comma. And after that I can provide the name of my second
interface. In this case, it's test interface. But once again, you can see that we are getting
the error somewhere inside the declaration of our class, I can press Control plus i and
i can implement my method. So you can have multiple interfaces implemented at the declaration
of your class. Okay, I think that's enough talking about interfaces. Later on in the
course we are going to use interfaces a lot and Dave users will be much more clear. So
hang in there if something is vague. Okay, let's delete all of these extra classes and
extra interfaces. Let's also delete these lines of codes. Now let's talk about abstract
methods. For that I'm going to create a new class inside my package. Let's say new Java
class, I'm going to name this fun test abstract class. If you want to make your class abstract,
you need to add another keyboard between the access modifier and the keyword class. Let's
add abstract in here you can see public abstract class. And immediately after I've added this
abstract keyword, if you take a look at your package, you can see that the icon for this
class has been changed. This means that this is an abstract class. inside an abstract class
like interfaces. You can have abstract methods, let's quickly see them. For example, I can
say public abstract, let's say void, and let's name this font print name. Let's receive a
name string name. Once again, like interfaces. When you create abstract methods, you cannot
have a body. For example, if I put a pair of curly braces once again, I can see the
warning in here like before I can finish my sentence with the semicolon. The use of abstract
classes is a lot like interfaces. But there are a few slight differences. For example,
let's see the first difference in here inside my package, I'm going to define a new class,
let's name this one test class. When we wanted to implement some interface, we could have
said implements. And after that the name of our interface. But when we want to use abstract
classes, we can say extends, let's say extends test abstract class. Right now we are getting
the warning again by pressing Ctrl plus if you can see the necessary methods. So the
first difference is between this extends and implement keyword. The other difference is
that when you want to use abstract classes, you cannot extend multiple abstract classes.
For example, in here, I cannot add a comma and add another abstract class in another
word abstract classes does not support multiple inheritance. So that's the second difference
between an interface and abstract class. The other difference is that inside your abstract
class, you can also have non abstract methods. For example, in here, I can say public, let's
say void. Let's name this method print. Let's receive something in here. For example, let's
say a string text. And because this is not an abstract method, we can have a body for
this method. We didn't have this option inside an interface, we can have public non abstract
methods inside an abstract class. Like every other class, we can also have fields and maybe
a constructor as well. For example, let's say private string name. Let's create a constructor
for this class, you can see that all is working well. So the other difference is that you
can have fields constructors and non abstract fields. If you want to see the use of abstract
classes like before we can switch to our main class. And in here, I can say test abstract
class. Let's name it test abstract class is equal to new test class, the class that extends
artist abstract class. After that, we can use the methods for example, I can say this
abstract class dot, let's say print name. In here, we need to provide a name, let's
say Mesa, but we didn't have any functionality inside artists class, let's quickly define
some functionality in here. Also know that I've switched to this test class. Because
we have created a constructor inside our test abstract class, we need to create the constructor
in here as well. But for the sake of simplicity, I'm going to delete all three of these, because
I just wanted to show that these three options are available inside an abstract class. Let's
bring something inside our test class inside this print method. Let's just print the name.
Let's run the application and let's see if we can see Mason printed into our console.
You can see my Sam has been printed. So once again you can see that inside artist abstract
class, we have this abstract method which does not have anybody instead we have an extended
this class inside our test class. And we have declared the functionality inside this test
class, you can see that there are a lot of similarities between interfaces and abstract
classes. Sometimes the decision for using an abstract class or an interface can be hard
for that let me show you an Oracle web page in which may help you to decide better you
can see the address in here. I think if you check this webpage, it might be helpful. They
say that use abstract classes when your classes are related to each other. Or the other case
is that when you think that the class that is going to extend the abstract class may
use some common methods or fields or also it may require some access modifiers other
than public such as private and protected or the other case is that when you want to
declare some fields that are non static and non final, this option is also not available
in interfaces, but they say that Use interfaces when your classes are not related to each
other, or in cases that you want to use multiple inheritance we can see that when we use interfaces,
we can implement multiple interfaces. Once again, I suggest you take a look at this webpage,
it might be useful. Okay, I think that's enough for this video. In the next video we are going
to talk about concurrency and threading in Java See you in the next video.
In this video, we are going to talk about concurrency. In Java. concurrency means doing
things simultaneously and at the same time. For example, imagine that you're working with
your phone and you're reading a book at the same time, you may want to listen to some
music simultaneously, you may be downloading some files from internet This is called concurrency
or doing things at the same time in programming. In Java. The first option that we have in
order to create a concurrent programming is to create a thread a thread is a unit of execution
in your device's central processing unit or CPU. Basically, different threads come together
inside something called a process. All of these processes will be handled by your device's
CPU. By default, when you run your Java application or for that matter, your Android application,
a thread called the main thread will be created. Also, some might call this main thread the
UI thread or the user interface thread as well. Later on, if you want to do some background
task, for example, downloading a file, you will create a worker thread inside your main
thread. The first option in Java that you have in order to create a thread is like this.
You can say thread, let's name this thread is equal to new threat. Like the finding any
other Java class. Let's finish our sentence in here. The constructor of this threat requires
an interface and that interface is the runnable interface, we can pass that like this, we
can say new runnable, you can see that this runnable is an interface. When you pass this
runnable interface, this run method will be created. Basically this run method is the
place that you put the code that you want to be handled inside of worker thread. For
example, let's print something in here. And let's see if we can have a complete application.
For example, let's say hello from another thread. In order to start this thread, we
need to come down after the declaration of our thread and say thread dot start, let's
run our application. And let's see if we can see any result, you can see that our message
has been printed into the console. But how do we see the difference between the main
thread and this worker thread for that I am going to write a piece of logic in here. For
example, inside this run method, I'm going to create a for loop. Let's say for int i
is equal to zero, i less than, let's say five, and i plus plus. Inside this for loop, I'm
going to print the value of this I and after that I'm going to asleep the thread we will
see how we can asleep the current thread. First of all, let's say print printing, let's
say plus I plus, let's say in a worker thread. In order to asleep your thread, you have multiple
options. The first one and I believe the easiest one is to say something like this you can
say thread with a capital T dot sleep. Inside the parentheses of this sleep method you need
to pass some numbers in milliseconds. For example, if I want to sleep my tread for one
second, I need to pass 1000 milliseconds this asleep method is going to cause our loop to
wait one second for the next record for example in here we are going to print zero after that
we are going to wait one second. And after that we are going to print one but right now
as you can see, we are getting a warning in here and the warning says that unhandled exception
Java dot Lang dot interrupted exception. In the previous videos briefly, we have talked
about exceptions. We will talk about them in this video later on. But for now, you just
need to know that whenever an exception occurs, your application would crash. So because of
that, we need to do something for this error in here. The easiest way to overcome this
error in here is to click on the error and press ALT plus insert. After that, select
this surround with try catch, you can see that this code has been added and our thread
dot sleep method has been moved to this try block later on. We will talk about this. But
for now let's move on from this part. And let's write the same logic inside our main
thread so that we can see that the code inside the worker thread and the main thread can
be executed at the same time. So basically, I'm going to copy this for loop. Let's copy
it from here and inside the main thread outside of this run method. Let's base the button
here. I'm just going to change this message to main thread. Let's run the application
and let's see If this code is going to work, as you can see, we are printing the value
of oil two times. Once in a worker thread, and once in the main thread, it means that
the code inside the worker thread and the code inside the main thread are happening
at the same time. In this situation, we say that our application is asynchronous, or it
is concurrent. So once again, when you run your Java or Android application, one thread
will be created by default, in which we call that thread the main thread or the UI thread.
After that inside that main thread, you can create as many worker thread as you want.
The simplest way to create a thread is to say thread thread is equal to new thread.
And after that, we can pass a new runnable to the constructor of your thread in which
we will create this run method that will be executed in a background thread. But by this
way of defining your thread, there are a lot of considerations that you need to be worried
about. For example, you need to be worried about cancelling your thread when the work
is done. Also, there are all sorts of new concepts, for example, thread pools, deadlocks
and interrupting your thread. I do not intend to talk about all of those concepts in here,
because in Android, we have all sorts of options available for handling background tasks. In
fact, later on in the course, we have a complete section dedicated to handling background tasks.
In Android, we have options like async, task services, Job Scheduler, and work manager,
we are going to talk about all of them. But more on that in future videos. Okay, now let's
talk about this interrupted exception that is happening in here. Basically, whenever
an exception occurs, your application will crash and your application will be closed
by the operating system. There are all sorts of exceptions that can occur. For example,
the ones that we have seen previously, were arithmetic exception, null pointer exception,
and the one that we can see in here is interrupted exception. So in order to avoid crashes, you
need to do something about the exception. Let's comment all of these and let's see what
we can do about the exceptions.
Let's create an application in which we will create an arithmetic exception. In here I'm
going to define two new integers. For example, let's say int, a is equal to two, and int
b is equal to zero. Let's bring something in here. Let's say print a divided by b. If
we run our application, right, now, we should see an arithmetic exception. You can see that
down in here. The thing that we can do about this exception is that to catch it whenever
it happens, and we can do that by using a try catch block. Let's quickly see that for
example, I can say try. Let's move this line of code to inside our try block. We can say
try this in case some exception happens catch that exception. The kind of exception was
arithmetic exception. So let's say arithmetic exception, let's name this exception E. And
inside the catch block, we can do something else with our code. For example, we can print
something. In this case, let's say B was zero. Now, if we run our application, we wouldn't
get an exception. Instead, we are printing this message. Sometimes this message might
not to be useful. And in fact, in most cases, this message will be useful for debugging
purposes, but it's much better than a crash. The kind of exception that we are catching
in here is arithmetic exception. If we do not know the kind of exception, we can pass
the generic term exception. If you pass this exception, it's going to accept all of the
exceptions. If we run our application, once again, we should see the same result. And
here is the same result. The other kind of exception that we have seen so far was null
pointer exception that would occur when we do some operation on a null object reference.
For example, if we had a string in here, let's say a string name is equal to Now, if we do
some operation on our name inside the try block, for example, if we say name, dot equals,
let's say, Mesa, if we do that, we are going to get a nullpointerexception. Because we
have done this dot equals method on a null object inside the catch block this time, instead
of saying B was zero, let's say name was no. If you run the application, it's going to
work fine because we are passing the generic term exception, but we can safely pass null
pointer exception. Let's run the application. You can see that name was no the other kind
of exception that we just saw was this interrupted exception. It's going to happen whenever we
interrupt the current thread. For example, imagine that inside a worker thread, you are
downloading some file at the same time, you may receive a phone call. If you don't catch
the interrupted exception, you may get a crash. We have all sorts of exceptions. For example,
we have audio exception, which stands for input output exception We have class cast
exception, we have all sorts of other exceptions in which we are going to talk about them whenever
we face them. Also on our exception, we have a useful method for debugging purposes. And
that method is called print stack trace, we can see the name of our exception dot print
a stack trace, we will talk about this stack in future videos when we talk about activities
and fragments in Android. But for now, just know that this line of code in here is going
to print something into our console that might be useful when you're debugging your application.
Let's run the application in here. You can see the stack trace of your exception in here
it says that the exception occurred in line 34, which is this line, and you're also seeing
the name of your exception, this is not an actual exception, you're just seeing some
information about your exception, this in here might be useful for when you're debugging
your application. Okay, I think that's enough talking about concurrency and exceptions in
Java. In the next video, we are going to talk about Singleton pattern in Java. See you in
the next video. In this video, we are going to talk about Singleton pattern. Basically,
we use Singleton pattern when we want to make sure that we have only one instance of some
class in our entire application. In some cases, we may want such a thing. And here is how
we can implement that pattern. For example, one case of using Singleton pattern is when
you are using a database in your application. Don't worry about the databases, we have a
complete section dedicated to them. But in here, we are just going to create a simple
Java class named database. Let's quickly create that inside our source folder inside our package.
And we will see how we can implement that patter. Let's name this class database. For
this database class, I'm going to define a field let's say private string name. Also,
let's create a constructor and also getters and setters.
Right now, because we have this public constructor, we can create as many instance as we want
from this database class. But if you want to make sure that you have only one instance
of this class in your entire application, you can create another field in here, for
example, I'm going to say private, static as the data type of this field, I'm going
to specify the class that we are in for example, in this case, I'm going to say database. So
let's say private static database, make sure to import the one that comes from our package
or dot maker dot Singleton pattern. For the name of this field, it's convention to name
it instance. And that's it. Now we can create a getter method for this field. And here is
how we can create that field, we can say public, because we are going to use this instance
inside this field, I'm going to say static, the return type of this getter method is going
to be the class itself. So I'm going to say database. Let's name this method. Get instance,
as you can see the suggestion in here and let's have a body for this method. In here,
instead of just simply passing this instance, I'm going to create an if statement, I'm going
to say if let's say now, is equal to our instance, if that's the case, I'm going to create a
new instance, I'm going to say instance, is equal to new database. Notice that we need
a name for the constructor of this database. So I'm going to receive that name via the
input of this method in here, let's say the string name. And later on, let's first this
naming here. So if our instance is now we are going to create a new instance, and after
creating that we are going to return it, we can say return instance. But in the other
case, in case if our instance is not not in the else case, I'm just going to simply return
instance, right now if we use this get instance method, instead of this constructor, we are
sure that we have only one instance of our database class in our entire application.
For example, if we go to the main class, and if we try to instantiate our database, we
can have only one instance we can say database, let's name the database is equal to database
with capital D dot get instance, of course, we need the naming here. Let's pass a name.
For example, let's say DB music. Notice that because we have declared this get instance
method as a static, now we can use this database class with this capital D, it means that we
are not relying on the instance of this database class. Instead, we are using the object itself.
But right now someone can declare the database class as we did for any other class. For example,
in here, someone can say database. Let's name this one test database
is equal to new database because in our database class, we have the constructor, we can pass
a name in here and if we do that, we have another instance of this database class. For
example, let's say DB movies. In order to restrict users from using the constructor,
I'm going to change the access modifier of this constructor inside my database class,
I'm going to change this public to private, so that this constructor would be available
only from inside this class and not from inside any other class like this main class. Right
now we have an error in here and the error says that the constructor is private. So by
changing the access modifier from public to private, now, we are restricting others from
creating multiple instances of our database class. But because our constructor inside
our database is private, we can use this constructor inside this get instance method, as you can
see in line 10, in here, right now, because of this static keyword. And also because of
this, if statement that we have put in here, we are sure that we have only one instance
of our database class. But there is also another point that we need to be worried about. And
that's when someone tries to create multiple instances of our database from multiple threads.
In some cases, if someone tries to create multiple instances of our class asynchronously,
they might be successful in doing such a thing. In order to make our get instance method thread
safe, we have an option in here after this static keyword, we can add another keyword
in which we haven't seen so far, we can say synchronized, this synchronized keyword will
make our get instance method thread safe, it means that no more than one thread can
call this get instance at the same time. So this is the whole Singleton pattern. First
of all, you declare a static field of the kind of your class for example, in this case
database, after that, you create a get instance method in which is a static once again and
also synchronized. Inside that get instance method you are checking that if the instance
of your database is now or not, if it is not, it means that it's the first time that you
are calling this get instance method. So you need to create a new instance like we are
doing in line 10. After that, we are returning the instance but in the else case, we are
not going to create a new instance, we are just going to return the existing instance
beside this get instance method. In order to restrict others from using the constructor,
you need to change the access modifier of your constructor to private so that it would
be usable only inside your database class. We can also make these if statements shorter
for example, in here, I can delete this whole else case. And I can delete this return instance
from line 11 in here, and after the if statement, I can return that instance. So by the time
we reach to this line 13 in which we are returning the instance, we are sure that our instance
is not not and we are returning either a new instance or the existing instance. Okay, I
think that's enough talking about the singleton pattern later on in the course, you will see
that this pattern might be useful from time to time. Before I finish off this video, I'm
going to talk about one more methods available in every class in Java. And that method is
called to string right now we don't have any to string method inside our database class.
But if we switch back to the main class, and if we say print database.to string, you can
see that we have such a method. And if we run our application, we should see some information
about our database class. Let's quickly run it and let's see if we can see any result.
You can see that we are getting the address of our database class. If you want, you can
change this for example inside your database class, you can override the two string methods
down in here inside the scope of our class, we can press Ctrl plus or and from here we
can select this two string method. Right now we are returning the super statement. If you
want to change that we can delete this whole statement. And we can prepare a string in
here. For example, we can say a string text is equal to let's say database class. After
that, let's add a line. And after that, let's print the name of our database.
Let's say name plus the start name. After creating this text, we can return for example,
we can say return text. Now that we have overrided, this two string method, if we run our application,
we should see some other results printed into our console. From time to time this two string
method can be useful as well. It's available in every Java class and basically it will
print some detail about that class. Okay, I think that's enough for this video. And
for that method. I think that's enough talking about Java. Our Java section is done and hopefully
we have learned a lot in the next video we are going to have a quick challenge in order
to make sure that we have learned everything that we have talked so far. But before I finish
off this video, I'm going to say that with Java, you can do a lot have things for example,
besides writing Android applications, you can use Java in order to create desktop applications
for Windows, Linux and Mac OS operating systems. And also with the help of spring framework,
you can use Java in order to create enterprise web applications. There are much more to Java
language. For example, you can use Java in order to create applications for quantum computers.
You can also use it for data analysis purposes, and literally everything else that you want.
In this course we have touched the surface of Java language. But if you want to know
more, I'm currently recording a new course for Java language. And I'm sure that you will
find that course helpful beside the Java course I plan to record a complete course for a spring
framework in order to create web applications. By the time you're watching this video, those
courses may be released, so make sure to check them. Okay, I think that's enough. In the
next video, as I said, we are going to have a quick challenge See you in the next video.
Here is our final challenge for the Java section, I want you to simulate your phone's contacts
and messages applications. Here is how our application is going to work. First of all,
we are going to greet the user with some message. After that, we are going to show three options
to the user, one for managing the contacts, another one for handling the messages, and
another one for creating the application. In case if the user selects the first option,
which is managing the contacts, we are going to show these options, one for showing all
of the contacts another one for adding a new contact. Another one for searching for a specific
contact. One more for deleting a contact. And the last one is to go back to the previous
menu, which was these three options. But in case if the user selects the second option,
which is handling messages, we are going to show these three options. The first one is
to see a list of all of the messages. The other one is to send a new message and one
last one in order to go back to the previous menu in which is our initial menu, which has
three options. In case if the user selects these third option, we are going to create
the application. We haven't talked about databases and permanent data storage. But as a hint,
if you want to store contacts and messages, you can use a static variables. Okay, pause
the video in here and go solve the challenge. After that, come back. And let's see my solution
for this application. Okay, let's go to see how can we create this simple application.
First of all, I have created my project, and I have created this package inside my source
folder. Inside this package I have created this main class in which contains this main
method. Let's continue from here. First of all, I'm going to create a new Java class
called message. Inside this message class, I'm going to have three fields, let's say
private string text. Another one, let's say private string recipient. This recipient in
here is going to be the name of our contact. Also in here, I'm going to define an integer
called ID, let's say private int ID. This ID is not necessary for this application in
here. But because in most cases, when you're working with databases, you have a column
called ID in order to identify every unique message. I want to be as close as I can to
a real world application. I just wanted you to see this ID field inside a model in which
is a message. Okay, now that we have these three fields, let's quickly create a constructor.
And let's accept all of these three fields. After that, let's create getters and setters.
I'm not sure that we are going to need all of these getters and setters later on. If
we didn't use them, we will delete them. Also inside this message class, let's quickly create
another method called get details. Basically, we are going to print the details of every
message. So let's say public void. Let's name it get details.
Let's say print. First of all, let's say contact name. And let's print the recipient. After
that, let's print the text itself. Let's say message plus the text. We also need to add
a plus in here. Sorry about that. And I think if we add a backslash n in here, it would
be much better. After the text. Let's also print the ID Let's say backslash n, Id plus
our ID. That's it for our message class, let's quickly create the contact class as well.
New Java class, let's name this phone contact. Inside this contact class, I'm going to have
four fields, let's say private string, name, private string. Number, you may think that
this number should be an integer, or maybe a long, but because different numbers can
have different formats. For example, they can have some parentheses or maybe some hyphens.
I am saving this number as a string. But if you want, you can always use integers or long.
After name and number, I'm going to save another field in here, let's say private string email.
After all of these, I'm going to have another field in here, let's say private ArrayList
of different messages. Let's name this phone messages. So basically, I'm going to save
different messages for every contact inside an ArrayList. Okay, now that we have our four
fields, let's quickly create our constructor. But in here, I'm going to create two constructors,
because at the time of creating a new contact, that contact does not have any messages. For
that, I'm going to create another constructor in here, which will not accept an ArrayList
of different messages. This time, let's select the first three items. But because if I don't
do anything about this messages, this ArrayList will be now inside this constructor, I'm going
to see these dots messages is equal to new ArrayList. So at the time of creating a new
contact, I'm saving the messages array list as an empty array list. Okay, now that we
have two constructors in here, let's quickly create getters and setters. And also after
that, let's create a get details method. Let's say public void, get details. We can also
use two string methods. But in here, I'm just more comfortable with this get details method.
let's print all of the information about each contact. Let's say name plus the staff name.
After that, let's print the number and email. Okay, that's it for our contact class. Let's
switch to main class. And let's write the logic for our application. First of all in
here, I'm going to create an ArrayList of different contacts. But I'm not going to do
that inside this main method because we are going to have multiple methods and we need
to have access to our contacts are released in different methods for that I'm going to
define my ArrayList above in here in the fields of this main class. I can say private, static,
because the methods that I'm going to use later on are static methods, I need to create
this ArrayList as a static, so let's say private static array list of different contact. Let's
name it contacts. But this is not a good place to initialize our contacts ArrayList because
this is not the place that our code will be started. We can initialize our contacts ArrayList
for example, inside this main method. But before I do that, I'm going to also define
a scanner in here because we are going to need the scanner in multiple methods. So let's
say private static scanner, let's name it the scanner. After that inside the main method,
let's initialize our contacts ArrayList. Let's say contact is equal to new ArrayList. As
you can see in here, I'm going to greet the user with some message, let's just print something.
After that, I'm going to show these three different options to the user. Because I want
to have a clean code. I'm going to show these three options inside another method. Let's
name that method show initial options. But as you can see, right now I'm getting a red
warning and that's because I need to create this method down in here. So let's say private,
static void, show initial options. Let's bring the options in here. After that, we need to
get the user's input. And for that we can use the scanner. So let's initialize our scanner.
Right now we have declared our scanner above in here, but we never initialize that. It's
a good place to initialize our scanner in here. We can say a scanner is equal to new
scanner. Let's pass the system that after that, we can say it's choice is equal to a
scanner dot next int. Let's create a switch upon this choice. Let's say switch on choice.
In case the choice is one, we are going to manage the contacts. Once again, let's do
that in another method, let's say manage contacts. Don't worry about this read warning, we will
create this method later on. Let's also add a break in here. But in case and the choice
is to, we are going to handle the messages. So let's say manage messages. In case the
choice is three, or for that matter, any other number, we are going to create the application.
So in here, I can say default. In the default case, I'm just going to break out of the application.
Let's minimize this project main for now. And then in here, let's create our two methods.
First of all, let's manage our contacts. Let's say private, static void, manage contacts.
Once again, in here, I'm going to show a list of options. So let's print those options.
One, once again, we need to get the user's input for that we can use the scanner, let's
say int choice is equal to a scanner dot next int. Notice that we are not initializing our
scanner in here. And that's because we have done that inside this short initial options
method. So by this time, we have an instance of our scanner. After that, once again, we
need to create a switch on our choice. In case it's one, we are going to show all of
the contacts, let's create that method, show all contacts. And also we need to break. Let's
also write the rest of the cases. In the default case, I'm going to go back to the previous
menu. So I'm going to once again run this show initial options method, so that we can
see all of these options once again. So in the default case, I'm going to say show initial
options. After that in here, we need a default as well. I'm saying default in here, because
I'm going to go back to the previous menu in case if the user enters any other number.
Okay, let's create these methods one by one. First of all, let's show a list of all of
the contacts. I'm going to do that down in here. Also, instead of writing the declaration
of your method, you can use the help of your ID. For that, you can click on the red arrow
in here and by pressing ALT plus insert, the first option is to create this method, you
can see that we have created this method successfully, the void is fine for the return type. And
down in here, I can't create the logic for this method. In order to show a list of all
of the contacts, I'm going to simply create a for each loop. Let's say for contact, let's
name it see inside our contacts, or at least, let's just say C dot get details the method
that we have created inside our contact class. After the for loop once again, I'm going to
show the initial options. For that I'm going to save show initial options. We are going
to quit the application when the user tells us that's all we need to do inside this method.
Let's just minimize it for now. And let's create the second method which is add new
contacts. Once again, we can use the help of our ID. Inside this add new contact method
we are going to receive some users input. But before that, let's print something you
Let's say adding a new contact. After that, let's say please enter the contacts name.
Once again, we are going to use our scanner, let's say a string name is equal to a scanner
dot next, after we get the contacts name, we are going to get the contact number. So
once again, let's bring something in here. Let's say please enter contacts number. Let's
see string number is equal to a scanner dot next, let's also get the contacts email string
he made is equal to a scanner dot next. After that, we need to make sure that the user enter
something as the name number and email of our contact. And don't just press enter and
leave us with empty strings. For that, I'm going to create an if statement in here, let's
say if
name dot equals an empty string. Let's say or if number that equals an empty string.
Or if email is equal to an empty string. If each one of these is true, we are going to
show some message to the user we are going to ask to enter all of the required information.
Let's say please enter all of the information. After printing this line of code, I'm going
to recall this whole method I'm going to start from the beginning of this method. For that
I'm going to say add new contact. I'm calling this method from inside the method. This is
called a recursive call, you can see the icon at the left side in here. If you hover over
that you can see the recursive call calling a method from inside that method. So by this
way of calling our method, we are starting once again from the beginning of this method,
but in the else case, we are going to create our contact object, I'm going to say contact,
let's name it contact is equal to new contact. And after that, I'm going to pass name, number
and email. If you remember inside our contact class, we had two constructors. And in here
we are using the second constructor, we didn't pass any messages ArrayList to this constructor
in here. After creating our contact object, we can say contact our ArrayList dot add.
Let's pass our contact. As simple as that. Now we have added a new contact after this
if statement. Once again, I'm going to show the initial options. So let's say show initial
options. Because once again, we are not going to create the application unless the user
tells us Ok, we are done with our second method. Let's see what else do we have in here. Let's
create this search for contact method. The Void is fine for the return type. Inside this
method first of all we are going to ask for the name of the contact. So let's bring something
let's say please enter the contact name. Let's say string name is equal to the scanner dot
next. After that we need to make sure that we have received any so let's say if name
dot equals an empty string. We are going to print some message we are going to ask for
the contact name once again. Let's say please enter the name and after that once again we
are going to use a recursive call. We are going to say search for contacts once again.
But in the else case, we are going to search for this contact inside our contacts ArrayList.
For that once again I'm going to create a for each loop. Let's say for contact. Let's
name it see inside our contacts are a list. Let's say if c dot get name is equal to the
name that we just received. If that's the case, let's print c dot get details. Let's
also define a Boolean before this for loop. We will see why I am defining this Boolean.
Let's say Boolean. Let's name it does exist. Let's initialize it to false in this if state
Wherever you have found the contact first of all I'm going to change the value of this
does exist to true, let's say it does exist is equal to true. And after the for loop,
I'm going to create an if statement, I'm going to say if does exist is not equal to true.
Then we are going to print something I'm going to save there is no such contact.
After all of these logics, we are going to show the list of options once again. So let's
say show initial options. Okay, that's it for our search for contact method. Let's also
create this delete contact method. Once again, in here, we are going to ask for the contacts
name. So let's say please enter the name. Like before, let's say a string name is equal
to a scanner dot next, next, we need to create our if statement. If named, that equals an
empty string, we are going to ask for the name once again. And we are going to start
from the beginning of this method. In the else case, we are going to look into our contacts
ArrayList. So let's say for contact see inside our contacts list. Before I write the logic,
let's also define a Boolean above in here like we did for the search for contacts method.
Let's say Boolean does exist. Let's initialize it to false. So in here, let's say if I don't
know why I said as in here, let's quickly change this one to see. So let's say if see,
that gets name is equal to the name that we have received. If that's the case, first of
all, we are going to change the value of this does exist. Let's say it does exist is equal
to true. After that, we are going to remove this contact from our contacts ArrayList we
can save contact dot remove. This remove method requires an object which I'm going to pass
see the object that we are currently looking in this iteration. After the for loop, I'm
going to say if does exist is not equal to true then we are going to say there is no
such contact. And after all of these logics, we are going to show the list of all of the
initial options. Okay, that's it for this method. Let's minimize this one. So far, we
have created the logic for the first option in our initial options, we have created the
whole logic for this manage contacts. But right now in the logic for this manage contacts
option, we have one or two possible points for multiple bugs, we are going to fix those
two and also we are going to write the rest of the logic for this application in the next
video. See you in the next video.
In the previous video, we have created the logic for this manage contacts option. But
if you remember I said that we have some issues right now with this manage contacts logic.
And in this video, first of all, we are going to fix those issues. The first issue is inside
this add new contact method. In here we are adding the contact into our contacts array
list. But before that, we need to make sure that we do not have a contact with this name
saved on our device for that before adding this contact to my contacts array list. I'm
going to check that if I have such a contact on my device. So first of all, let's create
a Boolean in here. Let's say Boolean does exist. Let's initialize it to false. After
that, I'm going to create a for each loop. Let's say for contact see inside my contacts
I have a list. Let's see if c dot get name is equal to the name that we just received.
Inside this if statement, I'm going to change the value of this does exist to true so let's
say does exist is equal to true. After the for loop I'm going to check that if this does
exist is equal to true or not. Let's say if does exist is equal to true. Then we are going
to print something we are going to say we have a contact with this name. After that
we are going to start from the beginning of this method. So I'm going to call this add
new contact method once again. But in the else case, I'm going to add the contact to
my account. Tech's ArrayList, so I'm going to call this line of code from here. And I'm
going to paste them inside this else case. Also, after adding the contact to our contacts
array list, I'm going to show some message to the user. So let's say name, added successfully.
Okay, that's the fix that we needed inside our add new contact method. Let's also change
the show all contacts method a little bit as well. So inside this for each loop, after
printing the details of a contact, I'm going to print another line. And this line is going
to be just a separator that will indicate the difference between different contacts.
Nothing special in here. Okay, let's move on. And let's create the method that we haven't
created in here, this manage messages method, by pressing ALT plus insert, we can create
that method. First of all, we are going to show a list of options. So let's print something.
After that, we need to receive the user's input, I can say int choice is the call to
a scanner dot next int. And let's create our switch statement on this choice. In case it's
Vaughn, we are going to show a list of all of the messages. So let's say show all messages,
we will create that method in a minute. But in here, let's add a break. And let's write
the other cases. In case it's true, we are going to send a new message. So let's say
send a new message. And let's add a break. In the default case, I'm just going to show
the initial options. After that, in here, we need a break as well. Okay, let's create
this show all messages method. In here, first of all, I'm going to save all of the messages
from all of the contacts into an array list. So let's say array list of different messages.
Let's name it all messages is equal to new ArrayList. After that, I'm going to create
a for each loop. For contact, see inside my contacts or in this. Let's see all messages
that at all, we haven't used this add all method, but in here, we are going to do that.
I'm going to say C dot get messages. So by using this add all method, we are adding all
of the messages of one contact to our all messages ArrayList. After the for loop, I'm
going to check that if the size of these all messages is zero or not. So let's say if all
messages that size is greater than zero, I'm going to show the details of every message.
So for that, I'm going to create another for each loop in here. Let's say for message,
let's name it m inside out all messages, at least. let's print m dot get details. And
after that, let's also print some separator. But in the else case, I'm going to say that
you do not have any message. After everything after the if statement, I am going to show
the initial options. Okay, that's it for this show all messages method. Let's also create
the other method, which is send a new message. In here, first of all, I'm going to get the
contact or recipients name. So let's ask the user that, let's say Who are you going to
send a message. After that we need to get the name of our contact. So I can say string
name is equal to scanner dot next. After that, we need to check that if this name is empty
or not for that, I can say if name that equals an empty string. We are going to ask the user
to enter a name. So let's say please enter the name of the contact. After that we are
going to have a recursive call. So in here, I'm going to say send a new message. But in
the else case, I'm going to check that if we have such a contact in our contacts ArrayList.
For that I'm going to define a Boolean in here Boolean does exist, is equal to false.
After that, I'm going to create a for each loop. Let's say for contact see inside our
contacts or at least if see that gets name, dot equals the name that we just received.
Let's say it does exist is equal to true. After the for loop, I'm going to check that
if does exist is equal to true or not. First of all, let's write the else case. In the
else case, I'm going to say there is no such contact. But inside the if block, if we do
have such a contact, we are going to ask for the message itself. So let's say what are
you going to say? Let's say a string text is equal to a scanner dot next. After that,
we need to make sure that this text is empty or not. Let's say if text that equals an empty
string. If that's the case, let's say please enter some message.
And after that, we are going to start from the beginning of this method. So let's say
send me a message, which once again is a recursive call. But in the else case, we are going to
create our message object. And after that, we are going to add it to the contact that
exists in our contacts ArrayList. But if you remember, inside our message class, we had
a field called ID. Basically, whenever you are receiving these messages from a database,
or maybe a web server, this ID is unique to every message. In here, I'm trying to be as
close as I can, and to a real world application. So I'm going to make this ID unique. In order
to have a unique ID for every message, I'm going to define a field above in here inside
my main class I am talking about here. And after that, we will increment the number of
this ID by one every time that we create a new message. For that I'm going to say private,
static integer. Let's name this phone ID. And let's also initialize it to zero. After
that down in here, when we create our message object inside this else case, down in here,
I can say ID plus plus. So because this ID is a static integer, whenever we increment
its value, it's going to be unique for every message. So right now we have the name of
our recipient, which we have received above in here. And also we have the message body,
which is this text. And also we have generated this ID which is unique. These are the three
things that we need in order to create our message object, I can say message, let's name
it new message is equal to new message we need the text. After that we need the recipient,
which is name. And after that, we need the ID. Next, I need to find the proper contact
to add this message to his or her array list of different messages. For that I'm going
to create a for loop. Let's say for contact see inside our contacts array list. Let's
say if see that get name is equal to the name that we received. If that's the case, first
of all, we need to get the array list of different messages of this contact. For that I can say
array list of messages. Let's call this ArrayList new messages is equal to c dot get messages.
After that, we need to add this new message to this new messages ArrayList so let's say
new messages that add new message Sorry, I have used this add all methods went to remove
this all. After that we are going to save the current contact that we are looking into.
because later on we are going to remove this contact from our contacts array list. And
we are going to update his or her array list of messages. And after that, we are going
to add it once again to our contacts ArrayList. So for that I can say contact. Let's name
it current contact is equal to see. Next we need to say current contact dot set messages.
And we need to pass our new messages are released. After that, it's time to remove the C from
our contacts or at least we can say contacts that remove and we can pass the C object.
Now that we have removed the previous contact, we can add the new one by saying contacts
dot add. Let's say current contact. So this way we have added this new message to our
specific contact and we have updated our contacts ArrayList Okay, that's the entire logic that
we need in order to add a new message just after everything after we are done with this
method. We need to show the initial options once again. Let's minimize this method. And
let's see what else do we have? I think we have covered everything. And we are ready
to test our application. So let's run the application.
In here we are seeing the greeting message, which is fine. After that we are seeing the
initial list of options. If we select one, we can see the options for managing different
contacts. Right now we do not have any contacts in our ArrayList. So if we select one, you
can see that we are not seeing anything, it's better to print some message in here in case
if we do not have any contact in our phone, for example, that says, you do not have any
contact on your device, but we will write that later on. For now let's just see the
other options. Once again, let's select Manage contact. This time, let's try to add a new
contact, you can see that the console is waiting for a name, let's say may Sam, the console
is waiting for a number. Let's add some number. After that, we need to provide some email,
let's say may sam@gmail.com. And as you can see Mason added successfully. If you want
to make sure of that, once again, you can check the list of all of your contacts by
saying show all contacts. And in here you can see the details of Mason. Let's also add
another contact. Let's say Sarah, this thing. Let's add a random number. After that an email.
This time let's try searching for a new contact. So in here, if I say three, you can see that
the console is waiting for the name of our contact. Let's type Brad in here, you can
see that we are seeing this message there is no such contact in your phone. Once again,
let's try searching for contact this time. Let's try Sarah. You can see that we are seeing
the details about Sarah. Let's also try removing one of the contacts. Let's say for in order
to delete a contact, let's say me Sam. In here we are not seeing any message indicating
if that we have deleted Maxim successfully or not. But if we check the list of our contacts,
we should see only Sarah. And as you can see, we have only Sarah in our contacts list. Let's
also try this go back as well. So let's say manage contacts once again. This time if I
put any number beside one through four, for example, if I say six, you can see that we
are going back to the main menu. Also, let's try adding a new contact with empty values.
Sorry about that, I need to go back and select one. Let's say to in order to add a new contact.
Let's don't provide anything for the name. You can see that the scanner is waiting for
some input. But even if the scanner moves to the next line, we have write the logic
in order to prevent adding an empty contact. Okay, that's good. Let's add many samples
again. Let's add some number. And some you mean. Let's see what we have inside the messages
option. In order to see a list of all of the messages. Let's press one, you can see this
message you don't have any message. Once again, let's go to the messages option. I've entered
two in here and let's try sending a new message. Who are you going to send a message? Let's
say Sarah, what are you going to say? Let's say hello. It seems like we are getting a
crash and the crash happened because of this exception. Concurrent modification exception.
Okay, let's take a deeper look into our code. And let's debug our application for the first
time. It seems like the exception occurred when we try to add the new message. Also,
you can see the line 99 in here, if you click on that, you will be navigated to that line.
Yes, we have this exception inside the add new message or send a new message inside this
for loop. I think the error in here is happening because first of all, we are using this C
to assign a new value to this current contact. And immediately after that we are trying to
remove this contact from our contacts ArrayList. So this causes the concurrent modification
exception. If you want to get over that exception, you can simplify this if statement. For example
in here I'm going to delete all these three lines of code. So by this point, we have this
new messages are released. We have added our new message to this ArrayList and after that,
in order to update the array list inside this contact, I can say see dot set messages. And
I can pass my new messages ArrayList. So no need for removing and adding a new contact
into our ArrayList. concurrently. Let's run the application once again. And let's see
if we are seeing the exception once again. First of all, we need to add a contact. So
let's say manage contact, add a new contact. Let's Provide a name and number after that
some email. And now we can send a new message, let's say messages. Send a new message. Who
are you going to send a message? Let's say Mesa, what are you going to say, after this
line of code, we got the exception. Let's say hello. And this time, it seems like we
have added or we have sent a new message. If you want to make sure of that, you can
go to the messages option. And you can show the list of all of your messages, you can
see that we have one message in which we have sent it to Mesa. Okay, that seems better.
Once again, let's go to this messages option. And let's try going back to the main menu.
So if I type three or any other number beside this one and two, we should go back to the
main menu. For example, let's say for and as you can see, we are seeing the initial
options. For the last thing, let's try creating our application. And as you can see, we have
created the application successfully, let's just fix the few errors that we had. In the
show all contacts method. If you remember I said that we are going to show some message
in case if we don't have any contacts in our ArrayList. So for that, first of all, I'm
going to check that if we do have any contact. For that, I'm going to say if contacts that
size is greater than zero. First of all, let's write the else case. In the else case, I'm
just going to print you do not have any contact. But in the if block view are going to do the
same thing that we were doing previously. Let's run the application one more time in
order to see that these changes are effective or not.
So right now, we do not have any contacts in our contacts list. Also, you may be wondering
why we are losing the contacts that we are keep adding into our application. That's because
we are not using any permanent data storage like a database, we are using a static array
list and that ArrayList will be cleared when we close our application. For that reason
all of the contacts are being removed whenever we stop the application. So let's say manage
contacts. And let's say show all contacts, you can see the message in here you do not
have any contacts. But it's better to show the list of initial options once again after
we print this line of code. So down in here after this print statement, let's say show
initial options. Okay, this was the last video of this section. And this was my solution
for this challenge. Your solution is probably different than me. And that's okay, because
there are multiple ways of creating the same application. Hopefully you have practiced
and learned everything that we have talked about in this section. In the next section
of the course, we are going to switch back to Android Studio, and we are going to talk
about user interface and how we can design a beautiful layout for our application. See
you in the next section. What's up everyone, I hope you're having fun. Before we start
our talk about user interface, I just wanted to remind you that there is an extended version
of the scores that you can watch. by enrolling in the extended course you will have lifetime
access to more than 60 hours of videos, you can ask your questions directly from me and
I will come back to you within hours, you will have access to all of the source codes
that I write in the videos. And also you can have access to all of the new videos that
I upload. Just check out miko.org for more details about the course. And don't forget
to use Free Code Camp as the coupon code to get 20% discount. If you want to get serious
in Android app development. Taking the extended course is almost a necessity. Okay, everyone,
I just wanted to remind you that there is an extended version of this course that you
can take without further ado, let's just start our talk about user interface. Have fun learning.
Hello everyone and welcome back. This is the user interface section of our course and we
are going to talk about a lot of concepts and tools that we have related to user interface.
We are going to talk about a lot of UI elements and they are attributes. We are going To talk
about different kinds of event listeners, for example, click listeners on different
UI elements. We will talk about how can we create applications that is compatible with
different screen sizes, and also different languages. Besides that, we will also talk
about material design and all sorts of stuff in this section of the course. Without further
ado, let's jump into our project. And let's implement some UI elements. As you can see,
I'm no longer using IntelliJ ID, and I've switched to Android Studio. We needed IntelliJ
ID for the Java section of the course. But in here, we can switch back to Android Studio,
just in case if you forget how to create a new project in Android Studio, I'm going to
start from scratch. Let's start a new Android Studio project. In here, we needed to select
a template. If you remember, I said that these templates are some applications return to
some level. Later on in the course, specifically, after we talked about activities, we will
see that how can we create all of these from scratch, but for now, let's just select this
empty activity. After that in here, we needed to name our project. For example, for this
application, I'm going to say UI basics. I'm going to stay with this package name, I think
it's fine. We needed a save location. Once again, the default is fine. For the language,
I'm going to say Java for the minimum API level. As I said in previous videos, in the
most videos for this course, I'm going to save with API level 19. After selecting all
of these, let's just create our project by clicking on this Finish button down in here.
It seems like our project has been created successfully. Now that we know about Java,
let's have another look at this class that we have created in here. But before that,
let's minimize this Ron pane.
As you can see, this is a Java class in our package, you can see that it's a class called
main activity. This class is extending the app compat activity, whatever that is. Inside
this class, we have this onCreate method which is overriding you can guess it probably because
of this app compat activity. And also above in here we have some imports. Right now we
are importing this app compat activity and also this bond. Don't worry about this, we
will talk about them later on in the course. But for now you can see the general syntax
of a simple Java class in here as well. If you remember I said that when you create an
activity, a Java class and also XML file will be created. The Java part is responsible for
the behavior of your application. For example, in your layout file, you may receive the user's
input in the Java file, you may decide to what to do with the user's input. Hopefully,
now that we know about Java, we can understand all of these much better. This onCreate method
is a part of something called the activity lifecycle. We will talk about that in future
videos. But for now, just know that this onCreate method is somehow like the main method inside
a simple Java application. This is the start point of every activity. And right now, because
our application has only one activity, this is the start point of our entire application.
So this line 13 in here would be the start point of our code. You can see this super
statement in here. Once again, it means that do whatever you are doing inside the parent
class. In this case, the parent class is this app compat activity. For the time being we
are not concerned about that. But as you can see, we are passing this save instance state
which is a bonded to the super statement. Later on, we will talk about that. Also, it's
important to note that inside activities, you have some inner methods, we didn't have
this option in Java. For example, you can see this set content view method, you don't
see the declaration of this method anywhere inside this class. But you can see that by
using it, you are not getting any red warning. Similar to this setcontentview. If you have
a lot of other inner methods inside an activity, we will talk about them later on in the course.
Okay, let's move on from this part. And let's start working on our user interface. I just
wanted to have another look at Our application and specifically this jar file. Now that we
know about Java, okay, let's close this java file. And let's start talking about this activity
main dot XML file. If you remember in the first videos of this course, I said that XML
is a markup language. Once again, if you are a web developer, you know the concept of markup
languages. Those are just helpful for defining some elements that are going to be shown to
the user. Before everything. Let's scripting locate this file in our project. Right now
we are inside the Android view. And if you want to find this activity main dot XML file,
you can come to this Resources folder. down in here inside the layout folder, you can
see the file. And if you switch to projects view, you can go to your application folder,
inside the app folder. Inside the source folder, you have this main folder. And in here you
have the Resources folder. Once again, inside the layout file, you have this activity main
dot XML file. Okay, let's switch back to Android view. And let's minimize this project pane
for now, if you remember from the previous videos of this course, I said that whenever
you want to define a layout file, you have two options. First of all, you can use this
design view for example, by dragging some items into your design view.
You can have different elements and later on, you can define some attributes for your
elements. This is one way but the other way is to switch to text view and manually type
everything that you need. In my opinion, when you are an Android developer, most of the
times you are going to work with the TextView. So in here, we are going to start from this
TextView. First of all, if you're not seeing this preview in here, you can always enable
it and disable it from this preview option in here. If you have a low memory RAM, my
suggestion is to disable this review, because it's going to render the layout file in real
time. And that's going to load a lot of pressures on your device's memory RAM and also CPU.
For that if you have low memory RAM, disable this preview, it will speed everything up.
Right now inside this simple layout file, we have this TextView you can see that when
I click on this TextView this TextView inside our XML file has been highlighted, it means
that these are the same. I'm going to delete this text view for now. Let's select all of
it. And let's press Delete. The next thing that I'm going to do is that I'm going to
change this constraint layout. In future videos, we will talk about what the constraint layout
is. But for now, for a starter, I think it's better to start with relative layout. Let's
select that. Also, if you have noticed, you can see that when we have changed the opening
tag in here to relative layout, the closing tag has been changed to relative layout as
well. Inside these opening and closing tags, I can have different UI elements. For example,
if I want to define a TextView, I can open an anchor bracket and by typing TextView.
In capital T, we have a list of options. Let's select the first one TextView. Immediately
after pressing Enter, we are getting some attributes. So for every UI elements, you
need at least two attributes a width and also a height. The values for these two attributes
can be some numbers. But Alternatively, you can see that we have other options in here
as well. We will talk about this match parent and wrap content in a minute. But for now,
let's just select wrap content. Basically, it's going to wrap the content of this text
view for the height. Let's also select wrap content. If you want you can finish creating
your text view by adding a slashing here. And that's if you can have a text view. But
if you want to have more attributes, you have a lot of options. For example, before this
slash I'm going to say let's say text, you can see that we are getting a suggestion and
recall that text let's select that. And in here I can pass a text for example, let's
say hello world.
As soon as I type this, you can see that text in the preview as well. So text is one of
the attributes of this text view. Let's talk about the other attributes found by Vaughn.
For example, right now this text is at the corner of my screen I don't like this, I'm
going to change the position of this TextView to the middle of this line. For that I have
multiple options First of all, I can add some margin in which will add a space between this
TextView at the edge of the screen. For example, I can say margin and I can pass 50 for example
50 dP dp is the unit when you're working with your layout files, we can see that we have
some margin, but we can be more specific for example, we can say margin top margin left
margin right and also margin bottom. For example, I can say margin left let's say 100 Db this
time, margins are extremely useful but in here they are not going to help us because
this is not accurate, we want our TextView to be exactly in the center of our line. For
that, instead of using a margin, I have a better option, I can say, center horizontal
in order to center my text view horizontally. As you can see, I can pass through or false
in here, let's pass through. And this time and the TextView has been centered horizontally.
If you want, you can also center it vertically by saying center vertical and passing a value
of true, you can see that it has been centered both horizontally and vertically. Beside these
two, you also have another option. For example, in this case, I'm going to delete both of
these attributes. Instead, I'm going to say center in parent, this one down in here. Let's
pass through as the value of this attribute. This center parent is going to center our
elements inside its parent element. an XML file is like a tree, we have these Child Elements.
And above them, we have the parent elements. For example, in this case, the parent element
is a relative layout. If we want, we can have another relative layout in here or maybe some
other UI elements. And inside that element, we can have this TextView we can do these
kind of inheritance as many times as we want. So in this case, the TextView is our child
element. And this relative layout is our parent element. And when we use this center implant
attribute, it's going to center our TextView in its parent element. Okay, let's move on
from this part. If you want, you can also increase the size of your text for that you
can say text size, this attribute in here, and you can pass a number for example, you
can say 20 SB. When you're working with the size of your fonts, the unit is SP, we can
see that it's larger now. Also, if you want you can have some styles on this text as well.
For that we can say text style. This attribute you can see three styles bold, italic, and
normal. For example, let's fast bold. If you want your attribute to be both bold and italic,
you can add a pipeline in here. And after that you can see italic. This time you can
see both of these attributes in the same element. If you want, you can also change the color
of your text for that we can say text color. And we can pass a color what that color should
be defined inside another XML file called color. Right now we have three colors, this
color, accent, color, primary and color primary dark. Basically, these are the names of some
corners. Let's select the first one. And as you can see the color has been changed successfully.
But where does this color dot XML file locates in our project? Well, if you want to take
a look at that, you can open the project pane. Inside the Resources folder, inside the values
folder, we have this colors dot XML, you can see that we have some colors in here with
the exact name that we just saw. Don't worry about this, we are going to talk about different
kinds of XML files in future videos. But for now, I just wanted you to see this XML file.
So instead of passing a color from inside the color dot XML file, you can also pass
the hexadecimal value of that color. For that you can search the internet for something
like HTML color picker.
Let's search that. For example. Let's select the green color in here. This one maybe, and
in here, you can see the hexadecimal value. Let's copy that. And instead of facing a color
from inside our color dot XML file, I'm going to paste the value of that color, you can
see that the color of the TextView has been changed to the color that we just selected.
If you remember from the previous videos of this course, we have seen that when we wanted
to have access to all of these UI elements inside our Java file, we needed an ID. Let's
add an ID attribute for this TextView as well, let's say ID, the convention is to say add
plus ID slash. Let's select that. And let's define some ID. Let's say txt welcome. The
ID attribute should be unique inside every XML file. It means that two different elements
inside the same XML file cannot have the same ID. Later on we will use this ID in our Java
file in order to have access to this TextView there are much more attributes to text us
but I think if we continue we are going to get word for that. I'm going to move on from
this TextView. And I'm going to define a button after this TextView. For that, once again,
I can open an anchor bracket. And in here I can say button. With this capital B, let's
select the first option. Once again, we have these two mandatory attributes. And in here,
let's talk about the difference between this wrap content and match band. Right now, if
I select match parent, let's also select match parent for this height, you can see that this
button is covering all of this screen. And that's because we have set the width and height
to match parent. If you want, you can change this height for example to wrap content. And
here you can see the difference the width is matching the parent but the height is wrapping
the buttons content. Let's also change the width to wrap content. Beside wrap wanted
and match parent, you can always pass the values manually, for example, I can say 150
DPI, you can see that the width has been changed. But this is highly discouraged because our
application is going to be installed on devices with different screen sizes. Sometimes if
we put some numbers for these, if we pass the values manually, we may not know the exact
position of every element. And also sometimes our elements might not fit in the screen in
some devices. So for that matter, I'm going to change this font to wrap content in here.
Also, now that I have talked about different screen sizes, I'm going to say that in here
inside this preview, you have this option called devices for preview. If you click on
that, you can see a drop down. And from here, you can select different screen sizes. For
example, let's select this font, and you can check the looks of your application in different
screen sizes. For example, let's take a tablet, you can see that it's somehow different. Don't
worry about this. Later on in the course we are going to cover how we can have a consistent
look for our application in different screen sizes. I just wanted to mention these options
above in here. Like for the text view, we have a text option for the button as well.
Let's say text. And let's say Say hello. Similarly, we have the option to add some margins. But
I'm not going to do that right now. Instead, I'm going to move this button to below this
TextView. For that we have an attribute called below this one here layouts below. And we
need to pass the ID of the other element. For example, in this case, we need to pass
the ID of this TextView I can say at plus ID slash. And I can say txt welcome, you can
see that the button has been moved to below this TextView. But this is not entirely but
we want, we also need to center it horizontally. Or that once again, we can see center horizontal
and we can pass through. Let's also add a margin top.
Let's say margin top. And let's pass for example 15 dP, that's much better. For both of these
texts here and button you have another attribute called background. Let's also see that you
can say background. And for example, we can pass a color, let's pass the first color,
you can see that the background color of this button has been changed somehow, you have
the same option for the TextView. If you want you can apply that. I think this is an ugly
color. So I'm going to change that. Let's see the other colors. Let's select this primary
dark. Nope, this is not my color. Okay, this is better, you can always add the hexadecimal
value of the color that you want in here as well. For different UI elements, we also have
another attribute called on click we have seen the use of this athlete. When we have
created our first Android application, you can guess it, we are going to set an event
listener for this button and we are going to see what happens when we click on this
button. So let's say on click, and let's pass the name of some method. For example, I'm
going to see on Hello btn clicked. Right now you can see that we are getting this red warning
and that's because we need to create this method inside our Java file. Let's finish
creating our button by using this slash. And let's create this method inside our main activity
dot java file. So let's open that file. In order to open some files, I can always switch
to this project thing. Or alternatively I can press the Shift key two times You can
see that when I do that I have a search menu. For example, in here, I can search for main
activity. And if I press Enter on the first option, we can open our main activity dot
java file. This is just a helpful shortcut that we can use from time to time. Okay, we
need to create the onClick method inside the scope of our class and outside of the scope
of this onCreate method. If you remember the signature of that method, it was something
like this, we needed to set the access modifier to public, the return type was void, the name
of the method is going to be exactly the name that we have passed in our XML file, let's
say on Hello btn clicked. For the inputs of this method, if you remember, we had a view
object with capital V. This view object is not important right now we just need to know
that we need to receive it as the input of this method. Let's fast view as the name of
this input. And in here, we can have the logic to handle the click listener on our button.
Also, if you have noticed, when I added this view, and your import has been added above
in here, let's also minimize these imports for now. This is one way of creating event
listeners for buttons. And also for that matter for different UI elements. We have other options
in which may be more useful from time to time. We'll take a look at them in a minute. But
in here, let's quickly write some logic. For example, in here, I'm going to change the
text of my text view. For that, first of all, I need to have access to my text view. I can
say text view, if you take a look at your inputs, you will see that this TextView from
the widget package has been added to our class. If you remember I said that equivalent to
every UI element. You have a class in Java. Okay, let's name this TextView. txt welcome.
And let's initialize it, we can initialize it like this, we can say find view by Id like
this set content view method, this find view by ID is also another inner method inside
every activity. Right now this find view by ID method is waiting for some integer, and
the integer is going to be the ID of our elements. And here is how we can pass our ID for example,
we can say our.id. Once again, if you remember, I said that these are in here stands for resources.
It's a special class in Java, and it will give us the access to all of the static contents
in our project. So in here, I can say our.id dot, let's pass the ID of our text here, which
was txt. Welcome, I believe now that we have access to this txt, welcome. We can do some
operation on that. For example, I can say txt. Welcome. That's it. txt. We have seen
this method previously. And inside this method, I can pass a text. Let's say hello again.
Let's run the application. And let's see if we can change the text of this TextView. I'm
going to run the application on pixel three API level 29, you can select whichever device
that you want. Okay, right now we can see the layout that we have created so far, which
is honestly oddly, but don't worry about that we are going to work on on our designing power.
If I click on this button, you can expect what would happen we are going to change the
text from hello to Hello again. Okay, there was nothing special. In this video, I just
wanted you to have another look at what we have done in the first section of this course,
which was to create your first Android application, I just wanted you to have a better understanding
on what you have done in that video. Also, we have learned about some other attributes
in here as well. Okay, in the next video, we are going to continue our talk on different
UI elements. For example, we will talk about edit texts. And also we will talk about different
ways that we can implement an onclicklistener for our button. Okay, see you in the next
video. In the previous video, we have seen one way of creating an onclicklistener for
our button. There are two more ways and in here we are going to see them. For example,
in our layout file, let's quickly create a button. But before that, I'm going to change
this constraint layout to a relative layout. Let's delete this text and let's create a
button. That's a wrap content and wrap content for the bits Android. Let's give it a text.
For example, let's say Say hello once again. Let's say center and parent. And let's pass
through, we have seen these in the previous video, so I'm not going to discuss them. Also,
if you're going to need an ID, let's say btn. Hello, you can pass whatever you want. So
in the previous video, when we wanted to create an onclicklistener, we would have add this
onClick attribute. In here, we need to pass the name of the equivalent method inside our
Java file. But in this case, I'm going to use another approach. And for that, I'm going
to delete this attribute. Okay, let's switch back to our Java file. And let's define this
button. Inside my onCreate method, I'm going to say button. Let's name it btn. Hello, is
equal to find view by ID dot btn. Hello. For setting an onclicklistener for your button,
you can use the equivalent method you can say btn Hello, dot set onclicklistener. You
can see that we have this method inside the parentheses we need to pass an interface.
In this case it can be new view dot onclicklistener. If you take a closer look, you can see that
this is an interface. Okay, now that I have passed this new interface in here, this onClick
method has been overrided. And inside this onClick method, I can write my code. For example,
if I want to print something into my console, I can say print. Let's just say hello, let's
run the application. And let's see if we can see any result. I have opened this Ron paneling
here and if I click on this say hello button, you can see that Hello is being printed in
here. So this way, by using this set onclicklistener method on our button, we can define the onClick
listener. Inside the parentheses of this method, we have passed this new view dot onclicklistener
method. But there is also another way, let's quickly see that. So instead of passing a
new interface in here, I'm going to implement this exact interface in the declaration of
my class. If you remember from the Java session, we can implement interfaces like this, we
can say implement, let's say onClick listener. There are two options in here. The first one
is coming from Android dot view that view package and the next one is from another one,
we need the first one. When we implement interfaces, we also need to implement the methods, we
can do that by pressing Ctrl plus by we need to implement this onClick method. As the input
of this onClick method, we are getting a view object.
And now we can use this view in order to create different cases. For example, we can say switch
on view, or V in this case, it's his name. Let's say that get ID, we are going to create
our switch depending on the ID of this view object. And after that I can say case, in
case it's our.id dot btn. Hello, the exact ID that we have passed in here. In case this
is the ID let's write some code. For example, I'm going to copy this line of code from here.
Let's also add a break and also a default case. Now that I have implemented this method
at the declaration of point class, and also I've implemented this onClick method, I can
remove this btn hollow dot set onclicklistener method. Let's write it once again, let's say
btn Hello dot set onclicklistener. This time instead of saying new view dot onclicklistener
I can say this. If you remember from the Java session, this refers to the current object.
Right now we are inside this class which implements this interface. So this method down in here
is happy with accepting this as the input. So how's the follow things in here? First
of all, we are setting the onclicklistener and we are passing the class itself as the
interface. Whenever the user clicks on this button, this onClick method will be triggered
a view will be passed to this method in this case it's a button later on we will talk about
that why a view and a button are equivalent. We are getting the idea of this view and we
are creating a switch statement. In case the ID of this view is our.id dot btn Hello the
ID of our button. We are printing this Hello in here. If we had other buttons or for that
matter other views, we can create different cases for them in here as well. Let's run
the application once again and let's see if we can see the same result. Once again if
I click on this say hello bata we can see that Hello has been printed into are on Council.
Also, I don't like to come down in here inside this round in order to test my application
means that I'm going to show something to the user in our application. For that, instead
of using this system dot out dot print line, I'm going to use another option called toast
messages. Let's search for those messages. And let's see what those are like. So basically,
a toast message is something like this. This is not a permanent message, and it will be
disappear after a period of time. Let's quickly see that how can we define this in our code.
So in here, I'm going to delete this line of code. Instead, I'm going to say toast,
you can see that I have two options in here. The first one is a Java class, but the second
one is a template. Basically, templates are provided by the ID that we are using. For
example, in here, if I select this template, you can see that this whole line of code has
been created. So we are using this test class in which has this static method. Inside this
static method, we are passing a context whatever that is, we will talk about context later
on in the course. But for now, so we are using this toast class English has this static method
called make text, we have three inputs for this make text method. The first is a context,
don't worry about the context yet. We will talk about that later on in the course. But
for now, just know that every activity in Android is a context. And because right now
we are inside an activity, we can pass this as our context. The next input is a text.
Basically, this is going to be the text that we are going to show for example, let's say
hello button clicked.
After that, we have this constant in here, which is toast dot length short, basically,
this is just a constant for determining how long do we want our toast message. For example,
if you want to see the other time periods, you can see that we have length long and length
short. Let's select length short. And after this make text methods, we have this dot show
method, which you can guess is going to show our toast message. So before this, we have
created our toast message. And after that, with the help of this show method, we are
showing it to the user. In here we have used the template. But if you don't want to use
the template, you can always create your own toast message. You can say toast This time,
let's select the first one. Let's say make text for the context list past these, we need
the text in here, let's say second text. And after that, we need the constant, let's say
toast that last long dot show. Basically, these two lines of code are the same. We just
can't create them with the help of the ID. Okay, this time, let's run the application.
And let's see what would be the result. So this time, if we click on this, say hello
butter, you can see that some toast message is showing down in our screen. And after that
it will be disappeared. I think those messages is much better. For our testing purposes.
It's much better than this round pain down in here. Okay, let's close the application
in here. And let's talk about this view that we are receiving in here in a logical way.
Instead of this view, we need to receive a button because we are creating our switch
statement based on the ID of our button. But there is a point in here, if you press down
the control key. And if you click on this button class, once again, if you press down
the CTRL key and if you click on the class that this button is extending, in this case,
this text view, you can see that this text view class is extending the view class. It
means that after two times of inheritance, our button is somehow a view view just extended
the view class two times. So at the end, a button is a view. If you remember from the
Java session when we have talked about inheritance. For example, when we extended the animal class
and we created our dog class, we said that a dog is an animal. In this case, a button
is also a view. For that matter. Every UI element is a view, like text views, edit text,
and also other UI elements that we will talk about later on in the course. So this in here
proves that we can create an onclicklistener for our TextView and other UI elements as
well. So this onClick method is not limited to our button. It's also worth mentioning
that when you create your own listener this way, by implementing the interface in your
class declaration, you just need to create one onClick method for all of your UI elements.
For example, if I had another method down in here, I could have passed the ID of that
button to another case. And inside that case, I could have write my logic. So I can have
as many methods and for that matter, as many onClick listeners for my view elements, also,
this onClick listener is one of the available events, listeners, we have others as well,
if you want, you can take a look at them by saying btn Hello dot set, let's say set listener.
And you can see a lot of these options we have onClick listener we have on direct listener,
you can guess the meaning of each one of these, we have set on hover listener in case if we
hover over our button, we also have another useful one. on long click listener, it means
that if we like press on a button, that's great. Let's see this one. This time, instead
of implementing the on long click listener in my class declaration, I'm going to use
the easier way by saying new view dot online click listener.
This one, you can see that in here we have a Boolean as the return type of this online
click method. This Boolean is just the callback indicating if we have long press our button.
If you want your code inside this method to work fine, you need to pass through. Let's
write some logic. In here. For example, I'm going to say toast. Once again, we can see
this context in here. But the format is somehow different. Because right now we are inside
an interface, we are inside this online click listener interface. If we pass this, this
will refer to the current object, which in this case is an interface. But interfaces
are not context. And we need to pass our activity. For that matter, we can say main activity
dot this, don't worry about this main activity that this if you didn't get that I will talk
about that later on in the course when we have talked about activities. But for now,
just let's pass this as our context. And let's create a text. Let's also change the time
period in here to Lance long. I think I need to delete this case from here as well. Okay,
let's run the application. So like before, if I click on this button, you can see that
we are seeing this message Hello button clicked, which is a simple onclicklistener for our
button. But if I press my button and hold the key, you can see that we are seeing this
long press message. So this time, instead of this onClick, we have executed this set
online click listener method. You can also check the other event listeners for your view
items. Later on in the course we will check a few more of them. But in general, you can
say btn Hello dot set listener. And you can use each one of these that you want. Okay,
let's move on from the event listeners. And let's define another UI element in our layout
file. Before this button, I am going to define an edit text. Let's say Edit Text. For the
width, that's a match fine this time. And for the height, that's a wrap content, you
can see that it's something like this, if you click on that this is our edit text. Let's
also add a text in here. Let's say name and name has been added. That seems to do better.
Let's also center our edit text, I'm going to say centering parent, and I'm going to
pass through. But right now we have a conflict between this edit text and our button. For
that I'm going to move this button to below this edit text. But before that, I need to
give an ID to this edit text. Let's say ID. Let's just name it edit text name. Let's finish
creating this edit text. And then in here inside our button element, I can say below
or to be a specific layout below. And I can pass the ID of my edit text before that I
need this ad plus ID slash. That's the Edit Text name. So this way, we have moved this
button to below our edit text. I think it's better to have some margins. That's a margin
top. And let's pass 20 dP, that seems better. Okay, let's switch back to our edit text.
I don't like the text inside my edit text. Because if I run the application, and if I
try to type something in here, first of all I need to delete this text for that instead
of this text attribute. I'm going to use another attribute called hint. I think we have hint
at the first videos of this course. Let's just pass name. This time it's much better.
So whenever I click on this edit text, and if Try to type something, this name hint will
be disappeared. Also, if we want to decrease the width of our edit text, we can pass a
number in here, for example, we can say 250 dP, which is better in this case. But if you
remember I said that it's never a good idea to pass your width and height manually, because
in different screen sizes, you cannot predict the size of your different elements. For that,
once again, I'm going to change this font to match that I believe it was. And after
that's done in here, I'm going to add two margins, let's say margin left 250 dP, for
example, I think it's better to change it to 100. And also, let's add a margin right
of 100.
Adding different margins is one way but we also have another way, let's we can see that
as well. So I'm going to delete both of these margins. Instead, at the declaration of my
relative layout inside the opening tag. As you can see, this is our closing tag, and
this is our opening tag. Inside the opening tag, I can define an attribute called padding,
once again, like margins, and we have multiple paddings. We have padding bottom padding left,
right top, let's just use padding in here. And let's pass 100 dP. So as you can see,
the size of our edit text has been changed, the padding and the margin are a lot like
each other. Just there is one difference. When you are adding a margin. For example,
in the previous case, for our edit text, the margin would be added from the end of our
element, for example, from here to the end of our screen. But when you're adding a padding,
depth padding, we'll add some space from inside your element. For example, in this case, the
element is this relative layout itself. And we have added the padding from inside the
relative layout. So once again, margin adds the space from the outside of the elements,
but the padding will add the space from the inside of the element, you can use both of
them depending on your need. Let's see what other attributes do we have for our edit text,
like a TextView, we can change the color for this edit text as well. For example, I can
say text color. And let's pass this color that we have in our color resources. Right
now we do not have any text inside our edit text. But as soon as we type something, the
color will be changed to this color that we can see in here. We can also style the text
by saying text style this one, we can say italic bold normal. Or if we want to use two
sides at the same time, we can add a pipeline. There is another attribute that we didn't
have in text views. And that's called lines. For example, there are times that you may
need a bigger input from the user, you can specify the number of lines in here. For example,
if I say four, for example, now our edit text is going to accept four lines of inputs. In
this case, I'm not going to use these lines. So I'm just going to delete that the default
number for different lines is one. With edit text, you can also define the kind of input
that you're going to receive. For that you can say input type this one, and you can pass
one of these for example, if it's a date that I'm going to receive, I can pass a date or
maybe this date time. If I'm going to receive a number password, I can pass this number
password. If it's just a simple text, I can pass this text, we have other options, for
example, for different emails, and all sorts of options that you can see in here. For example,
if I select this text password in here, and if I add some text instead of this hint, let's
say text. You can see that when I typed the text, it will change it to these dots. Probably
you have seen this on your phone when you are trying to log into some application or
some website, this will add some level of security. Also when you define it to email,
this text email address when the user clicks on your edit text, the keyword will be prepared
to type the email address. I'm not going to define any specific type of input so I'm going
to delete this one in here as well. And let's also change this one to him. Okay, I think
that's enough about edit text. We have all sorts of different options in which you can
check them from here. As you can see, there are 1000 of these attributes. Okay, below
this button. Let's also define a TextView. I'm going to say TextView wrap content and
wrap content. Let's give it an ID, let's say. txt Hello. After that may be some placeholder
text. Let's just say hello. Let's also move it to below this button, I'm going to say
layout below. Also, it's worth noticing that if you are not using a relative layout, for
example, if you're using a constraint layout, you do not have this layout builder option.
This is one of the features of this relative layout. We will talk about that in future
videos. Okay, in here, let's pass the idea of our button, let's say btn. Hello. Also,
let's move it to center horizontal.
And let's add some margins. Margin top of maybe 30 dP, let's increase the size of this
text by saying text size. And by passing I believe 20 would be fine. I'm almost done
with this layout file. Let's just go top in here into this code. And let's select this
reformat code in order to rearrange our code so that if I publish the code, you will have
the same code that I'm writing in here. Okay, let's switch to the main activity. And let's
define all these three items. Let's say Edit Text. Let's name it. EDT. txt, I believe it
was name is equal to find view by ID r.id dot edit text name. Let's also define our
text view. Let's name it txt name is equal to find view by ID dot txt name, or txt Hello
was it I think it's better to change the name to txt Hello. But right now there is a problem
with our code. We are instantiating our UI elements inside this I create method, which
once again is the starting point of our application. But we are going to need this to UI elements
inside this onClick method. Because we are going to change the value of this TextView
whenever the user clicks on our button. And if I try to have access to this txt, hello,
for example, from inside this method, if I type txt Hello, you can see that I'm getting
a red warning, you can see that the ID cannot resolve this symbol, it means that inside
this method, we do not have access to this TextView. I'm not sure if I've mentioned this
in the Java section. But the problem in here is occurring because of the scope in Java.
So right now the scope of this TextView is inside this onCreate method. And outside of
that we cannot use that if we want to have access to this TextView. Inside both of these
methods, we can say something like this, we can declare this TextView in the declaration
of our class as different fields. For example, above in here, I can say private, let's say
TextView this one. And let's name it. txt Hello. But right now in here, if I try to
instantiate my TextView, for example, if I say find view by ID and I pass my ID, because
this part in here is not a part of this activity lifecycle. We are not going to instantiate
our TextView at all, we need to instantiate our element inside this onCreate method, which
is once again the starting point of our application. So this way, we are separating the declaration
and instantiating of our UI element, I can put a semicolon down in here. And this time,
instead of defining the whole TextView I can use this field I can say txt Hello response
is equal to find view by ID r.id dot txt Hello. Now that I have declared this txt Hello as
a field, I can use it in both of these methods. For example, in here, I can say txt Hello
dot txt. Inside the set text method, I'm going to need the text of this edit text. For that
I'm going to define this edit text as a field as well. So let's say private edit text. Let's
name it really txt name. And then in here, let's remove this edit text. Now I can pass
this edit text for example to the set text method. Let's say hello plus the Edit Text
dot get text. If you remember from the first videos of this course, I said that we need
a two string method as well. We have seen this two string method in different Java classes.
It's just going to generate some text. In this case, if we delete this to string and
if we just pass this edit text name that get text if you press down the CTRL key and if
you hover over this get text method you can see The return type of this get text method
is an editable. We do not know what that is yet. But we just know that this is not a string,
and in here, we are going to need a string. For that we need another level of conversion,
we need to convert the editable to a string. So we can use this two string method. Okay,
I think after a lot of talking, our application is ready, let's just run the application.
And let's see if we can see the behavior that we wanted. Let's type something in here. Let's
say Mesa. And let's press this pattern alone based on our application is working fine.
And hopefully, we understand each piece of the code that we have wrote so far, few minutes
earlier, I said that different UI elements extend the view class, we have seen that for
this button, that's also check the case for this edit text and text, you can press down
the control key, and you can click on this edit text class, we can see that it's extending
the text view, if you click on this text view, once again, by pressing down the control key,
you can see that it's extending the view class. The same is for the TextView itself.
If you press down the control key, and if we click on the text view, we can see that
it's extending the view class. Now that we know both of these are extending the view
class, we can create an onclicklistener for each one of them. And let's run inside this
onClick method. If you want, we can create another case for each one of them. For example,
down in here, for the edit text, I can say Edit Text name, dot set onclicklistener. You
can see that we have this option in here. And because we are implementing the onclicklistener
interface inside our class declaration, I can pass this in here. Later on inside the
onClick method, I can create another case. For example, let's say case r.id dot, let's
say Edit Text name. If that's the case, let's toast some message. Let's say attempting to
type something. Before I run my application, I'm going to delete this toast because I'm
not going to have a conflict between different hosts. So this time, if I click on the btn
Hello, the text is going to be changed to Hello plus the text inside our edit text.
And if we click on the edit text itself, we are going to see this toast message. By clicking
on the text, we can see the toast message down in here that seems to be perfect. Okay,
I think that's enough for this video. In the next video, we are going to talk about some
other UI elements like checkboxes, radio buttons, and radio groups and also progress bar. Just
before I finish off this video, I'm going to say that I will upload the source code
at my website@miko.org slash code. So feel free to check that if you need. Okay, see
you in the next video. The first UI element that we are going to talk about in this video
is checkbox. Let's quickly see that in our layout file. Before everything, I'm going
to delete this TextView. And also I'm going to change the constraint layout to relative
layout. Then in here, we can create our checkbox like this. We can say checkbox for the width
and height, let's say wrapped content. You can see that this little checkbox has been
added in here. If you want we can add a text to this check box as well. Let's say text,
I'm going to enter the name of a movie. So let's say Harry Potter. We can have an ID
as well as let's say ID. Let's name it checkbox hiring. So basically a checkbox is a box that
we can check or uncheck. For example, in here we have an attribute called checked. We can
pass through or false for this for example, if we say true, you can see the checkbox will
be checked. Let's quickly add two more checkboxes. Basically, I'm going to copy and paste this
one two more times. But this time, first of all, I'm going to change the text to let's
say the matrix. And also let's change the idea as well. Also, I'm going to add another
attribute I'm going to move this new checkbox to the right of my previous checkbox. For
that we have an attribute called to write of layout to write of. Let's pass the ID of
our first checkbox, let's say checkbox, Harry. Let's also add a margin left. We can say margin
left and let's pass 15 If you For example, let's copy and paste this one once again,
let's change the text to let's say Joker. Let's also change the ID, let's say checkbox,
Joker. And also let's move it to the right of checkbox matrix.
So this way, as you can see, we can have multiple checkboxes, you can check and uncheck them
by default by setting an attribute of checked, let's change the value for the Joker to let's
say, false. If we want to center this checkbox horizontally, we can add a margin for the
first one. For example, we can say, margin or margin left. And we can pass 25, for example.
But as you can see, this is not accurate. Instead, what we can do is that we can create
another relative layout and move all of these three checkboxes into that relative layout.
Let's quickly see how we can do that. So inside my first relative layout, I'm going to create
another relative layout. Let's say relative layout, for the width and height, let's say
wrap content. Let's also give it an ID. Let's say ID. I'm going to name this fun movies,
relative layouts. Now, if I want, I can center this relative left, I can say center horizontal,
let's pass through. Beside that let's add a margin top, let's say margin top of 20 dP.
In other UI elements normally in here, we could have finished creating our elements
by adding a slashing here. But in this case, I'm not going to do that. Instead, I'm going
to finish creating my first tag my opening tag. For that I can add a left anchor bracket.
And as you can see, this closing tag has been created automatically. Now I can put my three
checkboxes inside these two tags, the opening tag and the closing tag. Let's move all of
them to inside those two tags. If you noticed, you can see that these three checkboxes are
now centered horizontally, and also they have a margin top of 20 dP. That's because the
parents layout which is this relative layout has these two attributes. So inside XML file,
you can have relative layout nested inside another relative layout. Okay, now let's see
what can we do with these checkboxes inside our Java code. For that, I'm going to open
my main activity dot java file. If you're wondering, I'm pressing double shift in order
to see the search menu. First of all in here, let's define our checkboxes. Once again, I'm
going to define them as fields, let's say private checkbox. For the first one, let's
say checkbox, Harry. In here I have two options. First of all, I can create my checkboxes line
after line. For example, I can say private checkbox, and for the second one, we can name
it checkbox matrix. Or alternatively, because the kind of these objects are the same, I
can add a comma in here and I can define my second checkbox. For example, I can say checkbox
matrix. Let's also define the checkbox Joker. And later on inside our on create method,
let's instantiate them. Let's say checkbox Harry is equal to find view by ID r.id dot
checkbox sorry. Similarly for the next two. If you want, you can have a click listener
for each one of these checkboxes For example, I can say checkbox hiree dot set on check
change listener, this is the listener that I need. Let's pass new unchecked change listener.
Once again, you can see that this is an interface. The exact name is compound button that unchecked
change listener. Inside this interface, we have this unchecked change method in which
we receive two elements. The compound button itself, which is our checkbox in this case,
and also this is checked Boolean. So in here, I can create an if statement, I can say if
is checked. Let's show a toast message. Let's say you have watched Harry Potter. But in
the else case, let's show another source message. This time I'm going to say you need to watch
Harry Potter. Before I write the logic for the next two checkboxes, let's run our application
and let's see what would be the result.
Right now Harry Potter is checked but we are not seeing any toast message and that's because
the onclicklistener in here is going to react when we click on this checkbox. For example,
if I uncheck this checkbox, we can see that you need to watch Harry Potter. Let's also
check it. You have watched Harry Potter. But what if we want to see the toast message when
we run the application without any clicking on our checkbox. For that we have an option
here before setting this onclicklistener. We can check that if our checkbox is checked
or not. For example, for that I can create an if statement, I can say if checkbox hiree.is
checked. As you can see, this method is going to return a Boolean. And as its name applies,
it's going to check that if our checkbox is checked or not. So if it's checked, I'm going
to show the toast message. Let's copy it from here. But in the else case, I'm going to show
another toast message. Once again, we can copy it from down here. Let's run the application.
As you can see, when I ran the application without any clicking, I saw that was message.
So from time to time, this is check method is going to be useful. I'm not going to define
an onclicklistener for the next two checkboxes because I'm sure that you got my point. Also,
like in the previous video, instead of passing this new compound button that unchecked change
listener, you can implement this listener at the declaration of your class for example,
in here, and after that you need to override this unchecked, change the method. After that
you can pass this instead of this new listener in here into your set on check change listener
method. But once again, I'm not going to go through that. Instead, let's switch back to
our activity main dot XML file. And let's talk about another UI element. But before
that, I'm going to minimize this relative layouts. The next UI element that I'm going
to talk about is a radio button. That's a radio button, this one down in here. For the
width and height, let's say wrap content, you can see that this is this circular button.
Let's move on to below our relative layout. I can say below or layout below. I think it
was named movies relative layout. Let's also add two margins, margin left of let's say
25 dP and also margin top of maybe 20 db, like checkboxes, we can also have text, that's
a text. For the text, I'm just going to say married. This is going to determine the marital
status of the user. So that's what this text is about. Let's also give it an ID. Let's
say radio button married. Like checkboxes. If you want to check this married radio button,
you can say checked and you can pass through. If you want to have multiple radio buttons,
you can say something like this, you can copy and paste this radio button, but there is
going to be some problem with our code. Before that, let's change the text and ID of this
radio button. For example in here, let's say syngo. And after that, let's change the ID
to RV singer. Let's also move this one to write off our radio button married, let's
say to write off radio button marriage. So the purpose of radio buttons in programming
is that among multiple radio buttons, you can only select one. This is the difference
between radio button and checkbox. When you have checkboxes, you can check multiple checkboxes,
both when you have radio buttons, you can only have one radio button checked. But as
you can see, our program is not functioning right in here. And that's because we need
to group these radio buttons. And we can do that by creating another item in here called
radio group. Before that, I'm going to delete these two radio buttons. Let's say radio group.
This one. Once again, for the width and height I'm going to say wrap content. Let's move
it below our movies relative layout.
Layout below ID movies rely on a margin top maybe of 20 dP. And also let's center it horizontally,
center horizontal to true. Once again, because I'm going to put some radio buttons inside
this radio group, I'm not going to finish creating this radio group. Instead, I'm going
to finish creating this opening tag by adding a left anchor bracket like a relative layout,
you can see that now we have an opening tag and also a closing tag. Inside this radio
group. I can have my radio buttons, let's say radio button, wrap content and wrap content.
Let's give it an ID let's say radio button married a text maybe. And also, let's change
It's default value to checked, let's say checked through. Now we can have multiple radio buttons.
Let's copy and paste this one. So as you can see, I never moved this radio button to below
our first radio button, but it's moved automatically. If we change the ID to, for example, RV single,
you can see that even though both of these radio buttons are checked, but only one of
them is showing the checked value, so when you are using radio buttons, only one of them
can have a checked value. For that, I'm going to delete this checked attribute from the
second radio button. And also I'm going to change the text to single. Let's also add
another one. For the ID, let's say radio button in REL. And let's change the text to in a
relationship. Let's also delete this check attribute. Right now our radio buttons are
vertical. If you want, you can have horizontal radio buttons as well for that inside your
radio group. You can have an attribute called orientation in which you can pass horizontal
or vertical. If I pass horizontal, you can see that now we have our radio buttons horizontally
ordered, I think it's better to have some margins between these radio buttons. So let's
quickly add some margins. Let's say margin left of maybe 15 dP, I guess. Let's add another
one down in here. Okay, now let's see how we can use these radio buttons, our radio
group inside our Java file. For that I do not need to define my radio groups. Instead,
I just need to instantiate my radio group, let's say radio group. Let's name it radio
group marital status. But I believe I need to give an ID to this radio group. And I think
I didn't do that. Yes, in here I can see ID let's say radio group marital status. In our
I create method that's also minimize this if statement and also this onClick listener.
And after the checkboxes, let's instantiate our radio group. We can say radio group, marital
status, fine view by ID r.id dot, radio group marital status, we can have an onclicklistener
for this radio group by saying radio group dot set unchecked change listener. So this
method is going to be called whenever we change the radio button selected, and inside the
parentheses, you can guess I can pass new unchecked change listener, radio group dot
unchecked change listener, which is an interface and it will create this object change method.
One of the inputs of this method is this check ID in which we can use that for example, we
can create the switch statement on this check ID. And we can create our cases we can say
in case it's our.id dot, let's say radio button married. So basically this checked ID is the
ID of our radio button, whichever is selected. So in case if the user selects this radio
button married, I'm going to show a toast message. Let's just say married. But in case
it's our that Id dot, let's say radio button single. Let's show another toast message.
Let's also add another case for RV in well. As usual, let's also create our default case.
And let's add a break. Let's run our application. And let's see if we can see any result.
Right now this married is selected. If we select the single you can see that the married
one has been unchecked. And also we have saw the toast message. Let's select another one.
You can see the toast message which is perfect. Right now we have an onclicklistener for our
different radio buttons inside this radio group. And depending on that button, we are
doing something for example, in this case, we are showing a toast message. But if we
want to do something without clicking on these buttons, we can do something like this in
our code. So before this set on check change listener. I can say radio group marital status
dot get checked radio button ID. As you can guess this will return the ID of the checked
radio button. And I can save that inside an integer. For example, I can say me checked
button. After that, I can create a similar switch statement based on the checked button.
So let's say switch on the checked button. And let's create our cases, I'm going to copy
and paste the cases from here. Let's run the application. And let's see if we can see any
result. As you can see, we are not clicking on any of these radio buttons, but we just
saw the Maritimes that seems to be perfect. In some cases, we might need to check for
the checked radio button before clicking on them. I'm going to talk about another UI elements
in this video. And that's called a progress bar. Let's quickly see that as well. Before
that, I'm going to minimize this radio group. After that down in here, let's say progress
bar, draft content and wrap content. Let's give it an ID, let's say progress bar. Let's
also center it, let's say center and parent. And let's pass through. Let's run the application.
And let's see what our application is going to look like with this progress bar. As you
can see, an infinite progress bar is constantly circulating, this might be useful, for example,
when we are downloading something from the internet. In order to make our progress bar
visible or on visible, we have an option. For example, in our XML file, if we want it
to be not visible, we can say visibility. And we can pass on. Also we have this visibility
attribute for almost all of the other UI elements. If we want, we can change it to gone. But
by default, it's visible. Let's change it back to Bob. Later on in our Java code, we
need to change this visibility to visible and whenever we are done with it, we are going
to change it to gone. Let's see how can we do that in our Java code. Before everything,
let's minimize all of the extra methods and codes. And let's instantiate our progress
bar. Above in here, I'm going to say private progress bar. Let's name it progress bar.
And then in here inside the onCreate method, let's say progress bar is equal to find view
by ID r.id dot progress bar. I do not have any button in here in order to change the
visibility of this progress bar. Instead, I'm going to change its visibility whenever
the user clicks on the radio button singer for that insight the radio group marital status
listener inside the switch case, in case if it's always single, instead of showing the
toast message or after the toast message, I'm going to say progress bar dot set visibility.
And I can say visible means that whenever the user clicks on this radio button in Braille,
I'm going to change the visibility of my progress bar to gone. Let's copy this line of code.
And after the toast message. let's paste that. And let's just change this constant to God
view.com. Let's run our application.
Right now we cannot see any progress bar because in our XML file, we have added the visibility
attribute and we have set the gun value in case if we click on this single, you can see
that we are seeing the progress bar. In case if I click on this in a relationship, you
can guess the visibility will be changed to gone. There is also another form of progress
bar. Let's quickly see that as well. So in my activity main dot XML file, I can define
a style for my progress. But before that, I'm going to change the visibility once again
to visible in order to see the progress bar in our preview. And after that, let's say
style. If I type horizontal, we can see this styling here widget dot appcompat that progress
bar dot horizontal. Let's change the width to match bands so that we can see it better.
And also in here I have another option I can say progress. And I can pass an integer For
example, I can say 30 you can see that the progress of this progress bar has been changed
to 30. This type of progress bar is extremely useful when you want to show the progress
of something for example, downloading a file. Sometimes you may want to show that how much
of the file has been downloaded. You can also set a maximum for your progress. For example,
you can see Max and can pass 100. Let's also add some margin left and right margin left
of 20 dP margin right after ntdp. Okay, now let's switch back to our Java file. And let's
see what we can do with this progress bar. Before everything, I'm going to delete these
two lines of code, because I'm not going to make my progress bar visible or on visible.
Let's also minimize this switch statement and this whole listener for our radio group,
after instantiating, our progress bar, I'm going to create a threat. If you remember
from the Java session, this is the simplest way that we can create a threat. Let's say
threat threat is equal to new threat, we needed to pass a runnable, let's say new runnable.
Inside this run method, I'm going to create a for loop. Let's say for int i is equal to
zero, i less than 10, i plus plus, for every time of looping, I'm going to increment the
progress of this progress bar by 10. So let's say progress bar, dot increments progress
by as you can guess we need to pass an integer in here, for example, let's pass that. After
that. Let's freeze this thread for half a second. If you remember from the Java session,
we have used this way we said thread dot sleep. But this way, there is a probability that
we create interrupted exceptions. For example, you can see that we have an error in here.
And the error says that unhandled exception. There is also another way in Android, I'm
going to use that in here. I can say system clock dot sleep. So this way, we don't need
to create a try catch block. This wasn't available in Java, this system clock is coming from
Android dot voice, you can check that by take a look at these imports. This one in here,
Android dot o s that system collect is the one that we just imported. After creating
our thread, we just need to start it we can do that after the definition of our trade,
we can say thread dot start. So in here, we have created this worker thread in which inside
that we are creating a for loop. And for each time of looping, we are incrementing the progress
of our progress bar by 10. Also, after each time of looping, we are sleeping the worker
thread for half a second. Let's quickly switch back to our layout file. And let's delete
this progress attribute it was just for demonstration. So if I remove this one, you can see that
the progress will change to zero. Let's run the application. And let's see if we can see
the progress of our progress bar. As you can see, we are changing the value of this progress
bar for every half a second. If you want, you can also get the progress of your progress
bar at any moment that you want. For example, in here you can say progress bar dot get progress.
As you can see this get progress is returning an integer which is the current progress of
your progress bar. Sometimes you may need that and here is how you can get that.
Okay, I think that's enough for this video. Once again, like previous videos, I'm going
to upload the source code at my website@maker.org slash quotes. Feel free to check that if you
need and also in the next video. Finally, we are going to talk about the differences
between a relative layout, a constraint layout and also a linear layout. I think it's going
to help you a lot with understanding the layout files better. Okay, see you in the next video.
In this video, we will talk about different layouts in Android. Specifically, we will
talk about relative layout, we will talk about linear layout, and also constraint layout.
Let's start by talking about the relative layout. Like before, I'm going to switch to
text view in my layout file. Let's also minimize this project thing. And let's change this
constraint layout to relative layout. I'm also going to enable this preview so that
we can see what we are doing. First of all, let's delete this TextView. And let's start
from the beginning. So inside the relative layout, all of our UI elements are somehow
related to each other, it means that the place of every UI element is relative to another
layout, or maybe its parent, let's quickly create a TextView and let's see the relativity
in action. Like before, I'm going to say wrap content for the width and height. Let's also
specify a text. Let's just say hello world. As you can see right now the TextView is placed
at the left corner of my screen if I want I can change that. For example you have seen
in the previous videos that I Can center this TextView vertically or horizontally. For that
I can say center horizontal. Let's pass through, you can see that it's being centered. And
this is the relativity that I was talking about. When we set this layout center horizontal
attribute to true. We are defining relativity between this TextView and its parent. Let's
also center it vertically. Or instead of these two attributes, I can simply say centering
parent. Let's also give an ID to this TextView. Let's say txt Hello. Now if I define another
UI element, I can place that related to this TextView. Let's quickly create a button in
here. Let's say wrap content and wrap content for the width and height. Let's define a text.
Like before, let's say Say hello. Also, let's define an ID. Now if I want to move this button
to below this TextView, I can simply say layout below. And let's pass the ID of our TextView.
txt Hello, I can center this button as well. For example, I can say center horizontally.
And let's pass through, we can also have a margin for example, we can say margin top
layout margin top and we can pass some number for example 15 db. Inside a relatively at
you can also have another relative layout. Let's quickly see that for example in here,
I'm going to define a relative layout and inside that relative layout, I am going to
put three text views. After that, I'm going to move this TextView this high level two
below that relative layout. So I can see relative layout. For the width and height. Let's say
wrap content, you can say match parent. Like other UI elements, we can define an ID for
our relative layout. For example, I can say first relative layout. So up until this point,
when we wanted to create our UI elements after setting the attributes, we could have used
this backslash in order to finish creating our UI elements. But in this case, because
we are going to put some other elements inside this relative layout, we need to create an
opening tag and instead of using the backslash, I am going to use the left anchor bracket.
By doing that, we also create this ending tag. And inside this opening and ending tag,
now we can put our UI elements. For example, we can pass our text views, let's say TextView.
Wrap content wrap content, I'm going to fast forward everything for these three text views.
Like before, we can place our two text views inside this relative layout correctly. For
example, we can move them to right of each other. So for this second text view, I can
say to right off, let's say txt name, let's also add a margin left of 10 dP. Similarly
for this third TextView, let's say to write off our txt last name, and margin left of
maybe 10 dP once again, once again, this whole relative layout is placed at the left corner
of our screen. If we want, we can use the center vertical or center horizontal attributes
for this whole relative layout as well. So inside the opening tag of this relative layout,
I can say center for example. And let's fast through let's also add some margin top, let's
say margin top. And that's past 100 dP. You can see that when I use these attributes on
a relative layout level, all of the UI elements inside that relative layout respond to those
attributes. For example, in here we have centered horizontally and also we have added the margin.
Now that we have this relative layout, we can minimize that so that we can see everything
better. And after that, we can move this Hello text to below this relative layout. But before
that, I need to delete the center parent attribute. Once again, you can see that it moves to the
corner of our screen and because we have set the layout below for this button, it moves
as well. So in here I can say layout below and I can pass the ID of my relative left
it was first relative layer. Let's also center it horizontally and let's pass some margin.
Center horizontal through margin top, let's say 10 db. So my point being here was that
inside a relatively at or for that matter inside any other kind of layout, you can have
other relative layouts as well. Some of these attributes, for example, this layout below
are specific to relative layout. And if you're inside some other kind of layout, for example,
a linear layout, and also constraint layout, you cannot use this layout below. During the
course, we will see that each of these attributes are specific to relative layout, before we
move to the next kind of layouts, let's also switch to this design view. At the left pane
in here, you can see this component three, as the name of this component tree applies.
This in here is showing us the hierarchy of different elements in our UI. So for example,
in here, we have this parent relative layout. Inside that we have this first relative layout,
and also txt, hello, and btn. Hello. Inside our first relative layout, we have three text
views. Sometimes when you have a messy UI, you may use this component tree in order to
keep track of different UI elements. Okay, let's switch back to our TextView. And let's
start talking about linear layout. Before that, I'm going to delete all of these. And
let's change the strategy of layout to a linear layout. Inside a linear layout, all of the
UI elements will be placed line after line, let's quickly see that I'm going to define
a TextView. Once again, wrap content and wrap content. Let's pass the text. Let's say hello
world once again. Let's also give it an ID. That's a txt Hello. So the first thing that
is different inside a linear layout is that you do not have the center in Parent Center
horizontally or center vertically attributes when you use linear layout. For example, in
here, I cannot say centering parent, you can see that it's not available. If you want to
center your UI elements, you can have for example, a relative layout inside this linear
layout. Let's move on from this part. And let's add another UI element. Let's say button
wrap content wrap content for the text, let's say Say hello.
And let's give it an ID, let's say btn. Hello, you can see that even though I didn't use
any attribute to move this button to the right of our TextView, it automatically placed to
the right of our TextView. And that's because we are using a linear layout. Inside a linear
layout, all of our UI elements will be placed one after another. If you want, you can change
the orientation of this linear layout so that the UI elements will be placed one after another
vertically. Let's quickly do that. So in the definition of my linear layout, I'm going
to pass an attribute in here, let's say orientation. That's fast vertical, you can see that they
have been placed vertically. There is also another attribute that I'm going to talk about
in linear layout. Let's quickly delete this button. And let's define another TextView.
For the text, let's say name. And for the ID, let's say txt. Like before, do you have
the margins. For example, you can say margin top, and we can pass any number that we want.
But there is also another attribute in here before I talk about that attribute, I'm going
to give some background color for these two text fields so that we can see the difference
better. So I can say background, and I can pass a color, for example, this color accent
that we have in our color resource. Let's also set the color for the first TextView.
Let's say background, and let's fast color primary this thing. So the attribute that
I'm going to talk about is wait. Let's quickly see that you can see this layout waiting here.
And we can pass any number that we want. For example, I can say 20. Let's also pass a weight
to this second TextView. And we will talk about it. Let's say Wait, let's fast at for
example, you can probably guess the usage of this weight attribute right now, it will
give our attribute so weight according to the numbers that we have used. For example,
in this case, the entire width of our layout is 20 plus 80, which is 100. And it will give
the first TextView 20% of the weight of the whole layout. It doesn't matter what number
you put in here. For example, in here, if I say two and for the second one, if I say
eight, it will work the same. Also, you can see this space in here. That's because we
have this margin top if we remove that, you can see that the whole layout will be divided
accordingly. So this rate was another attribute that I was going to talk about. If I change
this orientation to horizontal, you can see that it's working the same. There is not much
to this linear layout, I just wanted to show you that there is value in your layout with
some specific attributes, for example, this orientation and this weight. And in this course,
we are not going to use linear layout that much it's for simple layouts, but you can
use it if you want. Before I remove all of this, I'm going to say that inside a linear
layout, you can also have other layouts, for example, a relative layout or a constraint
layout. Okay, let's remove one of these. And let's talk about constraint layout. Before
that, I'm going to remove this orientation because it was specific to linear layout.
Okay, let's say constraint layout. We have two options in here, visit constraint and
visa dot constraint layout, this is the one that we are going to need. For this constraint
layout, I'm going to switch to design view because I think it's much better to work with
the design view. First of all, I'm going to enable the blueprint so that we can see the
difference. If there is any, you can click on this blue stack in here, and you can select
this design plus blueprint, we will talk about the difference between this design mode and
this blueprint in a minute. Okay, for working with the constraint layout, you can drag your
UI elements like before you can drag, for example, a TextView. And as soon as I did
that, you can see that we have four circles at the four edges of our UI element. Let's
zoom in a little bit, you can see that the four circles are at the four edges of our
UI element. Let's zoom out a bit, I just wanted to show you the four circles. Also, the shortcuts
that I'm using in here is Ctrl. Plus Plus in order to zoom in and control plus or minus
in order to zoom out. Also at any time, you can use Ctrl plus zero in order to have a
fit screen. It's also worth mentioning that if you have a keyboard that has some numbers
and keys for the calculator, the plus minus and zero from the calculator keys are not
going to be helpful in here. So the shortcuts are going to be only useful for when you are
using the upper numbers. And also the upper plus and minus. The most basic thing in a
constraint layout is to constrain your UI elements right now you can see that we have
a red area in here. Similarly, we have the same error in our components three, it says
that we need to constraint your UI element. In order to constraint your UI elements, you
can drag one of these circles to for example, the edge of your screen. So right now we have
added one constraint, if we switch back to our TextView, you can see that we have this
constraint in here. I'm talking about these two line of code in here. Okay, let's go back
to the design view, we can add another constraint. For example, for the right constraint of our
UI element, we can add the constraint to the right edge of our screen. But still this constraint
layout is not happy. It says that not vertically constraint, it means that we need to at least
add another constraint in here. And now the error is gone. And it has been changed to
some warning. If you want to center your UI element into the center of your parent, you
can add another constraint for example, from the bottom of this TextView to the bottom
of the screen. If you want to remove one of these constraints, you can just click on the
circle and you can press the delete key, you can see that the constraint will be deleted.
Let's press Ctrl Z in order to redo that. If you take a look at this attributes painting
here, you can see that you have a slider for this TextView for different constraints. Right
now the number says 50. But if you want you can change that, for example, if you move
it a little bit, you can see that the TextView responds accordingly. It will move according
to this slider. Similar to that we have this horizontal slider in which you can guess we
can move our UI element. So constraining your UI element to the edges of the screen is one
way you can also add constraints to other UI elements. For example, if I have a button
in here, let's quickly add that and let's delete the constraints of this text view all
of them. Let's move it so that we can see everything better. This though, if we want
we can constrain this button to for example, the bottom of this text view, we can see that
it moves accordingly. You can also see that when I click on one of these circles, I can
see all of the available targets for example, I can constrain it to the right or My TextView
sometimes it might not work, and you may want to add that one more time. So this way, we
have constraints with our button to the bottom of this TextView, and also to the right of
this TextView. If we want to make it center, we can add another constraint to the left
of this TextView. And both of these UI elements are now somehow centered. Let's also add a
margin so that we can see everything better. So by clicking on one of your UI elements,
you can see that in the attributes pane, we can add a margin. For example, from top, let's
add 32. If you take a look at this componentry in here, you can see that we have no error
about this button. But we have an error about this TextView. It means that we have all of
the necessary constraints about this button. But we need to add some constraints about
this TextView. For example, we need to constraint it vertically or maybe horizontally as well.
Like before, we can add the constraints to the both edges of our screen. You can also
see that when I add the constraint, this button moves as well, it means that no matter where
this TextView is, we want our button to be concentrated to this TextView. Let's add another
one. Let's also add a margin top for this TextView as well.
I think 32 is not enough. So let's say 100. Let's also add another TextView. And let's
see other kinds of constraints. So constraining your text fields, for example, from top of
this TextView to the top of this TextView. And from bottom of this TextView to the bottom
of this TextView is one way of having two text views at the same line horizontally.
There is also another way, I'm going to press Ctrl Z two times so that we can do the constraints,
we can right click on one of our UI elements, and we can select this show baseline. Now
you can see this baseline, instead of adding the two constraints from top and bottom, we
can select this baseline, and we can constraint it to the baseline of this TextView. This
time, once again, they are at the same line horizontally. Let's zoom in a little bit.
In order to navigate through all of your layout, you can press the space key, and you can drag
your mouse like if you work with Photoshop, you have the same ability, you can see that
both of these text views are at the same line horizontally by adding some constraint to
the baseline. Once again, the baseline is not available by default, you can right click,
and you can select this show baseline. Let's zoom out. So we have seen that we can constrain
our UI element to know both edges of our screen. But what if we want to, for example, have
another line for example, if we want to move these TextView or constrain this TextView
to some other space beside these two edges. For that, we can right click on our layout.
down in here inside these helpers. We have these guidelines add vertical guideline and
add horizontal guideline, you can guess each one of these will create a line. For example,
let's add a vertical guideline. Let's zoom in a little bit so that we can see better,
you can see that this line has been added to our layout, we can move it for example,
we can move it to the right of our screen. As soon as I did that, you can see that we
have some margin from the left side of our screen, you can see this triangle in here.
If I click on that, it will change the margin from left to the right of our screen. Sometimes
this might be useful as well. Right now the constraint of this TextView is the edge of
our screen, let's quickly delete the left constraint of this TextView. And if we want,
we can add this constraint to this guideline, for example, we can add it in here. Sometimes
once again, it might not work. You can see the difference the TextView now constraints
to this guideline instead of the edge of our screen. If you want you can constrain this
TextView to this guideline as well. But before that, we need to delete this baseline constraint.
Let's quickly delete that, sorry, I deleted the whole TextView and let's constraint it
to this guideline and also to the edge of our screen. If you want you can also have
multiple guidelines, we will add another one in a second. So for example, I can once again
right click on the layouts. by going to this helpers. I can add another vertical guideline.
We can see that this guideline has been added and I can use it like I've used this first
guideline. We also have a vertical guideline. In our helpers, we can add this horizontal
guideline. We can Guess it will add a horizontal guideline. Once again, we have this triangle
in here, if we click on that it will change the margins from top to bottom. Let's also
constrain this TextView to our guideline. Sometimes when you're working with different
UI elements in a constraint layout, adding some foam strength might be difficult. For
example, here, we don't have much space. For that, we can select both of the items that
we want to add constraints, for example, this TextView. And this guideline, you can select
multiple UI elements by pressing down the control key. After you have selected both
of them, you can right click, you can go to this constraint in here, we want to constrain
our TextView to which is the ID of this TextView to our guideline, so I'm going to say top
two, let's say bottom of our guideline tree. This way, we can add the constraint sometimes
where there is not much space to work with, you can use the right click menu by selecting
this constraint, you can add the required constraint path for that, once again, you
need to select the path of items that you want. For example, this takes you on this
guideline. Or if you want to add the constraint from this TextView to this button, for example,
you can select this TextView and this button. Right now, if you have set the left constraint
of this TextView to this guideline, we can also move this guideline for example, we can
move it accordingly to right and left, you can see that once I removed this guideline,
the UI elements move as well. But depending on your system, this operation in here might
be a slow because as you can see, where we are tracking the live response of our UI elements.
If you want, you can disable this live rendering, you can go to this icon here. And you can
uncheck this live rendering. Let's zoom in a little bit. Now if I move this guideline,
you can see that the UI elements themselves do not move. But we can see the outline of
every UI element. So in some cases, when you do not have all of the required resources,
for example, some large amount of memory RAM, you can disable this live rendering. But in
here, you can see the difference between this blueprint and also this design view, you can
see that when I moved this guideline, the UI elements inside the blueprint view are
moving accordingly. And that's because the blueprints view do not render our UI elements
at all. It's just some blueprint of all of our UI elements. Later on, when we add some
image view to our layout, we will see the difference much better. Okay, let's move on
from this part. Sometimes when you're creating your layout files, you may want to have some
sample data, or as we might call it in programming, you may want to have some placeholders. For
example, right now, the placeholder for this TextView is this text view. If we want, we
can change that, we can right click on our element, we can go to this set sample data.
And we can select some text, we can see that we have this cities for example, it will change
the text of our TextView to Shanghai, for example. Let's see others. For example. Another
popular choice is this date in here, you can see different formats, we can select that
and we can have some sample data about the date. The other one is lorem ipsum. If you
search on the internet for sample text, you probably get this lorem ipsum text. Let's
select that. And we can have some sample text. And according to that, we can have this text
on our layout, we also have the Lorem text. Let's quickly see that.
So instead of Lorem random, we can select this Lorem, which is basically this Lorem
text. Before we finish off this video, let's also see the sample data for this image view
as well. I know that we didn't talk about image view so far in the course, but we will
talk about that in the next video, we can drag some image view to our layout. First
thing we need to set some sample data for our image view. For example, we can select
from the avatars. Right now this is the avatar that by default we have selected for our imagery.
If you want to you can change that by right clicking and set some sample data, we can
select another avatar or we can select one of these for example this background. Let's
select another avatar. For example this one like before you can constrain this image view
for example to the both edges of your TextView. Let's quickly do that. If you take a look
at the attributes painting here, you can see that for the layout width, we have set the
width to content, we can change that, for example, we can say match constraint. When
we do that, the width of our image view will change according to its parent constraint,
which is this TextView in this case. But there is also another difference when we change
this layout width to match constraint. When we do that, if you take a closer look in here,
inside this constraint widget, you can see that we have this line in here. Let's click
on that. And once I do that, you can see that this ratio in here has been added. It's useful
for when you want to show some images from the internet, when you don't know the size
or the ratio of your image. Right now, the ratio is one to one, if you want, you can
change that, for example, you can say one to two, you can see that now the ratio has
been changed, or let's say two to one, the ratio of our image view responds accordingly.
Also, you have the same option from above in here layout constraint ID, which you can
change this to 212. Once again, one to one. Also know that you have used this image view,
you can see the difference between this blueprint and this design view, you can see that in
the blueprint view, we are not rendering the image view. And in the blueprint view, we
are not rendering our layout. It's very useful for when you're working with constraint layout,
especially if you do not have the required resources. For example, if you do not have
large amount of RAM, you can always go off to this blue stack icon. And you can select
this blueprint so that you have only the blueprint. Okay, I think that's enough for this video
in the next video. First of all, we are going to work with this image view element. See
you in the next video.
In this video, we are going to talk about images in Android, and I believe it will be
a short video. As always, let's switch back to our layout file. And let's change this
constraint layout to a relative layout. You can keep working with constraint layout, I'm
just more comfortable with this relative layout. First of all, I'm going to delete this TextView.
And let's create our image view. This time instead of wrap content or mass filing for
the width and height, I'm going to put some number for example, I'm going to say 150 db.
Right now, you cannot see any image in your layout file in this preview because we do
not have any placeholder. But you can see the outline of your image. If you want to
have some placeholder you can say something like this, you can say source this src. And
you can add a sub image from your project. By default, you have some images in your project.
For example, in your driver folder, you have this IC launcher background. Let's select
that, for example. And as soon as I select that, you can see that we can see this green
image in our preview. Later on, we will talk about this driver folder and other folders
that are related to images in Android projects. So this is going to be a placeholder. And
if you want we can later on change this image in our Java file. Let's also give an ID to
our image view. Let's say ID, let's name it might image. Let's also move it to the center
of our path. Let's say center your parent and through. So when we have address this
image, we have used this app driver, which means that this image this IC launcher background
lives in our driver folder. If you take a look at your project pane, in the Resources
folder, we have seen the layout folder in which we have this activity main dot XML file.
But beside that we have this driver folder. Inside this driver folder we have some XML
files as well. So if you take a look at this IC launcher background, you can see that we
have this XML file which at the end it result is this image, or to be specific is this drawable.
Inside this XML file, we have some elements like vector path and everything. Don't worry
about them yet. We will talk about them later on in the course but at the end, the result
of this XML file is this driver in here. If you want you can add other drivers in your
driver folder as well. For example, if you right click on your driver folder by selecting
new vector asset or image asset depending on your need, you can add new drivers. Let's
select this new image asset. In here first of all, we can select the type of our image
right now. It's launcher icons if you want you can change that. For example, you can
see action bar and tap icons. So basically this You can add some icons to your project.
First of all, you need to name your icon. This is going to be the name that later on,
you will pass to your images, for example, your image view, you can see that we have
passed this IC launcher background. That name is coming from here. IC stands for icon. It's
the convention to name your icons with this IC. So let's say I see alarm. For example,
in Android Studio, you have some icons. For example, if you click on this icon here, this
Android icon in here, you can see the clip art, you can see that there are all sorts
of icons, and you can use whichever you want. Also notice that these icons are available
under this Apache License, so make sure to check that before you use them in your project.
Because I've named this icon Ico alarm, I'm going to select one of these alarms. For example,
this one, let's select okay. And in here, you probably can see the icon, it's somehow
void. If you want, you can change the color by opening this drop down and select a custom
color. For example, let's pass in black.
Now you can see the alarm better. Also notice that there are multiple sizes of this icon.
And that's because in different screen sizes, the Android system is going to select one
of these sizes. For example, if it's a larger screen size with a large pizza rate, it will
select one of these larger images similar to that if your application is going to run
on a smaller screen size device, the Android system will select one of these smaller icons.
Okay, let's select Next from here. Once again, you can see that in your Resources folder
inside the driver folder, multiple images, multiple driver files with multiple qualities
will be added. And you can also see the output file this is going to be the XML file when
you create your driver file. Basically this at the end is going to create that alarm icon.
Let's click finish in here. And if you take a look at your driver folder, now you can
see that we have this icy alarm, which once again is presented with five different qualities.
Now in our activity main dot XML file, instead of passing this IC launcher background, we
can pass IC Allah. So this way, by adding some deriva into your driver folder, you can
add some images to your project. Similarly, you can add some images into your mipmaps
folder as well. Right now we have some images in here, for example, we have this IC launcher.
And also we have this IC launcher around which at the end we'll create around it icon. If
you want to add some extra images into your project from for example, let's say your computer,
you can add them into this magma folder. For example, I have prepared this PNG file, I
can copy that that's a copy. And in my micpa folder, I can paste that. Let's select the
directory. And also Let's name our file. It's also very important to note that when you
are importing some images into our project, the name of your map file, the name of your
PNG or JPG file shouldn't have any spaces. And also for that matter, they shouldn't have
any awkward cases. So the naming here I think is fine. Let's press OK. And as you can see,
this maker dot png has been added to my lead map folder. Now that I have this image into
my project, I can address that from inside the activity main dot XML file. For example,
in here instead of saying iclm, I can say me. But because we have added that file into
our map folder, we are getting a red warning because we are saying as driver. For that
I'm going to delete all of this. And I'm going to say add map. And I can add this makeup,
we can see that the logo has been added into our project successfully. So far, we have
added these images and drivers in our project in this Android view. Let's also check the
project view. And let's see if we can locate different images in the project view as well.
So you can guess that it's in our app folder inside the sources folder. Inside the main
folder inside Resources folder, we can see all these drivers and mipmap folders. Once
again, each one of these is going to respond to the quality of the screen of the user's
device. So for example, if we want to locate this macro dot png because it was a high quality
image, I believe it will be inside this week map x x hdpi. As you can see it's in here.
But if you want to have an application that response to each one of the screen sizes and
the screen qualities correctly, you need to create images for each one of these qualities
and add them into your project. Right now, if you take a look at, for example, this mdpi,
you can see that inside this folder, we do not have any macro dot png, it's not going
to cause any problem in order to show the images in different screen sizes. As long
as there is some image with the name maker dot png, it's going to show something to the
user, but it may cause some bad user experience. Okay, let's switch back to our Android view.
And let's talk about how you can change the icon of your application. But before that,
let's quickly run the application. And let's see what is the current icon for our application.
Right now you can see that the name of this application is images. And if you take a look
at the list of your applications on your device, you can see that the logo of these images
is this rounded Android icon. This is the icon that we have seen inside our MC map folder.
So if you take a look at your map folder,
then in here we have this IC launcher round, which is this icon, which at the end will
be somehow rounded. And if you want to change that you can go to your manifest folder, you
can see that we have this Android manifest that XML file. If you click on that, you can
see some general information about your application. later on. We will talk about this manifest
file and also other XML files in your project. But for now, you can see that inside this
application tags, we have two attributes this icon and also this round icon. Both of them
are addressing the IC launcher icon for now, if you want you can change that, for example,
in the devices that you are not going to show around the icon, you can change this, let's
quickly change this one and let's address our makeup. Similarly for the round icon,
I'm going to pass me but let's run the application. And let's see if we have changed the icon
successfully. If you take a look at the list of your applications, once again, you can
see that the icon for this images application has been changed. Okay, I think that's enough
for this video. In the next video, we are going to start working on the list view and
also a spinner. So in the next video, we are going to show how can we have a list of different
items. See you in the next video.
In this video, we are going to talk about how we can show a list of different items
in our application. For example, at least have different cities or at least have different
students. For that, first of all, I'm going to start talking about ListView. ListView
is one of the options that we can use in order to show a list of different options. For that,
first of all, I'm going to close this java file for now inside my XML file, first of
all, I'm going to delete this TextView. And after that, I'm going to change this constraint
layout to a relative layout. If you want, you can work with constraint layout, but I'm
more comfortable with relative layout. After that down in here, I'm going to say list view.
For the bid standpoint. This time I'm going to say match parent. And as soon as I do that,
you can see that in here in our preview, we can see a list of different items on our screen.
These are just some sample data indicating that we are going to use a ListView. I'm also
going to add a margin top, let's say margin top. And let's say 100 dP, let's also give
an ID. But before that if you take a closer look in here, I'm not sure that if you can
see this scroll bar at the left pane in here, but it indicates that we can scroll our ListView
in case if all of our items do not fit on the screen. So if we have a large amount of
items, we can scroll over our list view. Okay, let's give an ID to our list view. Let's say
ID. I'm going to name it cities ListView or cities list. And I think that's enough for
now. Let's quickly switch back to our Java file and let's initialize this list view.
Like before above in here I'm going to say private list view. Let's name it ListView
or Let's name it cities list. down in here inside the onCreate method I'm going to say
city's list is equal to find view by ID. Also if you have noticed I'm using a shortcut for
this find view by ID method. Instead of typing all of the method I'm using FB shortcut. If
you press Enter or tab you will get the whole method we will talk about these available
shortcuts in IntelliJ and Android Studio later on in the course. Okay, let's pass the address
of our ListView, let's say our.id dot cities list. After that, we need to create the data
that we want to show in our list view. For example, in here, I'm going to create an ArrayList
of different strings. And later on, I'm going to pass it to this city's ListView. For that,
I'm going to say ArrayList. This one in here, of different strings, let's name it cities,
is equal to new ArrayList. And after that, I'm going to add some cities to this array
list, let's say cities dot add. And let's add some items.
In here, I'm adding all of these cities to my city's array list manually. But in most
cases, the list of your objects will be coming from the web server or maybe your database.
Because we didn't talk about this stuff yet. We are going to hard coding them and we are
going to pass them manually to our ArrayList. Okay, after providing your data, you need
to pass this data to your ListView. But before that, you need to create an adapter. The purpose
of the adapter is to fetch the data to your ListView. Let's quickly see how we can create
our adapter. There are multiple kinds of adapters, but the one that I'm going to use in here
is this array adapter. This is the simplest one, and we are going to work with that in
here. Let's say array adapter of type string. Let's name it cities adapter. That's equal
to new array adapter. Inside the parentheses of this array adapter, we need three things.
First of all, we need a context, which I'm going to pass this later on in the course,
we will talk about context. But in here, because we are inside an activity, I'm talking about
this main activity and activities are context. So this one in here is going to work fine.
So by passing this, we are passing our main activity as the context. Don't worry about
that yet. We will talk about context later on in the course. After that, we need to pass
a layout file for every item in our list view. So for example, if you are going to show the
name of a city, we need a layout for that city in our list view, we can create our customized
layout, but in here, I'm going to use one of the built in layouts. For that I'm going
to say Android dot r dot layout dot, let's say simple list item. So basically, this is
a built in layout file in which we can use in order to show different cities in our list
view. After passing the context, and also the layout file for every item, we also need
to pass the data. In this case, we need to pass the city's array list. So let's say cities
in here. And now we have our adapter for our ListView. Once again, the purpose of this
adapter is to fetch the data, in this case, this city's ArrayList to our ListView. After
creating your adapter, you can say city's list dot set adapter, and you can pass your
adapter, let's say city's adapter. before running the application, let's quickly review
what we are doing in here. First of all, we have created our ListView inside our layout
file. After that we have initialized it inside this onCreate method in here. After that we
have prepared our data in this case, we have created this simple ArrayList of different
strings called cities. After that we have created an adapter in order to fetch these
cities array list to our list view. And after creating the adapter, we have passed it to
our list view. Let's quickly run our application. And let's see if we can see these items in
our list view. As you can see, we have created this list view and also populated the data
that we have passed in our ArrayList. Also, if we had more data that wouldn't fit on the
screen, the ListView would be a scrollable display of in here for every item is this
simple this item that we have passed in here. If you want to take a look at that you can
press down the CTRL key in Mac it should be command key. And by clicking on the symbol
list item one you can check the layout file. You can see that it's a simple text view with
some attributes. Nothing special in here. You can also make your items clickable right
now if you click on one item, nothing happens. But you can set a listener for each one of
these items. Let's quickly see that. So after passing the adapter in here, I can say city
List
dot set on item click listener this one. And I can pass my interface, I can say new on
item click listener. Notice that it's adapter view.on item click listener, it's different
from on click listener. When we pass this interface, we have this on item click method
in which has some inputs. The one that we are interested in is this position in here.
Basically, this is going to be the position of our item in our ListView. Let's quickly
see how we can use that. For example in here I'm going to show a toast message after clicking
on each one of the items. Once again, when we create our toast message, first of all,
we need to pass a context. Because we are inside this on item click method which exists
in another interface, we cannot simply pass this we need to pass main activity dot this
in order to refer to our context. After passing the context, we can pass our text for the
text in here I'm going to say cities dot get. And for the index in here, I can pass this
position. So this way, we are going to get the city name that we have clicked on. We
are using this position as the index in order to get the appropriate city name from our
cities ArrayList. Also, if you have noticed when I use this cities inside this on item
click method, this final keyword has been added to the declaration of ArrayList. As
a reminder from the Java session, when you add a final keyword at the declaration of
a variable or an object, that variable or the instance of that object would be constant,
it means that you cannot change that instance. But although we cannot change the instance
of this cities, after declaring it as final, we can use all of the methods inside that
object. For example, we can use this dot add method, because we are using this city's ArrayList
inside this on item click method which exists in another interface, the city's ArrayList
needs to be constant. And this final keyword has been added automatically. And if I remove
it right now from here, you can see that we are getting this error down in here. And it
says that your variable should be constant. So let's add the final keyword once again.
Also, let's add another text in here, let's say plus selected. Okay, let's run the application
once again. And let's see if we have set on item click listener successfully. So now if
we click on one of the items inside our list view, for example, this New York, you can
see the toast message in here, New York selected Berlin Berlin selected. Okay, it seems like
our on item click listener is working fine. Once again, this on item click listener is
different from the onClick. listener. If you set the onclicklistener on your ListView,
it's not going to work according to every item in your ListView. Okay, I think that's
enough talking about ListView. You can work around ListView, if you want. But I do not
suggest that because in Android, we have modern solutions for populating a list of different
options. For example, we have RecyclerView, which has a lot of flexibilities. If you take
a look at your layout file inside the design view, you can find a ListView somewhere inside
this legacy. You can see that in here. And it's inside this legacy because these days,
almost no one uses ListView. I just wanted to show how can you create a ListView. The
reason that you may want to avoid using ListView is that ListView does not have much flexibility.
For example, you cannot customize the layout of every item in your list view. You can create
a customized layout for every item. But in recycler view, you have much more flexibilities.
And also, the other issue is about performance. If you have a large amount of data that you
want to show in a list for example, if you have 1000 items. In those cases, you will
see the performance issue when you're working with ListView. recycler view has done a much
better job when it comes to performance. We will talk about recycler view in one or two
videos from now. But let's move on from this part. The other layout for that I'm going
to talk about is called a spinner. Let's quickly see that I'm going to define my spinner above
this ListView. So in here, let's say a spinner. For the width, same height. This time, let's
say wrap content. Let's center it horizontally and also let's add some margin top. Center
horizontal through margin top, let's say 50 dP. Right now you cannot see your spinner
inside this preview very well. And that's because you do not have any data inside your
spinner yet. Basically, this is
Winner is going to create a drop down menu. For example, if you click on this, you will
see a list of different options. We will see that in action in a minute. But for now let's
just pass an ID for our spinner. Let's say students list or students spinner, it's a
better name. Also, let's move our ListView to below this has been a layout below. And
let's pass the ID of our spinner. Let's initialize our spinner in our Java file. Like before,
I'm going to come above in here and say, private spinner. Let's name it a student spinner.
And then in here inside the onCreate method, I can't initialize that. Let's see student
spinner is equal to find view by ID dot, let's say a student spinner. Once again, like the
ListView, first of all, we need to create the list of items, the data that we want to
show in our spinner. For that I'm going to create another ArrayList in here. So let's
say array list of different strings. Let's call it the students is equal to new ArrayList.
After that, let's pass on data to this students ArrayList.
Similar to when we are working with listviews. After providing our data, we also need to
create an adapter. Once again, the purpose of this adapter is going to be fetching the
data into our spinner, we can create our adapter like we have did for the ListView. So in here
I can see array adapter of different strings. Let's name it the students adapter is equal
to new array adapter. Like before, inside this parentheses, we need three items. First
of all, we need the context. After that, we need to lay out five for every item in our
screener. Once again, you can create your customized layout file if you want. But in
here for the sake of simplicity, I'm going to use a built in one. So let's say Android
dot r dot layout dot the layout font that I'm going to use this time is this simple
spinner drop down item. When we created the adapter for ListView. We have used this simple
list item Vaughn. But in here we are going to use this simple spinner drop down item.
After that, once again, we need our data which I'm going to pass my students I released.
After creating your adapter you can see the students the spinner dot set adapter, and
you can pass your students adapter. Let's run our application. And let's see what a
spinner looks like. You can see this winner above in here, it's going to generate a drop
down menu. If I click on this triangle in here, you can see the name of the students
that I have passed into my ArrayList. Once again, these items can be also clickable.
For example, if I click on one of these, we can take some action. Let's quickly see that
how can we create an onclicklistener for each one of these items. So after setting the adapter
down in here, I'm going to say students spinner dot set on item selected listener, this is
different from the listener that we have used for the ListView. For the ListView we have
used set on item click listener but it's not going to work in here. Instead we are going
to use this set on item selected listener. Let's pass our interface let's say new on
item select listener adapter view.on item selected listener and this interface is going
to generate two methods on item selected and on nothing selected. The names are descriptive
on nothing selected means that if you do not select anything inside your spinner, you are
not going to work with this method and we are not going to write any logic in here.
Instead inside this on item selected we are going to show a toast message. Once again,
you can see that we have this position in here in which we can use it in order to for
example get the name of that student. First of all like before we need the context because
we are inside another interface we are going to pass main activity that this after that
for the text I'm going to say students that get as the index I'm going to pass position.
That's a plus selected.
Let's run the application and let's see if everything is going to work. So in here you
can see that even though I never clicked on any item I see the toast message. And that's
because we have used this set on item selected listener. Once again, this is different from
the on item click listener. I don't know why. But we cannot use an item click listener for
the spinner, if you try to use that you will get an exception. Instead, the other option
that you have is this set on item selected listener. Let's see if we can show the toast
message. If we select another item, for example, Sarah, you can see that those messages down
in here. This is one way of showing data inside your spinner. It's useful for when you do
not know what data you're going to show. For example, if you're going to retrieve the data
from a web server or a database, to be precise, in case your data is dynamic. But in case
you know that data previously in case your data is static, you can use another way, let's
quickly see that. So before that, I'm going to open this project pane. Inside my app folder
inside this Resources folder, we have this values folder in here, in which inside that
we have this strings dot XML file, we can create a string array inside this XML file,
and later on, we can pass it into our espinar. Basically, this strings file is going to be
the place that we put all of the static strings. For example, right now you can see that we
have this app name string, in which we have used it inside our manifest file. Let's quickly
see that inside this Android manifest dot XML file, we have this label attribute. If
we click on the label, you can see that it's addressing the string file. And this is the
name of our string from strings that XML file, we know for sure that the name of our application
is not going to change, for example, it's not dependent on some string from our database
or the web server. So we have passed it in here. In the next video, we will talk about
the usages of these strings that XML file and also for that matter, other XML files
in our application. But for now, let's create a string array statically in here, and let's
pass it to our spinner. So inside this strings dot XML file, you can create two different
kinds of items, you can create a string, or alternatively, you can create a string array.
The way to do that is like this, you can say a string array, you need to name your array.
For example, in here, I'm going to say to students after that, inside this opening and
closing tag, I can create different items, let's say item. And inside these item tags,
I can provide the value for example, I can say Mesa, you can copy and paste this item
as many times that you want.
Once again, don't worry, if you don't understand these strings, that XML file, we will talk
about it in the next video. Now that we have created this string array, we can pass it
to our spinner item inside our activity main dot XML file. When we have created our spinner,
you have an attribute called entries. This attribute in here, you can see that we have
these add airy students this is the address of our string array in our strings dot XML
file that's passed that and as soon as I pass that, you can see that we have some sample
data in our spinner. So now that I have passed the data to our espinar, aesthetically, I
don't need to pass them in my jar file dynamically. For that I can comment all of these lines
of code, for example, from here to top in here. But right now we are getting an error
inside this on item selected method. And that's because we no longer have this students array.
If you want to show the name of the student when you select some item, instead of using
the students array list, you can use another way. Let's delete this line of code and let's
create a new toast message. For the text. This time I'm going to say students spinner
dot let's say get selected item. You can see that this method is going to return an object,
let's say get selected item after that we can see.to string. This to a string is just
another level of conversion in order to convert the object that has been returned to a string.
After that, let's say plus selected once again before running the application. Let's review
what we have done. First of all we have created this string array after that we have passed
it to our spinner statically and after that inside our Java five, we have created this
set on item selected listener. Inside the on item selected method, we have created this
toast message. And we are getting the value of every item by using a student's spinner
dot get selected item method. Let's run the application. And let's see if you have the
same behavior. You can see that we have some data in our spinner. If I select breath for
example, you can see the toast message Brad selected. It seems like our spinner is working
fine. So far, we have talked about different layout files, for example, this activity main
dot XML, which is a layout file. In the next video, we are going to talk about other kinds
of XML files, for example, this strings that XML or this Android manifest dot XML. Also,
we have other kinds of XML files. In the next video, we are going to talk about them. Okay,
see you in the next video.
So far, we have talked about different layout funds in our application placed inside this
Resources folder inside the layout folder. For example, we have talked about this activity
main dot XML file, in which we have defined the looks of our layout file for our main
activity. But in Android, there are a lot of other XML files. For example, at the end
of the previous video, we have take a look at the strings dot XML file. Let's close these
days. Let's talk a bit about that strings dot XML file. As I said, it's placed inside
this Resources folder inside the values folder, you can see these strings that XML file. If
you remember, I said that this is the place that you will put all of the static strings
of your application. Let's quickly see what I mean by that. So for example, inside my
layout file, if I define a TextView, we have a TextView. In here, let's give it an ID.
Let's say txt Hello, let's go to our Java file. And let's initialize this TextView.
Let's say private TextView. Let's name it. txt Hello. And then in here inside the onCreate,
let's curricular initialize this TextView. Let's see what would happen if we set the
text of this text a fellow manually. For example, in here, if we say txt Hello dot set text.
And if we pass Hello, you can see that we are getting some highlights in here. And the
highlight says that this string cannot be translated use Android resources instead.
If you want, you can have some more information about this warning, as the suggestion says,
you can press Ctrl plus f1. And you can see a whole dialogue about this morning. Basically,
this in here means that this text is going to be the same in all of the languages. For
example, if you have an application that has some users around the world, this text is
going to be the same in all of the devices regardless of the user's language. As the
warning says you can define this text inside your strings dot XML file. Let's go to see
how we can use this strings dot XML file in order to localize our application. So in here,
I'm going to define another string, the way to do that is to open a tag and say a string.
After that, we need to name our string. For example, in here, I'm going to simply say
hello. And for the value of this string, I'm going to say hello. Now I can pass this string
to my text view inside my java file. Instead of hard coding this text, I can say get a
string. This get a string method like this find view by ID is an inner method inside
every activity, we can use that because we are extending this app compat activity. So
inside these practices, we can address our string file. For example, I can say R dot
string, dot let's say hello, right now the result is going to be exactly as before. For
example, we are just going to show hello to the user. But the option that we have in here
is to create another strings dot XML file inside our values folder. For example, in
here, I can right click on my values folder. I can say new value resource file. I need
to name this new XML file exactly strings, let's say strings. The name is important in
here it should be exactly the same. And in here you have a few options. We will talk
about few of them in here. But the one that we are We're interested in here is this locale.
Let's select that. And let's add it to this chosen qualifiers. So basically this locale
is going to be useful for when you want to expose your application to different languages.
For example, if you know that you have some users from Germany, you can translate your
application this way for those users as well. Let's quickly see that if we can see Germany
here,
you can see that we have this the German D stands for Deutsche in the right hand side,
we can select a specific region, we know that there are multiple countries that spoke German,
let's select Germany. And let's press OK. If you take a look at your values folder,
you can see that now you have these strings folder in which inside that you have two strings
dot XML file, the names are exactly the same, but the second one is for the German language
users. Right now, this strings dot XML file for German users is empty. If you want, you
can define some strings in here. For example, I'm going to define my Hello text in here
as well. Let's see a string. Let's name it Hello. It's very important that this name
would be exactly the same name that you have passed inside the other strings dot XML file.
If you remember in here, we have this string called Hello, we are going to name it in here
hello as well. Now let's pass on value for this string. For example, the German translation
of Hello is Hello, I believe. So inside this strings dot XML file for the German users,
we have a string called Hello. But the value in here is different from the one that we
had inside the default strings dot XML. Now that we have created two strings, that XML
file, because inside our main activity, we are using this get string method and we are
addressing our Hello. If some German language user uses our application, the value of this
TextView will be different in different languages. Let's quickly run our application. And let's
see the difference. Right now the language of this device is English. And because of
that, we are seeing this Hello text. But if we change the language of this device to let's
say German, we should see how though, let's close this application, and let's quickly
change the language of this device. And let's see if we can see the difference. So we can
go to the settings. down in here inside the systems, I believe, or system, language and
input. Let's go to languages. And let's add another language. Let's search for German,
or its equivalent storage, we need to select a region let's select Deutschland. And also,
we need to make the storage as the default language of this phone. So let's move this
to above this English. And now you can see that the language of this phone has changed
to Deutsch. If we run our application, once again, we should see the difference. This
time, you can see that we are seeing hollow instead of Hello. Without changing a line
of code, we now have an application that looks different in different languages. So these
are the strings dot XML file is extremely useful for localization purposes. If you want,
you can add another string file. For example, once again, on our values folder, we can right
click, we can say new value resource point. Once again, we can name it strings. And once
again, we can select this locale. And if you want, you can select another language and
you can do the same process. After creating your application, if you want to translate
your application for another language, you can pass this strings dot XML file to maybe
a translator and ask him or her to change some of the values for your string files.
As simple as that you can localize your application for different languages, you can see that
we also have an error in here this URL is not important. Basically, it says that you
do not have an equivalent string for this app name. You can also change the app name
in different languages as well. But we are not going to do that in this application.
Okay, let's close all of these. And let's talk about another kind of XML file in Android
applications. The other XML file that I'm going to talk about is this colors dot XML
file. Once again, it's placed inside this values folder. Let's take a look at that.
You See, that's right now we have three colors in here. Like strings, this is going to be
the place that we pass all of our static colors. If we want, we can define another color in
here for that we can come between these resources tag. And we can say color. We can name our
color. For example, let's say blue. And for the value, we need to pass a hexadecimal value,
we can get that value by searching for HTML color picker. Let's quickly do that.
For example, in here, if we want a blue color, a lighter blue, maybe we can copy the hexadecimal
value from here and we can paste it in our project. We can see that at the left painting
here we have this blue color. Now that we have this blue color, we can use it in different
places of our application. For example, inside our activity main dot XML file. When we have
defined this TextView, we can change its color. We can say color or to Riverside's text color,
and we can address our color. For example, I can say add color slash blue. This add color
is referencing our colors that XML file this morning, here, you can see that the color
of our text has been changed to this light blue. Instead of searching for different colors
on the internet, you can also define them another way. For example, you can copy this
color accent color. Let's copy that. Let's paste it in here. And let's define avoid color.
But for the value of this white color, I can click on this a square. And I can select a
void color. For example, let's select this white. And as you can see, now we have a white
color without searching the internet. We can get the hexadecimal values for different colors.
Okay, there is not much more to these colors that XML file, I just wanted to show you how
you can define new colors. Let's talk about the other XML file in Android applications,
which is this styles dot XML. But before that, let's close these two. And let's open this
styles dot XML. Once again, it lives inside our values folder. So basically, this styles
dot XML file is the place that we define the theme of our application. Right now we have
one style in here. Later on, we will see that where we have used this, the name of this
style is app theme. But if we want we can override this for example, we can change the
value of some of these colors if we want. Or we can change other attributes. You can
also define multiple styles. For example, let's copy this whole style. And let's paste
it down in here. We need to name our style differently. For example, let's say customized
app team.
And let's override the previous style. For example, we have this parent attribute in
here, which you can guess it's implementing all of the features of this exact theme in
here. If you want, first of all, you can change this parent. For example, let's take a look
at the other ones. Let's say light.we have these no action bar in which we are going
to use in future videos, we have these dialogues, I'm not going to change the parent in here
instead, if you want, you can change some of the values of this color. So in different
parts of our application, we may use this color primary for different texts. The value
of this color primary is right now, this value in here add color slash color primary. If
you want, you can change that, for example, you can say blue the color that we just created.
You can see that now the color primary in these two styles is different. For the first
one is this dark green for the second one is this light blue. Now that you have defined
this customized app theme slide you can pass it to let's say different activities in your
application. It means that different activities in your application different pages in your
application can have different styles. For example, one of them may have some toolbar,
some of them may not have that toolbar if you use another parent. Later on we will see
that how we can do that. Also we will talk about activities later on in the course Don't
worry about activities if you don't understand them yet. So this styles dot XML file is the
place that you define different styles for your application. We will be coming back to
this styles dot XML file later on. In the course, specifically when we talk about material
design, but for now let's move on from this part, I believe you grasp the general idea.
The other XML file that I'm going to talk about is this manifest file, this Android
manifest dot XML file. I believe this is the most important XML file in every Android application.
Basically, this is where you define the general attributes the general features of your application,
you can see that we have this manifest tag. Inside that we have this application tag,
we have seen this icon and round icon previously. Basically, these are helpful for when you
want to define different icons for your application. We also have this label, which is basically
the name of your application that is going to be shown to the user. You also have a general
theme for your application, which as you can see is addressing the style, the app theme
style, if you want, you can pass your customized app theme. It will override all of the activities
in your application. But in here, I'm not going to do that.
Right now, inside the application tag, you have one activity, which is basically our
main activity. So basically, every activity needs to be declared inside the manifest file
inside this application tag as well. This activity in here has been added automatically,
because when we have created our application, we have selected the empty activity template.
But if we don't select that template, one of the things that we need to do is to come
inside this manifest file and declare our activity. Inside this activity tag, you have
this intent filter tag, don't worry about that yet. We will talk about that later on
in the course, I believe in the broadcast receiver section of the course. But probably
you can guess the user of this intent filter from this launcher and main, it means that
this main activity is the launcher activity when we run our application. Don't worry about
all of these yet. These are just some basic properties, some basic features of our application.
So beside activities, you need to define three other elements in the manifest file as well.
So in every Android application, we have four main components, activities, content providers,
broadcast receivers, and services. We will talk about all of them later on in the course.
But these are the four main components of every Android application, all of them needs
to be declared inside the manifest file. So as you can see, this manifest file is very
important. It contains all of the important features of your application, we will be coming
back to this manifest file a lot during the course. So don't worry if you don't understand
all of these new stuff that we are seeing in here. Okay, the next XML file that I'm
going to talk about is menus, let's quickly see them as well. So in your project pane
inside the Resources folder, you can create another directory for different menus. By
default, you do not have that directory like we did have four different values and layout
files, we need to create that directory, we can right click on our resource folder, we
can say new directory, it's very important to name this directory menu. And after that,
you can see that we have this menu folder in here. You can right click on that, and
you can say new menu resource file. This way, you can create a menu dot XML file. For example,
let's name this menu file main menu. Let's create this file. And let's see what does
it look like. Let's switch to text view. In here you can see that we have this menu attack.
Basically menus are useful for when you want to create some sort of menu in your application.
And the way to do that is to come inside this menu tag and create different items I can
say item you can define multiple attributes for your item but the one that is mandatory
and you have to create that is this title. You can say Android column title, and you
can pass a title for your menu. For example, we can say settings in here. You can see that
some menu has been created in here. later on. We will pass this menu to for example,
our main activity to have some sort of menu in our main activity. The other attributes
that I have in here is ID for example, I can say Settings menu. We also can have an icon
for our menu item. But I believe we do not have any icon in our project yet before by
An icon let's quickly create some icons in our driver folder, we have seen how we can
create different icons, we can right click on our driver folder by saying new image asset,
we can create icons. In the icon type, I'm going to say action bar and tap icons. For
example, inside this clip art, let's search for settings icon. You can see that we have
one icon in here, let's add that. Let's change the name to IC settings. And let's change
the color to black. Let's add another icon in our driver folder. Mirror Image asset like
before. Let's name this one icy alarm.
Let's search for alar. Color is fine, let's just create this icon. Now that we have these
two icons, we can pass them in our menu file. For example, in this item, I can say icon.
Let's pass our settings icon icy settings. Right now we cannot see the icon of the setting
in here. But if you want to see that you can define another attribute that's called show
as action. So we have some values for this show as action attributes. If you say always,
it means that we always want our menu items in the toolbar, you can see that we have other
options. For example, we have this if row, it means that place them in the toolbar if
there is room for this menu item. We have also this never it means that always show
the menu items like this and never show them in the toolbar. Let's change the value for
now to always. And let's create another item in here. So I can say item like before the
title is mandatory. And also we are getting a warning for the previous items title. Once
again, it says that you need to define the title in your strings dot XML file. Like before,
this is for the localization purposes. And the best practice is to create this settings
title in your strings dot XML file. In your real world applications, make sure to do that.
But in here for the sake of simplicity, I'm not going to do that I'm going to cope with
this warning. So let's pass a title in here. I'm going to say alarm. Let's give you time
Id let's say alarm menu. After that, let's give you an icon. Let's say icy alarm. Let's
set the show as action attribute for this one to e4. You can create as many items as
you want. But I think for this simple menu item, these two would be fine. Okay, now that
we have created this menu file, let's see how we can pass these two for example, our
main activity. Let's close this file. Once again, it was placed inside our Resources
folder inside the menu directory that we just created. In order to show this menu in your
main activity, you need to open the main activity dot java file. And you need to override another
method in here. So because we are extending the app compat activity inside this main activity,
we have some methods that we can override one of these methods is this onCreate method.
If you come outside of this onCreate method and press Ctrl plus or you can see the list
of all of the available methods you can see that there are a lot of them. The one that
we need in here is called on create Options menu. If you type on create Options menu,
you can see this method in here let's select that. And in here we have this method. First
of all I'm going to delete this statement from here this return super.on create Options
menu and I'm going to create or inflate my menu for that I can say menu inflator this
class in here. Let's name it inflator is equal to get menu inflator this gets menu inflator
is like this, find view by ID method. It's an inner class in every activity. So basically
with the help of this inflator, we are going to inflate or create our menu after accessing
to our inflator we can say inflator dot inflate. For this inflate method. First of all we need
to address the menu font that we just created. For that we can see our that menu that main
menu, the menu that we just created. The second argument is the menu that He's being passed
to this method. That's fast that you can see that the return type of this method is a Boolean.
This Boolean is indicating that if we have successfully inflated our menu, by this point,
we have inflated our menu and after that we can return through. This is all of the logic
that we need in order to inflate or create our menu in our main activity. Let's run the
application. And let's see if we can see our menu in our application.
You can see that in our activity, we have these two items, this settings icon and this
alarm icon. We can also set an onclicklistener for each one of these menu items. Let's quickly
see how we can do that. So we need to override another method outside of this onCreate Options
menu method. If we press Ctrl plus Oh, and if we search for on options item selected,
this method in here if we select that, we can override that this time, I'm going to
keep this super statement we will talk about I'm going to keep that. As you can see as
the input of this method, we are getting a menu item. This is the item that user is going
to click on. And we can use it. For example, to create a switch statement. Let's create
our switch statement. So our switch statement is going to work on the item dot get item
ID. If you remember in our menu file for different menu items, we have set different IDs. Let's
quickly see that in our main menu dot XML file, we have set ID for each one of these
items. And in here, we are going to act upon those IDs. Let's create our switch statement.
Let's say in case it's our.id. That settings menu, we are going to show a toast message,
let's say toast. For the text let's see settings clicked or settings selected. After showing
the toast message in here, instead of adding a break for distribution statement for this
specific case, we can return something. Once again you can see that the return type of
this method is a Boolean as well. This Boolean indicates that if we have acted according
to this item successfully or not. In here, after showing the toast message we are going
to return true. So let's say return true. And let's create other cases. For example,
in case it's our that Id does alarm menu. Once again, we are going to show another toast
message. Let's say alarm selected. In Here I'm showing a toast message. But in a real
world application, you may want to navigate the user to some other part of your application.
But more on that later on when we know about different activities and fragment. Okay, after
showing the toast message, let's return through once again. And also let's create the default
case. In the default case, I am going to return this super statement. Because I'm not interested
in other items besides these two. So do whatever you're doing inside the parent class. So basically,
I can call this super segment from here, I can return it in the default case. Before
we run our application. Let's review what we are doing in here. As the input of this
on options item selected method we are getting a menu item, we can use this item ID in order
to create a switch statement. In case the ID is the ID of our settings menu item, we
are showing this source message setting selected. In case it's alarm menu item ID we are showing
another toast message. But in the default case in case if it's another item beside these
two, we are passing the job to the parent class. Let's run the application and let's
see if we have set the listener successfully. So for example, if we click on this settings
icon, you can see that settings selected. If we click on this alarm, we can see alarm
selected. It seems like you have done a good job inside this on options item selected method.
Okay, I think that's enough for this video. In the next video we are going to continue
our talk about other kinds of XML files available in Android. See you in the next video.
Let's continue our talk about different XML files in Android. In previous videos we take
a look at different XML files for example drawable for its layout files, some files
inside our mipmap folder. We have seen a strange and colors and styles. Beside these, we take
a look at menus and also our manifest file. Let's see what else do we have in here. Right
now our main activity has only one layout file this activity main dot XML. But if you
want, you can create more layout files for this activity. For example, right now, this
layout is for the portrait mode, if you want, you can create another layout file for the
let's say landscape mode. If you don't know what the portrait and landscape modes are,
let me quickly show that. So right now, this is called the portrait mode. If we change
the rotation of our device, and this is going to be the landscape, I think I need to activate
the rotation of this device. Let's go to the certification center. And from here, I believe
this is the option, this auto rotate, we need to activate that. And now if we rotate our
device, you can see that now we have a different look for our application. Sometimes this might
work. But if you have a complex lab file that you want to show it differently in different
rotations, you need to create two separate files for each one of these rotations, one
for the portrait mode and one for the landscape mode. Let's quickly see that how we can create
another layout file for this main activity. So inside my layout folder, on the layout
folder, I can right click by selecting new layout resource file, I can create a layout
file, or alternatively, inside this activity main dot XML file. From here, if you click
on this icon, you can see that we have this portrait selected right now, if you want,
you can create a landscape variation for the layout of your main activity. But before I
do that, I'm going to add another text. And also I'm going to add some constraints to
this layout file. So I'm going to move this new TextView to below my level text. Let's
quickly add some constraints. As I mentioned before, if your items are too close to each
other, you can always select both elements. And you can use the right click menu by going
to constraint and add constraint from here, too. But I'm not going to do that. Instead,
I'm going to add some margins of let's say 500. So for the simple application, this is
going to be the look of my application in the portrait view, go to text views are placed
after each other vertically. But in the landscape mode, I'm going to put the two text beside
each other, let's quickly create a variation for our landscape mode. By selecting this
create landscape variation, you can see that inside our layout file, now we have to find
inside the activity main folder, we have this activity main dot XML, which is for the portrait
mode, and the other one, which is for the landscape mode. And now if we want we can
change this landscape for example, I'm going to change the constraint of this TextView.
Let's delete all of the previous constraints. I'm also going to delete the constraint of
this TextView the left constraint. Let's place this fun in here, and let's add some constraint.
Also, another point in here that you may find useful is that if you want to center your
elements, for example, horizontally, you can select the two items, for example, these two
by right clicking on the item, and by going to this center, and selecting horizontally,
you can center your items horizontally. So once again, this is the view for the landscape
mode, the layout for the landscape mode, and this is going to be the layer for the portrait
mode. Now if I run my application, I should see the difference. You can see that in the
landscape mode, the two texts are beside each other. But if you change the rotation to portrait
mode, the two texts are vertically after each other. So this way, by coming to this icon
in here and selecting create landscape variation, you can create another layout file for your
main activity. But this is not the only option. You can also create a nice version in case
if you want to activate a nice theme for your application. I'm not going to do that. Let's
quickly see how we can create that. So in here, I can go to this create other and from
here I have all sorts of options. For example, if we want to create a night mode for our
activity, we can select this night mode, we can pass it to this qualifiers. And for the
night mode. That's a night. Let's create this fun. And let's take a look at our project
thing. Now inside this activity main folder you can see that we have three activity main
dot XML files, the middle one is for the night view. If you want, you can change that, for
example, you can change some colors, but I'm not going to do that in here. I'm just going
to ignore this layout file for the night view. Let's see what other layouts we can create
for our activity. So once again, I can go to this icon in here by saying create other
you can see that we have all sorts of options. For example, In here, we have this version,
in which we can create different layers for IDs for different versions of the Android
on the user's device. Let's add this to this chosen qualifiers. And in here, we can put
a number. For example, if we want our application to look different in API level 21, we can
put 21 in here, and we can create the layout file. Let's take a look at our layout folder
inside activity main folder. Now we have this activity main dot XML, which is a specific
for the version 21 of the API. This way, you can also create other layout files, and you
can change them if you want. Before I finish off this video, I'm going to talk about another
point of XML files in Android. let's right click on our layout file, and let's select
new layout resource file. And let's see what an include is. So for the file name of this
XML file, I'm going to say trademark. And also I'm going to change the root element
to let's say, relative layout. Let's select Okay, and let's switch to TextView. Inside
this TextView, you can see that we have this relative layout. So basically you use include
whenever you want to reuse some layout file, for example, now I have created this trademark
dot XML file in which I'm going to put an image view and a text view inside that after
creating this layout file, I can reuse this trademark dot XML in order to show this trademark
in multiple activities of my application. So the use of include tag is to reuse your
layout files. First of all, let's design this trademark dot XML. In here, I'm going to say
TextView. Or before that let's define an image view,
let's say 15. dp for the width and height. Let's give it an ID.
And for the placeholder, I'm not going to import any image into my project. Instead,
I'm going to use this IC launcher background which exists in our driver folder. Let's also
define a TextView. In here, wrap content and wrap content for the text. I'm going to say
developed by me Sam. After that, let's move this TextView to the right of our image view,
let's say to the right of logo. Let's add some margin left, maybe 10 dP, let's style
this text a little bit. Let's say italic, and forked. Let's also change the size of
this TextView. Right now the width and height of this relative layout, this whole parent
is match parent in here, I'm going to change that I'm going to change both of these to
wrap content, you will see why when we use this layout fine. And also I'm going to add
another attribute for both of these elements. Let's center them vertically, center vertical
for both of them, let's say through between place them at the center vertically. So this
is our simple layout file. In here we are showing a TextView and also an image view.
If we want to show this layout file in multiple places in our application, we can use the
include tag include tag is very useful for when you have a complex layout file, and you
don't want to write the exact same file twice. Let's switch to our activity main dot XML
file. And let's see how we can use that trademark. So down here after the TextView, I'm going
to say include, and I can add a layout attribute. And I need to pass the address of my layout
file. In this case, it's at layer slash trade. As simple as that we can include another XML
file inside this layout file. If you want, we can also constraint this layout file, you
can see that I can select this whole layout file. But right now if I try to constraint
my layout file, you can see that it won't work, we can see that it will be placed once
again at the top of my screen. And that's because we need to override the width and
height of this layout, right. The way to do that is to compensate this include tag. And
in here for my whole layout file, I need to define my layout width and height once again.
So for example for the layout, which I'm going to say wrap content for the height as well,
I'm going to say wrap content This way, you can see that now we can constraint our layout
file, we just need to override the width and height of our layout file. Let's also add
two more constraints. I can use this trademark dot XML file as many times as I want in different
parts of my application that can be useful sometimes also where you're using include
tag, you may want another tag. Let's quickly see that. So instead of this relative layout,
sometimes you may see merge this tagging here. So basically what this merge is doing is that
it's avoiding redundancy. Let's talk about what I mean by that. So right now these two
elements have no idea about about their parents, they do not know where they are going to be
used inside relative layout, a linear layout or a constraint layout. For that reason, because
they don't know about the parent, you can see that our layout does not look very beautiful
in here. But if you know for sure that where you're going to use this layout file, for
example, if you are going to use this layout file inside a linear layout, you do not need
to write the linear layout. Once again, you can use this merge tag in order to avoid redundancy.
At the time of rendering your layout file, this merge tag will be ignored. And these
two elements will be placed inside the parent that they are going to be used. Sometimes
when you know for sure, that's where you're going to use your layout file using this merge
tag can improve the performance when rendering your layout. But in here, I'm just going to
change this to a relative layout. Okay, I think that's enough talking about different
XML files in Android, we have others as well, we will take a look at them later on in the
course. But for now, I think it's enough. In the next video, we are going to talk about
the material design library, see you in the next video.
In creating Android applications, designing is an important aspect. And creating a beautiful
layout will impact the number of downloads for your application on the Play Store. And
also it will impact the user's feedback. But sometimes designing can be challenging, especially
if you're like me if you are more interested in the code side of the application. But luckily,
we have material design as a guideline for our designing. Despite being a guideline Material
Design also introduces some new components that we can use in our application. Let's
check the material design website. And let's see how we can use that in our project. The
website for material design is material that
if you go to this Developer tab in here, you can see that besides Android applications,
you can also use material design for iOS app development, you can use it for web development,
and also you can use it with hybrid development with flutter. Let's go to the Android page.
As I said, Material Design consists of two things. First of all, it's a guideline for
designing your applications. Also, besides being a guideline, you have some components
that you can use in your application, we will take a look at those components in a minute.
But before that, if you take a look at this theming in here, for example, if you want
to check the guidelines for typography theming, you can click on this link. And after that,
if you take a look at this material design guideline for typography, you will see some
guidelines. Basically these guidelines are helpful for when you want to design and create
a beautiful application. Basically, these are some to do's and not to do's in order
to follow when you are designing different applications. So make sure to check this webpage
and the guidelines. In here we are not going to talk about guidelines. Instead we are going
to take a look at the components. If you go to this components link, you will see that
we have a lot of components. We are familiar with some of these components. For example,
we have seen different buttons and checkboxes. But some of them are new. Don't worry about
that later on. We will see a lot of these in the course. But let's take a look at a
few components that we already know. For example, let's take these buttons. Then in here in
this image, you can see that we have four different kinds of material buttons. These
are somehow different from the buttons that we have used already in our applications.
Basically, the main difference is in the styling of different buttons. Similar for button we
have all sorts of other components. For example, we have checkboxes for some of these components.
We have also some documentation and guidelines for designing them. For example, if you want
to check the guidelines for designing a checkbox, you can click on that link. And in here, you
can see some guidelines. So make sure to check that if you need but let's see how we can
use these components in our applications. Before that, I'm just going to say that in
this video, we are not going to vote with all of these components. During the course
we will be coming back to this web page and a lot of these components. But for now first
of all, we are going to start with material buttons. We will also take a look at the floating
action button. This one in here. We will also take a look at the material cart and things
like a snack bars. Later on in the course we will take a look at the top app bars, bottom
up bars, and also things like tab layouts and a lot more. Okay, instead of talking let's
implement these maffeo buttons in our application. In order to use material design components
in your application, you need to add a dependency in your application. We haven't used any extra
dependencies in our applications yet, but Let's see how we can do that. If you remember
I said that Gradle will combine all of the layout files, all of the Java files all of
the static resources in our application. And besides all of those Gradle will also use
the third party libraries that we use in our application. One of those libraries that we
are going to use in here and later on Gradle is going to handle combining it in our application
is this material design. So we need to add this material design library in our project
with the help of Gradle. In order to add the material design library, you can go to this
Developer tab. After that, you can select this Android. And inside this documentation,
you can go to this getting started. And from there, you can find the dependency that you
need. In this page, you can see a step by step guide for adding the library, the material
design library into your project, make sure to read this page if you need. But basically
what they are saying is that, first of all, make sure that you are using Google repositories
in your project. Let's see where that is in Android Studio, in Android Studio inside this
Gradle script inside this build that Gradle. This project module, you need to add the Google
repository, Google repositories has been added by default into our project inside this all
projects. But if you don't see this Google in here, make sure to add that the next step
after adding the Google repository is to add Material Design dependency, you can copy this
dependency from here, and you can add it to your project. Let's copy that this time, we
need to go to this build that Gradle module app this one in here. But before that, let's
close this one. And let's also minimize all these extra pains, we need to add that dependency
inside these dependencies in here. For example, you can add that dependency before all the
other dependencies. Let's face that in here. But we also need to change this line because
right now we have this version tag in here. And this is not the version, it's better always
check the internet for the latest version of material design, you can also use the help
of Andrew the studio. Right now I'm getting a highlight. If I hover over this highlight,
you can see that the latest version available is 1.1 point zero. So instead of this version,
I'm going to say 1.1 point zero. After adding this dependency, you need to click on this
sync now. And after that Gradle will download some files to this specific material design
library files from the internet. And it will add the library to your project. So let's
click on the sync. Now before that, make sure that you have an internet connection, it was
somehow fast. And now we can use material design components in our project. Also inside
these dependencies, you can see that by default, we have other dependencies as well. For example,
we have this constraint layout, we have these dependencies for testing. And also we have
these app compat dependencies which help backward compatibility, but more on them later on.
Okay, now that we have added this material dependency, let's see how we can use them.
But before using them, I'm going to add a button in my layout file. Let's quickly see
that. Let's also enable this preview. And before everything, let's delete this TextView.
So in here, I'm going to save button, let's say wrap content and wrap content. And let's
define a text,
let's say Say hello. Let's also change this constraint layout to a relative layout. And
let's center our button. You can see that we are seeing the previous slide for our button.
Now let's implement material theme. And let's see how that changes the size of our button.
So for that, I'm going to open my project pane. If you remember inside the Resources
folder inside the values folder, we have these slides that XML. Let's open that right now
we have this app theme style in which its parent is this theme that we are seeing in
here. If we want to implement Matthew, your theme, we can change this parent. Let's quickly
do that. And the theme in here is going to be themed material components. We also have
another theme called theme dot material components that light this one in here video, we'll take
a look at that in a minute. But in here, I'm not going to customize this theme that material
components. So for that, I'm going to delete all these three items. I'm going to use all
of the values from inside these material components theme, so let's delete them. We just change
the past theme of this afternoon. But right now if we switch to our activity main dot
XML file, you can see that we are getting this dark theme somehow we also change the
style of our button. This is good for a night theme. But in here I'm going to change the
parents theme to a light theme so that we can see better. So let's select this lighting
here. And if we switch back to our activity main dot XML file, you can see that we are
seeing this beautiful light theme out button is now installed, we have some round corners,
the font is somehow different. And also the spacing between the letters is different.
We didn't change anything for our button elements. But because we are using the material theme
as the parent theme of our application, all of these styles have been applied automatically.
We can also customize this button for example, we can change the background color of this
button. But before that, let's quickly add a color to our color resources. Inside colors
that XML I'm going to add a new color, let's copy this font. Let's face it, and let's change
the name to let's say orange. For the value, I'm going to select this orange color. Now
in our activity main dot XML file, if we want to change the background color of this button
to the orange, we can say background tint this morning here, and we can address our
orange color. We can also change the text color of this button. But I don't think that's
necessary, I just wanted to show you that you can customize your buttons. Let's delete
this color. As you saw on the material design website, we have some styles for our button.
Let's see other slides as well. For example, in here I can see a style. Let's search for
material button, you can see that we have this on elevated button style. Let's quickly
see that this is basically this style that we are currently using. It's the default case.
Let's remove that. And let's see others, we have this button style, we have this outline
button. If you take a look at that, you can see that the style of your button is changed
to some outline button. Sometimes this might be useful. We also have a text button, let's
quickly see that. This is the one material components Start button the text button, this
will change our button to a text button. Sometimes you may want to use this, I'm going to stay
with the default case. So I'm going to delete this whole state attribute. So you can see
that using material design is very easy. Basically, we just added the material dependency into
our project inside this, like build that Gradle module app. After that inside the slides,
we change the parent to this team, that material components team. And after that we can use
all of the elements from inside the material design library. If you add another element
into your layout file, for example, a checkbox By default, the checkbox from the material
design library will be used. Okay, let's move on from this part. And let's talk about some
of the components that we don't have in our applications. If we don't use the material
design library, the first of those components that I'm going to talk about in here is called
floating action button. Let's delete this button. And let's see that floating action
button. So in here I can say floating action button, we can see that we have two option,
we have this floating action button and we have this extended floating action button.
Let's use the first one for the width and height I'm going to say wrap content. And
as soon as I do that, you can see that we have this beautiful button in here, this floating
action button is going to be floating on our layout file. Even if for example, we scroll
the page, if you want, you can change the location of this button for example, you can
move it to down below in here. For that I can use two attributes that we haven't seen
so far. For example, I can see a line parent button and we can set it to true, we can see
that as soon as I do that its location will be changed to the bottom of our screen, we
can also use another attribute called align parent. And in order to move it to the right
of our screen, let's say through and now you can see the difference. Let's also add some
margins. Let's say margin. In here, we have two options, we can add this margin right.
Or alternatively, we can use this version. And if I use this module right in here, I
will get a warning. Let's quickly use that. And let's see the warning. For example. In
here, I'm going to say 20 dP, you can see the highlighting here. If you take a look
at this, it says that it's better to use margin. And the reason for this is because some of
the languages it starts from right. So for example, if the language of your user is Arabic,
in that case, this floating action button will be placed at the left of the screen.
And if you add a margin, right, it's going to put the floating action button to outside
of the screen. For that reason. It's better to use margin and so let's say margin and,
and let's fast 20 dP, you can see that we are getting the same result in here. We also
need to add a margin button. That's a margin button. And that's first 20 dP. You can also
add an icon for your floating action button as well. Let's quickly add an icon into our
driver folder and let's add it for our floating action button. Let's say new image assets
on our driver folder. Let's select action bar and tab icons. And let's select a plus
sign in here. Let's search for add. And let's add this one. For the name, let's say ICF.
And also, let's change the color. I'm not sure that if this is a white color for that,
I'm going to use this custom color. And I'm going to use white. Right now the selector
is on white. So let's say choose, and let's create our icon. And now you can add the icon
for your floating action button. But for some reason, I cannot use the source attribute,
which is the attribute to pass an icon for your floating action button. I think because
I added this dependency in my project, I need to restart my Android Studio, or the other
option is to rebuild my project. Or the better option is to go to this file, and invalidate
your cache and restart Android Studio.
Let's select that, let's see, invalidate and restart. Now that I have invalidated my cache,
I can use the source attribute. So inside this floating action button tag, I can say
source, this 100 source, and I can pass my ice icon, you can see that as soon as I do
that, this icon will be added to my floating action button. If you want, you can also change
the color of this floating action button. For example, let's change it to the orange
color that we have defined a few minutes ago. For that I can say background tint this one
here, and I can pass color orange, you can see that the color will be changed to orange.
Also, you can see that we have some sort of border for this floating action button. If
you want, you can also change that. The way to do that is like this, you can say app column,
background tint. And this way, you can pass your new collar for example, if you want to
pass avoid color. First of all, you can create that color inside your collar resources. And
after that pass that or alternatively, you can pass the hexadecimal value of that color
directly in here. For example, I know that hashtag FFF is for the void color. So if I
pass that, you can see that we have a void color in here. And also you can see that the
border of this floating action button is now white. So this Android column background tint
is for the background color of your floating action button. But this app column background
tint is for the border of your floating action button. You can also define another color
anything. Let's quickly see that you can see rebuild color this one in here, app column
repo color, and pass a color. For example, once again, I'm going to pass void. This repo
color is the color of the floating action button when you click on it, let's run this
application. And let's see that you can see that we have this beautiful floating action
button at the bottom of our screen. And if we click on that, you can see some color.
That white color is this rainbow color. Beside that you may also have noticed that the color
of this top bar has changed as well. This is because we are using material components
theme in our application. Later on, we will see that we have a beautiful toolbar when
we work with material design. We will implement that toolbar in our applications later on.
But for now let's talk more about this floating action button. You can also set an onclicklistener
for this button if you want as well. But for that we need to have access to this floating
action button inside our main activity dot java file. So for example, like before, I
can say private floating action button, this one that comes from material, that floating
action button package. Let's name it FA B. After that we need to instantiate it inside
our on create method. Let's say FA v is equal to find view by ID. But before that, we need
to give an ID to our floating action button. I don't think we did that in here. Let's say
ID, let's just name it FA B, let's say our.id dot FAA. After that you can say fab dot set
onclicklistener is fine in here. And we can pass our listener. For example, let's try
a toast message. Let's say fab clicked. Let's run the application. And let's see if we see
the toast message. You can see that you're seeing that toast message it seems to be perfect.
There are also other ways of using this floating action buttons for example, one way is to
use a combination of floating action button and app bar navigation. In that case, we will
see that we can implement a beautiful style and a beautiful design for our application.
But more on that later on when we talk about bottom apart navigations Okay, I think that's
enough for this video. In the next video we will talk about two more components in material
design library. We will talk about snack bars and also card views. See you in the next video.
In this video, we are going to talk about two more material components. The first of
those components is snack bar. Before we start implementing the snack bar, let's see what
does it look like. So if you go to material that i o in these components, in this listening
here, you can see a snack bar. Let's see that. This is what a snack bar looks like. It's
a lot like a toast message. But the style is a little bit different. And also inside
the snack bar, you can have a button that will do some action. Beside that you can make
your snack bars to be indefinite, it means that they won't be dismissed until the user
clicks on this action. Also, you can style your snack bar as well. For example, you can
change the color of this text and also the color of this action button. As we will see
later on in the course snack bars are extremely useful when you're developing Android applications.
In order to use the snack bars. First of all, you need to add the material design library
into your project. So let's go to this Developer tab. In this Android section, let's go to
the documentation getting started. Let's copy the dependency. In our project inside the
Gradle scripts build that Gradle. Let's quickly add that dependency. For the version, if you
hover over this, you will see the latest version, which is 1.1 point or after adding the material
dependency. First of all in my layout file, I'm going to do some changes, I'm going to
add a button. And also beside that I'm going to give an ID to my parent layout, both First
of all, you may have noticed some changes in the looks of Android Studio compared to
the previous video. That's because I have updated my Android Studio. And we have slight
differences. Don't worry about that we will talk about these differences in the next video.
For this video, let's just implement our snack bar. The first difference inside this new
Android Studio is that you no longer can see the text and design mode in here. And if you
want to go to your text, you can go to this split view in here. Once again, we will talk
about these differences. Let's give an ID to our constraint layout. Let's say ID, I'm
going to name it parent. Later on in this video, we will see that why I'm giving an
ID to this constraint layout. Let's also go back to our design view. And let's add a button.
Before that let's delete this text here. Let's quickly constraint it. And also Let's change
its text. I'm going to change it to show snack bar. Let's also quickly changed the theme
of my application so that we can see the material button. In our project like we saw in the
previous video in our values folder inside the styles folder. I'm just going to change
the panel theme here. I'm going to change it to this team that material components stuff
light. If you switch back to your layout file, now you can see that we are using the material
buttons in my java file. First of all, I'm going to initialize this parent this constraint
layout and also this button. Let's quickly do that, let's say private constraint layout.
Let's name it parent. And also private button. Let's name it btn. Show sniper. Down here
inside the onCreate method. Let's initialize these two parent is equal to find view by
ID r.id. That parent potential snack bar is equal to find view by ID r.id. That I believe
its ID was button after that set onclicklistener for this button. Let's say btn show snack
bar dot set onclicklistener new onclicklistener I'm going to hand the job to another method.
So let's say show snack bar. Let's quickly create this method down here. Private void
show snack bar. We don't need any input.
And here is how you can show snack what you can say snack bar dots make without instantiating
a new snack bar you can use this static make method in order to create your snack bar.
This make method needs three inputs. First of all we need to pass the parent layout the
layout that we are going to show our snack bar inside that in this case our parents layout
is a constraint layout. And as you saw I have initialized that constraint layout Here, that's
the reason I gave an ID to my constraint layout. Now in here, I can say parent. After that
I need to text that I want to show in my snack bar. For example, I'm going to say this is
a snack bar. The third parameter in here is a constant that will indicate the time that
you want to show your a snack bar. And you can pass that constant like this, you can
say, a snack bar, that you have three constants in here, this length long, net short, and
also left indefinite. The long and short are descriptive, but if you personally have indefinite,
you will show your snack bar indefinitely, and it won't be dismissed until the user clicks
on that action button. Let's select this one for now. indefinite after this make method,
you can call dot show method in order to show your snack bar. This is going to be a really
simple snack bar. And we don't have any action yet. Let's see if it's going to work. If we
click on this button, we should see our snack bar. And here it is. It's a really basic snack
bar. And we don't have any action beside that we haven't changed a slide of our text. Also,
because we have past this length, indefinite there is no way to dismiss this snackbar.
Let's quickly change this constant to length short, and let's see the difference. Now if
we show our snack bar, you can see that it will be dismissed after a few moments. Okay,
now let's see how we can add an action to our snack bar. But first of all, I'm going
to change this constant to left indefinite once again. And before the show method, I'm
going to call another method. That method is called dot set action. First of all, we
need a text in here, this is going to be the text of our action. For example, if you want
you can say retry in here. After that you need an onClick listener that's a new onClick
listener. This is going to be an interface. And inside the onClick method, we can do whatever
we want when the user clicks on this retry text. For example, in this case, I'm just
going to show a toast message. Let's say retry clicked. Let's see if we can see the action.
As you can see in here, now we have this retry option. Remember that the length is still
indefinite. But if we click on this retry, this snack bar will be dismissed. And also
we will see a toast message. Let's also see that how we can change the color of these
two texts this retry and also the color of the message of our snack bar. For that once
again before the show method in here, I'm going to call another method. Let's say that
set action text color in order to change the color of our action text. In here, you can
pass your colors in two different ways. First of all, you can address the color from your
color resources, if you remember in your project inside the Resources folder, inside the values
folder, you had these colors, that XML file in which you have few colors inside that you
can address one of these corners inside your set action text color like this, you can say
get color. And after that our dot color dot for example, color primary. Beside that if
you have noticed, we have some colors that are coming from the material design libraries.
You can see that we have these colors as well. But for example right now, if I use this color
primary in here, you can see that we are getting this warning. And this warning says that you
can use this method in API level 23. and higher. There is also another way to get our color
without seeing this error. Let's we can see that.
Let's say get resources. This phone dot let's say get column. Inside this method, you can
address your color, you can say our color, that for example, color accent. Let's also
see the other way. Instead of using the resources in my project, I can use a Java class. So
for example, instead of this whole method in here, I can say color with a capital C,
which is coming from Android dot graphics dot let's say read. This way we can get red
color with the help of this color class. So if you don't have this red color in your resources,
you can also use this class. Okay, now let's see how we can change the color of the message
of our snack. For that I can say that set text color this method. Once again, in order
to pass a color you have two options. In this case, I'm going to say color dot for example
yellow. Let's run the application and let's see if we have successfully changed the color
or not. Let's show our snack bar. You can see that the color of the message is yellow.
And also the color of our action button is red. Okay, I think that's enough talking about
the snack bar. Later on, we will use snack bars a lot in the course. The next material
component that I'm going to talk about is material court view. Let's quickly see what
does it look like in material that I have. Once again, inside this components tab inside
this cart, you can see your cart views. Let's scroll down and filled with this is what cart
view looks like. You can use it to, for example, show different items in your application,
you can mix UI elements in form of a cart. Beside that you can also have some actions
for your cart view. Also, you can have some coronary radius, and also you can have some
shadow or elevation to be precise beside your current view. In this video, we are not going
to implement the actions we will do that later on in the course. But for now, let's just
implement a simple cartoon in our application, we have added the material design dependencies
in our project, so we can add the cart view without any problem. But before that, let's
close this style and colors. In my layout file in here, I'm going to add my cart view
below this button. Let's switch to the split view. And let's add that down in here, I'm
going to define my cart view, let's say material cart view. This one, beside the material cart
view, you also have a native cards view. But style is a little bit different. If you don't
want to use the material card view, you can always go to developer.android.com and get
the dependency from the Android x libraries for cart view. But in here, let's implement
this material cart view. I'm going to enter my width and height manually. For the bits,
I'm going to say 100 dP, I think that's fine. And for the height, let's say 200 DPI, it
has an odd shape, I think it's better to change the width to 150 dpi. Let's give it an ID.
I'm going to say card view. After that inside this card view, let's enter some elements.
But before that, let's quickly concentrate our call to you. Inside this cart view, first
of all, I'm going to define a relative layout. For the width and height, let's say match
parent, the parent is this card view. So we are going to fit the whole cart view. And
inside this relative layout, I'm going to put my elements for example, I'm going to
put a TextView wrap content and wrap content. Let's also center it horizontally. For its
text, let's just say hello. Let's also change the style of this code, let's say text style.
And let's pass both beside that let's increase the size. Let's say 18. So after that below
this TextView, I'm going to define an image view, you can see that in a cart view, you
can have multiple UI elements for the width of this image, I'm going to save 140 dP. And
for the height, let's say match parent, I'm going to move this image view to below this
TextView. So let's say layout below. But before that I need to give an ID to this TextView.
Let's say ID. txt Hello. Let's also add a margin top and let's center this image view
margin top, let's say 10 dP centered horizontal through. Let's also use a placeholder, let's
say source. I'm going to pass this IC luncheon. Okay, I think that's enough for understanding
the cardview. Beside that in their current view elements, you have other attributes as
well. For example, if you want to make the coordinates of your card view radius, you
have this option corner radius, called corner radius. You can say five dB, for example,
right now you may not see the difference. But if we run our application, we will see
the corner radius You can also change the corner radius if you think that five DB is
not enough. The other option that we have in here is this card elevation in which will
give some 3d shape to our card, we have some shadow behind our cards. Once again, I'm going
to say five up, you can also define this card view like any other UI elements in your Java
file. Let's quickly do that private cart view. In here you can see that we have two options.
We have this card view which is coming from Android x card view. And also we have this
material card view which is coming from the material design libraries. In here we are
going to use this material called view but once again, the difference is only in the
style. Let's name it card view. And down in here inside the onCreate method. Let's
initialize it Let's quickly set an onclicklistener for this cart view, cart view dot set onclicklistener.
Beside that, you can see that we have a lot of options in here. We will talk about them
later on in the course, set onclicklistener. Let's just show a toast message. Let's run
the application. And let's see what does our cart view look like? Our cart view looks like
something like this, which is not that good. But we can do much better with the card views.
We will do that later on in the course. Specifically when we talk about recycler view, the combination
of recycler view and card view for its items will create a really beautiful layout file.
We will see that I believe in the next video. But in here, you can see that we have this
corner radius and also we have this elevation which is this 3d shape for our cart view.
Beside that later on in the course, we will see how we can create an expandable card view.
That means that we will have some button in here for example, an arrow in which by clicking
on that view, expand our cart view and we will see some more information and maybe actions
for our item in our cart view. Okay, I think that's enough for this video. In this video,
I just wanted to show you how you can create a snack bar and also implement this cart view.
In the next video, we are going to start talking about recycler view. In previous videos we
have seen list view for showing different items. But recycler view is a much more better
option to show lists of different items. In the next two or three videos we are going
to see how we can use recycler views. See you in the next video.
In this video, we are going to talk about recycler view, which as I said before is another
option for showing a list of different items in our application. But before that, as you
can see, I've updated my Android Studio and in here we have some changes. Before talking
about recycler view, I'm going to show some of these changes in the newest version of
Android Studio, I strongly suggest that you always keep your Android Studio updated. You
can do that by going to this Help menu. down in here, you can check for different updates.
And if there is any updates available, you can see a list of different options. This
in here is Firebase services, we are not going to use that I'm going to cancel this update,
you can see that I've also have some updates for my emulator and hexam installer. I will
update them after this video. But for now, let's just say remind me later. Also, if you
remember from the installation videos, I've said that always check the updates from a
stable channel. If you're not sure about your channel, you can always go to settings by
going to this file tab in here by going to settings. In here, if you check for updates,
you can see that my updates are being checked from inside the stable channel. We have others
as well. These other channels are for preview purposes. And it's basically for professional
developers to check new features in Android and report some bugs if there is any to the
Android team. But for you for the starter, it's probably best to check your updates from
a stable channel. The first thing that is different in this newest version of Android
Studio is the way you go to full screen, you can go to full screen by going to this view
menu. And by going to appearance. From here, you can go to full screen. In previous versions,
this option was somewhere down in here. Let's go to full screen mode. The other change that
you can probably see in here is this component tree. This type in here is a little bit different.
There are not much changes in here just this time. But there is a very important change
above in here. You can see that down in here we no longer have that design and text view
mode. Instead of in here we have three options. Right now we are inside the design view. We
can also select this one to go to the text view. And in between these two, we have this
splitter mode. As you can see, it acts like if you have enabled preview inside your text
view, but there is a difference. Before that let's quickly minimize this project pane.
From here from inside this splitter view, you can also enable attributes. For example,
if I select this button, I can see the text view of this button I can see the design view
of this button and also I can see all of the attributes in here as well. This is going
to be helpful, especially if you're using multiple monitors. For now I'm just going
to minimize this attribute pane. You You can also change the size of this preview. Besides
attributes, you can also enable this palette from here in which you can see all of different
layout elements. And you can drag them directly into your design view.
Once again, this is going to be helpful if you are using multiple monitors. The other
thing that you can see in here is this resource manager, we had this resource manager in Android
Studio 3.5. But there has been some improvements in this newest version. Basically, inside
this resource manager, you can see all of the static resources inside your project.
Previously, if you wanted to see different rival items inside your project, you could
have go to this project training here. And inside the resource folder, you could have
checked your drivers. Now with this resource manager, you can have access to those drivers
more easily. Also, beside that you can see different colors, you can see different layouts
mipmaps, the strings, and also if you click on these three dots in here, you can see all
sorts of resources in your project. Also, if you want, you can use these resources directly
from here. For example, right now, in our layout file, we have this image view, which
its source is this IC launcher background which exists in our driver folder. If we want
to change this to this IC launcher mipmap file, we can simply drag this IC launcher,
and we can paste it in here, you can see that the source of our image view has now changed.
Similarly, we can do the same thing for the strings. For example, right now in our string
resources, we have this app name string, we can drag that for example, instead of this
Hello, we can see that the text has been changed. Let's undo the changes. So from time to time,
this resource manager can be useful. The other thing that has changed in this newest version
of Android Studio is the font of my code. I actually don't like this font, and I'm going
to change it to the previous font that we had in our Android Studio. For that I can
go to this file inside the settings. You can search for fonts. And in here, you can change
the font. For example, I think I'm going to select this one. Let's press OK. And yes,
it is the previous font that we had when we worked with different layouts, right. Also,
besides Android Studio, I've updated my Gradle tool as well. If you want to check that you
can go to your project menu, you can go to this Gradle script, we have changed two things
about the Gradle. First of all, if you take a look at this Gradle dot rapper properties,
you can see that we have the latest version of Gradle in here. And also besides that,
if you go to this build dot Gradle project module, you can see that we are using the
latest Gradle tools version. So beside Android Studio, I've updated my Gradle as well. Probably
you wouldn't see any difference when you update your Gradle. In most cases, it's going to
fix some bugs, and also it may improve some performance. Once again, my suggestion is
to keep everything updated when you're working with Android Studio. Okay, let's move on from
this part. And let's talk about recycler view in Android Studio. For that, I'm going to
close this project. And I'm going to create a new project. Let's say start a new Android
Studio project. Now that we have in here under the studio has a new update in these templates
as well. For example inside this TV, you can see that we have other options for different
operating systems, or for different devices, some new templates has been added. But we
are not going to work with them. Like before we are going to use this empty activity. Let's
name this project recycler view. Or let's say RecyclerView example. It's interesting
that in here, we have this new option in here. It says use legacy Android dot support that
libraries. So basically, before Android x, we had to use support libraries for having
backward compatibility is in older devices. As you saw in the previous videos, the use
of Android X was mandatory. But now in here we have this new option. I don't think we
are going to use this legacy support libraries in our course but it seems like we now have
this option. Okay, let's create our project.
And let's see how we can use recycler view. for that. We have two options. First of all,
we can go to our activity main dot XML file. As you can see, we have this recycler view
option in here but before using it, we need to download its dependency into our project.
You can click on this download option in here and it will be automatically added to our
gradle file. Or if you don't want to add it from here you can always search for recycler
view dependencies and add Manually into a gradle file. Before I add this recycler view,
let's quickly open our Gradle scripts build that Gradle module app. down in here in these
dependencies, you can see that we don't have any recycler view. Let's download this recycler
view by clicking on this. You can see a warning in here. Would you like to add this now, let's
say okay, it seems like we have added recycler view successfully. Let's check out we've got
gradle file. And then in here, you can see the recycler view. But as you can see the
highlighting here, this is not the latest version. For that reason, it's always better
to check the internet for the latest RecyclerView dependencies. The highlighting here says that
you have version 1.1 point oh available. So let's change this one to 1.1 point oh, and
let's sync our project. Okay, now that we have added this RecyclerView dependency, we
can add it to our layout file. I'm going to switch to text view from above in here. And
I'm going to minimize this resource manager. First of all, I'm going to change this constraint
layout to a relative layout. After that's like before, I'm going to delete this TextView.
And let's add a recycler view. So you can see recycler view this option down in here.
For the width and height. Let's say match parent, I'm going to match the whole relative
layout. Let's also give you an idea. Let's say contacts recycler view. As you can guess
I'm going to show a list of different contacts in my recycler view. After that, we can finish
creating our recycler view. And if you enable your preview from inside this split view,
you can see that we have these options inside this recycler view. I'm also going to add
some padding to this parent relative layout. So in here inside the starting tag, let's
say padding, and let's say 10 dP. That seems better. After that, we need to initialize
our recycler view in our Java file. So inside this main activity dot java file. First of
all, I'm going to say private recycler view. This one that comes from Android x dot recycler
view v jet. Let's name it contacts recycler view. And down in here inside the onCreate
method, let's initialize it. Contacts, recycler view is equal to find view by ID dot contact
recycler view. When we have worked with ListView, right after this point, right after initializing
our ListView, we could have create an array adapter and after that, we could have passed
our array adapter to our list view. But when we are using recycler view, we need to create
our own adapter. Creating your own recycler view adapter is probably the most complex
code that you have written so far. But we are going to go through each step together.
Before creating our adapter. We also need a layout file. If you remember when we have
used ListView. For every item in our list view, we have past a built in layout. In recycler
view, we need to create that layout as well. So inside my project inside the Resources
folder inside the layout folder, I'm going to create a new layout resource file. I'm
going to name this phone contacts this item. I'm also going to change this route element
to a relative layout. Okay, let's create our layout file. And let's switch to our TextView.
If you remember from previous videos, I said that when you are working with RecyclerView,
you have all sorts of flexibilities. In designing your items layout files, we will take a look
at more complex layout files. But in here, I'm just going to add a simple text view.
Let's switch to the split view so that we can see the preview. Okay, let's say TextView.
Wrap content and wrap content. For the text, let's say contact Nate. Later on in our recycler
view, we are going to change this text view. Beside that let's also give an ID to this
TextView
let's say txt name. I'm also going to change the height of this relative layout to wrap
content because we don't want to occupy the whole screen for just one item in our recycler
view. For that, let's change this font to wrap content. Let's also add some interest
in here so that we can see it better. And also let's add some padding. Let's say padding.
And that's past 10 Up. Sorry for that. Let's also give an ID to this relative layout, we
will see how it can be useful. Let's just name it parent. So this in here is going to
be the layout file for every item in our recycler view. Now that we have created this context,
this item dot XML file, we can create our recycler view adapter. For that we need to
create a Java class in our project. So inside our package, I'm going to say new Java class.
Let's name this class contacts, recycler view adapter. Let's create our class and let's
see what do we have in here. So first of all, before everything, I'm going to create an
inner class inside this class, we will see what this inner class is. this inner class
needs to be public, let's say public class. I'm going to name it view holder because it's
the convention. This view holder class needs to extend recycler view dot view holder this
class in here, but as you can see, we are getting an error in here. And that's because
we need to create our constructor. Let's quickly do that by pressing ALT plus insert. This
inner view holder class is going to hold the view for every item in our recycler view.
For example, in this case, it's going to generate a new contact list item, in which later on
we will see how we can use this layout file and also all of the elements inside that.
So basically, this inner class is responsible for generating our view objects. If you want
to have access to elements inside your view object, you can add them as the fields of
this inner class. For example, in here, I can say private TextView. If you remember,
we had a TextView in our list item, the ID was txt name. Let's name it. txt name. And
down in here inside the constructor, let's say txt name is equal to find view by ID.
But because we are not inside an activity, we cannot use this find view by ID method
like this. Instead, what we can do is that we can use this view object that has been
passed to this constructor. For example, I can say item view dot find view by ID. You
can see that we have this method in here. So the only difference is that inside an activity,
you can simply say find view by ID. But in here because we are not inside an activity,
we can use this view or we can say item view dot find view by ID. And after that we can
address our TextView I can see our.id dot let's say fix the name. If you have other
elements inside your layer file, you can add them like this one by one. We will see that
later on in few minutes. But for now we are done with this interview holder class. Let's
minimize this. And now we can extend the RecyclerView adapter at the declaration of our adapter
class. The way to do that is like this, we can say extends recycler view dot adapter,
we can see that we have this option. Also take a look at this VH in here It stands for
view holder. If you import this adapter inside the diamonds, you need to pass view holder.
There are also multiple view holders Make sure to import the one that is inside this
class this orb that may code that your project name. You also have one in Android x that
recycler view, so make sure to don't confuse them. If you remember when we have used array
adapter for list views as the data type of our adapter, we have passed the string. This
view holder class is exactly like that. And we are saying that the data type of our adapter
at the end is this view holder class, the class that we created inside our adapter.
Okay, now that we have extended recycler view adapter, we need to create some methods or
to be specific we need to implement some methods. For that I can press Ctrl plus i and as you
can see, we have three mandatory methods on create view holder on bind view holder and
this get item count method. let's implement all three of them. You're also going to need
a constructor for this adapter class. Let's quickly create that by pressing ALT plus insert
by going to construct it we can create our constructor. Before explaining these three
methods in here, I'm going to create a model in my project. As I said previously, in this
video, I'm going to show some contacts inside my recycler view. And I'm going to create
a model for those contacts. So inside my project, I'm going to create a new Java class. Let's
name it contact. For this contact, I'm going to define some features. First of all, let's
say private string name. After that, Let's say private string email. After that, I'm
going to define a new string, let's say private string, image URL bvoc, how this one is going
to be useful. Basically, we are going to show some images from the internet by using the
URLs. And after that, we need to create our constructor. Once again, by pressing ALT plus
insert, we can do that. Let's also create some getters and setters. I'm not sure that
I'm going to use all of them. later on. If you did not use them, I will delete them.
Let's also create a two string method this might be useful as well. Okay, now we have
our contact model. Let's close this class. So once again, inside my recycler view, I'm
going to show a list of different contacts. Because of that, I need to pass the list of
different contacts to this adapter class. For that before everything above in here,
I'm going to create an ArrayList or to a specific private ArrayList of different contacts. Let's
say private ArrayList of contacts, let's name it contacts is equal to new ArrayList. It's
very important to initialize your ArrayList if you don't initialize it, you will definitely
get a nullpointerexception. Also, because we have set this context ArrayList as private,
we also need to create a setter method so that later on for example, from inside our
main activity, we can set the initial data for our contacts for that down in here. After
the get item count method, I'm going to press ALT plus insert. And by going to this setup
menu, we can create a setter for our contacts are released. Later on from inside our main
activity, we will pass our data we're using this set contacts method. But there is a very
important point in here. Imagine that you are getting your data for example, the list
of your contacts from a web server from internet, the list of different contacts that are coming
from the internet may change during the time, so the list of different contacts inside your
recycler view should be changed as well. If you want at any time, you can reuse this set
contacts method in order to change the data inside your adapter. But in order to refresh
the data inside your recycler view, you need to use another method inside the set contacts
method. That method is called notify data set changed this one in here. So basically,
the data set for our adapter in this case is our contacts ArrayList. After changing
the list of different contacts at any time, we are going to notify the adapter that the
data set has been changed. This way we can refresh our recycler view with the new data
that we have received or passed to this adapter class. Okay, let's move on from this part.
And let's start working on these three different methods. The simplest of these methods is
this get item count, you can guess its purpose, it's just going to return the count of different
items in your adapter. In this case, we know that the data in our adapter is a list of
contacts. so we can simply return the list of our contacts, we can say contacts that
size.
That's all we need to do inside this get item count. For some reason RecyclerView needs
the number of items inside your adapter. And in here, we can simply return that. Okay,
I think that's enough for this video. In the next video at the beginning we will start
working on these two methods. After that, we will see that how we can have more complex
layouts in recycler view. And after that we will see how we can use that image URL in
order to show different contact images in our application. Okay, see you in the next
video.
Okay, let's continue our talk about creating a recycler view adapter. But before that,
let's quickly have a review of what we have done so far. So first of all, we have added
the RecyclerView dependency in our Gradle script. If you take a look at build dot Gradle
module app inside these dependencies, you can see that we have added recycler view.
After that we have added this recycler view in our activity main dot XML file. Inside
this relative layout, we have this recycler view. After that inside our main activity
dot java file. We have initialized our recycler view. After that it was the time to create
the layout file for every item in our recycler view. We have done that by creating a new
layout file called contacts list item in our layers folder. If we want to take another
look It's a simple relative layout, which contains only one TextView. Later on, we will
change this. After creating the layout file for every item, it was time to create our
adapter class. We have done that by adding a new Java class. In our package, the name
of that class was contacts recycler view adapter inside this class. First of all, we have created
this inner class, we have named it view holder because it's the convention. After that, we
have extended the recycler view dot view holder. We needed a constructor so we have created
this constructor in here. As I said in the previous video, this view holder class is
responsible for holding the view items for every item in our recycler view. We can also
instantiate all of our UI elements from inside our contacts list item dot XML file inside
this view holder class if you want. After creating this view holder class this inner
class in here, we have extended the recycler view dot adapter class in which as its data
type, we have passed our inner view holder class. When we extended this recycler view
adapter, we needed to implement three mandatory methods. This oncreateview holder on bind
view holder and get item count methods. We are going to talk about these methods in this
video. But before that, we also created a model for our contacts. We have added that
in our package. This contact has a name, an email, and also an image URL. later on. In
this video, we will see that how we are going to show an image based on its image URL. After
creating our model inside our adapter class, we have created an array list of different
contexts in which we have named that contacts. It was also important to initialize this array
list I said that we are going to get a nullpointerexception if we don't initialize our ArrayList. Also
later on when we create an instance of this adapter class, we are going to need a constructor.
So for that we have created this empty constructor. I believe later on we will change this. Also
in order to pass the data into this adapter request. In this case an ArrayList of contacts
we have used this set contacts method in which we are going to call it from inside our main
activity. After passing the data to this adapter, we have notified the adapter that the data
set has changed so that we can refresh the list of items in our recycler view. Also in
the previous video, we have talked about this get item count method. Once again, it's going
to return the number of items in our adapter. In this case, we have returned our contacts
array list dot size. Okay, now it's time to work on these two methods. But before everything,
let's close all of the extra classes and files.
And let's start working on this onCreate view holder method. As you can see, the return
type of this method is a view holder. It means that this method is the place to generate
our view holder to place to instantiate our view holder class that we have created down
here. In order to create our view holder class. First of all, we need to create a view object.
We can do that by saying view this one in here. Let's name it view is equal to if we
were inside an activity, we could have said get layout inflator. But because we are inside
a class beside our activity, we can't use that instead we can say something like this
we can say layout inflator with capital L dot from this method in here, you can see
that this from method is waiting for our context. You can also guess by the style of this from
method that this is a static method. In here we don't have any context. But we can use
this view group in order to get the context. Let's get the context from this view group
and I will talk about it. You can see that the name of this view group is parent so I
can say parent dot get context. As you can see in here we are creating a view object
and we are using this layout inflator to generate that view object. The job of this layout inflator
is that to inflate a layout for some reason this layout inflator needs a context so we
can say dot from and inside that method we can pass our context. The way to inflate our
layout with the help of this layout inflator is that to use dot inflate method. So in here
after this Fromme method, we can say that inflate and after passing some arguments,
we can generate our view object. In here, we need to pass three arguments. First of
all, we need to pass the address of our layout file for every item in our recycler view.
I mean that contacts list item, that XML file that we just created. So for that I can say
our dot layout dot, let's say contact list item. After that, we need to pass our view
group, in this case, its name is parent, once again, I will talk about it. And also after
that, we need to pass a Boolean. I will talk about that after we talked about to be over.
Let's fast forward. So in this case, and now we have our view object. Okay, let's talk
about these two arguments in here, this view group and also this Boolean. Basically, the
view group is the parent of all of the layout files. For example, it's the parent of relative
layout, linear layout, and also constraint layout. This view group can be used to group
different views inside it. If you want to make sure of that, for example, you can go
to your main activity just for testing purposes. If you save private relative layout, let's
name with relative layout. from the previous videos, we know that if we want to check some
documentation in Android, we can press down the control key. And by selecting the class
name, we can have access to the relative layout documentation and also declaration. In here
you can see that this relative layout is extending view group. So once again, view group is the
parent of all of our layout files. But why we are passing this view group as one of the
arguments inside our adapter class. Before talking about that, let's quickly delete this
line of code. We have created this just to read the documentation and declaration. So
in here inside this inflate method, we are passing this view group. Basically, this tells
that Where do you want to attach your view object, for example, when we create an instance
of our adapter object from inside our main activity, because in our main activity layout
files, we have defined a relative layout, this view group is going to be a relative
layout. And this view object in here, our layout file for every item is going to be
attached to that relative layout inside our main activity. That's also the reason why
this view group has the name of parent in here. But what is this Boolean in here you
can see that the name of this Boolean is attached to root. And also I'm passing false. In here,
I just said that I want to attach my view object to its parent. I'm passing false in
here because if we pass through, it will be a redundant job, we already passed our view
group as the parent of our view object. And if you pass through, it's going to be redundant.
We have this Boolean because we can also pass null in here. For example, if you're not sure
about our parent, and about where our view is going to be attached, instead of our parents
view group, we can simply pass now, you can see that the name of this argument has changed
to route. So if you passed now, you're not going to be sure about the place that this
view is going to be attached to. But of course, this is an option that you have. Once again,
I'm going to pass parent. And for the Boolean I'm going to pass false. So at the end of
this line of code with the help of this layout, inflator dot inflate method, we have our view
object. Now it's the time to create our view holder object. And after that, return it,
if you take a look at the constructor of this class that you have created, you can see that
this constructor in here is accepting a view. Now that we have our view object, we can create
an instance of this view holder class. So above in here before the return statement,
I can say, view holder,
the one that exists in our adapter class, not the one inside the Android x that recycler
view, let's say view holder, let's name it holder, is equal to new view holder. And let's
first review. After that, let's return our holder. So once again, the purpose of this
method in here this oncreateview holder is to create an instance of our view holder class
for every item in our recycler view. Later on this holder will be passed to this unbind
view holder method as you can see in here, and we can use it in order to change the properties
of our UI elements. Let's see how we can do that. So inside this unbind view holder method,
which I believe is the most important method inside every recycler view adapter. We are
going to use the properties of our view holder class. For example, right now inside our layout
file, we have only one TextView. So if I want to change the value of my TextView I can say
something like this, I can say holder dot txt name, dot set text and we can pass our
text. You can see that in here we are able to use holder dot txt name if you remember
Inside our view holder class, we had such a field. But also this field was private.
If you remember from the Java session, I said that whenever you use inner classes from inside
the parent class, you can have access to all of the private fields and methods of your
inner class. In this case, inside this adapter class, we can use this folder dot txt name,
even though it's private. Okay, but what do we pass inside this set text method. As you
can see, beside this folder, we also are getting some integer called position. It's exactly
like the position that we have inside ListView adapters. This is going to be the position
of the item in our recycler view. Once again, we can use this position in order to get the
data for example from our contacts ArrayList. For example, inside the set text method, I
can say contacts, dot, let's say get. And in here as the index i can pass position.
Also, during the time, I've seen that the name of this integer was different. Sometimes
it was named it sometimes it was position and also I think I've seen others. But basically,
this integer is just the position of every item in your recycler view data set. Okay,
after getting the corresponding contacts, we can use dot get name, for example, in order
to set the name of our contact to this TextView. This is going to be the simplest recycler
view that you can create. You can have all sorts of UI elements inside your list item
layout file, and after that inside this unbind view holder method. You can also set different
onClick listeners for each one of you I elements. But more on that in few minutes. So by this
time, our adapter class is ready, we have done a great job and we have created our recycler
view adapter class. Now that we have created our recycler view adapter, it's time to switch
back to our main activity and pass some data to this adapter class. So inside this onCreate
method, we have instantiated a recycler view. After that, we need to create an ArrayList
of different contacts. For example, let's say array list of different contacts. Let's
name it contacts is equal to new ArrayList. Once again, I'm passing the data manually
in here because we don't know anything about databases and web servers yet. In a real world
application, these data are probably coming from a database or a web server. Let's quickly
add some data to our contacts ArrayList.
As you can see, I've added some contacts into my area list. For every contact, I've selected
a name also an email. And beside that I've get these image URLs from the internet. There
is just one point in here. And that's regarding these image URLs. Sometimes when you're copying
the address of some image from the internet, that address may not have an extension, for
example, like this dot jpg, or dot png. Those images are not going to be useful in here.
So make sure to select the images that have the extension of that JPG or dot png. Just
because some students asked me before how to get the address of these images. Let me
quickly show you one example. For example, in Google Images, you can search for different
images. And by right clicking on the image, you can copy the image address. For example,
in another tab, you can paste that address if it's ending with dot JPG or dot png, you're
good to go. Okay, after creating your contacts, it's time to create your recycler view adapter.
For that, I'm going to say contact recycler view adapter. Let's name it adapter is equal
to new contacts recycler view adapter. We didn't need anything for the constructor,
so you're good to go. After that, we can use the setter method inside our adapter we can
say adapter dot set contacts, and we can pass our ArrayList. After creating our adapter
instance, and setting the data, we can set this adapter to our recycler view. So down
in here, I can say contacts recycler view dot set adapter. And I can pass my adapter
in a ListView. That would be the end of everything. And we could have test our application. But
in recycler view, there is also one more step. We also need to set a layout manager for our
recycler view as well. Let's quickly set that and we will talk about let's say contacts
RecyclerView dot set layout manager. In here we have few options. First of all, we can
pass a linear layout manager and we can pass Like this we can say new linear layout manager,
in which for its constructor, we need the context. This time because we are inside an
activity, we can safely pass this as the context. By setting the layout manager of our recycler
view to a linear layout manager. We are saying that display the items in our recycler view
in a linear fashion. The other option that we have is grid layout manager, we will take
a look at that. But in here, this linear layout manager is by default vertical. If you want
to change that, for example, if you want to have a horizontal view, you can do that. We
will do that in a minute after running our application. So by this point, our application
is ready to test. We have created our recycler view we have instantiated down in here, we
also have created this dynamic list of contacts. After that we have instantiated our adapter
class. We have set the data inside this adapter class. After that we have set the adapter
to a recycler view. We also have set a layout manager for our recycler view. Let's test
the application. And let's see what does a recycler view look like. As you can see the
list of different contacts name are being shown inside a recycler view in my activity.
If I had more contacts, I could have scrolled my recycler view and I could have access to
all of them. Right now they all can fit on the screen. So this is not a scroll view.
Let's quickly change this linear layer to a horizontal linear layout and see the difference.
So in order to have a horizontal linear layout, after this context, you can say linear layout
manager dot horizontal, this one in here. This is a constant in which you can use in
order to have a linear layout manager. Beside that you also have a Boolean in here. Let's
pass that I'm going to pass false. As you can see, the name of this Boolean is reverse
layout. If you want to reverse the order of items inside your recycler view you can pass
through in here. But I don't see any reason for doing that. So I'm going to pass false.
Let's run the application once again. And let's see what a horizontal linear layout
looks like. You can see that this time right now we are seeing only one contact on the
screen. And that's because when we created the layout for every item in our recycler
view, we have past match parent as the width of the item. But if you want to see other
items, you can scroll the window horizontally, you can see that we are seeing other contacts.
So this is what this horizontal linear layout looks like.
Okay, let's
quickly see the other layout manager that I talked about. And let's move on from this
one. The name of the other layout manager was grid layout manager. So I'm going to delete
all of these. And inside this set layout manager method, I'm going to say new grid layout manager.
For the inputs of this constructor, we need two arguments. First of all, we need the context.
After that we need the number of columns. For example, I can pass two in here, these
two in here is going to divide our screen horizontally into different parts, or to be
specific to different columns. And it's going to show different contacts in those two columns.
But before I run my application, I'm going to change one thing in the application in
the contacts list item that XML file, I'm going to change the match parent attribute
of my relative layout to let's say wrap content. You can see that for this video, we have set
it to match parent. I'm going to change that to wrap content. Okay, now let's run the application.
You can see that the screen has been divided into two different columns horizontally, and
we can see the contacts name in two different columns. So sometimes you may want to use
this grid layout manager. Okay, now let's see how we can set an onclicklistener for
each one of these items in our recycler view. Right now, if we click on each one of these
items, they are not going to do anything. But if we want to set an onclicklistener we
need to do that inside our contacts recycler view adapter. Let's quickly open that class.
It was in our Java folder inside our project folder. Contacts recycler view adapter. If
you remember, I said that this unbind view holder method is the most important method
in your adapter class. And inside this method, you can set an onclicklistener for every UI
element that you want. Also, if you remember inside this contacts list item, I've defined
an ID for this whole relative layout. If you want you can set the onclicklistener on this
TextView but I think it's better To define the onclicklistener on the whole relative
layout, because as you can see, we have this padding of 10 dP and some places in our relative
layout are not going to be clickable if we set the onclicklistener on the TextView. But
because I'm going to define the onClick listener on this relative layout, inside my adapter
class, when I've created this view holder class, after creating the TextView, I need
to reference the relative layout as well. So I can say private relative layout. Let's
name it parent. We also need to instantiate it inside the constructor down in here, I
can say parent is equal to item view dot find view by ID, once again, let's say r.id dot
parent. After defining this relative layout, now you can have access to that inside this
unbind view holder method. So for example, in here, I can say holder dot parent, which
is the name of this relative layout, dot, let's say set onclicklistener. You can see
that we have this option. Let's pass our interface new onclicklistener. And in here, I'm just
going to show some toast message. Let's say toast. If you remember when we have used this
make text method on our toast, we needed a context. But in here, we do not have access
to any kind of context. Once again, we will talk about context later on when we talk about
activities in Android, both inside this adapter class. Right now, we don't have any context.
So we have to pass it inside the constructor. For example, I'm going to define the context
as the member variable above in here, let's say private context. Let's name it context.
And after that, I'm going to receive that where this constructor, let's say context.
Let's name it context. Inside the constructor, I can say this context is equal to context.
Whatever now that I have changed this constructor, if I switch back to main activity, I should
see some red warning. When we have instantiated our adapter class, we need to pass our context
once again, because this time we are inside this activity, I can simply pass this. So
this way inside our adapter, now we have a context. And when we create our toast message,
we can pass that as the context. For the text, I'm going to show the name of the contact.
So I can say contacts the ArrayList that we had, let's say that get once again, I'm going
to use this position as the index.
Let's say that get me. Also, if you have noticed that as soon as I've used this position inside
this onClick method, because we are inside an interface, this final keyword has been
added to the declaration of this integer. Let's also add another text in here, let's
say selected. Let's quickly run the application. And let's see if we have correctly set the
onclicklistener on our relative layout. Let's click on the first one margravine selected
social arona social romance. It seems like our onclicklistener is working fine. Okay,
I think that's a good point to stop the video in here. I did want to talk about that image
URL and how to show that in our recycler view. But it seems like this video is getting a
little bit long. So I'm going to talk about that in the next video. Besides that we will
also talk about using a combination of recycler view and card view. The combination of these
two will generate a really beautiful look for our recycler view. Okay, see you in the
next video. In the previous video, we have created a functional recycler view. By functional
I mean, we have created a skeleton of our recycler view. But right now, it's very simple.
If you take a look at your contact list item that XML file, you can see that every item
in your recycler view is just a simple text view. In this video, we are going to work
around this and we are going to see how much flexibility we have when we are working with
recycler views. The first thing that I'm going to do in here is that to use a cart view instead
of this simple TextView and its parent which is relative layout, we are going to show our
contacts in a card view. For that first of all, we need to add the card view dependencies
into our project. In previous videos when we wanted to use card view in our project,
we have added the material design library. And from there we have used the material cart
view instead of using the material design library. This time I'm going to use the native
Android cards Let's see where we can find that card view the finances. So if you search
for card view dependencies, you will end up with a link like this, make sure to put Android
x in your search because if you search for Android, you may get some link like this.
Let's quickly take a look at that. In here inside this dependencies, you can see that
you are using comm dot Android dot Support Library. We are not using support libraries
in our project right now, if you remember, when we have created our project, we have
added the Android x artifacts. If you want to make sure of that, you can always check
your gradle file. Let's quickly do that. So in my project pane inside this Gradle script,
in this build that Gradle module app, in these dependencies, you can see that we are always
using Android x. If for some reason you're using the support libraries, you can implement
this support libraries version. But because we are using Android x, this dependency is
not going to be helpful for us. So make sure to include this Android x in your search.
And the first link from the official Android developers website is probably our needed
link. Then in here, you can see the dependencies, you can see that this time, we are going to
add the dependency from Android x packages. Let's copy this line of code. And let's add
it into our project. So once again, inside this built up Gradle module app file. Inside
these dependencies, we are going to add our dependency. Let's quickly do that in here.
And let's sync our project. Now that I've added cardview dependencies into my project,
I can use it in my layout files, for example, inside this contacts list item that XML file,
instead of this relative layout, I'm going to use card view, you can see that it's coming
from Android x packages. If you remember in card views, we had two attributes that can
be helpful in here. The first one is card elevation, this one in here, which will give
some 3d shapes to our card. For example, if I say five dp, we will get an elevation of
FTP. The next one is called corner radius. This one in here, which once again, I'm going
to save five dB, I will change these numbers if it was necessary. And also inside this
card view, instead of this TextView, I'm going to use a relative layout. The reason for the
relative layout is that because I'm going to include multiple UI elements, and I'm going
to use some of the attributes like layout below center, horizontal and attributes like
that. And those attributes are available inside a relative layout. So for that, first of all,
in here, I'm going to create a relative layout. For the width and height, let's say match
parent. And I think that's enough for our relative layout. Inside that, first of all,
I'm going to move this TextView. First of all, let's center this TextView horizontally,
let's say center horizontal and let's pass through. Beside that let's change the style
of this text. That's a text style. And let's change it to book. After that, I'm going to
create another text view and I'm going to show the contacts email. Let's add that after
this TextView. Let's change the contact name to contact email. And let's change the ID
to txt email. Let's also move this to below our contact name. Layout below, let's say
txt name. Let's also change the styling here. Let's say it's added I think it's better to
add some margin top margin top, let's say seven dp. Also I'm going to delete the center
horizontal attributes. Okay after these two texts, I'm going to show the contacts image
for that I'm going to create an image view. Let's say image view for the width and height
of this image view because I want to have a consistent image view I'm going to put the
numbers manually because my images are coming from the internet. And I don't know anything
about their size. For example in here I'm going to say 550 dP, I think that would be
fine. For the height as well. Once again 150 dP that's also give an ID to this image view.
Let's just say image. I'm going to move these two below my contacts email. Let's say layout
below. And let's pass the email. Let's add a margin top, I think 10 dP would be fine.
I'm also going to center this image via horizontally. So let's say center horizontal. And let's
pass through. Let's also add a placeholder, I'm going to say source. And let's address
one of our images in our package. I think this IC launcher background would be fine.
Okay, I think that's enough for our current view, I don't think we need anything else,
let's just have a quick look of what our current view looks like. I think I need to increase
this coronary radius. For example, let's say 10. In here, that should be better. Besides
that, let's increase this card elevation. You can play around with these numbers until
you're satisfied. I'm almost done with this contact list item file, I'm just going to
reformat my code so that when I publish this code, you and I have the same base code. For
that, I'm going to go to this code tab in here.
And down in here, I'm going to select this reformat code, you can see that the code has
been rearranged. Okay, now that I have changed this layout file, we need to also apply the
changes inside our recycler view adapter file as well. So down in here, when we have created
our view holder class, we need to address all of the new elements in our contacts list
item dot XML file. First of all, we have deleted this relative layout, and we have replaced
that with the current view. So in here, instead of using a relative layout, I'm going to say
current view. And let's also add our email text, I'm going to add it in the same line
with this txt name. Let's say txt email. And let's initialize it down in here inside this
constructor. So let's say txt email is equal to item view dot find view by ID r.id dot
txt email. We also need to add some quotes inside this unbind view holder method. For
example, we are going to show the contacts email, we need to do that in here. Let's say
holder dot txt email, dot set text. Let's pass our contacts dot get which for the index,
I'm going to pass position, let's say dot get email. before showing the image view,
I'm going to run the application. And I'm going to see that if everything is working
fine. So let's run the application. You can instantly see that with this view changes
how much we have improved our layout. But right now this layout needs some modification.
For example, we need to add some margin between these card views. And also we need to add
a margin top for this txt name. Let's quickly apply them. So if I switch back to my contact
list item dot XML file, for this parent card view, I'm going to add a margin that's a margin
top or instead of margin top, I'm just going to say margin. I think 10 dP would be fine.
Beside that, let's also add a margin for this TextView. Let's say margin top and that's
a seven dp. Let's also add a margin was thought for this email. So in here for this txt email,
I'm going to say margin start this one in here and I'm going to say five dp. Let's run
the application once again. You can see that it's much better. Now, you can also see the
two attributes that we have added for our current views much better in here. First of
all, we have added this corner radius, you can see that in here and also this 3d shape
with a shadow behind our card view is for that called elevation attribute. Beside that
the reason that we are seeing two columns in here is because of that grid layout manager
that we have passed to our recycler view. If you want to have only one column, you can
change that, for example inside our main activity. down in here, instead of a grid layout manager,
we can pass a linear layout manager. So in here, let's say new linear layout manager.
And let's pass our context. Before I run this application, I'm going to make sure that I've
centered my recycler view. Let's switch to our main activity layout file. And let's center
our recycler view. Let's say center horizontal. And let's fast through. Also I'm going to
change the width attribute in here to wrap content. Let's run the application once again.
Right now you can see that we have only three contacts in our screen but we can scroll our
recycler view that's we could do that. You can see that if our item do not fit on the
screen. Our recycler view is a scroller but besides that, let's also check the onclicklistener
Let's click on this link. cart view. And you can see that social romance selected, it seems
to be perfect. Okay, now let's work on this image view, let's see how we can show our
images. For that we have multiple options, the easiest one is to use an external library
called glide. Let's add that into our project. And we will talk about it. So if you search
for glide dependency, the first link from this GitHub page is the link to that external
library. Let's see what does it look like. down in here, you should see the dependencies.
First of all, we need to add these two into our repositories. Let's see if we have them
in our gradle file. Let's open our project pane in the Gradle scripts inside this build
that Gradle project spawn in these repositories, we need to add the Maven repositories. Let's
quickly add them. I'm going to copy this one in here. And I'm going to add it inside these
repositories for all projects.
Beside that, we need to add these two dependencies into our project. Inside this build dot Gradle
design module app, we need to add the dependencies. Let's sync our project. And while it's sinking,
let's also talk about the SQLite. So glide is an image loader library in which has developed
by Google so you can safely use it in your projects. glide has a lot of functionalities
in which one of them is to load images from the internet. Later on in the course, we will
see other functionalities as well. But if you want to know more about why they have
documentation in here, let's click on this vehicle lights documentation. You can read
this page if you want. But we will talk about glide more later on in the course. Okay, let's
switch back to our project. And it seems like we have added the glide library successfully.
Let's close these two Gradle files. Also, let's close this activity main dot XML file.
And let's go to our contacts recycler view adapter. That's java file. So inside this
unbind view holder method, I'm going to show the images of different contacts. For that
I'm going to use the glide inside this method. Let's see how we can use glide. In order to
show images with glide library, you can say something like this, you can say glide with
capital G. And after that, you need to pass your context, you can do that by saying that
with for the context in here, I'm going to pass my context. As a reminder, we have get
the context via the constructor of this class. In the previous video, I'm talking about this
one in here. Now that we have our context, we can use it with this width method. After
that, I'm going to say that this image is going to be a bitmap. So let's say that as
bitmap. After that, we need to pass the source of our image. In this case, we are going to
pass the image URL for our contact image, let's say dot load. And let's pass our context
image URL for that I'm going to say contacts that get once again, I'm going to use the
position as the index. That's a dot get image URL. After that, we need to pass the image
view that we want to show our image inside that I'm going to save that into. And I'm
going to address my image view, which I can say holder dot image. But for that, first
of all, we need to add this image inside this view holder class. We never did that. So inside
this view holder class, first of all, I'm going to say private image view. Let's call
it image. And let's also instantiate it inside the constructor. Let's say image is equal
to item view dot find view by ID r.id dot image. This is the simplest way of using glide.
First of all, we need to pass our context. After that, we need to say that it's going
to be a bitmap file. After that, we need to pass the source of our image with this start
load method. And after that, we need to specify the image view that we are going to show our
image inside that this is going to work fine. But before I run my application, I need to
do one more thing. We are going to load different images from the internet. For that we need
to add a permission into our manifest file for accessing the internet. So far, we haven't
talked about requesting permission. Let's quickly see how we can do that. So in our
project main inside the app folder, inside this manifest folder, if we open this Android
manifest dot XML file, we can add our permission in here above the application tag. In here,
I can open a tag and the tag is going to be users permission, this funding here and we
need to assess To find the kind of permission, let's search for internet permission, you
can see this Android permission that internet. Let's close our tag. There are much more concepts
regarding these permissions, we will talk about them later on in the course, for some
permissions, you need to create a UI logic in order to ask the user to grant that permission
to your application. But this internet permission is a safe permission. And if you add it to
your manifest file, the Android system is going to grant it automatically. That's all
we need to do in order to request for an internet permission. Later on, we will talk more about
permissions. Let's just run our application. And let's see if we can successfully show
different images.
You can see that we are seeing different images in here. But there was a problem when I ran
this application after adding the permission for the first time. The first time I wasn't
able to see the images. And that's because we have changed our manifest file. Whenever
you change your manifest file, you need to uninstall the application and install it once
again. So if you're not seeing different images, make sure to uninstall the application and
install it once again. After that, you should see different images. Let's also check the
others you can see that the images are loading from the internet after a few seconds. You
can see that our recycler view our cart views and also the glide library is working perfect.
The combination of these three we'll create a really beautiful layout for when we want
to show a list of different items. And probably by now you can see the flexibility and all
of the functionalities of recycler view, you can see that we can modify our layout files,
our list item layout files however we want. Okay, I think that's enough for this video
just to see our recycler view better. I'm going to change the linear layout manager
once again to a grid layout manager. And after that, we will finish our video. So let's change
it back to grid layout manager and let's run our application once again. You can see that
our RecyclerView seems perfect. Okay, I think that's enough for this video. In the next
video, we are going to see how we can use different fonts for our text views, for example,
and external font that we don't have in Android Studio. Okay, see you in the next video.
In this video, we are going to talk about different fonts, how we can apply different
fonts to our TextView. Whether that font exists in our project, or whether we are going to
add it externally. I believe this will be a short video. First of all, let's see how
we can add an external font into our project. Before that, I'm going to go to this layout
file and give an ID to this TextView. Let's just name it. txt Hello. In order to add an
external font in your Resources folder, you need to create a new directory, you can right
click on your resource folder. By selecting new and Resource Directory you can create
that directory. We have done this when we have created a menu resource file. In here,
you just need to define the type of your resource, you can search for font in here. And that's
all you need to do. Now you can see that you have this font folder, and you can copy your
external fonts into this folder. For example, I have prepared this font. But before adding
this font into my project, I need to do some modifications. For example, I need to change
the name of this one. In order to add this font into your project, you need to follow
some rules. For example, you need to change all of the upper cases into lower cases. And
also you shouldn't use hyphens, instead of that you can use on their lines. So in here,
I'm going to change the name a little bit. Let's change this opportunity to learn. After
you have changed the name, you can simply copy your font and inside your font folder,
you can paste it. The name is fine. Let's just press OK. Now in your font folder, you
have this Amsterdam, the TTF. If your font has some preview, you can see that by double
clicking on your font. Okay, now that we have added this font, let's see how we can use
it on our text view. Let's switch to our TextView mode. And on this TextView we have one attribute
called font family. This one in here. And now we can address our font, which is add
font slash amster. No. Let's use to split here and let's see if we can see our font
in the preview. You can see that right now the font hasn't been rendered, but I believe
this will be changed when we run our application. Let's run the application and let's see if
we can successfully change the font up artix view, you can see that this beautiful font
has been applied on our text the beside using your own fonts in your project, you can also
use some fonts called Google fonts. Let's quickly see how we can use them. So for that,
I'm going to switch to design view and search for font family attributes in here. But before
that, we need to click on this text view. Let's search for font family. You can see
that we have this attribute, and beside our font, which is right now at font Amsterdam,
you have this drop down icon. If you click on that, you can see that in Android, we have
some fonts that we can use. But besides all of these, we have these more fonts option.
And when we click on that, you can see that we have at least have a lot of more fonts.
Also, you can see the source in here it says Google fonts, these Google fonts are free,
and you can safely use them in your applications. You just need to add a license in your application
somewhere. Okay, let's see what do we have in this Google fonts. For example, we have
this calligraphy T or something like that, that we can use. In here you can see that
we have two options, create downloadable font and add font to your project. The first option
means that you don't want to add the font to your project. And you want to use the online
font every time that a user uses your application. If you are creating an offline application,
this option in here might not be that useful. Instead of that, I'm going to use the second
option which will add the font to my project. But it will increase the size of the final
apk file. Also, then in here, you can see this preview for some fonts, you have a more
than one preview. And you can select between them both for this font, we have only this
regular. Also down in here, you can see the license that you need to show somewhere in
your application. For the simple application. I'm not going to do that because this is not
going to be a commercial application. Okay, let's add this font into our project by pressing
OK, you can see that this calligraphy font has been added into my font folder. I believe
this will have a preview. So if I click on that, you can see a preview of your font.
Now if I run my application, I should see this font instead of this Amsterdam font.
You can see that the font has been applied successfully. For some fonts, you may have
different styles for example, regular or normal, Italia, bold and bold italic. Let's quickly
see a font that has those styles. And let's see how we can apply those styles on our text
views. So once again, I'm going to click on this drop down menu. And once again, I'm going
to click on this more fonts. The font that I'm going to use in here is called lobster
two. If you click on that, you can see that in this preview pane, you have four options,
I'm going to add them one by one into my project. For the first one, I'm going to name it lobster
to underline regular. And also I'm going to add it to my project, I'm not going to make
a downloadable font. Let's press OK. And let's do the same for the other three slides.
You can see that now we have four different lobster two fonts. If we want to apply them
on our text views, we can do the same as we did for the other fonts, which we had only
one font. For example, inside our text view. For this font family in here, we can pass
the fonts manually by specifying the name of our font, or we can pass this styling job
to the Android. For example, we can add a text style in here. And we can specify the
size which can be it's fairly bold or normal. Right now if I select text style as bold and
if I change this lobster to Bold Italic to just lobster to regular. Let's quickly do
that. If I run my application, I'm going to get a bolder style for this specific font.
Let's quickly run that. You can see that we have this bold font but this in here is not
accurate because we have added the style manually. This actually in here is using our lobster
to regular font. And if we want to use a specifically this lobster to bold font, we can create a
font family for that. Let's quickly see how we can create a font family. So in my font
resource folder, I can Right click, and by pressing new fonts resource file, we can create
a new font family. For the file name, I'm going to put the font name, which in this
case is lobster two. Let's create that. Inside this font family. I'm going to define four
fonts, we need three attributes in here. First of all, we are going to need the font itself.
For the first one, I'm going to pass the lobster to regular font. After that, I'm going to
pass a style for this font, let's say font style. You can see that in here we have two
styles Italia, and normal. In here, I'm going to pass normal. But if you want to pass a
bolder style, you need to pass that as a font weight. Let's quickly see that you can see
that we have this font fate attribute in which we can pass a number. Basically, for normal
fonts, these weights should be 400, I will show a link for where you can find the exact
font weight that you need. But for now, let's just pass 400. And let's finish creating our
font. After that, I'm going to create three more font elements. And I'm going to pass
the other three fonts, let's say font. Once again, we need to address our font. This time,
let's say lobster to retallick. For this style, I'm going to say Italian this time. And once
again, because this is not a bold font, we are going to pass 400 as the font weight.
Let's quickly add two more fonts for the bold styles. You can see that for the normal bold
font I first normal as the font style. But for the Italian font I first Italian, the
only difference between a bold and a regular size font is in this wait for the regular
view half past 400 both for the bold ones we have past 700. Before we go further, let's
quickly see where we can find the proper font weight. So if you search for font weight Android
developer, you can see this font weight from the developer.android.com. Let's quickly see
that.
And in here inside this companion properties, we can see different weights for different
fonts. For example, we have past 400 for the regular fonts, which is the correct number.
For the bold ones, we also passed 700 I believe in Android, you won't see much difference.
If you pass for example 500. When the fonts are being rendered, the only difference is
between these 407 100. So if you want you can check this webpage. Okay, let's switch
back to Android Studio. Now that we have created this font family, we can pass it in our activity
main dot XML file. But before that, you can see that we have a lot of warnings in here.
If you hover over one of them, it says that these attributes are only usable in API level
26 and higher. So if your application is running on API level lower than that, this font family
won't be applied. I've seen that some people use App namespace instead of this Android.
For example, you can see up in here, of course, you need to add the namespace. By pressing
ALT plus enter, you can see that this new namespace has been added. I'm not going to
talk about name spacing here. But basically it's like importing in XML file. In other
programming languages, you also have namespaces. Basically, it will define that where this
attribute is coming from. So some people are using this app namespace. They say that if
you use this app namespace instead of Android, the font family will be applied in lower APA
levels as well. But honestly, I didn't see that difference. So I'm going to change this
one back to Android. And in my activity, main dot XML file, I'm going to pass the font family.
So in here instead of passing lobster to regular I'm going to pass lobster to this XML file
that we just created. And now this way we are passing the styling job to the Android
system. So even though we are using this lobster to because we have set a style to bold in
here, at the end, our font is going to be rendered to this lobster to bold. Let's run
our application and let's see if we can see a bold font. You can see that we are seeing
the bold font. Once again the difference in here is that this time we are using exactly
this lobster to bold. But when we have passed lobster to regular and we have set the style
to bold, only the Android Studio increase the size. Sometimes there might be some differences
between different font styles. When the designer created the font, so this way of creating
a font family is much more accurate. Okay, just before finishing this video, I'm going
to show you how you can apply different fonts in Java. For example, inside this layout file
in the design view, I'm going to add a button. And by clicking on this button, I am going
to change the font of this TextView. Let's change the ID of this button. First of all,
to let's say btn, change font. And also let's add some constraints. And let's change the
text. I'm just going to say change in our main activity. First of all, let's initialize
these two items.
After that, let's set an onclicklistener. For our button. Let's say that set onclicklistener
new onclicklistener. Before this onClick listener, I'm going to create a typeface. And after
that, I'm going to change the type face of this TextView inside this onClick method.
So above in here, let's say typeface, you can see that it's coming from Android dot
graphics package. Let's name it typeface is equal to and here we are going to get a reference
to our font in our Resources folder. If I tried get resources in here, as we can see,
that gets resources that get font. And if I pass my font address, for example, if I
say R dot font dot, let's say Amsterdam, you can see that we are getting a red warning
in here. And the warning says that this method is only available in API level 26. and higher.
You can overcome this problem by using resource compat. Instead of this get resources method.
Let's quickly see that. So in here, I can say resource compat dot get font. First of
all, we need the context which I'm going to pass this. After that we need to address our
font, let's say our dot font dot Amsterdam. So instead of get resources, you can use this
resource compat, which is a good option for backward compatibility. After you have defined
your typeface inside the onClick method, you can say txt. Hello, that's that typeface.
And you can pass your typeface. As simple as that you can change the font of your text
to let's run the application. And let's see if everything is working fine. Right now the
font is lobster too. If we click on this button, we can see that the font is changing to Amsterdam,
it seems to be perfect. Okay, I think that's enough for this video. And also for that matter
for this section of the course. There are a lot more concepts and tools that you have
when you're designing your layout files. For example, you have navigation drawers, you
have bottom navigation views, you have animations that you can apply in your application. But
I'm not going to talk about them in this section of the course because there are a lot more
concepts that we don't know anything about them yet. Later on in the course we will see
a lot more tools for designing our layout files. Okay, in the next video, we are going
to have a quick challenge for this section of the course so that we make sure we have
learned everything that we have talked so far. See you in the next video. As I said
at the end of the previous video, In this video, we are going to have a quick challenge.
The purpose of this challenge is to make sure that we have learned everything that we have
talked so far. And here is our challenge, I want you to create this simple layout file.
You can think of this layout file as a simple registration for in which we are receiving
the user's name, email, password, gender, and also country. Beside that we have this
image view in here plus this button which normally in a normal application, this potential
navigates to the user's gallery, or maybe the camera in order to take a photo. But because
we don't know anything about that yet, this button and this image view are just for showing
purposes, these are not going to do anything. Just if you want, you can show a toast message
after clicking on this image button. Beside that we have this License Agreement text which
down below that you can see this checkbox if everything works fine. When we click on
the register, we are going to show a snack bar indicating that the user has registered
successfully. But if for example, the user leaves one of these edit texts empty you're
going to show a warning. Beside that we are going to make sure that the user agrees to
our license agreement. Okay, this is our challenge. As you can see, this is a simple layout file.
I'm sure that you can do it by yourself. But as you can see, we are going to practice a
lot of things that we have learned in In this section of the course, we are going to work
with a lot of UI elements beside that as you can see, we are implementing material theming.
Also, we are going to show a snack bars. And beside that we are going to see how we can
initialize and work with our UI elements in the Java file. Feel free to use any kind of
layout for that you want in order to design the simple layout file in here I have used
in constraint layout. But if you want, you can use linear layout or relative layout.
Okay, pause the video in here and go solve the challenge. Whenever you're done, come
back to see my solution as well. Okay,
I hope you solve the challenge, let's quickly see my solution as well. I'm going to start
by creating a new project. For the name of this project, I'm going to say UI challenge.
I'm going to save it API level 19. In this simple application, it shouldn't matter. Okay,
let's start working on our layout file. But before that, I'm going to close all of my
extra files and panes. As I said, I'm going to work with constraint layout. But if you
want, you can work with linear layout or relative layout. Before everything, let's remove this
TextView. And let's drag all of our items. First of all, we need an image view for the
image profile. For the sample data, I'm going to select one of the avatars, we also need
a button. Beside that we are going to need material design components. We will add that
in a minute. But before that, let's quickly drag all of our UI elements, we need four
edit texts in here, the first two are going to be in plain text, and the next ones are
going to be password. The first two are for the name and email. Let's quickly add two
passwords in here. The difference between this password and this plain text edit text
is that when you type something inside the password edit text, it won't show the text
itself, but you will see some dots. Also beside that we need a TextView for our gender text.
After that we need a radio group. Let's quickly add that inside this radio group we need four
radio buttons. Also, I'm not sure that if you have seen this way of dragging radio buttons
in your radio group in the design view. Previously, we have seen how to add radio buttons inside
a radio group in the TextView. But here is how you can do that in the design view. Basically,
you can use these components three, for example, I can drag my radio buttons to inside this
radio group. Let's add two more. Let's also quickly change the orientation of this radio
group. Let's search for the orientation in the attributes. This one in here, I'm going
to change it to horizontal. Beside that we need a TextView for the countries. And also
we need a spinner in here for selecting one of the countries that search for a spinner.
Let's quickly change the viets of this spinner to wrap content instead of match parent, this
is not match parent in here, it's just some ntps we are going to change that to wrap wanted.
Also I think it's better to put it above in here beside our country's text. And after
everything. If you remember we had the button. Let's quickly add that this is going to be
our button register. Beside that we need a TextView in here for our license agreement.
And also we needed a checkbox. Let's search for checkbox. And let's change the checked
value of this checkbox to true let's search for checked. Or you can see that down in here.
Also, I'm going to change the checked value of this radio button to checked as well. Also,
let's quickly change the idea of our UI elements so that we can use them in our Java file.
I'm going to fast forward the process of giving them an ID basically, you can select your
UI element and you can change the attributes from this top right pane in here.
Just a quick review, let's quickly see the ID of each one of them because later on you're
going to use them in our Java file. This one is image profile. This one is btn peak image,
edit text name edit text email, edit text password edit text pass repeat. txt gender
this morning here is our agenda for our radio group. The three radio buttons are named or
be male or be female and or the other. This TextView is called txt country. The screener
is called a screener country and this button is btn register. This TextView in here is
called a txt agreement. And this checkbox is called agreement check. We are also going
to need four more text views. If you remember I said that when we click on this register
button, we are going to make sure that we have received some input from the user. And
if for some reason the user don't enter anything on our edit text, we are going to show some
warnings and we are going to use text views for those warnings. So let's quickly add four
more text views. I'm going to add Warning for each one of edit text above the editors.
Let's quickly give some ID to these. For example, for the first one, I'm going to say txt var
name. You can guess
the rest of them. This one is txt one email. This one txt one pass. And the last one is
txt var is repeat. Okay, now that we have the ID for all of these UI elements, let's
quickly change the initial values. For example, for this TextView, I'm not going to change
anything, but for this edit text, I'm going to delete the text and I'm going to add a
hint. Let's search for the hint. That's just a name. Sometimes like this, you can see some
options, which can be annoying by pressing the Escape key on your keyboard, you can get
rid of that suggestion, let's do the same thing for these three remaining editors. For
this TextView, I'm going to change its value to let's say gender. For the radio buttons,
let's change the first one to make the next one female. And the other one, I'm going to
name either this TextView in here, let's change it to countries or country. Let's change these
buttons text to peak image. And this font to register. Also this checkbox to I agree.
Beside that, we need to change the value of this text view. But we are going to do that
later on when we created our constraints. I'm not going to do anything about this yet.
Also, I'm going to change the color of these four warning text views. Let's quickly do
that. Let's search for color. And let's change it to some red color, I think this one would
be fine. Now let's start constraining our UI elements. For example, for this image view,
I'm going to constrain it to the left of my screen and also to the top of my screen. But
of course, I need some merging. I will add that in a minute beside that the right of
this image view to the left of my button, and also the right of this button to the right
of my screen. Beside that I'm going to constrain this button to the bottom of my image view.
Similar thing for the top of my image view. Also, let's add some margin for this image
view. For example, margin top, let's say 32, I believe is fine. And let's move it to left.
But as you can see, when I move this image view to the left and this button do not move,
I'm going to delete this right constraint. And I'm going to drag it from the left of
this button to the right of my image view. Now let's constrain this image view to the
right of our screen. And now we can move it. It seems better now for these text views.
First of all, I'm going to add a horizontal guideline. So let's right click on our layout,
and let's go to this helpers. And let's add horizontal vide line. Let's move this guideline
to somewhere about here. I think it's fine. Beside that we need a vertical guideline.
Let's quickly add that helpers add vertical guideline. And let's move it a little bit.
Now let's constraint our edit texts. For example, this one to our guideline, I'm going to constrain
this TextView to this guideline as well. Also, I'm going to add a constraint from the top
of this edit text to the bottom of this TextView. But as you can see these are too close to
each other. What you can do is that by pressing down the CTRL key, you can select both of
them. And by right clicking, you can use this right click menu, you can go to this constraint,
let's say Edit Text name, let's say top to the bottom of our txt warning name, we can
see that now we have a constraint. Beside that I'm going to add a constraint from the
top of this TextView to our horizontal guideline. So once again, let's select both of them.
And let's right click constraint txt warning name top to bottom of our guideline, I think
I'm going to remove the margin here you can see that we have an 80 margin. Let's decrease
that to zero, I believe is fine. Let's do the same thing for the rest of edit text and
text views. For these three remaining text views, I'm going to add a margin top to the
top edit text but I'm also going to add a margin. let's right click constraint txt warning
email top to the bottom of our edit text name. Let's see what is the margin here 15 I think
I'm going to save it 16 Okay, let's do the same thing for the remaining two edit text
and text views.
We can see that everything looks better. Now. Also, if for any reason your computer is slow,
you can always go to this icon in here and you can disable This live rendering, or alternatively,
you can work with blueprint view, which you can show from this blueprint option in here.
Let's quickly see that you can work with this one as well. But like before, I'm going to
save it to design view. Okay, let's add a constraint for this gender text. First of
all, I'm going to construct it to the top of this edit text, constraint. txt gender,
top to, let's say, bottom of our edit text, let's also add a margin, I think a margin
24 would be fine, it seems too much, let's say 60. Let's also add a margin to the left
of our screen, let's add a margin in here as for the radio group, I'm going to select
the radio group from this component tree. For the left constraint, I'm going to add
it to this guideline. Also, I'm going to add a constraint from the top of this radio group
to the bottom of this gender TextView. Once again, I'm going to use the right click menu,
let's say constraint. It seems like I didn't select the radio group, you can always use
this component three if you want. Let's select both of them. let's right click constraint,
let's say radio group gender, top to bottom of RT x d gender, that seems to be better,
let's also add a margin, I think 16 would be fine. For the country text, I'm going to
do the same thing. For the left margin, let's say 16. Let's constraint it to our radio group.
constraint. txt country talk to the bottom of our radio for the margin, let's say 16
months again, for the spinner, I'm going to constraint it to the right of my country TextView.
And also its bottom and top to the bottom on top of this country TextView as well. Of
course, we need a margin left. Let's quickly add that. For this register button, I'm going
to constraint it to the three edges of my screen. But I need a margin button. Let's
add 32. For example, I think that's fine. For this TextView. I'm going to add another
guideline in here. Let's quickly add that first add vertical guideline, I'm going to
move this guideline, let's move it to somewhere about here. And let's concentrate our TextView
to this one in here. For the top, I'm going to constrain it to the vertical guideline.
Let's also constraint this checkbox. I'm also going to constraint the top of this checkbox
to the bottom of our TextView. Let's add another guideline in here. This one is going to be
a horizontal guideline. And let's move it down below this edit text. Now I'm going to
add a constraint from the bottom of this checkbox to the to this horizontal guideline, it seems
like we don't have a proper constraint in here. For that I'm going to delete this top
constraint, we can delete the constraint by selecting the constraint and pressing down
to delete key. Let's also add a constraint from the bottom of this TextView to the bottom
horizontal guideline. Also, I'm going to move it a little bit above. That seems better.
Now, let's also change the text of this TextView. And also let's change the visibility of these
four warning text to gone. First of all, let's add the text for this TextView. But you can
see that as soon as I typed the TextView in here, the text of our TextView somehow occupies
other layout files. And also it doesn't fit on the screen. If you want to fix that you
can click on this to arrow in here. And you can change the constraint to a match constraint
what we also need a margin. Let's quickly add eight in here. Also for the right. Okay,
that seems with it. Also, I think it's better to move this checkbox to above a little bit.
Let's click on that. And let's move it above. Okay, that seems better. Let's quickly change
the visibility of this for one new text views as well. Let's search for visibility. And
let's change it to gone. Similarly for the next three text views. But now that I've done
that, you can see that we have two small margins, you can increase that. For example, let's
say eight in here. Similarly for the next two editors, sorry for the repetition. Okay,
that's about the size of our layout file. We also need to add some entry for our explainer.
Let's quickly add them in our strings values in our resources in values folder inside the
strings. I'm going to add an array string,
let's say array. Let's name it countries. And let's add our items. Now in our activity
main dot XML file, we can pass this as the entries of our espinar. Let's say For entries,
and let's address our country's array. Okay, that seems better. Besides that, for this
image view, we also need a source. Let's search for source in here. For some reason we can't
see source in here, let's switch to text view or this split view. And let's add it in here.
Let's say source. And let's pass this mid map file that we have in our project. Before
testing the application, let's quickly add the material design team. For that, I'm going
to go to material.io. Let's go to this Developer tab. We have seen all of this before in this
Android in the documentation getting started. down in here you can see the dependency. Let's
add that in our project Gradle script build dot Gradle module app. down in here inside
the dependencies. Let's add that we also need to add a version in here which as you can
see in the warning, the latest version is 1.1 point oh, let's sync our project. And
let's go to our styles and change the theme of our application. I'm going to delete this
theme in here. And I'm going to use team dot material components that lights this one.
We have used these previously in previous videos, let's switch back to our activity
main file, you can see that the state of our buttons has changed. Let's quickly test everything
I'm going to run the application on pixel three API 29. It seems like we have done a
relatively good job, we just need to increase the size of this image. For that I'm going
to change the width and height of this image view. I'm going to add them manually, let's
say 130 dP, I think that would be fine. Let's run the application. Once again, if you want,
you can also decrease the size of this TextView as well. Yes, that seems to be better. Your
design might be different than mine. It's okay. It's probably better than me. I'm not
a designer, but we just wanted to practice everything that we have learned. Okay, I think
that's enough for this video. In the next video, we are going to switch to our Java
file and we are going to work on the logic of our application. See you in the next video.
In the previous video, we have created this layout file, which isn't that bad. In this
video, we are going to work on the Java file and the logic for this application. So for
that, let's switch to our main activity dot java file. And let's close our layout file.
First of all in here, I'm going to initialize all of my UI elements. Before that, let's
quickly add a lock, I believe it will be useful, let's say private edit text we had for edit
text, I'm going to fast forward the process of defining and initializing these UI elements,
you can see that it's very boring. I'm also going to define a constraint layout element
in here, let's say private constraint layout. I'm going to name it parent, I believe later
on, we will use this constraint layout to show a snack bar, I also need to give an ID
to my constraint layout. So for that, let's switch to our activity main dot XML file in
our split view, in the parent constraint layout, I'm going to give it an ID. Let's say ID,
I'm going to say parent. Okay, now let's initialize all of these UI elements. I'm going to do
that inside another method. Let's name that method it needs to be used. First of all,
let's create that method down in here, private void in interviews. First of all, let's add
a look. Let's get started. Let's minimize this project thing. And let's initialize once
again, I'm going to fast forward the process of initializing these UI elements. I think
I have a typo in here. But I have set an ID for this pretend register. Let's quickly fix
that in our layout file. If I click on this register button in here, you can see my type
of sorry for that, I also need to change it in here. Okay, now that we have initialized
all of our views, I'm going to set an onclicklistener. First of all for my btn peak image, I'm talking
about this one in here. So after this method, I'm going to say btn peak image dot set onclicklistener
new onclicklistener.
Let's just show a toast message. We haven't seen how to pick an image for example from
a gallery, or for that matter to take a picture using the device's camera for that. We are
just going to show this first message. After that let's define an onclicklistener for our
btn finish or btn register. I believe I named it set onclicklistener new onclicklistener.
I'm going to do the job inside another method, let's say in its register. Let's create that
method down in here. Let's say private void we need to register Once again, let's add
a look. Let's say start in here. First of all, I'm going to check that if the user has
entered all of the data for that, I'm going to say if I'm going to create another method,
I will create that in a minute. But let's say if validates data. If that's the case,
we are going to continue. Let's quickly create this method down in here. And let's see what
we are going to do inside this method. Private the return type is going to be a Boolean,
let's say private, Boolean, validate data. Once again, let's add a lot. Let's say if
edit txt name dot get text.to string if it's equal to an empty string. First of all, we
are going to show the warning text. So let's say txt warning, txt warning name, dot set
visibility. Let's fast be Zeebo. I believe you have seen this previously in the course
after that we are going to return false it means that we didn't validate the data so
we shouldn't continue our code. In this method inside this if statement, let's create three
more if statement for the other three edit texts. If edit text, email dot get text.to
string is equal to an empty string. txt warning email, dot set visibility, once again, is
also going to return false in here as well. Also, in each one of these cases, before returning
false, we are going to change the text of our txt warning. Because if you remember,
in the layout file, we didn't change the text of that burning text view. So I can say txt
warning name, for example, dot txt, let's just say enter your name. Similarly for the
other three cases. And after all of these if statements after all of these cases, if
we reach to this point we are going to return true, it means that the user has entered all
of the data. So inside this validate data methods, we are validating that the user filled
all of the blanks after that inside this init register method inside this if statement,
we are going to check that if the user agrees to our license agreement. So let's say if
agreement check that is checked this option in here. If that's the case, we are going
to continue. But in the else case, we are just going to show a toast message. Let's
say you need to agree to the license agreement. But if the user reaches to this point where
the user enters all of the data and also agree to the license agreement, we are going to
show a snack bar. Let's do that in another method, let's say show a snack bar. Let's
create that method down here. Private void show snack bar. Once again, add a log. And
then in here inside this method, first of all, we need to change the visibility of all
of our warning texts, because those warnings are no longer valid. So let's say txt warning
name that says visibility, let's say gone This time, let's do the same thing for the
other three remaining burning texts. After that, we need to show our snack bar. If you
remember, we can do something like this, we can say snack bar that make First of all,
we need to pass our constraint layout, which is our parent in this case, after that we
need to text in here right now I'm going to show a dummy text. For example, I'm going
to say user registered. But later on, I will change this text for example, I will show
the details of the newly registered user. For now let's say user registered. After that
we need the constant for the length of our snack bar, I'm going to say snack bar dot
length indefinite. Let's also define an action button for our snack bar. I'm going to say
that's an action. For the text, I'm just going to say dismissed.
Let's also create an onclicklistener, let's say new onclicklistener. For the time being
I'm not going to do inside this onClick method. Later on, we will clear our edit text. Let's
finish creating our snack bar. But before that, we just need to show it. Let's say dot
show. Let's run the application. And let's see if we have right everything correctly.
First of all, let's click on the speak Image button, we should see a toast message. You
can see that you have to be talked about. Let's enter some data. And let's register
a new user. Let's say Mesa, let's add an email. Let's enter a password. Let's re type our
passport. We also need to check that if these two are the same. I forgot to do that. I will
do that after the testing of this phase of the application before registering the user.
I'm going to uncheck this alone. License Agreement checkbox, let's say register, you can see
that you need to agree to the license agreement. Let's check it once again. And let's register
this time we should see this network user register, it seems like our application is
working fine. If we press dismiss, the snack bar will be dismissed. Also, let's delete
this email in here. And let's see if we get the warning text. If we click on this register
button, you can see that insert your email, you're successfully validating our data. If
we enter an email in here, for example, may sound@gmail.com. And if we click on the Register
button, we should see that the email warning text disappears. Okay, now I'm going to change
the application. First of all, I'm going to check that these two are the same. After that,
I'm also going to empty all of these for any text. When I click on this dismiss button
so that we can add a new user. Also, I'm going to change the text of this snack bar to the
details of our newly registered user. Let's quickly do that. First of all, inside this
validate data method, I'm going to add another use case, let's say if edit text passport,
this font dot get text to string. If that's equal to our edit text, password, repeat that
get text to string. If that's the case, or let's change our logic, let's say if that's
not the case. First of all, we are going to change the visibility of this txt warning
password repeat. And after that, we are going to return false let's say p x the warning
password repeat dot set visibility, let's say visible after that, I'm going to change
the text I'm going to say txt warning, repeat that set text. Let's say password doesn't
match. Up. After that I'm going to return false. Okay, now that we have validated this
data successfully, let's go to this onClick method. And in here, let's clear our edit
text. I'm going to say Edit Text email or any text name, that's a text. Let's just pass
an empty string. Similarly for the other three edit text. Next thing that I'm going to do
is that I'm going to change the text of this snack bar. First of all, I'm going to create
that text above in here, inside this short snack bar method. Before everything I'm going
to get the user's data. For example, let's say the string name is equal to edit txt name,
dot get text.to string. Similarly, for the email, I'm going to say a string email is
equal to edit text email dot get text.to string, I'm also going to get users gender and also
country, let's say a string country is equal to countries a spinner that get selected item.to
string for getting the user's gender, I'm going to create a switch statement before
that I'm going to create a string, let's say string gender is equal to an empty string.
After that, let's create a switch statement on our radio group, or ad group gender that
get checked radio button ID. Let's say in case it's our.id dot radio button male, we
are going to change the value of this gender string, let's say gender is equal to make
Sorry about that. Let's also add a break in here. Another case our.id dot female. Let's
say gender is equal to female. This default case should never happen. But just for security
purposes, if something goes wrong, I'm assigning unknown to the gender a string. So after the
switch statement, we have all of the data about our user. Now we can create the text
that we want to show inside the snack bar. Let's say let's drink a snack text is equal
to
and let's create our text. First of all, I'm going to say name, plus the name that we created,
or we got from the Edit Text name. After that, I'm going to add a backslash n in order to
go to the next line, let's say email, plus the email. Once again and backslash n. Let's
also show the gender and country, let's say gender, plus the gender, once again, backslash
n and also let's show the country. Now that we have created this text, we can show it
in our snack bar. So this time, instead of this text, I'm going to save the snack text.
Let's run the application once again. And let's see if we have fixed our application
successfully. Let's test the different cases. First of all, let's check that if we see the
warning if we enter different passwords in these to edit text. In the password field,
I'm going to enter 123456 but in the reenter password edit text, I'm going to say 12345.
If we click on this register button, we should see the warning text password doesn't match.
Okay, let's add six in here. Now they do match, let's change the country to, for example,
Switzerland. And also let's change the gender in here to female. And let's see if we can
reduce there any user. In here you can see that we are seeing the snack text that we
have created. But we are not seeing all of it, we are seeing only the first two lines.
If you want to change the maximum line attribute of your snack bar, you can do that. First
of all, you need to get the instance of your snack bar. After that, you need to get the
text view of your snack bar. And after that, you can change the max line attribute on your
text view. This way, you can override the maximum line attributes of the text view of
your snack bar. But that seems like a lot of work. And we are not going to do that in
here. We will do that later on in the course in other places when we use a snack bar. But
in here, if you want to make sure that you have created your snack tics successfully,
you can simply log it. For example, in here, I'm going to add a look. Let's say log D,
let's say a snack bar text. Let's pass our snack text. But before I run my application
and test this, I'm going to click on this dismiss button. If you remember, we write
the logic so that we clear all of our edit text. Let's click that. And you can see we
are now ready to register a new user. Let's run the application once again. And let's
test this snack text. For that I'm going to open the logcat. Let's enter some data in
here. And let's click on this register. You can see the snack text in here name may Some
may be some@gmail.com gender male country, Norway, it seems like we have created our
text successfully. Okay, I think that's enough for this video, I hope that you have solved
the challenge. In the next section of the course we are going to create the first of
four of our applications. Now we know about Java. Now we know about user interface. And
in the next section of the course we are going to combine these two and we are going to create
our first real world application. Beside practicing everything that we have learned so far, we
are also going to learn a lot of new stuff as well. So stay tuned for the next section
of the course. And as always see you in the next video. What's up everyone, I hope you're
having fun. Before we start creating our sample application, I would like to remind you that
there is an extended version of this course that you can watch. by enrolling in the extended
course you will have lifetime access to more than 60 hours of videos, you can ask your
questions directly from me and I will come back to you within hours, you will have access
to all of the source codes that I wrote in the videos. And also you can have access to
all of the new videos that I upload, just check out my code.org for more details about
the course. And don't forget to use Free Code Camp as the coupon code to get 20% discount.
If you want to get serious in Android app development. Taking the extended course is
almost a necessity. Okay, everyone, I just wanted to remind you that there is an extended
version of this course that you can take without further ado, let's start creating our sample
application. Have fun learning.
Hello, everyone, and welcome back. In this section of the course we are going to create
the first application of the for real world application that I promised. It's important
to say that in creating this application beside practicing everything that we have talked
so far, we are also going to learn a lot of new stuff. You will see them in action when
we create the application. Okay, now let's talk about application itself, we are going
to create an application that will help the user to manage his or her library. In this
application, we are going to show a list of different books to the user. And the user
can see the details of that book. later on. If the user wants he or she can add that book
to different lists, for example, at least that indicates that the user has already read
this book, or maybe a wish list or also a favorites list. We will see all of them later
on in the course. In the process of creating this application, we are going to practice
all of our knowledge about user interface and Java. And beside that we are going to
learn about a lot of new concepts. After watching this section of the course you can change
the application that we create together and you can even publish it in the Play Store.
So I highly suggest that you watch this section of the course and don't skip it. Okay, without
more talking, let's create our project and let's start writing our application. I'm going
to start with empty activity as before. Let's change the name of this application to my
library. For the other fields in here, I'm not going to change anything For the package
name, once again, I'm going to put my website's name in backward, or dot maker dot project
name, which is my library to save location is fine languages Java, and we are going to
save with API level 19. For the minimum SDK, we are also not using the support library,
so I'm not going to check this option in here. Let's create our project. And let's start
working on our layout file. Before everything, I'm going to add material design library dependencies
into my project, because we are going to need them for that, let's go to material.io. Let's
go to this Developer tab. Let's select Android from here, documentation getting started.
Let's copy this line of code. We have done this previously. So I'm not going to export
anything here in our Gradle scripts, build that Gradle module app into dependencies.
Let's add that. And also the latest version is 1.1. Point. Oh, so let's change this one.
And let's sync our project. It seems like we have added the material components successfully.
First of all, let's change the style of our application in values as those that XML instead
of this app compat theme, I'm going to use theme that material components that light.
Let's close this. And let's switch to our activity main dot XML file. Our application
is going to have multiple pages later on, we will see how we can create multiple pages
or to be specific multiple activities. But for now, this is going to be the first page
or the first activity of our application. And let's quickly design that. First of all,
I'm going to have a text view for the name of this application. After that, I'm going
to add a few buttons. One for the list of all books, I'm going to show the list of all
of books by clicking on this button. I'm going to add five more buttons and I will talk about
them. Let's also add an image view above in here for our logo. For now I'm going to stay
with one of our avatars. And also I'm going to add a text view in here for licensing.
Let's give some ideas to our elements. For this one, I'm going to name it IMG logo, because
it's going to be our logo. For the TextView I'm going to say txt name, but I'm not sure
that we need an ID for this one. This button in here is going to show a list of all of
our books. So let's say btn all books. Also, let's change its text to show all books or
see all books. Beside that I don't like that this text is in all caps. If you want, you
can change that you need to search for all caps, this text all caps, and you can change
its value.
Let's change it to false. Now you can see that this text is much more better. For the
second button, I'm going to change its ID to btn currently reading because this button
is going to show the list of the books that the user is currently reading, let's say btn
currently reading. And let's change the text to currently reading books as well. Like before,
let's change the text all caps value to false as well. For the third button, I'm going to
change its ID to btn already read because this button is going to show the list of books
that the user already read. So let's say btn already read. Let's change the text to already
read books. Once again, let's change the text all caps attribute to false. For the fourth
button, I'm going to change its ID to btn want to read book. Let's say btn want to read
because this button is going to show the list of different books that the user wants to
list. Basically, this is the wish list. Let's also change the text. Let's say your wish
list. Let's change the all caps attributes. That seems better. The fifth button I'm going
to change its ID to btn favorite books, let's say btn favorite. And for its text, I'm going
to say see your favorites. Once again, let's change the all caps attribute. And for the
last button, I'm going to change its ID to btn. About by clicking on this about button,
we are going to show a dialog in which we will show some information about this application,
let's say within about, and let's change its text to about. For this button, I'm not going
to change its all caps attributes, because I think it's better this way. Let's also give
an ID to this text view as well. Let's see. txt license, I'm going to change its text
to developed by mesa@miko.org. I'm also going to change the style of this book to atelic.
Let's search for text style. Let's change this font to its attic. Also, let's change
the text of the first text view to the name of our application. I'm just going to put
my library in here. Later on, we will change the font of this TextView as well. Okay, let's
add our constraints. Also, you can see that I'm using the constraint layout in here, but
feel free to use the relative layout or linear layout. I'm more comfortable with constraint
layout. So I'm using constraint layout, let's add our constraints. I'm going to add a margin
top of maybe 70 dP for this image view. But before that, I'm going to move my TextView
because right now I cannot see that. Okay, now let's add the margin top, I think 50 dP
would be better. Now let's concentrate our TextView to the bottom of our image view.
And also to the both side of our screen. We also need a margin top. I think once again
50 dP will be fine. It seems too much, let's say 40 dP, let's concentrate all of these
buttons. For this one, I'm going to constraint it to the bottom of my TextView. And also
to both sides of my screen, I need the margin top, let's say 30 dP. After that, let's constrain
the second one, I'm going to fast forward the process in here. Sometimes like this,
if the items are too close to each other as before, we can select both of the items. And
by right clicking we can use this right click menu. Let's say we can currently reading top
to bottom between all books. Let's add a margin in here by selecting our button margin top
I think 16 would be fine. It seems too much. Let's say 10 db. Also for the second month.
For this last TextView in here, first of all I'm going to constrain to to the bottom of
my screen. And also beside that I'm going to concentrate it to the both edges of my
screen so that it would be centered. Also, let's add a margin bottom of maybe 16 db,
that seems better. For this image view, I'm not going to use a sample data. Instead, I'm
going to show the actual image. I have prepared an image in here on my desktop. Let's use
that I'm going to copy this image. Let's add it into our mid map folder. Resources map,
right click and paste I think the name is fine. And let's switch to our split view.
For this image view, if you click on that, first of all, let's delete this last attribute
this tools source compat and instead of that, let's use source. let's address the image
that we just added into our project. It seems too large, we need to pass the width and height
manually. Let's see 150 dP I think that's fine. And also 150 dP for the height. That
seems a little bit better. For this mind library. If you remember we had an attribute called
font family. Let's search for that in the design view. font family if we click on this
arrow more fonts. Let's search for a good font. Once again, these are the Google fonts,
and you can use them in your projects free of charge. I think this lemon font is good,
let's add font to our project. And let's press OK. You can see that the font has changed.
Let's also increase the size a little bit. Let's search for text size. And instead of
14 sp, let's say 18. So that seems better. The next thing that I'm going to change in
this layout file is the width of all of these buttons. I want my buttons to have the same
width right now you can see that these have an ugly shape. If we click on one of our buttons,
let's switch to a split view. Once again, let's click on that. For the width. If we
say 200 DPI, I think that would be fine. Yes. For all of our buttons for the video, I'm
going to say 200 DPI,
you can see that it's much better now. Okay, this is going to be the layout for the first
page of our application. Let's quickly initialize the necessary items in our Java file. By pressing
double shift, you can search for main activity dot java file. And in here, let's initialize
our UI elements.
Let's also initialize these buttons down below inside this onCreate method. I'm going to
do that inside another method, let's say you need to be used. And let's create that method
by pressing ALT plus enter. Once again, I'm going to fast forward the process of initializing
these buttons.
By clicking on each one of these buttons beside this within about I'm going to navigate the
user to another activity. Up until this point, our applications had only one activity, which
was named main activity. But if we want, we can create another activity in our application
as well. We are going to talk about activities and fragments in the next video. But creating
an activity is really simple. There are a lot more concepts regarding activities, we
will talk about them later on. But in here, if you want to create an activity, you can
do something like this in your project inside Java folder. Inside your project folder, you
can right click on that by saying new activity. In here you can see that you have few options.
The names of some of these activities may be familiar from that gallery view from where
we created our project. But basically in here, we need this empty activity. You can also
select this gallery, in which you will get the same dialog when you created your application.
Let's select empty activity. Once again. In here we can name our activities. The convention
in here is that to use the keyword activity in naming and also, you cannot use a spaces
and also other strange characters. For the name of this activity in here, I'm going to
name it all books activity, because this is going to be the activity that we are going
to navigate the user when the user clicks on that see all books button. So let's say
all books activity.
You can also see a checkbox in here it says that if you want to generate a layout file
for your activities, it is possible to create an activity with only the Java file and not
the XML file. We will talk about that in the next section of the course. But in here, we
do need a layout file. So I'm going to leave this one checked in here. You can see that
when I changed the name of my activity, also the layout name changed as well. In the layout
name, you cannot use upper cases letter. And as you can see all the letters change to lower
cases. Beside that you cannot have spaces. You have another option in here to indicate
that if this activity is going to be a launcher activity. Basically, in every application,
one of the activities can be launcher. Right now the launcher activity in our application
is this main activity. It means that when we launch our application, this main activity
is going to be shown. And for that I'm not going to check this option in here. Also,
if you don't check this option in here, if you change your mind, you can change in your
manifest file later, but more on that later on in the course, the package name is fine,
we are going to create this activity in our package. And for the language of this activity
once again, I'm going to use Java. Let's finish creating our activity. And let's see what
happened in our project. So first of all, a new Java class has been added into our package,
you can see that we have this all books activity dot java file. Beside that in our Resources
folder, in the layout folder, we have this activity all books dot XML. Both of these
has been created by the Android Studio. Beside that if you take a look at your manifest file,
this Android manifest dot XML, you can see that in here we have a new line, we have a
new activity tag in our application tag, it means that now our application has two activities.
If you want to create your activities yourself without the help of Android Studio, that is
possible. Basically, you can create a java file. After that you can create a new layout
file. And beside all of these, you need to add this tag into your application tag inside
the manifest file. Also, you need to link the Java file and layout file in your onCreate
method. We will take a look at that in a minute. But instead of all of these, we have used
the help of Android Studio. Okay, let's close this manifest file. And let's switch to activity
all books dot XML file, you can see that in here we have another layout in which we can
design for this activity. In this video, I'm not going to design the layout for this activity,
instead, I'm just going to change its background color. If we switch to a split view in here,
once again, you can see that we have a constraint layout. And inside the opening tag of this
constraint layout, I'm going to say background, and I'm going to pass a color. For example,
I think I'm going to pass this color accent. I'm doing this because I'm just going to indicate
that we have navigated the user from the main activity to this artbooks activity. Okay,
now let's see how we can navigate the user. Let's close both of these files. These are
all books activity dot XML file, and also this albox activity dot java file. I said
that we are going to navigate the user to the other activity by clicking on this button
all books activity, so we need to create an onclicklistener in here, let's say btn. All
books that set onclicklistener. Let's pass in the onClick listener. And inside this onClick
method is the place to write the logic to navigate the user. In here, I'm going to use
an object that I have never used so far, and that object is called intent. You can see
that it's coming from Android dot contents package. Let's import that. Let's name it
intent is equal to new intent. It's a simple Java class that we have defined. It's like
any other Java class. The constructor of this intent needs two things. First of all, we
need to pass a context in here, because we are inside this main activity. And hopefully
by now we know that activities are context, we need to pass main activity dot this. Let's
pass that main activity dot this. After that we need to pass the destination activity where
you want to navigate the user to for example, I can say all books activity dot class.
So this way, we are saying that this intent is going to navigate us from the main activity
to all books activity. After that when we created our intent, we can call an inner method
in here called start activity, you can see that this start activity needs an intent.
And we can pass our intent safely. This start activity is an inner method like this fine
view by ID which exists inside every activity. Now if we run our application, we should see
that when we click on this button, the user will be navigated to all books activity. Let's
quickly test that. Right now you can see that we have few issues with our layout. First
of all, we cannot see this license TextView for that we need to decrease the margins from
here. And also beside that this currently reading books text does not fit in our button.
We will increase the width of all of our buttons as well. But for now, if we click on this
see all books button, we can see that we are navigating to the other activity. Beside that
if you press this back button. Once again we will be navigated to our main activity.
It seems to be working perfect. Let's quickly fix the issues with our layout and let's To
finish off this video in our activity main dot XML file. First of all, let's decrease
the margins. For example, this margin top, I'm going to change it to 30 dP beside that
margin top of this my library text. Let's decrease that to 30 dP. That seems better
for all of my buttons. Instead of 200 dP, I'm going to say 230 dP. For example, for
the first one, let's say 230 dP. Let's run the application once again. And let's see
if we have fixed the issues successfully. The layout seems better. Okay, I think that's
enough for this video. Just before I finish off, I'm going to say that you can check the
source code for this application@maker.org slash code. So feel free to check that if
you need I upload the source code at the end of every video. Also, we'd be very happy to
see your feedback. Okay, in the next video, first of all, we are going to create a model
for our books beside that we will create a recycler view and recycler view adapter to
show a list of different books inside the second activity. See you in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.