Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,450 --> 00:00:04,980
Hello, everyone, and welcome to the complete
Android app development masterclass. My name
2
00:00:04,980 --> 00:00:10,620
is Mason. I'm a professional Android app developer.
And I'm going to be your instructor for this
3
00:00:10,620 --> 00:00:15,150
course. Before starting the course, let's
talk about it for a few minutes. This course
4
00:00:15,150 --> 00:00:20,690
is about 15 hours. And by watching it, you're
going to learn a lot about Android app development.
5
00:00:20,690 --> 00:00:26,220
But it is impossible to cover everything in
just 15 hours. For that I recorded an extended
6
00:00:26,220 --> 00:00:30,710
version of this course, which is about 60
hours. And in that you're going to learn everything
7
00:00:30,710 --> 00:00:35,460
that you need for a successful career in Android
app development. You can find the extended
8
00:00:35,460 --> 00:00:39,950
course by clicking on the link down below
this video. Let's see what we are going to
9
00:00:39,950 --> 00:00:45,070
cover in this video. In this 15 hours, you
will start from the very beginning. That means
10
00:00:45,070 --> 00:00:50,540
that even if you don't have any prior experience
in programming, you're going to do just fine.
11
00:00:50,540 --> 00:00:54,790
We will cover everything in the course in
the first section of the course he will set
12
00:00:54,790 --> 00:00:59,309
up the environment that you need for Android
app development. That includes installing
13
00:00:59,309 --> 00:01:05,180
Java Development Kit, Android Studio software
development kit for Android app development,
14
00:01:05,180 --> 00:01:10,039
and emulator for testing your applications.
Besides setting up the environment. In the
15
00:01:10,039 --> 00:01:15,560
first section, you will create a small application
to get familiar with Android app development,
16
00:01:15,560 --> 00:01:20,149
and each piece in an Android application.
In the second section of this course you will
17
00:01:20,149 --> 00:01:25,630
learn Java language Java is the most popular
language for making Android applications and
18
00:01:25,630 --> 00:01:29,960
in this section, we will take a deep look
at that you will learn about different variables
19
00:01:29,960 --> 00:01:36,000
and operators, loops and conditional statements,
different collections in Java concurrency
20
00:01:36,000 --> 00:01:41,939
and trading, handling exceptions Singleton
pattern and a lot more. You will also learn
21
00:01:41,939 --> 00:01:48,100
about object oriented programming in Java.
That includes things like classes and interfaces,
22
00:01:48,100 --> 00:01:53,859
inner classes and abstract classes, inheritance
and polymorphism. And a lot more, we will
23
00:01:53,859 --> 00:01:58,090
end this section with a challenge. So to make
sure that you have learned everything we have
24
00:01:58,090 --> 00:02:03,539
talked about. In the next section, you will
learn about user interfaces, you will learn
25
00:02:03,539 --> 00:02:08,200
about how to design modern layouts for your
applications. In this section, we will talk
26
00:02:08,200 --> 00:02:13,739
about things like buttons and text views,
different layouts, working with images and
27
00:02:13,739 --> 00:02:19,880
fonts showing a list of items with ListView
and recycler view. We will talk about snack
28
00:02:19,880 --> 00:02:25,110
bars and card views, adding animations to
your applications. And last but not least,
29
00:02:25,110 --> 00:02:30,070
we will talk about material design in Android.
Once again, we will end this section with
30
00:02:30,070 --> 00:02:34,689
a challenge so to make sure that you have
learned everything. After learning Java and
31
00:02:34,689 --> 00:02:40,430
designing user interfaces, you have the ability
to create a lot of applications. In the final
32
00:02:40,430 --> 00:02:45,379
section of this course we will create one
real world application from scratch. We will
33
00:02:45,379 --> 00:02:50,030
combine all of our knowledge about Android
app development and we will create this application
34
00:02:50,030 --> 00:02:54,660
together in this section beside practicing
everything we have learned so far, you will
35
00:02:54,660 --> 00:02:59,530
learn about a lot more topics. For instance,
you will learn about persisting your data
36
00:02:59,530 --> 00:03:04,659
with shared preferences. And also you will
learn how to show different websites in your
37
00:03:04,659 --> 00:03:08,969
application. And of course a lot more. You
can also include this application in your
38
00:03:08,969 --> 00:03:17,349
portfolio if you want. We are going to set
up the environment that we need in order to
39
00:03:17,349 --> 00:03:22,099
create Android applications. We are going
to install few things. First of all, we are
40
00:03:22,099 --> 00:03:27,939
going to install Java Development Kit or as
some might call it JDK. After that we are
41
00:03:27,939 --> 00:03:32,870
going to install Android Studio. And after
everything we are going to create a virtual
42
00:03:32,870 --> 00:03:38,190
device in order to test our Android applications.
As you can see, I have already downloaded
43
00:03:38,190 --> 00:03:44,239
JDK and Android Studio. Let's also see where
we can get them from. For JDK, you can simply
44
00:03:44,239 --> 00:03:50,170
search for Java JDK, and a link from Oracle
website should pop up. The first link from
45
00:03:50,170 --> 00:03:54,969
the Oracle web page is probably our needed
link. As you can see, we are going to download
46
00:03:54,969 --> 00:04:01,159
and install JDK version eight, it's going
to be enough for our purposes in this course,
47
00:04:01,159 --> 00:04:06,010
down in here. First of all we need to agree
to the license agreement. And after that,
48
00:04:06,010 --> 00:04:10,269
depending on your operating system, you can
download one of these versions. I'm on Windows,
49
00:04:10,269 --> 00:04:16,359
so I'm going to download and install this
windows x 64 version. If you're not sure about
50
00:04:16,359 --> 00:04:23,350
this x86 or x 64, you can right click on your
my computer on your desktop in properties.
51
00:04:23,350 --> 00:04:28,940
In here you can see the architecture of your
CPU minus 64 bit for you it might be x86.
52
00:04:28,940 --> 00:04:34,440
Depending on that select one of these two
files, just click on this download and the
53
00:04:34,440 --> 00:04:39,750
download should be started. Sometimes like
here, it might ask you to log into your Oracle
54
00:04:39,750 --> 00:04:45,400
account. Let's do that creating an Oracle
account is free and you won't be charged.
55
00:04:45,400 --> 00:04:50,690
Okay, let's select Sign in. And as you can
see the download has begun. I'm going to select
56
00:04:50,690 --> 00:04:56,530
the canceling here because I have downloaded
the JDK previously. Let's select that. It's
57
00:04:56,530 --> 00:05:01,760
going to be a really simple installation process.
Let's select Next.
58
00:05:01,760 --> 00:05:08,570
Once again next. Once again, let's click on
Next. And as you can see, we have successfully
59
00:05:08,570 --> 00:05:13,800
installed JDK. Let's click on this plus, if
you want to make sure that you have installed
60
00:05:13,800 --> 00:05:19,340
Java JDK successfully, you can always go to
your command prompt by going to start menu
61
00:05:19,340 --> 00:05:26,810
and typing cmd. In here, you can type Java
dash version. As you can see, we are seeing
62
00:05:26,810 --> 00:05:32,780
some information about the version of Java
installed on our system. As long as it's 1.8.
63
00:05:32,780 --> 00:05:38,160
and higher, we are good to go. Let's close
our command prompt. Next, we are going to
64
00:05:38,160 --> 00:05:45,110
download Android Studio. For that we can go
to developer.android.com. Let's see developer.android.com.
65
00:05:45,110 --> 00:05:51,050
During the course, we will come back to this
website a lot because this is the official
66
00:05:51,050 --> 00:05:55,820
website for Android developers. Okay, for
downloading Android Studio, just go to this
67
00:05:55,820 --> 00:06:01,520
Andrew the studio tab. And from here, you
can download one of the versions. If you don't
68
00:06:01,520 --> 00:06:06,291
see your version of operating system, you
can click on this download options. But in
69
00:06:06,291 --> 00:06:12,160
here, I'm just going to click on the download,
let's say download Android Studio, we need
70
00:06:12,160 --> 00:06:16,980
to agree to the terms and conditions. And
as you can see, the download has started.
71
00:06:16,980 --> 00:06:21,500
Once again, I'm going to select to cancel
in here because I have already downloaded
72
00:06:21,500 --> 00:06:27,270
Android Studio. After the download is finished,
just click on that. Once again, this is going
73
00:06:27,270 --> 00:06:31,830
to be a simple installation process. Let's
select Next in here, make sure to check this
74
00:06:31,830 --> 00:06:37,660
Android virtual device. Let's select Next
in here you need to select a path to install
75
00:06:37,660 --> 00:06:44,660
Android Studio. Notice that installing Android
Studio and all of its SDKs. And also emulators
76
00:06:44,660 --> 00:06:50,730
sometimes might take something like 20 or
even 40 gigabytes. So make sure to have at
77
00:06:50,730 --> 00:06:55,831
least 20 gigabytes on the drive that you are
going to install your Android Studio. After
78
00:06:55,831 --> 00:07:01,470
that. Let's select Next, and install. After
the installation is finished, just click on
79
00:07:01,470 --> 00:07:07,520
this next. And let's start our Android Studio.
Don't worry about these three projects that
80
00:07:07,520 --> 00:07:12,670
I have in here. Basically, these are the recent
projects that I was working on. I have Android
81
00:07:12,670 --> 00:07:18,870
Studio installed on my system previously,
and I uninstalled it for this video. But for
82
00:07:18,870 --> 00:07:23,940
you this list would be empty. Also along the
way of installing Android Studio you might
83
00:07:23,940 --> 00:07:29,460
see some windows is about selecting a theme
or other preferences. I didn't see them because
84
00:07:29,460 --> 00:07:34,900
I have installed Android Studio previously
on my system. Those are not important depending
85
00:07:34,900 --> 00:07:40,580
on your preferences, select one just if you
see something like installing SDK on check
86
00:07:40,580 --> 00:07:46,090
that because we are going to install everything
here together. In order to install SDK, you
87
00:07:46,090 --> 00:07:52,740
can come to this configuring here. And you
can select this SDK Manager SDK stands for
88
00:07:52,740 --> 00:07:58,080
software development kit. And for developing
Android applications, you need to install
89
00:07:58,080 --> 00:08:04,120
at least one SDK. Once again, as you can see,
I have installed two SDKs in here and return
90
00:08:04,120 --> 00:08:09,830
an Android nine API's both for you this list
should be empty and none of these should be
91
00:08:09,830 --> 00:08:14,520
checked. First of all, let's check this show
package details in order to see the package
92
00:08:14,520 --> 00:08:20,250
details for every SDK. By the time I'm recording
this video, the latest version of Android
93
00:08:20,250 --> 00:08:27,250
is Android 10 or aka level 29. To be precise,
I suggest that you always install the latest
94
00:08:27,250 --> 00:08:32,690
version. But sometimes there might be some
preview versions in here, I don't suggest
95
00:08:32,690 --> 00:08:38,630
installing those because those can be buggy.
And those are not yet released on a stable
96
00:08:38,630 --> 00:08:44,340
channel. So make sure to install the latest
stable version. If you want, you can install
97
00:08:44,340 --> 00:08:50,670
the complete SDK. But I don't suggest that
because these can be memory and hard drive
98
00:08:50,670 --> 00:08:55,810
consuming. And these can occupy a lot of the
storage on your device. But the necessary
99
00:08:55,810 --> 00:09:01,670
things in here is first of all Android SDK
versions, whatever the version of your API
100
00:09:01,670 --> 00:09:07,900
is. So the first one, also the sources for
that version of SDK. The second option, also
101
00:09:07,900 --> 00:09:13,110
I suggest that depending on the architecture
of your system, select one of these two version
102
00:09:13,110 --> 00:09:19,820
of atom mine was 64 architecture. So I'm going
to select this one. If you are going to work
103
00:09:19,820 --> 00:09:25,340
with different Google API's. For example,
if you are going to work with Google map or
104
00:09:25,340 --> 00:09:34,310
YouTube API's, check one of these, for example,
this Google API's for atom 64 and also check
105
00:09:34,310 --> 00:09:40,000
one of these two Google API versions. This
is going to install at least one SDK on my
106
00:09:40,000 --> 00:09:47,110
system, and return or API level 29. But if
you want, you can select and install multiple
107
00:09:47,110 --> 00:09:53,500
SDK versions. Basically, this SDK versions
and API's means that in what version of Android
108
00:09:53,500 --> 00:09:58,760
Are you going to run and test your applications
later on. We will see that when we are going
109
00:09:58,760 --> 00:10:04,650
to run our application On a virtual device,
you're going to need at least one SDK, installing
110
00:10:04,650 --> 00:10:10,210
multiple SDKs can be memory consuming and
also harddrive consuming. But if you have
111
00:10:10,210 --> 00:10:16,240
the resources, I suggest that you at least
install the latest version after that, install
112
00:10:16,240 --> 00:10:22,240
this API level 29. Because there were a lot
of changes specifically in the data storage
113
00:10:22,240 --> 00:10:30,670
structure also installed API level 28, down
in here installed API level 26. And after
114
00:10:30,670 --> 00:10:37,720
that, level, 21, and also 19. Because in this
version of SDKs, there were a lot of changes.
115
00:10:37,720 --> 00:10:43,650
And I'm just saying this because of my experience
in Android app development. But if you don't
116
00:10:43,650 --> 00:10:48,800
have the proper resources, selecting one of
these is going to be just fine. After selecting
117
00:10:48,800 --> 00:10:53,620
what you need in here, just click on this
apply, make sure to have a stable internet
118
00:10:53,620 --> 00:10:58,500
connection because it's going to download
some stuff. Let's click on this apply. In
119
00:10:58,500 --> 00:11:03,550
here you can see a dialog that will show you
some information about the stuff that are
120
00:11:03,550 --> 00:11:08,460
going to be downloaded and also the size of
the download file. Next, select OK in here,
121
00:11:08,460 --> 00:11:13,260
and our download should be started. I'm going
to skip the video in here until the download
122
00:11:13,260 --> 00:11:18,640
is finished. After the downloading and installing
all of the components that you have selected.
123
00:11:18,640 --> 00:11:23,200
Before it's finished, just click on this finish.
And let's see what else do we have in here.
124
00:11:23,200 --> 00:11:29,360
Okay, we have successfully installed at least
one SDK, let's switch to this SDK tools. And
125
00:11:29,360 --> 00:11:35,060
let's see what we need from here. First of
all, once again, check this show package details,
126
00:11:35,060 --> 00:11:40,250
install one of these Android SDK build tools,
I suggest that installed the latest version.
127
00:11:40,250 --> 00:11:47,090
As you can see, mine is installed in here
29.0 point two at the time is the latest version.
128
00:11:47,090 --> 00:11:52,910
Also down in here, you need to install Android
emulator mine is not updated. So I'm going
129
00:11:52,910 --> 00:11:59,180
to update that. The next two things that are
mandatory are these SDK platform tools and
130
00:11:59,180 --> 00:12:04,300
SDK tools, make sure to select those two.
Also down in here we have this documentation
131
00:12:04,300 --> 00:12:11,020
for Android SDK. This option is not necessary,
but it's going to help us a lot during our
132
00:12:11,020 --> 00:12:15,380
journey in Android app development. Later
on, we will see that these documentations
133
00:12:15,380 --> 00:12:21,540
will help us a lot when we want to debug something
or when we faced some errors. So I strongly
134
00:12:21,540 --> 00:12:26,880
suggest to install these documentations as
well. The other thing that is not necessary,
135
00:12:26,880 --> 00:12:33,080
but I suggest to install is this hexam installer,
it is going to be an accelerator for the speed
136
00:12:33,080 --> 00:12:39,380
of your emulator. The native emulator in Android
Studio can be various low, especially if you
137
00:12:39,380 --> 00:12:45,510
have a low memory Ram. For that reason, I
suggest you to install this hex installer.
138
00:12:45,510 --> 00:12:50,460
But there is one thing that I need to mention
in here. I've seen a lot of students having
139
00:12:50,460 --> 00:12:55,500
problem with this hexam installer, I've seen
them that they cannot install these hacks
140
00:12:55,500 --> 00:13:01,110
them successfully. This hex m cannot be installed
on all of the CPUs. And depending on the structure
141
00:13:01,110 --> 00:13:09,020
of your CPU, this XM might not be installed.
But there are some CPUs, especially Intel
142
00:13:09,020 --> 00:13:15,839
CPUs that they have a special option in the
devices bias is specifically for this emulator
143
00:13:15,839 --> 00:13:21,710
accelerator. If that's not enabled, you may
want to enable that in the BIOS settings as
144
00:13:21,710 --> 00:13:27,500
well. And also if for any reason you couldn't
install this handsome installer, this emulator
145
00:13:27,500 --> 00:13:32,370
accelerator. And also for that matter, the
Android emulator itself, there is no need
146
00:13:32,370 --> 00:13:38,420
to worry there is another way as well. Besides
the native Android emulator, you can install
147
00:13:38,420 --> 00:13:43,589
external emulators as well for example, the
one that I know that is working very well
148
00:13:43,589 --> 00:13:49,690
is called genymotion. In here, I'm not going
to install that but I will put a link to download
149
00:13:49,690 --> 00:13:55,240
genymotion in the description of this video.
And also besides all of these, you can always
150
00:13:55,240 --> 00:14:00,510
install and test your applications on the
real device that you have. Later on in the
151
00:14:00,510 --> 00:14:05,980
course we will see that how we can install
and test our applications on our mobile phones.
152
00:14:05,980 --> 00:14:10,950
Okay, after selecting everything from here,
just click on Apply once again. Once again
153
00:14:10,950 --> 00:14:16,040
we are seeing the download the information
like Select OK and that the download begin.
154
00:14:16,040 --> 00:14:20,420
Once again. I'm going to skip the video in
here until the download is finished. Once
155
00:14:20,420 --> 00:14:25,050
again When the download and installing is
finished, just click on this finish. And we
156
00:14:25,050 --> 00:14:31,070
have successfully installed SDK platforms
and SDK tools just before I close this window,
157
00:14:31,070 --> 00:14:36,250
let's switch to this appearance in here. From
here you can change all of the preferences
158
00:14:36,250 --> 00:14:42,110
that you want. For example, you can change
the theme Dracula is the dark theme. Also
159
00:14:42,110 --> 00:14:46,990
we have this high contrast theme. The intelligent
theme is the light theme. I'm going to stay
160
00:14:46,990 --> 00:14:52,339
with that because it's good for recording
purposes. If you want you can use custom fonts
161
00:14:52,339 --> 00:14:56,820
for the fonts of your Windows is from here.
You can also change the size of your font.
162
00:14:56,820 --> 00:15:02,440
I'm not going to do that. Also then in here,
you can change The font size. Also for the
163
00:15:02,440 --> 00:15:07,781
presentation mode. If you want to change the
size of your codes, you can search for fonts
164
00:15:07,781 --> 00:15:13,529
in here, I believe it's in this editor tab
in here in this font, you can see that mine
165
00:15:13,529 --> 00:15:20,140
is 18, you can increase or decrease that if
you want. Let's delete this one. And also,
166
00:15:20,140 --> 00:15:24,940
let's minimize this editor. I have another
suggestion for you in here as well, in this
167
00:15:24,940 --> 00:15:31,180
appearance inside the system settings inside
the updates, make sure that your updates are
168
00:15:31,180 --> 00:15:36,779
being checked from a stable channel. We have
other channels like developer channels and
169
00:15:36,779 --> 00:15:42,960
betha channels as well make sure that you
have the stable channel. Others can be buggy.
170
00:15:42,960 --> 00:15:48,190
Okay, that's it for installing SDK, we will
be coming back to this setting menu later
171
00:15:48,190 --> 00:15:54,550
on. Let's select OK. OK, now it's time to
install at least one emulator. For that we
172
00:15:54,550 --> 00:15:59,839
can once again select this configure this
time, we can click on this AV manager AV D
173
00:15:59,839 --> 00:16:05,839
stands for Android virtual device. As you
can see, once again, I have two devices installed
174
00:16:05,839 --> 00:16:11,250
in here pixel two and pixel three, this list
should be empty for you. But because I had
175
00:16:11,250 --> 00:16:16,670
Android Studio installed on my system, I can
see them let's select this create visual device
176
00:16:16,670 --> 00:16:22,270
in order to create a new virtual device. First
of all, you can create your own and customize
177
00:16:22,270 --> 00:16:28,590
the hardware by selecting this new hardware
profile. From here you can name your device,
178
00:16:28,590 --> 00:16:34,430
you can select that if it's a phone or tablet,
you can set the screen size and resolution.
179
00:16:34,430 --> 00:16:40,480
Also then in here you can specify some ram
to it and all sorts of sensors and hardwares.
180
00:16:40,480 --> 00:16:47,190
I'm not going to create my customized device.
Instead, I'm going to install one of the predefined
181
00:16:47,190 --> 00:16:52,760
devices. As you can see, I have pixel two
and pixel three in here. Let's select another
182
00:16:52,760 --> 00:16:59,640
device from here. Also from this left panel,
you can see all sorts of devices like TV viewers
183
00:16:59,640 --> 00:17:05,049
and tablets. Let's select one device from
here. I don't even know all of these devices.
184
00:17:05,049 --> 00:17:10,789
Let's just select this pixel Excel, select
Next. Okay, from here, you need to specify
185
00:17:10,789 --> 00:17:16,159
the Android version that is going to be installed
on this device. I'm going to install Android
186
00:17:16,159 --> 00:17:22,089
q or API level 29 on this device, so we need
to download that first, let's select Download
187
00:17:22,089 --> 00:17:26,620
once again, I think it's going to be a large
five. So I'm going to skip the downloading
188
00:17:26,620 --> 00:17:32,159
here until the download is finished. Yes,
it's 1.1 gigabytes. Okay, the download and
189
00:17:32,159 --> 00:17:37,170
installing is finished just click on Finish.
Now we can select the downloaded API, sometimes
190
00:17:37,170 --> 00:17:42,110
you might not see the version of API that
you're looking for. For that, first of all,
191
00:17:42,110 --> 00:17:47,119
you can always refer to this list. After that,
you can check the second tab and also the
192
00:17:47,119 --> 00:17:54,179
third tab. But we are going to install Android
q on our device. Let's select Next in here
193
00:17:54,179 --> 00:17:58,940
we can customize our device. For example,
we can change the name of our device, we can
194
00:17:58,940 --> 00:18:04,649
also change the default orientation. For example,
the landscape and portrait I'm going to save
195
00:18:04,649 --> 00:18:11,649
its portrait. In this Show advanced settings,
we can do some more customization, for example,
196
00:18:11,649 --> 00:18:17,700
you can change the camera, the network. Also
down in here, you can change the size of RAM,
197
00:18:17,700 --> 00:18:24,320
and SD card and all sorts of customization.
I'm not going to change any of these. I'm
198
00:18:24,320 --> 00:18:29,980
just going to click on this finish. And as
simple as that we have installed a new emulator
199
00:18:29,980 --> 00:18:34,840
on our device. Okay, let's close this window.
Okay, I think it's a good point. To finish
200
00:18:34,840 --> 00:18:40,489
off this video. In this video, we have installed
JDK eight we have installed Android Studio.
201
00:18:40,489 --> 00:18:45,580
After that we have installed SDK and we have
done some customization on the appearance.
202
00:18:45,580 --> 00:18:53,909
And after everything we have installed a new
virtual device.
203
00:18:53,909 --> 00:18:59,980
We are going to create our first application
we can do so by opening Android Studio and
204
00:18:59,980 --> 00:19:04,799
selecting this start a new Android Studio
project. First of all, in this window, we
205
00:19:04,799 --> 00:19:10,869
can see some templates. These templates are
just some small applications that have some
206
00:19:10,869 --> 00:19:16,009
codes written to some level. For example,
in this basic activity, you can see that we
207
00:19:16,009 --> 00:19:21,029
have a small button and also a menu above
in here. We can see other templates in here,
208
00:19:21,029 --> 00:19:27,249
for example, a template for Google Maps, we
have a template for navigation drawer. Don't
209
00:19:27,249 --> 00:19:32,610
worry about all of these because we will learn
how to create all of these elements ourselves
210
00:19:32,610 --> 00:19:39,009
later on. Even we select this ad no activity
template. But the Android team are nice guys,
211
00:19:39,009 --> 00:19:45,619
and they have provided us these templates.
In a lot of these templates. You can see the
212
00:19:45,619 --> 00:19:50,130
word activity Don't worry about this activity
yet. If you remember from the content videos,
213
00:19:50,130 --> 00:19:55,389
I said that we are going to have an entire
session which we will talk about activities
214
00:19:55,389 --> 00:20:00,430
and fragments. But for now you can think of
activities as different parts of your app.
215
00:20:00,430 --> 00:20:04,999
application. For example, if your application
has different pages and activity is one of
216
00:20:04,999 --> 00:20:11,460
those pages, this definition is not accurate,
it might not even be correct. But for now
217
00:20:11,460 --> 00:20:17,129
we can think of an activity as a page of our
application. In our first application, I'm
218
00:20:17,129 --> 00:20:21,850
going to save it this empty activity, which
has no element, it's just going to create
219
00:20:21,850 --> 00:20:27,960
some files for us. But if you want to save
it, at no activity, you can do so as well.
220
00:20:27,960 --> 00:20:32,399
Once again, later on, we will see that how
we can create all of these templates ourselves.
221
00:20:32,399 --> 00:20:37,409
Okay, let's select empty activity. And let's
hit next. In here, we need to provide some
222
00:20:37,409 --> 00:20:43,169
information about our application. For example,
the name of our application, I'm going to
223
00:20:43,169 --> 00:20:49,770
change the name of my application to let's
say, Hello world. As it's a cost among developers
224
00:20:49,770 --> 00:20:56,090
to name their first project, I level just
for no reason. The second part, you will define
225
00:20:56,090 --> 00:21:01,919
the package name for your application. This
package name should be unique to every application
226
00:21:01,919 --> 00:21:06,799
that is being released into the world. later
on. When you want to publish your application
227
00:21:06,799 --> 00:21:13,120
to play store, your application will be identified
with this package name. The convention in
228
00:21:13,120 --> 00:21:18,509
here is to provide the name of your website
in backwards. For example, My website is called
229
00:21:18,509 --> 00:21:25,820
maker.org. So my package name is or dot maker
dot the name of my project. Don't worry if
230
00:21:25,820 --> 00:21:29,700
you don't have any website, if you don't,
you can use something like conduct example.
231
00:21:29,700 --> 00:21:37,919
Let's say comm dot example dot the name of
my project. But since I have a website, I'm
232
00:21:37,919 --> 00:21:43,899
going to use that in here, I'm going to say
port that. Once again, this package name should
233
00:21:43,899 --> 00:21:49,460
be unique to every application. After that,
you need to define the path that you want
234
00:21:49,460 --> 00:21:54,639
to save your project. If you want, you can
change this path by selecting this folder
235
00:21:54,639 --> 00:22:00,700
in here and select the path but in here, I'm
not going to change that. After that you can
236
00:22:00,700 --> 00:22:06,230
select the language that you're going to work
with. Android applications can be written
237
00:22:06,230 --> 00:22:11,419
with Java language, and also kotlin language.
In this course, we are going to stay with
238
00:22:11,419 --> 00:22:17,759
Java. But if you want, I am currently recording
a new course about developing Android applications
239
00:22:17,759 --> 00:22:24,320
with kotlin. Feel free to check that if you
want, but let's select Java in here. After
240
00:22:24,320 --> 00:22:30,169
that, you need to define a minimum API level.
This minimum API level in here means that
241
00:22:30,169 --> 00:22:35,570
how many older Android devices can install
your application. For example, if you click
242
00:22:35,570 --> 00:22:41,029
on this drop down in here, you can see the
list of different API levels. Right now, the
243
00:22:41,029 --> 00:22:48,110
minimum API level that I can select these
14, but I can go off to API level 29, or Android
244
00:22:48,110 --> 00:22:53,929
10. To be precise, there is a balance in here.
If you select your minimum API level very
245
00:22:53,929 --> 00:23:00,950
high, you can work with a lot of newer functionalities
in Android, but fewer devices will be able
246
00:23:00,950 --> 00:23:06,519
to install your Android applications. For
example, if I select API level 29, which is
247
00:23:06,519 --> 00:23:13,049
the latest version, only less than 1% of the
devices can install my application. If I select
248
00:23:13,049 --> 00:23:21,039
something like 19, we can see that 95% of
devices by this time can install my application,
249
00:23:21,039 --> 00:23:26,690
you as the developer should decide this balance
for this course, probably, we are going to
250
00:23:26,690 --> 00:23:33,409
stay with API level 19 for most of our projects,
because I think it's a good balance, and 95%
251
00:23:33,409 --> 00:23:41,289
is a good number. Leave this check empty for
now. Also, using Android x artifacts is mandatory
252
00:23:41,289 --> 00:23:46,720
since few months ago, we will talk about it
later on in the course Let's hit finish. And
253
00:23:46,720 --> 00:23:52,340
let's create our first project. If it's the
first time that you are creating your Android
254
00:23:52,340 --> 00:23:58,249
applications, this process can be very slow
because Android This video is going to download
255
00:23:58,249 --> 00:24:04,570
some stuff, for example, the gradle file from
internet so for that reason, this process
256
00:24:04,570 --> 00:24:10,019
can be slow if it's the first time that you
are creating your application. The first thing
257
00:24:10,019 --> 00:24:16,450
in here that is noticeable is that Android
Studio have created two files for us this
258
00:24:16,450 --> 00:24:22,740
activity main dot XML file, and this main
activity dot java file. These have been created
259
00:24:22,740 --> 00:24:28,260
because we have selected that empty activity
template when we have created our project.
260
00:24:28,260 --> 00:24:33,029
Inside each of these files. There are some
codes. Once again, we can create all of these
261
00:24:33,029 --> 00:24:37,989
codes if we don't select that empty activity.
But if you select that, you can have some
262
00:24:37,989 --> 00:24:45,380
codes written to some level. The other thing
in here is this view pane down in here. This
263
00:24:45,380 --> 00:24:51,359
build painting here will show the progress
of building your Android applications whenever
264
00:24:51,359 --> 00:24:56,419
you want to run your Android application,
whether that is in the debug mode, which we
265
00:24:56,419 --> 00:25:03,809
will be working in our entire course. Whether
it's in the production level, this building
266
00:25:03,809 --> 00:25:08,629
here will show the progress of building your
Android applications. I'm going to minimize
267
00:25:08,629 --> 00:25:13,779
it for now. And let's talk about all the other
stuff that we are seeing in here. First of
268
00:25:13,779 --> 00:25:20,009
all, let's talk about this project thing here,
I can minimize and maximize that. Also, talking
269
00:25:20,009 --> 00:25:25,859
here, we have multiple options. For example,
we have this project option. This project
270
00:25:25,859 --> 00:25:32,320
option view exactly shows the structure of
files on your system. For example, you can
271
00:25:32,320 --> 00:25:38,299
maximize every folder. If you want to have
access to this main activity dot java file,
272
00:25:38,299 --> 00:25:45,899
you can go to App folder inside the sources.
Inside the main folder inside the Java folder,
273
00:25:45,899 --> 00:25:52,970
you can see that we have a folder in here,
which contains the main activity java file.
274
00:25:52,970 --> 00:25:58,280
This is the exact hierarchy of files on our
system. If you want to make sure of that,
275
00:25:58,280 --> 00:26:04,971
you can right click on the name of your project
and select Show in Explorer. This option in
276
00:26:04,971 --> 00:26:11,940
here, it will open the Explorer on your Windows,
you can see that we have a Hello World project,
277
00:26:11,940 --> 00:26:18,379
don't worry about the others inside this project.
As you can see, in the hierarchy, we have
278
00:26:18,379 --> 00:26:25,450
this app folder. Inside this app folder, we
have a source folder, main folder, Java folder,
279
00:26:25,450 --> 00:26:32,440
org, may code HelloWorld. And this is the
Java file that we are seeing here. So this
280
00:26:32,440 --> 00:26:38,009
project view will show the exact hierarchy
of different files on your system. There is
281
00:26:38,009 --> 00:26:43,210
another view in here and that's called Android
view, we are going to stay with Android view
282
00:26:43,210 --> 00:26:47,999
for the most of this course, because it's
much more easy for eyes to follow. As you
283
00:26:47,999 --> 00:26:54,009
can see, we have this app folder. And inside
that we have this Java folder, we have this
284
00:26:54,009 --> 00:27:00,200
package name. And inside that we have this
main activity dot java file, it's much more
285
00:27:00,200 --> 00:27:05,909
easy for eyes to follow different files. Also,
besides this app folder, we have these Gradle
286
00:27:05,909 --> 00:27:11,590
scripts down in here as well. Before I talk
about these Gradle scripts, I need to talk
287
00:27:11,590 --> 00:27:17,960
about the structure of every Android application.
So in most Android applications, there are
288
00:27:17,960 --> 00:27:23,549
at least two different kinds of files, the
layout files, which will define the looks
289
00:27:23,549 --> 00:27:29,249
of your application. The layout files in Android
are called XML, you can see this activity
290
00:27:29,249 --> 00:27:35,860
main dot XML file. If you click on that, you
can see the layout of your application. Right
291
00:27:35,860 --> 00:27:41,979
now it has only one simple text. The other
part of every Android application is the Java
292
00:27:41,979 --> 00:27:47,809
file. If you are developing in kotlin language,
there are kotlin files. The Java or kotlin
293
00:27:47,809 --> 00:27:54,149
file in most cases will define the logic of
your application. For example, what happens
294
00:27:54,149 --> 00:27:59,979
if you click on a button, or what to do with
the user's input, all of those will be handled
295
00:27:59,979 --> 00:28:06,479
in the Java or kotlin files. Beside the layout
files and Java files. In every Android applications.
296
00:28:06,479 --> 00:28:12,750
There might be some other files, for example,
you may have some images, and also some audio
297
00:28:12,750 --> 00:28:19,570
files as well. Those will be inside this Resources
folder, for example, inside the meat map folders.
298
00:28:19,570 --> 00:28:25,789
Right now we have an icon for our application,
which is this simple Android icon. Either
299
00:28:25,789 --> 00:28:31,789
images or other files can be put inside this
Resources folder as well. Basically, this
300
00:28:31,789 --> 00:28:38,840
Resources folder is for the static variables
or static phones on your application. So these
301
00:28:38,840 --> 00:28:44,489
are the three main components of every Android
application. We have layouts, we have the
302
00:28:44,489 --> 00:28:51,090
Java file, and also we have the static files.
Beside these we have this manifest file inside
303
00:28:51,090 --> 00:28:58,190
the manifest folder. This manifest file Vila
is specify some general properties for your
304
00:28:58,190 --> 00:29:02,690
application. For example, you can see the
icon of your application in here also, you
305
00:29:02,690 --> 00:29:07,720
can see the name of your application, which
is HelloWorld, in this case, and some other
306
00:29:07,720 --> 00:29:13,440
properties or features about your application.
They will talk about this manifest file later
307
00:29:13,440 --> 00:29:18,259
on in the course. But for now, let's close
it. We have all of these different files in
308
00:29:18,259 --> 00:29:23,830
every Android application. But for example,
when you are going to download some application
309
00:29:23,830 --> 00:29:29,820
from the Play Store, you will see that we
have only one file, one apk file. That's the
310
00:29:29,820 --> 00:29:35,309
final application that will be released, how
are we going to generate that apk file? Well,
311
00:29:35,309 --> 00:29:41,059
in that case, this Gradle tooling here is
going to be useful. gradle is a build tool
312
00:29:41,059 --> 00:29:48,070
that will combine all of these files and other
files, and we will create an APK file. No
313
00:29:48,070 --> 00:29:53,330
matter if you want to release your application
or if you want to test your application on
314
00:29:53,330 --> 00:29:59,739
an emulator, you are going to need Gradle
for that reason we are going to work extensively
315
00:29:59,739 --> 00:30:06,119
with The other use of Gradle is that if you
want to use other people's code in your project
316
00:30:06,119 --> 00:30:12,379
as well, for example, if you want to use YouTube
player in your project, you will add its dependency
317
00:30:12,379 --> 00:30:19,070
inside this built up gradle file down in here,
just by writing one line of code Gradle will
318
00:30:19,070 --> 00:30:25,320
download the code for YouTube video player,
as simple as that Gradle will add the YouTube
319
00:30:25,320 --> 00:30:31,029
player code to your project. And after that,
you can use it. So Gradle is a build tool
320
00:30:31,029 --> 00:30:37,529
that will handle the combination of every
file, and also other codes and we will generate
321
00:30:37,529 --> 00:30:43,330
an APK file, we will talk about different
Gradle scripts later on in the course. Okay,
322
00:30:43,330 --> 00:30:48,499
let's close all of these extra files. Okay,
this was the project when we can minimize
323
00:30:48,499 --> 00:30:54,049
or maximize this pain from here. Let's minimize
that. And let's talk about other pains available
324
00:30:54,049 --> 00:30:59,740
in Android Studio. Then in here, you have
some useful pains as well, for example this
325
00:30:59,740 --> 00:31:05,919
to do is very helpful when you are creating
or when you're developing Android applications
326
00:31:05,919 --> 00:31:11,340
to those are some works that you don't want
to do at the time, but you don't want to forget
327
00:31:11,340 --> 00:31:17,460
those works. For example, inside this main
activity, I can add a to do as simple as that
328
00:31:17,460 --> 00:31:20,059
I can say to do, let's say complete
329
00:31:20,059 --> 00:31:26,080
this. It's just for the developers to remind
themselves or other colleagues to do something
330
00:31:26,080 --> 00:31:32,020
later on. As you can see this to do has been
added inside this to do pain as well. If you
331
00:31:32,020 --> 00:31:37,450
want, you can check that it says that it's
inside the main activity. Later on, we will
332
00:31:37,450 --> 00:31:43,389
talk about how we can define to dues and also
other kinds of comments. So this is one of
333
00:31:43,389 --> 00:31:48,999
the other pains. The other one is this terminal
dining here. This terminal is like the command
334
00:31:48,999 --> 00:31:55,820
prompt in Windows or let's say terminal in
Linux or Mac. And it's very helpful if you're
335
00:31:55,820 --> 00:32:01,080
working with something like ADB or Android
debug bridge. Later on, we will see the use
336
00:32:01,080 --> 00:32:08,190
of ADB in our projects. Also, it's very useful
for working with SQL lite databases, we have
337
00:32:08,190 --> 00:32:13,109
worked extensively with this terminal in the
database session. After that we have this
338
00:32:13,109 --> 00:32:18,539
new tool here we have seen it when we have
created our project, you can see the progress
339
00:32:18,539 --> 00:32:23,960
of every application when it's going to be
built. In a lot of cases, there might be some
340
00:32:23,960 --> 00:32:30,169
problem when we build our project. And from
here, you can exactly locate the problem.
341
00:32:30,169 --> 00:32:35,059
After that we have this logcat in here, it's
going to be very helpful when we are going
342
00:32:35,059 --> 00:32:40,509
to debug our Android applications. In fact,
we are going to work a lot with this look
343
00:32:40,509 --> 00:32:46,369
at Okay, let's minimize all of these for now.
Above. In here we have some menus, we have
344
00:32:46,369 --> 00:32:53,450
File, Edit view and all of these, we will
work with most of them, I believe in the course
345
00:32:53,450 --> 00:32:59,109
but it's impossible to work with all of them.
Also, there is one thing that I need to say
346
00:32:59,109 --> 00:33:04,469
in here. When you create your first application,
you may get some warnings, for example, a
347
00:33:04,469 --> 00:33:10,369
rendering warning or some other kind of warning,
if you get those probably there isn't something
348
00:33:10,369 --> 00:33:14,799
wrong with your application, you just need
to rebuild your project. For that you can
349
00:33:14,799 --> 00:33:19,919
come to this build option in here and make
your project once again, in case if you get
350
00:33:19,919 --> 00:33:25,149
some errors at the first time that you create
your application. Sometimes newer versions
351
00:33:25,149 --> 00:33:31,610
of Android Studio can be buggy. And creating
your project once again simply can solve your
352
00:33:31,610 --> 00:33:37,239
problem. Also, there is another option from
here from five, you can close your project
353
00:33:37,239 --> 00:33:43,700
and reopen it that can be useful or in some
cases, you may want to invalidate your cache
354
00:33:43,700 --> 00:33:50,159
and restart Android Studio. So if you get
those warnings or errors, we can try these
355
00:33:50,159 --> 00:33:55,989
three different ways in order to overcome
the problem. There is one very useful tool
356
00:33:55,989 --> 00:34:02,309
that has been added to Android Studio recently
in the past few months. And that's called
357
00:34:02,309 --> 00:34:09,020
Android profiler. Let's add that to this pane.
down in here as well. We can add that by going
358
00:34:09,020 --> 00:34:15,589
to this view to Windows and profiler, it's
going to be helpful for monitoring the resources
359
00:34:15,589 --> 00:34:21,379
that your application is using, for example,
the amount of RAM the CPU that your application
360
00:34:21,379 --> 00:34:27,429
is using, or even the amount of network that
your application is using. If you're on our
361
00:34:27,429 --> 00:34:33,419
application, we can track the live amount
of different resources that our application
362
00:34:33,419 --> 00:34:38,659
is going to use. Let's run our application
and see for ourselves. You can run your application
363
00:34:38,659 --> 00:34:44,279
by clicking on this green triangle from here,
but before that, make sure to select the current
364
00:34:44,279 --> 00:34:50,639
app that you're working. And also beside that
make sure to select the device. In the previous
365
00:34:50,639 --> 00:34:56,839
video we have created a virtual device and
we can see the list of different virtual devices
366
00:34:56,839 --> 00:35:04,339
available. I'm going to run my application
on these So through API level 29, for no reason,
367
00:35:04,339 --> 00:35:09,819
let's just run. If we click on this building
here, you can see the build progress, I said
368
00:35:09,819 --> 00:35:16,940
that even if you are going to debug your application,
or even if you are going to run your application
369
00:35:16,940 --> 00:35:23,760
at the production process, you can check the
progress of build as well. As you can see,
370
00:35:23,760 --> 00:35:30,069
this emulator has been opened. And we will
see our application in a few minutes. Also,
371
00:35:30,069 --> 00:35:35,550
from down in here, you can see the current
status of running your application. Right
372
00:35:35,550 --> 00:35:41,670
now it has created or built my application
successfully, and it's waiting for the device
373
00:35:41,670 --> 00:35:47,069
to turn on. Also, if you check this logcat
in here, you can see that there are a lot
374
00:35:47,069 --> 00:35:53,079
of things going on in here. We will be using
this logcat extensively later on in the course,
375
00:35:53,079 --> 00:35:59,631
for different debugging purposes. Let's minimize
that. Also, if you open this profiler, you
376
00:35:59,631 --> 00:36:06,170
will see the live tracks of different resources
that your application is using. Let's open
377
00:36:06,170 --> 00:36:12,839
our emulator. Right now we have this simple
application in which has a predefined text
378
00:36:12,839 --> 00:36:20,589
called hello world. And if you want, you can
check the amount of CPU or other kinds of
379
00:36:20,589 --> 00:36:22,079
resources that your application is
380
00:36:22,079 --> 00:36:27,609
using, it's going to be helpful when you're
not sure how much resources your application
381
00:36:27,609 --> 00:36:33,289
is using. Let's minimize that for now. If
you want, you can close your application with
382
00:36:33,289 --> 00:36:39,910
this red button here, you can stop it. And
also if you want, you can turn off your device.
383
00:36:39,910 --> 00:36:47,490
By long pressing this turn off button, you
can say power off and your device will be
384
00:36:47,490 --> 00:36:51,900
shut down. As you can see, we are getting
some warnings in here right now, these are
385
00:36:51,900 --> 00:36:58,529
not important. But if you want to check them,
you can click on this event log. And everything
386
00:36:58,529 --> 00:37:05,069
that happens since we click on this green
triangle, we will be logged in here. Okay,
387
00:37:05,069 --> 00:37:10,339
let's minimize that. And let's switch back
to our activity main dot XML file. And we
388
00:37:10,339 --> 00:37:16,700
have a lot of options in here as well. Right
now you can see two different views of your
389
00:37:16,700 --> 00:37:22,710
application. This white one is called the
design view. And this one in here is the blueprint
390
00:37:22,710 --> 00:37:28,869
view. There are some slight differences between
these two, for example, if you have some invisible
391
00:37:28,869 --> 00:37:35,240
item inside your activity that might not be
visible inside this design view. But you can
392
00:37:35,240 --> 00:37:40,650
keep track of that inside the blueprint view.
For now, I'm going to disable this blueprint
393
00:37:40,650 --> 00:37:46,099
view because we are not going to work with
that just for now. In order to disable that,
394
00:37:46,099 --> 00:37:52,079
you can click on this stack icon. And you
can see just design, we can also minimize
395
00:37:52,079 --> 00:37:59,670
and maximize that by pressing this plus button.
At the left panel in here, you can drag different
396
00:37:59,670 --> 00:38:05,869
elements different user interface or UI elements.
For example, if you want to add a button to
397
00:38:05,869 --> 00:38:12,250
your application, you can simply drag it like
this. And this button will be added to your
398
00:38:12,250 --> 00:38:17,589
application. As you can see right now because
this element is clicked, you can see different
399
00:38:17,589 --> 00:38:23,640
attributes. At the right panel, we have a
lot of attributes for every user interface
400
00:38:23,640 --> 00:38:30,480
element. The most important one is this ID
above in here. This ID is the unique ID of
401
00:38:30,480 --> 00:38:35,829
your element in which we will use it in order
to have access to this button, for example,
402
00:38:35,829 --> 00:38:42,240
from inside of Java file. Later on, we will
talk about that if we click on our text view,
403
00:38:42,240 --> 00:38:47,970
you can see that we have different attributes
as well. These are just a few attributes.
404
00:38:47,970 --> 00:38:52,090
If you want to check the list of all of the
available attributes, you can click on this
405
00:38:52,090 --> 00:38:58,970
all attributes. And you can see that there
are a lot of them. We are going to get familiar
406
00:38:58,970 --> 00:39:05,000
with different UI elements in the UI session
we have that I think in two sessions from
407
00:39:05,000 --> 00:39:11,349
now, after the Java session we will be talking
about user interface in a specific details.
408
00:39:11,349 --> 00:39:18,420
Also, when you are inside your XML file, you
have two views design view and text view design
409
00:39:18,420 --> 00:39:24,200
view is this view that we are working right
now. And if we switch back to text view, you
410
00:39:24,200 --> 00:39:31,780
can see the exact value of every UI element
in XML code. xml is a markup language. If
411
00:39:31,780 --> 00:39:37,910
you are a web developer, you know the concept
of a markup language. HTML is also a markup
412
00:39:37,910 --> 00:39:44,930
language. markup languages are just for defining
some elements that user is going to see, for
413
00:39:44,930 --> 00:39:49,619
example, a button or text view, you can see
that there are a lot of attributes in here
414
00:39:49,619 --> 00:39:54,839
but that the don't know anything about them
yet. We will be talking about them later on
415
00:39:54,839 --> 00:40:02,040
in the force. So there are two ways of defining
UI elements. One is that You drag simple items,
416
00:40:02,040 --> 00:40:08,400
for example, a button to your design view,
or from the text view, you can type it, you
417
00:40:08,400 --> 00:40:13,440
can see that we have a button in here, we
can select the whole button. And we can simply
418
00:40:13,440 --> 00:40:18,240
press Delete, and that button will be deleted,
you can see that we no longer can see that.
419
00:40:18,240 --> 00:40:23,930
Also, if you don't have this preview enabled,
you can enable it from here, let's just press
420
00:40:23,930 --> 00:40:29,280
Ctrl Z in order to get back our button. And
let's switch back to our design view. I'm
421
00:40:29,280 --> 00:40:35,160
going to define an ID for this Hello text
because later on inside my java file, I'm
422
00:40:35,160 --> 00:40:40,289
going to identify that and I'm going to work
with that. So by clicking on HelloWorld, you
423
00:40:40,289 --> 00:40:46,730
can see the list of attributes. At the top,
we have this ID, we can set an ID for our
424
00:40:46,730 --> 00:40:52,089
TextView. Also, if you don't know the difference
between a text view, edit text, and all of
425
00:40:52,089 --> 00:40:58,049
these don't be very TextView is just a fancy
way of saying a simple text, you can define
426
00:40:58,049 --> 00:41:03,420
the ID whatever you want. For example, in
here, I'm going to say txt message. But notice
427
00:41:03,420 --> 00:41:08,950
that there are some conventions in here, you
can't use a spaces when you are going to define
428
00:41:08,950 --> 00:41:15,019
an ID also beside that you can't use some
weird characters like $1 sign or a hashtag
429
00:41:15,019 --> 00:41:20,609
sign. The other convention in here is that
you type the first letter in lower cases.
430
00:41:20,609 --> 00:41:26,549
For example, this team here is learn cases.
And also after the first word, you type the
431
00:41:26,549 --> 00:41:32,620
first letter of the second word. in capital
letters. For example, we have this m here,
432
00:41:32,620 --> 00:41:38,930
there are some just basic conventions among
programmers by pressing Enter, we now have
433
00:41:38,930 --> 00:41:44,990
an ID for this hello world. If we switch back
to our TextView from down in here, we can
434
00:41:44,990 --> 00:41:51,059
see that for this TextView, an attribute has
been added the ID attributes. So no matter
435
00:41:51,059 --> 00:41:56,280
what you do inside your TextView or design
view, they are basically the same. But as
436
00:41:56,280 --> 00:42:01,590
you can see, right now we have some warning
for this button. The warning says that this
437
00:42:01,590 --> 00:42:07,160
button is not constrained to anywhere. That's
because we are using this constraint layout
438
00:42:07,160 --> 00:42:12,730
for this whole XML file. Don't worry about
this constraint layout yet we have other layouts
439
00:42:12,730 --> 00:42:18,260
like relative layout and linear layouts as
well. But for now, just know that when you
440
00:42:18,260 --> 00:42:23,160
are using constraint layout, which is the
default case, when you are creating an Android
441
00:42:23,160 --> 00:42:28,710
application, you need to concentrate or chain
your different elements to software. For example,
442
00:42:28,710 --> 00:42:34,280
if I don't constrain this button to anywhere,
it's going to be floating at the wrong time.
443
00:42:34,280 --> 00:42:40,390
Because of the different screen sizes, we
cannot know the exact position or exact place
444
00:42:40,390 --> 00:42:45,460
of this button. And right now if we run our
application, probably This button will be
445
00:42:45,460 --> 00:42:51,599
moved to the place zero and zero, which is
exactly this corner in our layout. In order
446
00:42:51,599 --> 00:42:58,270
to overcome this warning, we can simply add
some constraints by clicking and dragging.
447
00:42:58,270 --> 00:43:06,680
As simple as that I'm constraining this button
to the both edges of my screen. Also for the
448
00:43:06,680 --> 00:43:11,710
top and the bottom, I can constraint that
as well. For the top, I'm going to constrain
449
00:43:11,710 --> 00:43:17,680
that to this TextView. For the button, I'm
going to constrain that to the bottom of my
450
00:43:17,680 --> 00:43:24,250
screen. As you can see whenever I add a constraint,
and attributes will be added to this TextView
451
00:43:24,250 --> 00:43:30,269
in here as well. Now we don't have that red
one. Also, it's worth mentioning that by default,
452
00:43:30,269 --> 00:43:36,299
when we have created this button, when we
have dragged that into our design view, an
453
00:43:36,299 --> 00:43:42,270
ID has been added to it as well. The idea
of this button is just button, we can change
454
00:43:42,270 --> 00:43:48,950
that if you want. For example, I can say btn
let's say hello, or whatever you want. But
455
00:43:48,950 --> 00:43:57,339
in an XML file, the ID should be a specific
to every element.
456
00:43:57,339 --> 00:44:02,260
Right now this button doesn't do anything.
In this video, we are going to make our application
457
00:44:02,260 --> 00:44:07,779
a little bit more interactive. For example,
we are going to change the text of this TextView
458
00:44:07,779 --> 00:44:13,589
by clicking on this button. For that we need
to define this TextView in our Java file so
459
00:44:13,589 --> 00:44:18,849
that we could have access to its attributes.
But before I go further to my java file, I'm
460
00:44:18,849 --> 00:44:23,799
going to switch to full screen so that you
won't be distracted with all of my icons down
461
00:44:23,799 --> 00:44:28,859
in here. If you go to this view option in
here, down here at the bottom, you can see
462
00:44:28,859 --> 00:44:33,839
that we have few options. I'm going to go
to full screen. For the rest of the course
463
00:44:33,839 --> 00:44:38,809
i'm going to stay with full screen. Okay,
let's switch to our main activity dot java
464
00:44:38,809 --> 00:44:43,471
file. As I said in the previous video, you
can see that we have some code written in
465
00:44:43,471 --> 00:44:49,040
here. And that's because when we created our
project we have selected the empty activity
466
00:44:49,040 --> 00:44:55,289
project for that now we have an empty activity
which has an activity main dot XML file and
467
00:44:55,289 --> 00:45:00,740
also main activity dot java file. In future
we will see that we can create All of these
468
00:45:00,740 --> 00:45:05,990
folds if we select no activity at the time
of creating our project, but in here we have
469
00:45:05,990 --> 00:45:11,400
these codes returned to this level. Also,
we have this plus sign in here beside this
470
00:45:11,400 --> 00:45:16,849
import. If you click on that, you can see
that we have imported some packages to this
471
00:45:16,849 --> 00:45:22,609
class. If you want, you can always minimize
and maximize that input. And for that matter,
472
00:45:22,609 --> 00:45:27,109
you can minimize and maximize all of your
methods. For now, I'm going to leave these
473
00:45:27,109 --> 00:45:32,210
imports like this so that we can see what
we are importing into our Java file. Don't
474
00:45:32,210 --> 00:45:37,210
be worried about all of the new keywords that
you're seeing in here, for example, this probably
475
00:45:37,210 --> 00:45:42,300
class extends protected void. We will talk
about all of them later on in the course.
476
00:45:42,300 --> 00:45:48,000
But for now, just know that this onCreate
method is going to be the start point of our
477
00:45:48,000 --> 00:45:52,420
application, it means that when we run our
application, the code inside these two curly
478
00:45:52,420 --> 00:45:58,589
braces is going to be the code that will run
first, once again, don't worry about the term
479
00:45:58,589 --> 00:46:03,400
method that we used. We will talk about that
later on in the course. So inside this onCreate
480
00:46:03,400 --> 00:46:08,140
method, two things are happening, we have
this super.on create, we are not going to
481
00:46:08,140 --> 00:46:13,130
talk about that right now. And after that
we are setting some content views by setting
482
00:46:13,130 --> 00:46:18,809
content views we are accessing to the activity
main file, you can see that we have this activity
483
00:46:18,809 --> 00:46:24,660
main in here. Basically it means that this
Java class is somehow related to this activity
484
00:46:24,660 --> 00:46:29,320
main layout file. We will talk in more depth
about this later on in the course. But for
485
00:46:29,320 --> 00:46:34,819
now just know that up until this point in
our code, we have said that this java file
486
00:46:34,819 --> 00:46:39,720
is related to this layout file. After that,
we can write the quotes that we need. For
487
00:46:39,720 --> 00:46:45,740
example, in here, I'm going to access to that
TextView that we have created in the previous
488
00:46:45,740 --> 00:46:50,950
video for that I can say text view. As you
can see, when I type something intelligent,
489
00:46:50,950 --> 00:46:56,350
the idea behind the Android Studio is suggesting
some options. There are a lot of options in
490
00:46:56,350 --> 00:47:01,930
here, but the one that I need is going to
come from Android dot widget package. So in
491
00:47:01,930 --> 00:47:08,049
Java, we have classes equivalent to the UI
elements or user interface elements in our
492
00:47:08,049 --> 00:47:13,769
layout file. If we are going to define a button
we have that class in java file as well. In
493
00:47:13,769 --> 00:47:19,120
here we are going to use text view. So we
can import that in our project as well. As
494
00:47:19,120 --> 00:47:24,430
you can see, a new line of code has been added
to our imports. This line of code is importing
495
00:47:24,430 --> 00:47:31,089
the TextView from the widget package into
our Java file. Next, I need to name my TextView
496
00:47:31,089 --> 00:47:35,670
The name is optional, and you can name it
whatever you want. For example, in here, I'm
497
00:47:35,670 --> 00:47:41,309
going to say txt Hello, this is just the basic
convention you can name your class, whatever
498
00:47:41,309 --> 00:47:46,190
you want, but we will talk about conventions
later on in the course, after naming your
499
00:47:46,190 --> 00:47:50,769
TextView you need to locate the TextView from
inside the layout file. And the way to do
500
00:47:50,769 --> 00:47:55,940
that is like this, you can say is equal to
there is a helpful method in every activity
501
00:47:55,940 --> 00:48:02,170
that is useful for finding different views
by their IDs. And that method is named find
502
00:48:02,170 --> 00:48:08,339
view by ID. Once again, this method is useful
to find different views different user interface
503
00:48:08,339 --> 00:48:13,819
elements from your layout files by date ID
inside the parentheses of this method, we
504
00:48:13,819 --> 00:48:18,820
need to pass the ID of our TextView from inside
our layout file. And the way to do that is
505
00:48:18,820 --> 00:48:24,539
like this, we can type r r is a special class
in Java, which will give us the access to
506
00:48:24,539 --> 00:48:31,029
all of our resources, our static files in
our project. Once again, it stands for resources,
507
00:48:31,029 --> 00:48:38,369
let's select that, you can say r.id dot after
that, we need to add the ID of our tapes we
508
00:48:38,369 --> 00:48:44,809
have which was txt message, I believe this
ID in here should be the exact ID that you
509
00:48:44,809 --> 00:48:50,540
have put inside your layout file for this
TextView you can see that the ID is txt message.
510
00:48:50,540 --> 00:48:56,569
And we are using that to have access to our
text view. So this way by using this fine
511
00:48:56,569 --> 00:49:02,210
view by ID method, and after that passing
the ID in this shape. Now we have access to
512
00:49:02,210 --> 00:49:07,519
our text view. But you can see that there
is one more error in here. And that's because
513
00:49:07,519 --> 00:49:13,519
in Java, every time you write a sentence,
you need to finish your sentences with a semicolon.
514
00:49:13,519 --> 00:49:18,619
That's just syntactical and it really doesn't
matter some languages does not require the
515
00:49:18,619 --> 00:49:23,329
semicolon, but in Java, you need to put a
semicolon at the end of every sentence. Okay,
516
00:49:23,329 --> 00:49:29,180
now up to this point, we have our TextView
in our Java file. Now we can use it for example,
517
00:49:29,180 --> 00:49:33,960
if we want to change the text of our TextView
we can say something like this, we can type
518
00:49:33,960 --> 00:49:39,269
the name of our TextView which is txt Hello.
After that we can say dot Don't worry about
519
00:49:39,269 --> 00:49:44,200
this dot operator. We will talk about it in
the next section in the Java section. But
520
00:49:44,200 --> 00:49:50,180
for now just know that when you are typing.on
an object, you have access to all of its methods
521
00:49:50,180 --> 00:49:55,269
and attributes. For example in here I can
see set text once again you can see that we
522
00:49:55,269 --> 00:50:00,119
have multiple options. Let's import the first
one inside the parentheses of this method.
523
00:50:00,119 --> 00:50:05,140
You need to pass a text. And whenever you're
going to pass a text statically, whenever
524
00:50:05,140 --> 00:50:10,710
you're going to hard code the text, you need
to pass it inside double quotations. Let's
525
00:50:10,710 --> 00:50:16,160
pass a double quotation inside this double
quotation, I'm going to say hello, this way.
526
00:50:16,160 --> 00:50:21,230
Now we have changed the text of our TextView.
Let's run the application and see if it's
527
00:50:21,230 --> 00:50:27,319
going to work. Remember that inside our activity
main layout file, the text is HelloWorld.
528
00:50:27,319 --> 00:50:32,799
Inside our Java file, we are changing it to
Hello, let's run the application. As you can
529
00:50:32,799 --> 00:50:38,019
see, in here, the text of our TextView has
been changed to Hello. Still, our application
530
00:50:38,019 --> 00:50:42,970
is not interactive, we want to change the
text when we click on this button, not when
531
00:50:42,970 --> 00:50:48,550
we run the application. For that we need to
define an event listener to be precise, an
532
00:50:48,550 --> 00:50:53,900
onClick listener for this button so that when
we click on this button to take off our TextView
533
00:50:53,900 --> 00:50:59,049
changes, there are multiple ways of defining
an onclicklistener. For our button, the simplest
534
00:50:59,049 --> 00:51:04,400
one is to do that inside your main activity
layout file. If you come down in here inside
535
00:51:04,400 --> 00:51:10,710
your button element inside the anchor brackets
of your button, if you type on click, you
536
00:51:10,710 --> 00:51:15,269
can see that we have an option in here. Let's
pass a name in here, for example, I'm going
537
00:51:15,269 --> 00:51:21,930
to say on btn clique, this name in here is
going to be the name of a method in our Java
538
00:51:21,930 --> 00:51:26,849
file. As you can see, right now we are getting
a red warning, it says that there is no such
539
00:51:26,849 --> 00:51:32,359
method in your Java file. Let's copy the name
from here. And let's go to our main activity
540
00:51:32,359 --> 00:51:37,960
and create that method. So in order to create
a method, we need to do that outside of the
541
00:51:37,960 --> 00:51:44,030
scope of this onCreate method. And inside
the curly braces of this class and talking
542
00:51:44,030 --> 00:51:48,980
about this, we need to define our method inside
this class. And the way to do that is like
543
00:51:48,980 --> 00:51:54,769
this, we can say public void after that the
name of our methods, which is on btn. Click
544
00:51:54,769 --> 00:51:59,769
a pair of parentheses, because it's going
to be the onclicklistener. For our button,
545
00:51:59,769 --> 00:52:04,630
we need to accept a viewing here, don't worry
about this viewing here, we will talk about
546
00:52:04,630 --> 00:52:09,849
all of these in Java session. Let's name this
view. After that, we need a pair of curly
547
00:52:09,849 --> 00:52:15,500
braces for our method. And in here, we need
to put the code that we need to execute when
548
00:52:15,500 --> 00:52:21,980
the user clicks on our button. If you switch
back to your activity main dot XML file. Now
549
00:52:21,980 --> 00:52:27,859
you can see that the red error has been disappeared.
Okay, let's go to main activity dot java file,
550
00:52:27,859 --> 00:52:33,380
I'm going to change the text of our TextView
to Hello when the user clicks on our button.
551
00:52:33,380 --> 00:52:39,480
So for that method, I'm going to move these
two line of code to inside this method, you
552
00:52:39,480 --> 00:52:44,369
can probably guess the behavior of our application
right now. Let's try it once again and see
553
00:52:44,369 --> 00:52:48,990
how is it going to work. You can see that
the text is still says hello world. If we
554
00:52:48,990 --> 00:52:54,420
click on our button, the text will change
to Hello, this is okay. But this is not entirely
555
00:52:54,420 --> 00:53:00,269
what we want. It's just saying hello right
now we want to say hello to the exact user.
556
00:53:00,269 --> 00:53:05,980
For example, we are going to get the name
of our user. And by clicking a button we are
557
00:53:05,980 --> 00:53:10,720
going to say for example Hello may sound Hello
Tom and something like that we are going to
558
00:53:10,720 --> 00:53:17,259
get the user's input, the way to get the user's
input is by using some element in our UI called
559
00:53:17,259 --> 00:53:23,000
edit text. Let's switch to design view. You
can add an edit text into your project by
560
00:53:23,000 --> 00:53:28,029
going to this text option in here, you can
see that we have multiple options. The first
561
00:53:28,029 --> 00:53:33,609
one is for text view, we have seen the text
view it's this hello world text. After that
562
00:53:33,609 --> 00:53:39,450
we have other options, plaintext, password,
email, and all of these, these are all helpful
563
00:53:39,450 --> 00:53:44,920
for when you want to get different kinds of
users inputs. But for our purpose in here,
564
00:53:44,920 --> 00:53:49,809
we are going to use plain text. Plain Text
is just a simple text that we are going to
565
00:53:49,809 --> 00:53:55,180
get from the user. Let's drag that into our
view. If you take a look at the attributes
566
00:53:55,180 --> 00:54:00,119
in here, you can see that it's ID is edit
text. Let's quickly change that. Let's say
567
00:54:00,119 --> 00:54:07,759
EDT. txt name. Because we are inside the constraint
layout, we also need to constraint this edit
568
00:54:07,759 --> 00:54:12,799
text as well. But before that, I'm going to
change the top constraint of this button,
569
00:54:12,799 --> 00:54:19,030
I am going to change its constraint from the
bottom of this TextView to the bottom of these
570
00:54:19,030 --> 00:54:24,240
editors. For that I am going to direct the
top constraint of this button to the bottom
571
00:54:24,240 --> 00:54:29,099
of this edit text. Now let's add some other
constraints for this edit text as well. For
572
00:54:29,099 --> 00:54:35,130
example, for the top, I'm going to constrain
it to the bottom of my text view. And also
573
00:54:35,130 --> 00:54:40,619
for the edges. I'm going to go to the edges
of the screen. Right now it's too close to
574
00:54:40,619 --> 00:54:45,630
our text view. If you want we can add some
margins. For example in here, I'm going to
575
00:54:45,630 --> 00:54:52,070
say 70. That seems to be better. The other
attribute that every edit text has is this
576
00:54:52,070 --> 00:54:56,730
text in here. I'm talking about this attribute
in here. You can see that right now it says
577
00:54:56,730 --> 00:55:01,519
name if you want you can change that but in
here, I'm going to completely delete that,
578
00:55:01,519 --> 00:55:07,030
instead, I'm going to define some hints. You
can see this hint attribute in here. The difference
579
00:55:07,030 --> 00:55:13,140
between hint and text is that when you click
on your edit text, the hint will be deleted.
580
00:55:13,140 --> 00:55:19,029
It's just a hint for the user to know that
what he or she is going to enter inside this
581
00:55:19,029 --> 00:55:23,730
edit text. For example, in here, I'm going
to say me, you can see that it's somehow grayed
582
00:55:23,730 --> 00:55:29,720
out. Okay, now we have an edit text inside
our layout file inside our Java file, we need
583
00:55:29,720 --> 00:55:35,039
to have access to this edit text so that we
could get the user's input once again, the
584
00:55:35,039 --> 00:55:40,880
start point of our activity. And our application
is this onCreate method. So we can do that
585
00:55:40,880 --> 00:55:47,259
inside this onCreate method, we can have access
to our edit text like we did for this TextView.
586
00:55:47,259 --> 00:55:52,950
We can say Edit Text. Once again, you can
see that we have an equivalent class for this
587
00:55:52,950 --> 00:55:57,440
edit text as well. Also, if you take a look
at in here, you can see that this line of
588
00:55:57,440 --> 00:56:03,830
code has been added to our class as well.
Let's say Edit Text, edit GTX. The name, like
589
00:56:03,830 --> 00:56:09,359
before, we can say is equal to find view by
ID. And once again, we need to pass the ID
590
00:56:09,359 --> 00:56:16,680
of our edit text which I can say r.id dot
EDTA. txt name. To this point, we have defined
591
00:56:16,680 --> 00:56:22,849
our edit text. Now, we need to get the text
of our edit text for that we can say Edit
592
00:56:22,849 --> 00:56:24,339
Text name,
593
00:56:24,339 --> 00:56:29,440
dot get text, you can see that we have this
option. But this is not enough, you also need
594
00:56:29,440 --> 00:56:34,579
to do another level of conversion. And we
do that by saying.to string. Don't worry about
595
00:56:34,579 --> 00:56:39,460
this two string method yet, we will talk about
that later on in the course. It's just for
596
00:56:39,460 --> 00:56:45,109
converting whatever the text is inside our
edit text to a text that we can use in our
597
00:56:45,109 --> 00:56:49,650
Java file. Don't worry about that. We will
talk about it later on. But there is a problem
598
00:56:49,650 --> 00:56:54,770
with our code in here, we are going to get
the user's name and we are going to say hello
599
00:56:54,770 --> 00:56:59,509
to the user. When we click on the button.
For that we need to have access to this edit
600
00:56:59,509 --> 00:57:04,720
text inside our on button click method. But
if you try to do that, for example, if you
601
00:57:04,720 --> 00:57:10,900
say Edit Text name, you can see that everything
is in red, it means that you don't have access
602
00:57:10,900 --> 00:57:15,920
to that edit text inside this method. And
that's because something called a scope in
603
00:57:15,920 --> 00:57:21,190
Java. The scope of this edit text is this
onCreate method within the curly braces of
604
00:57:21,190 --> 00:57:26,700
this onCreate method, we can have access to
these elements from inside are on button click
605
00:57:26,700 --> 00:57:31,160
method. In order to solve that problem, we
need to move these two line of code to inside
606
00:57:31,160 --> 00:57:36,839
our on button click method. Let's quickly
do that. Inside our on button click method,
607
00:57:36,839 --> 00:57:41,980
let's paste them. But right now we are not
doing anything with the users input we are
608
00:57:41,980 --> 00:57:46,930
getting the user's input, but you're not doing
anything with that, we need to pass this line
609
00:57:46,930 --> 00:57:52,890
of code to the set text method. Let's quickly
delete this line of code. And inside the parentheses
610
00:57:52,890 --> 00:57:58,690
of the set text method. I'm going to pass
that I'm going to say Edit Text name, dot
611
00:57:58,690 --> 00:58:05,279
get text.to string. This way, we are setting
the text of our TextView to whatever the text
612
00:58:05,279 --> 00:58:10,119
is inside our edit text. Let's quickly run
the application and see if it's going to work.
613
00:58:10,119 --> 00:58:14,910
This is how our application and this edit
text looks right now. If we click on that,
614
00:58:14,910 --> 00:58:20,380
you can see that we can type something for
example, let's type my name. And if we click
615
00:58:20,380 --> 00:58:26,420
on our button, the text of our text view changes
to Mesa, this is not entirely what we wanted,
616
00:58:26,420 --> 00:58:32,059
we want to say hello to myself. In order to
do that we need to make some changes to our
617
00:58:32,059 --> 00:58:37,480
code. Also notice that I didn't put any double
quotation in here. And that's because we are
618
00:58:37,480 --> 00:58:43,500
passing this text dynamically to our set text
method. If we wanted to pass our text statically
619
00:58:43,500 --> 00:58:47,240
we need to put that inside double quotation
but in here we don't need to double quotation
620
00:58:47,240 --> 00:58:52,530
if we want to say hello to the user, we can
say something like this, we can say double
621
00:58:52,530 --> 00:58:58,440
quotation, let's add a plus in here. And inside
these level quotations, let's say hello. Also
622
00:58:58,440 --> 00:59:03,940
let's add a space so that we could see everything
better. This in here is called concatenation
623
00:59:03,940 --> 00:59:09,119
in programming, adding the two texts together,
let's run the application once again. And
624
00:59:09,119 --> 00:59:14,609
let's type something in here. Let's say Mesa
by pressing button. This time, you can see
625
00:59:14,609 --> 00:59:22,930
that the text has been changed to allow Mesa
our application is now much more interactive.
626
00:59:22,930 --> 00:59:28,640
Okay, this is our challenge, we are going
to create a registration form like this, we
627
00:59:28,640 --> 00:59:34,029
are going to get the user's first name, last
name and email. And by pressing this register
628
00:59:34,029 --> 00:59:39,180
button, we are going to show them inside these
text views. Let me quickly show what I'm talking
629
00:59:39,180 --> 00:59:47,019
about. So if we have a first name, last name,
and also an email. If we click on this register
630
00:59:47,019 --> 00:59:51,740
button, we are going to show them inside these
text views to the user. It's just a simple
631
00:59:51,740 --> 00:59:56,530
application so that we can practice everything
that we have learned so far. Okay, I want
632
00:59:56,530 --> 01:00:01,190
you to pause the video in here and create
the simple application After you have created
633
01:00:01,190 --> 01:00:07,079
it, come back to the video and we are going
to create this exact application together.
634
01:00:07,079 --> 01:00:11,200
I hope you solve the challenge but if not,
that's totally okay, we are going to create
635
01:00:11,200 --> 01:00:16,200
that in here together. Also notice that whenever
you run Android Studio, you can see a list
636
01:00:16,200 --> 01:00:21,059
of recent projects in the left panel in here.
Okay, let's move on. And let's create our
637
01:00:21,059 --> 01:00:26,249
project we can say start a new Android Studio
project. Once again, we are going to select
638
01:00:26,249 --> 01:00:31,339
this empty activity. Let's name our project
in here. I'm going to name it the first challenge
639
01:00:31,339 --> 01:00:38,039
or Let's name it registration for you need
to specify a package name, mine is fine. Change
640
01:00:38,039 --> 01:00:43,009
the Save location if you want. And also I'm
going to stay with API level 19. Okay, let's
641
01:00:43,009 --> 01:00:48,260
create our project. Let's start by creating
our layout file. I'm going to close these
642
01:00:48,260 --> 01:00:53,119
jellified for now. And let's also minimize
this project name. First of all, let's delete
643
01:00:53,119 --> 01:00:57,559
this HelloWorld text, you can click on it
and by pressing Delete, you can delete that
644
01:00:57,559 --> 01:01:03,510
in our registration form, we needed to edit
texts, let's add them quickly. In text, we
645
01:01:03,510 --> 01:01:08,900
need to add three plain texts in here. Let's
quickly add them, we will constrain them later
646
01:01:08,900 --> 01:01:14,710
on. Don't worry about that right now through
edit texts. And also we need a button. Let's
647
01:01:14,710 --> 01:01:20,289
add that as well. We also needed three text
views. For the Edit texts.
648
01:01:20,289 --> 01:01:26,910
Let's also change the IDs. For the first one,
I'm going to say Edit Text first name. Let's
649
01:01:26,910 --> 01:01:33,599
also delete this text and also add something.
That's the first thing. Let's do the same
650
01:01:33,599 --> 01:01:39,730
thing for the other two edit texts. Sometimes
when you're defining a hint, or maybe a text
651
01:01:39,730 --> 01:01:44,240
attribute for your elements, you can see some
annoying dialogue like this in here, you can
652
01:01:44,240 --> 01:01:49,640
avoid that by pressing the Escape key on your
keyboard. Okay for the button. First of all,
653
01:01:49,640 --> 01:01:56,300
let's change its ID to let's say btn register.
You can name it whatever you want. Let's also
654
01:01:56,300 --> 01:02:01,990
delete this text. And let's say register.
Also, you can see that we have this onClick
655
01:02:01,990 --> 01:02:07,039
attribute in here, we can define an onClick
attribute for our button from here or like
656
01:02:07,039 --> 01:02:12,369
we did in the previous video in our TextView.
I'm going to define that right now in here,
657
01:02:12,369 --> 01:02:17,989
let's say on Register btn. Click later on,
we will create this method for the text views.
658
01:02:17,989 --> 01:02:23,019
First of all, let's change their IDs. For
example, for the first one, let's say txt
659
01:02:23,019 --> 01:02:30,049
first name. Also, let's change the text to
Firstly, similarly for the other two. Okay,
660
01:02:30,049 --> 01:02:35,109
and let's greatly constraint all of these
elements, we can click on them. For the first
661
01:02:35,109 --> 01:02:40,809
one, I'm going to constraint it to the top
edge of my screen. And also both sides of
662
01:02:40,809 --> 01:02:47,230
my screen. Let's also add a margin top of
maybe 100. That seems to be better. For the
663
01:02:47,230 --> 01:02:53,430
next one, I'm going to constraint it to the
both edges of the first name edit text, and
664
01:02:53,430 --> 01:02:58,740
also to the bottom of my first name edit text,
add a margin top of 24. I'm going to do the
665
01:02:58,740 --> 01:03:04,180
same thing for this email. For the button,
I'm going to do the same thing, but I will
666
01:03:04,180 --> 01:03:09,559
probably change the top constraint. Also,
if you did a mistake, when you're constraining
667
01:03:09,559 --> 01:03:15,020
your elements, you can click on the constraint
by pressing Delete key you can delete the
668
01:03:15,020 --> 01:03:21,829
constraint, let's change the top constraint
to maybe something like 90 db. For the first
669
01:03:21,829 --> 01:03:27,239
name, I'm going to constraint it to the both
edges of my button. Sometimes it may not work
670
01:03:27,239 --> 01:03:33,789
just try once again. And also the top constraint
to the bottom of my button. For the top constraint
671
01:03:33,789 --> 01:03:40,239
once again, let's say 90, I think 90 is fine.
Let's delete its top constraint because right
672
01:03:40,239 --> 01:03:47,260
now we are not seeing our last name TextView.
And let's add it once again. For the last
673
01:03:47,260 --> 01:03:53,519
name, I'm going to do the same thing. Let's
change the top constraint margin to let's
674
01:03:53,519 --> 01:03:59,869
say 24. Same thing for the email. Okay, this
is going to be the look of our application.
675
01:03:59,869 --> 01:04:04,420
And now that we have defined our layout file,
let's switch to our Java file. And to find
676
01:04:04,420 --> 01:04:09,559
the behavior of our application, I'm going
to close this activity main dot XML just before
677
01:04:09,559 --> 01:04:14,859
everything, let's just copy this onClick attribute
so that we don't make any mistake when we
678
01:04:14,859 --> 01:04:20,410
define this method in our Java file. In our
projects being inside Java folder inside our
679
01:04:20,410 --> 01:04:27,289
package folder inside our main activity dot
java file, I need to create my onClick method.
680
01:04:27,289 --> 01:04:32,880
Once again, I can say public void the name
of my method, which I've copied from my layout
681
01:04:32,880 --> 01:04:38,180
file, inside the parentheses, I need to say
view with capital V after that view with the
682
01:04:38,180 --> 01:04:43,809
Lord v. And now I have created my own button
click listener. Once again, don't be worried
683
01:04:43,809 --> 01:04:48,730
about the syntax of this method. We will talk
about it later on when we have talked about
684
01:04:48,730 --> 01:04:54,569
Java in here. First of all I'm going to define
my text views. If you remember we can do something
685
01:04:54,569 --> 01:05:00,849
like this. We can say TextView for the first
one. Let's say txt first name is Call to find
686
01:05:00,849 --> 01:05:10,450
view by ID r.id dot txt. Firstly, I'm going
to do the same thing for email. And lastly,
687
01:05:10,450 --> 01:05:15,519
after you have defined your text views, it's
time to define your edit texts. You can do
688
01:05:15,519 --> 01:05:20,430
that like this, we can say Edit Text. For
the first one, let's name it edit text first
689
01:05:20,430 --> 01:05:27,690
name is equal to find view by ID r.id dot
edit text first name. Once again, I'm going
690
01:05:27,690 --> 01:05:33,609
to do the same thing for edit text, email
and also edit text last name. Okay, now we
691
01:05:33,609 --> 01:05:38,890
have access to all of the UI elements that
we wanted. Now let's change the text of our
692
01:05:38,890 --> 01:05:43,400
text views. For example, for the txt first
name, I can say something like this, let's
693
01:05:43,400 --> 01:05:48,890
say txt first name. That's a text. We have
seen this in the previous video. Nothing new
694
01:05:48,890 --> 01:05:55,700
in here. Let's say first name, plus the text
of our edit text. Firstly, we can say Edit
695
01:05:55,700 --> 01:06:02,779
Text first name, dot get text.to string. As
simple as that. Now, we are setting the text
696
01:06:02,779 --> 01:06:08,980
of our text view to the text of our edit text.
Let's do the same thing for last name and
697
01:06:08,980 --> 01:06:17,140
email. Edit Text last name, that's a text.
Let's say last name, first of all, was edit
698
01:06:17,140 --> 01:06:25,019
text last name dot get text.to string, edit
text email. That's a text. Sorry, I have put
699
01:06:25,019 --> 01:06:31,779
the wrong element in here. This should be
an txt. Last name, not edit text last name.
700
01:06:31,779 --> 01:06:34,680
Same thing for the email, txt email.
701
01:06:34,680 --> 01:06:43,380
That's it text. Let's say email plus edit
text email dot get text.to string. That's
702
01:06:43,380 --> 01:06:48,430
all we need from our application. Let's just
run it. Okay, it seems like we have done a
703
01:06:48,430 --> 01:06:53,359
good job with the layout of our application.
I think it's just better to decrease the margin
704
01:06:53,359 --> 01:06:58,019
top of this register button and also these
first name edit text so that we could see
705
01:06:58,019 --> 01:07:03,849
all of them better. Let's also test its behavior
for the first name, let's say Emma. For the
706
01:07:03,849 --> 01:07:10,269
last name, let's say Watson for the email,
Mr. watson@gmail.com. Let's click on this
707
01:07:10,269 --> 01:07:15,660
register button. And as you can see, we are
seeing the text that we wanted. It seems like
708
01:07:15,660 --> 01:07:23,690
our application is working perfectly. In this
section of the course we are going to talk
709
01:07:23,690 --> 01:07:31,099
about Java for writing Java applications,
we are going to need an ID ID stands for integrated
710
01:07:31,099 --> 01:07:37,620
development environment. Android Studio has
been built upon an ID called IntelliJ. Until
711
01:07:37,620 --> 01:07:43,279
few months ago, you could have right and drawn
Java applications on Android Studio bought
712
01:07:43,279 --> 01:07:48,799
I think it was around July 2019, that you
can no longer do that. And that was because
713
01:07:48,799 --> 01:07:53,759
of a new release in Gradle. I have recorded
a video on that exact problem at the time.
714
01:07:53,759 --> 01:07:58,849
And it's on my YouTube channel. If you want
to take a look at that just search for makeup
715
01:07:58,849 --> 01:08:03,680
on YouTube and you will probably find a video
let me quickly show that this is the video
716
01:08:03,680 --> 01:08:08,150
that I'm talking about. Andrew the sudo does
not support plain Java code anymore What to
717
01:08:08,150 --> 01:08:13,430
do now. And you can also see my channel name,
so make sure to check that if you need. But
718
01:08:13,430 --> 01:08:18,330
basically what I was talking about in that
video was that instead of Android Studio,
719
01:08:18,330 --> 01:08:24,670
you need to use another ID. The most famous
IDs that are available for writing Java applications
720
01:08:24,670 --> 01:08:30,000
are Eclipse and IntelliJ. In this course,
we are going to use intelligent because it's
721
01:08:30,000 --> 01:08:35,870
a lot like Android Studio. And also it's free.
In that video I have talked about how to install
722
01:08:35,870 --> 01:08:41,390
intelligent Id make sure to check that if
you need but it's a lot like installing Android
723
01:08:41,390 --> 01:08:47,040
Studio beside intelligent, you're going to
need JDK Java Development Kit, which we have
724
01:08:47,040 --> 01:08:52,930
already installed on our system, you can get
intelligent ID from JetBrains website, let's
725
01:08:52,930 --> 01:08:58,720
say intelligent ID. The first link from the
JetBrains website probably is our needed link.
726
01:08:58,720 --> 01:09:03,480
Just one thing that was mentioning here is
that there are two versions, the community
727
01:09:03,480 --> 01:09:08,790
version and also an ultimate version, the
community version is free. And also it's enough
728
01:09:08,790 --> 01:09:13,210
for our needs in this course. So make sure
to download that and after you have downloaded
729
01:09:13,210 --> 01:09:19,100
it and also installed it come back to this
video to write Java applications. I have intelligent
730
01:09:19,100 --> 01:09:24,400
installed on my system and I'm going to open
it right now you can create a new project
731
01:09:24,400 --> 01:09:29,540
in here by saying create new project. As you
can see, the process is a lot like Android
732
01:09:29,540 --> 01:09:35,990
Studio, I have the ultimate version of intelligence
ID and for that I can see all of these options.
733
01:09:35,990 --> 01:09:40,690
These might not be available for you if you're
using community version, but no matter what
734
01:09:40,690 --> 01:09:45,380
version you're using, you should see this
Java option in here select that in here you
735
01:09:45,380 --> 01:09:51,200
need to specify the path to your JDK. If you
have installed JDK with me at the beginning
736
01:09:51,200 --> 01:09:56,270
of this course you should be good in here
you can just select the version of your JDK
737
01:09:56,270 --> 01:10:02,670
my version in here is 13. But we are not going
to use All of the features of Java 13. Because
738
01:10:02,670 --> 01:10:07,580
Oracle does not support this version of Java
for long term, we are going to stay with all
739
01:10:07,580 --> 01:10:13,530
of the features of Java eight. Okay, After
you have selected the Java and your JDK, let's
740
01:10:13,530 --> 01:10:18,690
click next, we are not going to create our
project from template, so make sure to uncheck
741
01:10:18,690 --> 01:10:24,470
this option. After that, click Next. In here,
we need to name our project and also specify
742
01:10:24,470 --> 01:10:30,140
a location for the name of this project. I'm
going to say hello world. Also, if you don't
743
01:10:30,140 --> 01:10:35,700
see these settings in here, you have this
more Settings option down in here, make sure
744
01:10:35,700 --> 01:10:40,720
that your module name is exactly your project
name. Also feel free to change the project
745
01:10:40,720 --> 01:10:45,260
location if you need, I'm not going to change
that in here. Let's just click Finish. And
746
01:10:45,260 --> 01:10:51,100
let's create our project. As you can see,
intelligent ID is a lot like Android Studio.
747
01:10:51,100 --> 01:10:56,600
And the reason for that once again is that
Android Studio has been built upon this ID.
748
01:10:56,600 --> 01:11:01,410
Let's just close this tip dialog. And let's
switch to full screen mode. And let's go to
749
01:11:01,410 --> 01:11:07,230
full screen mode. Once again, in IntelliJ
ID, you can see this project pane you can
750
01:11:07,230 --> 01:11:12,620
minimize and maximize that you can also move
through the folders right now because we didn't
751
01:11:12,620 --> 01:11:18,270
use any template our folders are empty, we
can create our project like this by clicking
752
01:11:18,270 --> 01:11:23,930
on the source folder by right clicking on
that selecting new package, you can specify
753
01:11:23,930 --> 01:11:28,940
a package for our application. In this case,
I'm going to say for the maker
754
01:11:28,940 --> 01:11:36,100
dot let's say hello world. And inside this
package, we can create our Java classes Don't
755
01:11:36,100 --> 01:11:39,880
worry about the term class that are used.
We are going to talk about that in object
756
01:11:39,880 --> 01:11:44,470
oriented session. But for now just know that
we are going to need at least one Java class
757
01:11:44,470 --> 01:11:49,630
in order to run our Java applications. By
right clicking on our package and selecting
758
01:11:49,630 --> 01:11:55,180
new Java class we can create our class Let's
name this class. Hello. As you can see, a
759
01:11:55,180 --> 01:12:00,130
new Java class has been created in our package.
And also we have few lines of code. Let's
760
01:12:00,130 --> 01:12:05,160
minimize our project pane. Let's focus on
this Hello dot java class. Basically what
761
01:12:05,160 --> 01:12:10,720
we have in here is the package that we are
currently in which is orb dot maker dot HelloWorld.
762
01:12:10,720 --> 01:12:15,720
And also we have the declaration of a class
once again, we are going to talk about classes
763
01:12:15,720 --> 01:12:20,830
later on in future videos. In order to have
a start point for our application, we are
764
01:12:20,830 --> 01:12:25,780
going to need at least one method and that
method is called the main method. In order
765
01:12:25,780 --> 01:12:32,670
to create that method we can type p SVM. By
pressing tab or enter, we can have that method.
766
01:12:32,670 --> 01:12:37,250
As you can see, this green triangle has been
added in here it means that now we can run
767
01:12:37,250 --> 01:12:42,250
our application from this point, we will talk
about the meaning of all of these new keywords
768
01:12:42,250 --> 01:12:47,440
later on in the course. But for now, just
know that in order to run your Java applications,
769
01:12:47,440 --> 01:12:52,900
you need at least one Java class in which
has one method called main this main method
770
01:12:52,900 --> 01:12:58,590
is like the onCreate method of our activities.
Now we can write our codes inside this main
771
01:12:58,590 --> 01:13:02,930
method. For example, in order to print something
to the console, we can say something like
772
01:13:02,930 --> 01:13:09,130
this we can say s o UT or south and by pressing
tab or enter you can see this line of code
773
01:13:09,130 --> 01:13:14,880
System dot out dot print line. Or alternatively,
you can type this manually. Instead of using
774
01:13:14,880 --> 01:13:20,510
the shortcut, you can say system, notice that
the s should be in other cases, make sure
775
01:13:20,510 --> 01:13:27,921
to use the upper case for us. Let's say system
dot out. Basically this out means output in
776
01:13:27,921 --> 01:13:32,750
here it means that we are going to add 14
something in this case we are going to print
777
01:13:32,750 --> 01:13:37,531
something later on we will see that we have
an in as well in which we will get the user's
778
01:13:37,531 --> 01:13:43,510
input. Let's say System dot out dot print
line, you can see that we have print and print
779
01:13:43,510 --> 01:13:48,030
line. For now we are going to use the print
one we will talk about the difference later
780
01:13:48,030 --> 01:13:53,250
on. In order to print something to the console,
you need to pass it inside the double quotation,
781
01:13:53,250 --> 01:13:58,270
you can say something like this. For example,
let's say hello world. Now if you have the
782
01:13:58,270 --> 01:14:03,370
simplest Java application that can be written,
let's quickly run our application. In order
783
01:14:03,370 --> 01:14:08,800
to run our Java application for the first
time, we need to right click inside the curly
784
01:14:08,800 --> 01:14:15,750
braces of our main method and select Run hello.me.
Let's do that. As you can see, we are printing
785
01:14:15,750 --> 01:14:20,310
hello world into our console. Sometimes if
it's the first time that you're running Java
786
01:14:20,310 --> 01:14:25,640
applications, you may get some errors in here.
In the most cases those error would be gone
787
01:14:25,640 --> 01:14:32,140
by rebuilding your project, you can come to
this built up above in here and rebuild your
788
01:14:32,140 --> 01:14:36,670
project. After the rebuild is finished, you
can run your application once again by right
789
01:14:36,670 --> 01:14:41,960
clicking and selecting run. Hello, that's
me. In most cases, the error should be gone.
790
01:14:41,960 --> 01:14:47,400
But if you had any other problem, please ask
them in the q&a section of the course also
791
01:14:47,400 --> 01:14:52,030
notice that for the rest of this section,
we are not going to have any user interface
792
01:14:52,030 --> 01:14:57,910
it is possible to have a graphical user interface
GUI for your Java applications but because
793
01:14:57,910 --> 01:15:03,730
this is an Android course and later We've
talked about user interface in Android applications,
794
01:15:03,730 --> 01:15:08,670
we are not going to use any user interface
for our Java applications, we are just going
795
01:15:08,670 --> 01:15:13,790
to understand the language. Okay, now let's
minimize our round pain from here. And let's
796
01:15:13,790 --> 01:15:18,110
talk about the difference between print and
print line. The difference between print and
797
01:15:18,110 --> 01:15:23,230
print line is that when you are using print
line, after printing your text, the cursor
798
01:15:23,230 --> 01:15:28,670
will move to the next line. That is not the
case with print. In order to see the difference,
799
01:15:28,670 --> 01:15:35,270
let's say System dot out dot print. Once again,
let's pass our hello world text. And let's
800
01:15:35,270 --> 01:15:39,350
run our application to see the difference.
Also, if it's the first time that you're going
801
01:15:39,350 --> 01:15:43,790
to run your application, you can right click
inside the main method and click on this run
802
01:15:43,790 --> 01:15:48,900
Hello dot main. Or if it's not the first time,
you can click on this green triangle at the
803
01:15:48,900 --> 01:15:53,900
left or the green triangle. On the top of
your screen, you can also see the shortcut
804
01:15:53,900 --> 01:15:59,630
for Windows is shift plus fn. If you're using
Mac or Linux, we can hover over this green
805
01:15:59,630 --> 01:16:03,460
triangle and see the shortcut. Let's run the
application and see the difference between
806
01:16:03,460 --> 01:16:08,070
print and print line, you can see that the
text has been printed, but the cursor never
807
01:16:08,070 --> 01:16:13,040
moved to the next line. If we switch these
two lines of code, we can see the difference.
808
01:16:13,040 --> 01:16:14,040
But
809
01:16:14,040 --> 01:16:19,310
let's switch now. Now print line is the first
line and print is the second line. Let's run
810
01:16:19,310 --> 01:16:23,520
the application once again. This time, you
can see that the two text has been printed
811
01:16:23,520 --> 01:16:28,360
in two separate lines. Okay, now that we know
the difference between print and print line,
812
01:16:28,360 --> 01:16:32,810
let's talk about variables in Java. But before
I do that, I'm going to comment these two
813
01:16:32,810 --> 01:16:37,900
lines of code, you can comment them by pressing
Ctrl plus slash when you comment some code
814
01:16:37,900 --> 01:16:42,700
in Java, it means that you are telling the
compiler to ignore these two line of code.
815
01:16:42,700 --> 01:16:48,510
The use of comment is for developers to remind
themselves or other colleagues of something
816
01:16:48,510 --> 01:16:54,660
important. For example, in here I can say
slash slash, let's say do something, this
817
01:16:54,660 --> 01:16:59,320
line of code will be completely ignored. The
other kind of comments in here is like this,
818
01:16:59,320 --> 01:17:05,910
we can say slash slash to do after that we
can say do something. If we use this kind
819
01:17:05,910 --> 01:17:10,270
of comment with to do at the beginning of
your comment, you can see that first of all
820
01:17:10,270 --> 01:17:15,500
the color will be changed to blue. And also
if you click on the to do tab in here, you
821
01:17:15,500 --> 01:17:20,230
can see that you have one to do in your file.
It's very helpful when you're working with
822
01:17:20,230 --> 01:17:24,940
multiple programmers. By clicking on this
to do everyone can see the list of different
823
01:17:24,940 --> 01:17:29,090
tools. There are more to comments, we will
talk about them later on in the course. But
824
01:17:29,090 --> 01:17:33,840
for now just know that when you're commenting
something that line will be completely ignored.
825
01:17:33,840 --> 01:17:39,500
Okay, now let's talk about variables. Variables
are useful for when you want to store simple
826
01:17:39,500 --> 01:17:45,710
data in your job application. For example,
if you want to store a number or a text variables
827
01:17:45,710 --> 01:17:51,050
are useful for when you want to store some
small data in your Java application. For example,
828
01:17:51,050 --> 01:17:55,510
if you want to store a number or a text, if
you want to store a number, you will do that
829
01:17:55,510 --> 01:18:01,520
like this. You can say int int stands for
integer basically inside an integer, you can
830
01:18:01,520 --> 01:18:06,820
store whole numbers for example, negative
and positive numbers plus zero. And the way
831
01:18:06,820 --> 01:18:10,940
to define them is like this. First of all,
you type int after that, you need to name
832
01:18:10,940 --> 01:18:15,410
your variable. For example, in here, I can
say number this name in here is optional.
833
01:18:15,410 --> 01:18:21,100
And you can use whatever you want. After that
you can assign a value to your variable. For
834
01:18:21,100 --> 01:18:27,460
example, I can say five in here, this is the
data type of my variable, this number is the
835
01:18:27,460 --> 01:18:34,070
name of my variable. And this five is the
value of my variable beside positive numbers,
836
01:18:34,070 --> 01:18:39,350
you can also have negative numbers. After
you have stored your variables, you can do
837
01:18:39,350 --> 01:18:43,350
some operation on them, for example, you can
print them and the way to do that is like
838
01:18:43,350 --> 01:18:48,220
this, you can say System dot out dot print
line. And after that you can pass your number
839
01:18:48,220 --> 01:18:53,060
or whatever is the name of your variable.
Notice that in here I haven't used double
840
01:18:53,060 --> 01:18:58,290
quotation. Basically, whenever you are passing
some text explicitly, you need to pass the
841
01:18:58,290 --> 01:19:03,350
devil quotation but whenever you're passing
your variable, you shouldn't pass any double
842
01:19:03,350 --> 01:19:08,310
quotation, let's run the application and see
if we can see our number. You can see that
843
01:19:08,310 --> 01:19:13,660
the value of our number is negative five in
this case, but inside integers, you cannot
844
01:19:13,660 --> 01:19:19,960
have decimal values. For example, in here,
I can say 5.3 previoulsy, other kinds of variables
845
01:19:19,960 --> 01:19:25,560
in which you can use in order to store decimal
numbers, but more on that in a few minutes.
846
01:19:25,560 --> 01:19:30,760
The other kind of variable that you can use
in order to store whole numbers is long. Let's
847
01:19:30,760 --> 01:19:36,830
see that as well. We can see a long, you can
name it my long is equal to once again, let's
848
01:19:36,830 --> 01:19:42,270
say five. Let's also print that, let's say
System dot out dot print line and let's pass
849
01:19:42,270 --> 01:19:47,500
our log. If we run the application, you can
see five down in here. The difference between
850
01:19:47,500 --> 01:19:54,100
long and integer is that you can store larger
numbers inside a lung. And also a long variable
851
01:19:54,100 --> 01:20:00,160
will store more spaces in your device's memory
ram to be exact inside an integer you can't
852
01:20:00,160 --> 01:20:06,040
store numbers as large as the number of two
to the power of 31. But inside a long, you
853
01:20:06,040 --> 01:20:11,480
can store numbers as long as the number of
two to the power of 63. That's the difference
854
01:20:11,480 --> 01:20:16,910
between long and integer. But what if you
want to store decimal numbers floating point
855
01:20:16,910 --> 01:20:22,560
numbers for that you have other kinds of variables,
the first one is a double, let's see that
856
01:20:22,560 --> 01:20:29,650
we can say double, let's name it my double.
Let's assign it the value of 4.5. Like before
857
01:20:29,650 --> 01:20:31,730
we can print it,
858
01:20:31,730 --> 01:20:37,270
let's pass our data. If we run the application,
we should see the number of 4.5 printed, you
859
01:20:37,270 --> 01:20:41,890
can see that down in here. So double is one
kind of variable in which you can use in order
860
01:20:41,890 --> 01:20:47,750
to store floating point numbers. The other
option is to use float. Let's see that as
861
01:20:47,750 --> 01:20:53,560
well. We can say float, let's name it my float.
Once again, the names are optional, you can
862
01:20:53,560 --> 01:20:58,560
name your variables, whatever you want, we
can say 4.5 in here once again. But right
863
01:20:58,560 --> 01:21:02,940
now you can see that we are getting a read
error in here. And that's because floating
864
01:21:02,940 --> 01:21:08,140
point numbers by default are double in Java,
it means that if you want to define a float
865
01:21:08,140 --> 01:21:13,520
number, you need to cast your number to float,
the way to cast your number is like this.
866
01:21:13,520 --> 01:21:19,190
After the equal, we can open and close a parentheses
and inside the parentheses you can say float
867
01:21:19,190 --> 01:21:24,580
by casting, you will explicitly tell the ID
that this is going to be a float number and
868
01:21:24,580 --> 01:21:31,130
not a double number. Okay, let's quickly print
this as well. Let's pass our float. And let's
869
01:21:31,130 --> 01:21:35,830
run the application, you can see that once
again 4.5 has been printed. Once again, the
870
01:21:35,830 --> 01:21:41,710
difference between float and double is that
inside a float, you can store smaller numbers.
871
01:21:41,710 --> 01:21:47,890
And also the other difference is in the space
that each one of these are going to occupy
872
01:21:47,890 --> 01:21:53,520
in the device's memory. Basically double view
occupying more space, we won't be using floats
873
01:21:53,520 --> 01:21:58,790
that much in this course, I just wanted to
show you that float does exist in Java. Okay,
874
01:21:58,790 --> 01:22:04,270
now we have seen numbers. But what if we want
to store different characters in Java, for
875
01:22:04,270 --> 01:22:10,500
that we have a special kind of variable which
is char, for example, we can see char, my
876
01:22:10,500 --> 01:22:18,020
char is equal to, let's say M. Notice that
when I've assigned this character to my char
877
01:22:18,020 --> 01:22:22,870
variable, I have used this single quotation
that wasn't the case when we have worked with
878
01:22:22,870 --> 01:22:27,770
different numbers. When you're assigning a
character, you need to use the single quotation
879
01:22:27,770 --> 01:22:34,350
like before we can print our character. Let's
pass our character. And let's run the application.
880
01:22:34,350 --> 01:22:39,870
You can see em printed in here. When you're
using char variable, you can't have multiple
881
01:22:39,870 --> 01:22:43,790
characters. For example, in here, I can't
have ma you can see that we are getting an
882
01:22:43,790 --> 01:22:48,640
error. In order to store multiple characters,
we have another kind of variable in which
883
01:22:48,640 --> 01:22:52,810
we will talk about that in a minute. But before
I do that, I'm going to show you just one
884
01:22:52,810 --> 01:22:57,910
more thing. You can also store something like
this in your character, you can say backslash
885
01:22:57,910 --> 01:23:05,500
u 00. A, this is a special Unicode value for
registered symbol. Let's run the application
886
01:23:05,500 --> 01:23:09,810
and see what would be the result, you can
see that the Registered Trademark has been
887
01:23:09,810 --> 01:23:15,350
printed into our console. When you're using
a char variable. You can also assign Unicode
888
01:23:15,350 --> 01:23:20,040
values as well. You can search for different
Unicode values. For example, if we want to
889
01:23:20,040 --> 01:23:26,030
show the copyright symbol, we can say something
like this, we can say backslash use 00 a nine.
890
01:23:26,030 --> 01:23:30,730
If you've run the application, we should see
the copyright symbol Feel free to search for
891
01:23:30,730 --> 01:23:35,981
other Unicode values on the internet. Okay,
now let's talk about the string. If we want
892
01:23:35,981 --> 01:23:40,600
to restore multiple characters, we can use
a string, we can say a string with capital
893
01:23:40,600 --> 01:23:46,600
S. After that, like before, we can name our
string, let's say my string or Let's name
894
01:23:46,600 --> 01:23:54,400
it name is equal to Mesa meaning like before,
if we want, we can print our string. Let's
895
01:23:54,400 --> 01:23:59,620
fascinate and let's run the application, we
can see that Mesa has been printed into our
896
01:23:59,620 --> 01:24:04,410
console. But you may have noticed that there
is a difference in here, the color of this
897
01:24:04,410 --> 01:24:09,940
string is not blue, like the other kinds of
variables. That's because of a very important
898
01:24:09,940 --> 01:24:13,920
difference between a string and other kinds
of variables. The other kind of variables
899
01:24:13,920 --> 01:24:19,630
that we have talked about so far are primitive
data types. But a string is a class in Java,
900
01:24:19,630 --> 01:24:25,080
do we have talked about classes and objects
in Java, but for now, just know that when
901
01:24:25,080 --> 01:24:30,460
you are using a class, you can have some operations
on that. For example, if you type name, you
902
01:24:30,460 --> 01:24:35,710
can say dot and you can see a list of different
available methods. We will talk about few
903
01:24:35,710 --> 01:24:40,290
of them later on in the course. But you can
see that there are a lot of them. So the difference
904
01:24:40,290 --> 01:24:45,471
in here is that the string is a class, but
others are primitive data types. There is
905
01:24:45,471 --> 01:24:50,260
one more kind of variables that I'm going
to talk about in here, and that's called Boolean.
906
01:24:50,260 --> 01:24:57,540
Let's see that as well. We can say Boolean,
my Boolean, let's say is equal to true when
907
01:24:57,540 --> 01:25:04,270
you're using a Boolean, that Boolean can have
only true or false values. These are the only
908
01:25:04,270 --> 01:25:10,250
two values that a Boolean can have. Once again,
like before, we can print our Boolean. Let's
909
01:25:10,250 --> 01:25:15,210
pass my Boolean in here. And let's run our
application, you can see that true has been
910
01:25:15,210 --> 01:25:21,590
printed. As I said, Our Boolean can have false
values as well, let's run the application.
911
01:25:21,590 --> 01:25:26,960
False has been printed Boolean is very helpful
when you are working with conditional statements.
912
01:25:26,960 --> 01:25:31,400
We will talk about conditional statements
in the next video. Okay, this was our talk
913
01:25:31,400 --> 01:25:36,130
about different variables in Java, there are
a few more of these in which you can check
914
01:25:36,130 --> 01:25:41,211
out the Oracle documentation, but probably
you're not going to need the others. These
915
01:25:41,211 --> 01:25:46,130
are all that we are going to use in this course.
Okay, now that we have talked about variables,
916
01:25:46,130 --> 01:25:50,790
let's quickly talk about arithmetic operators
as well. But before that, I'm going to comment
917
01:25:50,790 --> 01:25:56,310
all of these lines of code, because these
can be distracting. The reason that I'm commenting
918
01:25:56,310 --> 01:26:00,870
these lines of code and not deleting them
is that I'm going to upload the source code
919
01:26:00,870 --> 01:26:05,550
so that if you need, you can check the source
code. Okay, let's move on down in here, let's
920
01:26:05,550 --> 01:26:11,930
quickly define two new integers, I can say
int a is equal to six. After that, let's say
921
01:26:11,930 --> 01:26:18,740
int b is equal to let's say two. You can use
operators in Java, in order to perform some
922
01:26:18,740 --> 01:26:24,060
kind of operation on your variable. We have
different kinds of operators. The first one
923
01:26:24,060 --> 01:26:28,770
that we're going to talk about in here is
called arithmetic operators. Let's quickly
924
01:26:28,770 --> 01:26:38,160
see them. For example, here I can say, answer
is equal to a plus b. This plus is the first
925
01:26:38,160 --> 01:26:42,930
arithmetic operator that we are going to talk
about thinking, this is going to add the value
926
01:26:42,930 --> 01:26:44,890
of B to A,
927
01:26:44,890 --> 01:26:50,790
let's quickly print our answer. And let's
run our application, you can see that the
928
01:26:50,790 --> 01:26:56,790
answer is eight, it has added B to A. The
other kind of arithmetic operator that I'm
929
01:26:56,790 --> 01:27:02,060
going to talk about in here is minus which
you can guess its value, it's going to deduct
930
01:27:02,060 --> 01:27:07,530
B from a, the answer in here should be four,
we can see that four has been printed. The
931
01:27:07,530 --> 01:27:13,940
other one is the star, which is used for multiplying
the two values together in here, the answer
932
01:27:13,940 --> 01:27:19,151
should be six times two, which is going to
be 12. Let's run the application. And you
933
01:27:19,151 --> 01:27:24,960
can see 12 in here, the other one is slash,
which is used for division, we are going to
934
01:27:24,960 --> 01:27:31,090
divide six by two, let's run the application,
we can see that the answer is three. The thing
935
01:27:31,090 --> 01:27:35,890
that you need to be extra careful with this
a slash operator in here is that the second
936
01:27:35,890 --> 01:27:41,870
number V in this case should never be zero.
Let's quickly change the value of b to zero.
937
01:27:41,870 --> 01:27:46,860
And let's see what will be the result. Let's
run the application, you can see that we are
938
01:27:46,860 --> 01:27:51,870
getting a read error in here we are getting
an arithmetic exception. We will talk about
939
01:27:51,870 --> 01:27:57,360
exceptions later on in the course. But for
now just know that whenever an exception occurs,
940
01:27:57,360 --> 01:28:02,061
our application crashes. Certainly we don't
want that behavior in our application. So
941
01:28:02,061 --> 01:28:08,100
be careful about the second number. Or alternatively,
you need to catch the exception that you think
942
01:28:08,100 --> 01:28:13,290
that might occur. We will talk about exceptions
later on in the course we have one more kind
943
01:28:13,290 --> 01:28:18,970
of arithmetic operator in Java, and that's
called remainder. Let's see that as well.
944
01:28:18,970 --> 01:28:25,020
It's the person so the remainder operator
is going to give us the remainder of a divided
945
01:28:25,020 --> 01:28:32,080
by b. In this case, if we divide six by two,
the answer is three. And its remainder is
946
01:28:32,080 --> 01:28:37,350
zero. So right now if I run the application,
I should see zero. You can see that zero has
947
01:28:37,350 --> 01:28:43,450
been printed down in here. But if I change
the value of a to, let's say five. Now the
948
01:28:43,450 --> 01:28:49,060
remainder of five divided by two is one. Let's
see that if we can see one. Yes, it has been
949
01:28:49,060 --> 01:28:54,310
printed from time to time this remainder operator
can be useful as well. There is one more thing
950
01:28:54,310 --> 01:28:59,890
that I need to talk about the slash operator
the division operator right now we can expect
951
01:28:59,890 --> 01:29:05,690
that if we divide five by two, we should see
2.5. Let's run the application and see if
952
01:29:05,690 --> 01:29:11,760
we can see that we can see that we are getting
two as the answer. And that's because we have
953
01:29:11,760 --> 01:29:16,440
saved the answer inside an integer. If you
remember at the beginning of this video, I
954
01:29:16,440 --> 01:29:21,620
said that integers can only contain whole
numbers. If you want to have floating point
955
01:29:21,620 --> 01:29:27,610
numbers, you can use something like double.
But this in here is not going to work as well,
956
01:29:27,610 --> 01:29:32,820
because the answer itself is an integer. If
you hover over the highlight, you can see
957
01:29:32,820 --> 01:29:38,860
that a divided by b is an integer. If you
want to cast your number to a double after
958
01:29:38,860 --> 01:29:44,630
the equal sign, you can open and close parentheses
and inside the parentheses you can say Java,
959
01:29:44,630 --> 01:29:50,670
this way you have cast an integer to a double.
Now if we run our application, you can see
960
01:29:50,670 --> 01:29:57,131
that we are seeing 2.5 so if you want to see
the exact value of division use doubles. Let's
961
01:29:57,131 --> 01:30:02,280
just talk a bit more about the plus operator.
After that finish off this video. So right
962
01:30:02,280 --> 01:30:08,070
now if I use plus in here, you can guess the
answer, five plus two is seven. Let's run
963
01:30:08,070 --> 01:30:13,900
the application. We can see seven down in
here, we can also use this plus with different
964
01:30:13,900 --> 01:30:20,750
texts. For example, if I have to text in here,
let's say string first name is equal to, let's
965
01:30:20,750 --> 01:30:29,320
say, Emma. Let's also define another text.
Let's say last name is equal to Watson, we
966
01:30:29,320 --> 01:30:34,850
can use the plus operator to add these two
text together. For example, let's say a string.
967
01:30:34,850 --> 01:30:43,060
Full Name is the call to first name, plus
our last name. If we print our full name,
968
01:30:43,060 --> 01:30:48,350
you can guess what would be the result. In
here we can see Mr. Watson. Let's also add
969
01:30:48,350 --> 01:30:53,960
a spacing here in order to see everything
better. Mr. Watson has been printed. This
970
01:30:53,960 --> 01:30:59,550
in here is called concatenating in Java, adding
two text together. So you can see that this
971
01:30:59,550 --> 01:31:05,780
plus operator have two usages, it can be used
to concatenate two text together or whenever
972
01:31:05,780 --> 01:31:15,200
we use it with different numbers, it will
act as the addition operator. We are going
973
01:31:15,200 --> 01:31:21,280
to talk about more operators in Java. Specifically,
we will talk about relational and logical
974
01:31:21,280 --> 01:31:25,960
operators. Before doing that, let's quickly
create our project by selecting create new
975
01:31:25,960 --> 01:31:32,220
project by selecting Java and clicking on
next. And next once again, we can name our
976
01:31:32,220 --> 01:31:39,700
project. Let's name this project operators.
Let's create our project. And inside the source
977
01:31:39,700 --> 01:31:46,340
folder, let's create our package. Let's say
new package. Let's name it orc. That's my
978
01:31:46,340 --> 01:31:52,760
code dot operators. Inside our package, let's
create our main class. Let's name this class
979
01:31:52,760 --> 01:31:59,590
main. Inside our main class, we are going
to need our main method. Let's type PS VM.
980
01:31:59,590 --> 01:32:05,440
By pressing tab, we have our main method.
Before I talk about relational and logical
981
01:32:05,440 --> 01:32:09,940
operators. Let's quickly talk a bit more about
arithmetic operators. If you remember from
982
01:32:09,940 --> 01:32:15,570
the previous video, when we have to find a
new integer, for example, a and we assign
983
01:32:15,570 --> 01:32:20,550
a value to that we could have say something
like this, we could have said in answer
984
01:32:20,550 --> 01:32:28,420
is equal to a plus two. Or instead of defining
a new integer, we can say a is equal to a
985
01:32:28,420 --> 01:32:34,650
plus two, it means that increase the value
of i by two, let's print a and see what would
986
01:32:34,650 --> 01:32:40,470
be its result. Let's run our application because
it's the first time I'm going to say run main
987
01:32:40,470 --> 01:32:46,700
dot Hello, you can see that the value of a
is seven, we can simplify this line of code,
988
01:32:46,700 --> 01:32:52,550
we can say something like this, I'm going
to comment that, and in here, I can say a
989
01:32:52,550 --> 01:33:00,300
plus equal to let's also add some spaces,
these two lines of code are exactly the same,
990
01:33:00,300 --> 01:33:05,570
you can use plus equal in order to simplify
the line six, let's run our application. Once
991
01:33:05,570 --> 01:33:12,440
again, you can see that the result is the
same. Also, if we want to increase a by one,
992
01:33:12,440 --> 01:33:18,050
for example, if you want to say a is equal
to a plus one, we can do something like this,
993
01:33:18,050 --> 01:33:25,360
we can say a plus equal one. Or alternatively,
we can say a plus plus, that is possible as
994
01:33:25,360 --> 01:33:31,470
well, this a plus plus is going to increase
the value of A by one. If we run our application,
995
01:33:31,470 --> 01:33:38,111
we should see six as the value of a you can
see six down in here. Alternatively, we can
996
01:33:38,111 --> 01:33:44,640
use minus as well, we can say a minus minus
it will deduct one from the value of a, if
997
01:33:44,640 --> 01:33:50,030
you're on our application, we can see four
in the console. Similar to that we can say
998
01:33:50,030 --> 01:33:55,710
a minus equal to let's say two. For example.
In this case, if we run our application, we
999
01:33:55,710 --> 01:34:02,850
should see three as the value of a like minus
equal You can also have multiplied equal and
1000
01:34:02,850 --> 01:34:08,820
also divided by equal it means that divide
a by two and store the value or a store the
1001
01:34:08,820 --> 01:34:13,980
answer inside a this sentence in here is the
simplified version of this line of code that
1002
01:34:13,980 --> 01:34:20,220
I'm going to write we can say a is equal to
A divided by two, both of these lines are
1003
01:34:20,220 --> 01:34:24,920
the same. If you want to make sure of that
you can delete this line and you can run your
1004
01:34:24,920 --> 01:34:30,900
application. five divided by two is 2.5. Because
we are saving that inside an integer, the
1005
01:34:30,900 --> 01:34:36,200
answer should be two. So we should see two
as the value of our a and you can see two
1006
01:34:36,200 --> 01:34:40,990
down in here. Before talking about logical
and relational operator. I just wanted to
1007
01:34:40,990 --> 01:34:46,670
talk about these simplifications. Okay, now
let's talk about relational and to be specific
1008
01:34:46,670 --> 01:34:52,480
comparison operators in Java comparison operators
are useful for when you want to compare two
1009
01:34:52,480 --> 01:34:57,290
things. For example, two numbers. Before that,
let's quickly define two numbers. I'm going
1010
01:34:57,290 --> 01:35:03,680
to say int a is equal to five After that,
let's say int b is equal to, let's say three.
1011
01:35:03,680 --> 01:35:10,690
After that we can say Boolean answer is equal
to the first comparison operator that I'm
1012
01:35:10,690 --> 01:35:15,480
going to talk about in here is greater than
operator. And the way to define that is like
1013
01:35:15,480 --> 01:35:21,870
this, we can say, a greater than B, I have
saved the result of this comparison inside
1014
01:35:21,870 --> 01:35:27,350
a Boolean, this left anchor bracket in here
means greater than operator, it means that
1015
01:35:27,350 --> 01:35:33,370
if a is greater than B, assigned the value
of true to this answer, right now, if we print
1016
01:35:33,370 --> 01:35:39,210
our answer we should see through, let's quickly
do that. Let's see answer. And let's run our
1017
01:35:39,210 --> 01:35:45,670
application, you can see that answer is true,
because A is five and five is greater than
1018
01:35:45,670 --> 01:35:51,070
three, the other kind of comparison operator
is less than, and that's this right angle
1019
01:35:51,070 --> 01:35:56,480
bracket, right now, a is not less than b,
so the answer should be false. If we run our
1020
01:35:56,480 --> 01:36:02,070
application, we should see false in the console.
And we can see that the other kind of comparison
1021
01:36:02,070 --> 01:36:06,570
operator that I'm going to talk about in here
is useful for when you want to compare the
1022
01:36:06,570 --> 01:36:13,030
equality between two integers. And that operator
is these two equal signs, right now, a is
1023
01:36:13,030 --> 01:36:18,380
not equal to b. So if we run our application,
we should see falls into our console. Let's
1024
01:36:18,380 --> 01:36:24,220
run our application. And here we can see false.
Remember that when we are using two equal
1025
01:36:24,220 --> 01:36:29,060
signs, it means that we are checking for the
equality of two number. But when we are using
1026
01:36:29,060 --> 01:36:36,130
a single equal sign like in here and above,
in here, we are assigning a value to our variable.
1027
01:36:36,130 --> 01:36:41,000
Let's change the value of b to five. To see
that if this is going to be true,
1028
01:36:41,000 --> 01:36:46,760
let's run the application. Now you can see
that the answer is true. We have other kinds
1029
01:36:46,760 --> 01:36:53,290
of comparison operators as well. One of them
is greater than equal. When we use this left
1030
01:36:53,290 --> 01:36:59,160
anchor bracket. And also this equal sign,
it means that check if a is greater than B,
1031
01:36:59,160 --> 01:37:05,450
or if a is equal to b, right now, a is equal
to b. So this answer should be true. If we
1032
01:37:05,450 --> 01:37:10,660
run our application we should see through,
you can see that down here we see true. Also,
1033
01:37:10,660 --> 01:37:15,850
if A is five, and B would be something like
three, if you run the application, we should
1034
01:37:15,850 --> 01:37:20,300
see through once again, we can see through.
But if we change the value of a from five
1035
01:37:20,300 --> 01:37:25,660
to something like two, because none of these
condition is true, right now, we should see
1036
01:37:25,660 --> 01:37:31,670
false, let's run our application. And we can
see false, the other comparison operator is
1037
01:37:31,670 --> 01:37:37,710
less than equal. And we use that like this,
we can use this right angle bracket and equal
1038
01:37:37,710 --> 01:37:45,110
sign this in here means that check if a is
less than b, or if a is equal to b, right
1039
01:37:45,110 --> 01:37:49,650
now, a is less than b. So the answer should
be true if we run our application we should
1040
01:37:49,650 --> 01:37:55,640
see through. But if we change the value of
a to something like five, we should see false.
1041
01:37:55,640 --> 01:38:01,800
And we can see false. Beside these, we also
have another kind of comparison operator.
1042
01:38:01,800 --> 01:38:07,290
And that's this explanation Mark plus the
equal sign this in here means that check if
1043
01:38:07,290 --> 01:38:13,120
A is not equal to b, this exclamation mark
in here as a negative meaning to our equal
1044
01:38:13,120 --> 01:38:19,110
operator right now, a is not equal to b. So
the answer should be true. If we run our application
1045
01:38:19,110 --> 01:38:24,440
we should see through and through has been
presented. Okay, these were the comparison
1046
01:38:24,440 --> 01:38:28,580
operators that I was going to talk about,
feel free to practice them because we are
1047
01:38:28,580 --> 01:38:33,670
going to use them a lot in our course. Next
I'm going to talk about logical operators
1048
01:38:33,670 --> 01:38:38,600
in Java. But before that, let's quickly comment
these two line of code down in here, let's
1049
01:38:38,600 --> 01:38:44,630
define a new Boolean, let's say Boolean answer
is equal to let me write the syntax and I
1050
01:38:44,630 --> 01:38:52,630
will talk about it, we can say a is equal
to five, or let's say B is equal to something
1051
01:38:52,630 --> 01:38:58,720
like two. This is our first logical operator
this in here means or you can add these two
1052
01:38:58,720 --> 01:39:06,200
pipelines by using shift plus backslash backslash
is somewhere above the enter in English keyboard.
1053
01:39:06,200 --> 01:39:13,140
So in here, this OR operator means that if
a is equal to five, or if b is equal to two,
1054
01:39:13,140 --> 01:39:18,260
assign through to this answer, if none of
this condition is true, so assigned false
1055
01:39:18,260 --> 01:39:24,410
to this answer, right now, a is equal to five
at least one of our conditions are true. So
1056
01:39:24,410 --> 01:39:29,650
the answer should be true. So if we print
our answer, we should see through, let's run
1057
01:39:29,650 --> 01:39:35,260
the application. And you can see through down
in here, if we change the value of a for example
1058
01:39:35,260 --> 01:39:42,430
to six, because none of these conditions are
true, we should see false. Let's run the application.
1059
01:39:42,430 --> 01:39:46,540
And then here we can see false. The other
kind of logical operator that I'm going to
1060
01:39:46,540 --> 01:39:52,600
talk about is to add science. Let's quickly
see that as well. This in here is an operator
1061
01:39:52,600 --> 01:39:58,390
it means that assign through to answer if
both of these conditions are true, if A is
1062
01:39:58,390 --> 01:40:04,680
equal to five and also if B is equal to two
if one of these conditions is not true, so
1063
01:40:04,680 --> 01:40:09,100
assign false the answer right now, none of
these conditions is true. So we should see
1064
01:40:09,100 --> 01:40:14,800
false as our answer. Let's run the application.
And as you can guess, we see false. If we
1065
01:40:14,800 --> 01:40:20,830
change the value of a to five, and run our
application, once again, we should see false,
1066
01:40:20,830 --> 01:40:25,950
because the second condition is not true.
But if we change the value of v2, and run
1067
01:40:25,950 --> 01:40:30,930
our application, this time we should see through
and we can see through them here. And these
1068
01:40:30,930 --> 01:40:36,180
were the two kinds of logical operators that
I was going to talk about or and and now that
1069
01:40:36,180 --> 01:40:41,570
we have talked about operators that we wanted,
let's talk about conditional statements, I'm
1070
01:40:41,570 --> 01:40:45,610
going to comment these two lines of code.
The first kind of conditional statements that
1071
01:40:45,610 --> 01:40:50,560
I'm going to talk about is called if statements,
we can type something like that we can say
1072
01:40:50,560 --> 01:40:56,730
if a pair of parentheses inside the parentheses
we need to provide our condition. For example,
1073
01:40:56,730 --> 01:41:03,510
we can say if a is greater than three, after
the parentheses we need a pair of curly braces
1074
01:41:03,510 --> 01:41:10,880
inside the curly braces, we can put our code
for example, let's say a is greater than three.
1075
01:41:10,880 --> 01:41:16,300
So in here, we are saying that if our condition
is true, execute the code inside the curly
1076
01:41:16,300 --> 01:41:22,650
braces. If it's not true, jump to after the
curly braces. For example, in here, let's
1077
01:41:22,650 --> 01:41:28,800
say continue. Right now a is five, and it's
greater than three. So we should see this
1078
01:41:28,800 --> 01:41:34,510
text printed, let's run our application, you
can see that a is greater than three. But
1079
01:41:34,510 --> 01:41:37,480
if we change the value of a to something like
two
1080
01:41:37,480 --> 01:41:43,570
and run our application, you can see that
we are no longer seeing this text in if statements
1081
01:41:43,570 --> 01:41:50,480
we can also have multiple conditions. For
example, in here, I can say else, I can provide
1082
01:41:50,480 --> 01:41:55,320
another pair of curly braces inside these
curly braces, I can say something like this,
1083
01:41:55,320 --> 01:42:02,510
I can say a is less than three. Right now
a is two, a is less than two. So we should
1084
01:42:02,510 --> 01:42:09,090
see only this text printed, let's run our
application, you can see that a is less than
1085
01:42:09,090 --> 01:42:14,480
three. As for the condition, you can provide
your condition like this directly inside your
1086
01:42:14,480 --> 01:42:20,040
if statement parentheses or you can define
your Boolean before your if statement. For
1087
01:42:20,040 --> 01:42:27,650
example, in here, if I say Boolean answer
is equal to a greater than three. Now I can
1088
01:42:27,650 --> 01:42:34,080
pass my answer. inside these parentheses,
I can say if answer is equal to true, let's
1089
01:42:34,080 --> 01:42:38,730
minimize this ramping for now. This is going
to work the same path. If you hover over the
1090
01:42:38,730 --> 01:42:45,170
highlighting here, the intelligent ID is warning
me about something the first one is that answer
1091
01:42:45,170 --> 01:42:50,690
equals equals to true can be simplified to
answer it means that I can delete this part,
1092
01:42:50,690 --> 01:42:54,890
this is going to work exactly the same. If
we run our application, we should see the
1093
01:42:54,890 --> 01:43:00,940
same result a is less than three. Let's run
our application. And you can see a is less
1094
01:43:00,940 --> 01:43:06,380
than three. If we want to negate this answer,
we can add an exclamation mark before our
1095
01:43:06,380 --> 01:43:11,490
variable. For example, in here we can say
if answer is not equal to true, if we run
1096
01:43:11,490 --> 01:43:16,710
our application, we should see the first line,
we can see that a is greater than three. This
1097
01:43:16,710 --> 01:43:22,780
is not true because A is true. But in here
we have changed the meaning of this brilliant,
1098
01:43:22,780 --> 01:43:27,070
we have added a negative meaning. Let's delete
this for now. And let's run our application
1099
01:43:27,070 --> 01:43:33,000
once again. Besides else, you can also have
an else if as well. Let's briefly comment
1100
01:43:33,000 --> 01:43:38,510
this if statement. And let's write another
one. down in here I'm going to see if inside
1101
01:43:38,510 --> 01:43:44,720
this if statement. Let's check that if a is
positive or negative, I can say if a is greater
1102
01:43:44,720 --> 01:43:51,610
than zero, inside the curly braces, let's
print as positive. In here instead of else,
1103
01:43:51,610 --> 01:43:57,130
I can use something like this, I can say else
if now we need to put another condition in
1104
01:43:57,130 --> 01:44:04,650
here we can say if a is less than zero. Once
again, we need a pair of curly braces. Inside
1105
01:44:04,650 --> 01:44:12,570
the curly braces, we can say a is negative.
You can have as many elsif cases as you want.
1106
01:44:12,570 --> 01:44:17,610
For example, I can add another one in here.
But of course in here, I don't think this
1107
01:44:17,610 --> 01:44:23,490
if statement would mean anything. But you
can have as many as you want. But in every
1108
01:44:23,490 --> 01:44:30,220
if statement you can have at most one else
case, let's delete all of these. For example,
1109
01:44:30,220 --> 01:44:35,510
I can have only one l space this else is going
to be the default case. In this case if we
1110
01:44:35,510 --> 01:44:42,440
use this else, it means that a is not greater
than zero. And also it's not less than zero.
1111
01:44:42,440 --> 01:44:48,970
So it is zero. In here we can safely say a
is zero. This is going to be the default case
1112
01:44:48,970 --> 01:44:54,870
for our if statement right now A is true.
So we should see a is positive. Let's run
1113
01:44:54,870 --> 01:45:02,550
our application. And as you can see a is positive.
Let's change a to minus two. If we run our
1114
01:45:02,550 --> 01:45:07,360
application, you can guess it, we should see
a is negative. And we can see that down in
1115
01:45:07,360 --> 01:45:14,360
here. If we change the value of oil to zero,
we should see a zero. And as you can see,
1116
01:45:14,360 --> 01:45:20,080
we can see that down in here. So if statements
are one kind of conditional statements, we
1117
01:45:20,080 --> 01:45:25,760
have another one as well, let's comment all
of these, the other one is called switch statements.
1118
01:45:25,760 --> 01:45:31,090
Let's see that as well, we can say switch
inside the parentheses of the switch statement,
1119
01:45:31,090 --> 01:45:36,170
we need to provide the variable that we want
to switch on. For example, I can pass a in
1120
01:45:36,170 --> 01:45:43,430
here, after the parentheses, I can provide
a pair of curly braces inside the curly braces,
1121
01:45:43,430 --> 01:45:48,920
I can define different cases. For example,
I can say in case a is equal to one after
1122
01:45:48,920 --> 01:45:55,060
that we need a column. And in case a is one,
we can print something for example, we can
1123
01:45:55,060 --> 01:46:02,190
see a is one. Also, it's very important to
provide a break after every case, let's do
1124
01:46:02,190 --> 01:46:07,540
that. I will talk about this break in a minute.
But for now, just know that after every case,
1125
01:46:07,540 --> 01:46:12,840
you are going to need a break, we can define
as many case as we want. For example, in here,
1126
01:46:12,840 --> 01:46:20,950
I can say in case a is to screen something,
let's say a is two. After that we need a break,
1127
01:46:20,950 --> 01:46:29,440
we can define another case. For example, for
three, let's say a is three. In every switch
1128
01:46:29,440 --> 01:46:34,610
statement, we are going to need a default
case as well. And we can pass that like this
1129
01:46:34,610 --> 01:46:42,050
we can say default. And we can print something
like a is not one two, or three. In the default
1130
01:46:42,050 --> 01:46:48,350
case, we need the break as well. Let's quickly
add that, right now the value of a is zero.
1131
01:46:48,350 --> 01:46:55,030
Let's change that to two. And you can guess
it we should see a is two, let's run the application.
1132
01:46:55,030 --> 01:47:00,660
And you can see a is two, if we change the
value of a two once again zero and run our
1133
01:47:00,660 --> 01:47:06,880
application. You can see a is not one, two
or three. Let's also talk about this break
1134
01:47:06,880 --> 01:47:12,860
in here. But before that, let's quickly change
the value of eight to one. This break in here
1135
01:47:12,860 --> 01:47:17,940
will cause our switch statement to break.
For example, if we don't have this break in
1136
01:47:17,940 --> 01:47:23,800
here at the first days, which is a is equal
to one. And if we run our application,
1137
01:47:23,800 --> 01:47:29,100
you can see that even though a is one we can
see both sentences, we can see a is one plus
1138
01:47:29,100 --> 01:47:34,750
a is two in here. Because we don't have any
break, we didn't break out of the switch statement.
1139
01:47:34,750 --> 01:47:40,150
And we have jumped to the second statement.
So don't forget to have a break after each
1140
01:47:40,150 --> 01:47:49,200
case in your switch statement. We are going
to talk about loops in Java. But before that,
1141
01:47:49,200 --> 01:47:54,880
let me just say that I have created my project,
I have added this package inside my source
1142
01:47:54,880 --> 01:48:00,920
folder. Inside this package we have only one
main class in which has this one main method,
1143
01:48:00,920 --> 01:48:05,940
I hope that you can come to this point by
yourself. If not, please watch the previous
1144
01:48:05,940 --> 01:48:12,520
videos on how to create a project and create
your main class and your main method. In programming
1145
01:48:12,520 --> 01:48:17,820
loops are useful for when you want to do a
repetitive job. For example, if you want to
1146
01:48:17,820 --> 01:48:24,060
print something 10 times like this in here.
If you want to print this Hello 10 times you
1147
01:48:24,060 --> 01:48:30,920
can just copy and paste it 10 times what in
general, it's a bad idea that you copy and
1148
01:48:30,920 --> 01:48:35,170
paste yourself whenever in programming you're
copying and pasting know that you're doing
1149
01:48:35,170 --> 01:48:41,160
something wrong. Instead of copy and pasting
we are going to use loops in this video. The
1150
01:48:41,160 --> 01:48:46,420
first kind of loops that I'm going to talk
about are for loops. Let me write it and we
1151
01:48:46,420 --> 01:48:53,000
will talk about its syntax. This is the general
syntax of a for loop first of all we write
1152
01:48:53,000 --> 01:48:59,640
for. After that we have a pair of parentheses
inside the parentheses we have three statements
1153
01:48:59,640 --> 01:49:05,020
in the first statement we are defining a new
integer called oil you can name your integer
1154
01:49:05,020 --> 01:49:11,550
whatever you want, and also we are initializing
our AI to have a value of zero. The second
1155
01:49:11,550 --> 01:49:17,190
statement is the condition of our for loop
we are going to continue this loop until this
1156
01:49:17,190 --> 01:49:23,400
condition is not met. And the third statement
is just a statement for changing the value
1157
01:49:23,400 --> 01:49:29,590
of i every time that we look PVC inaction
what all of these mean after the parentheses
1158
01:49:29,590 --> 01:49:35,660
we have a pair of curly braces in which we
can contain our code inside them. For example,
1159
01:49:35,660 --> 01:49:41,630
if we want to print something like hello here,
we can include that inside this pair of curly
1160
01:49:41,630 --> 01:49:46,580
braces. So what this for loop is going to
do is that first of all we are defining a
1161
01:49:46,580 --> 01:49:52,460
new integer called AI and we are assigning
the value of zero to that integer. After that
1162
01:49:52,460 --> 01:49:57,840
we are checking our condition we are checking
that if i is less than 10 in the first time
1163
01:49:57,840 --> 01:50:03,910
that we are going to look i is Hello So this
condition is met. If this condition is true,
1164
01:50:03,910 --> 01:50:08,550
we are going to execute the code inside the
curly braces. In this case we are going to
1165
01:50:08,550 --> 01:50:13,580
print hello. After printing Hello, we are
going to execute the third statement. In this
1166
01:50:13,580 --> 01:50:18,920
case, we are going to increase the value of
i by one. For the next time that we are going
1167
01:50:18,920 --> 01:50:23,940
to loop first of all we are going to check
our condition we are checking that if I is
1168
01:50:23,940 --> 01:50:30,510
still less than 10 or not. For the second
time I is one, I still is less than 10. So,
1169
01:50:30,510 --> 01:50:36,050
we are going to execute our code. This circling
here is going to continue and continue until
1170
01:50:36,050 --> 01:50:41,830
our condition is false. When it's false, we
are going to break out of the photo. So in
1171
01:50:41,830 --> 01:50:46,820
this case, we are going to print hello 10
times, let's run our application. And let's
1172
01:50:46,820 --> 01:50:52,780
see if we can see Hello 10 times, you can
see that we have printed Hello 10 times it
1173
01:50:52,780 --> 01:50:58,120
seems to be perfect. Instead of 10. You can
put any number that you want in here, for
1174
01:50:58,120 --> 01:51:03,250
example, you can say five, if you execute
your program, we will see that Hello would
1175
01:51:03,250 --> 01:51:09,020
be printed five times, instead of this five
in here, you can pass a variable for example,
1176
01:51:09,020 --> 01:51:14,850
if you had a variable before this for loop,
if we had int a is equal to five in here,
1177
01:51:14,850 --> 01:51:20,390
later on, we can pass a instead of five to
this photo. For example in here instead of
1178
01:51:20,390 --> 01:51:26,100
five, we can say if you run the application,
we should see the same result. Inside our
1179
01:51:26,100 --> 01:51:32,280
for loop we can also have access to the eye
itself. For example, instead of printing Hello,
1180
01:51:32,280 --> 01:51:38,990
we can print by, let's quickly paint that
as well. And let's run the application, you
1181
01:51:38,990 --> 01:51:44,590
can see that we are printing the numbers zero
to four. Okay for loop is one kind of loops
1182
01:51:44,590 --> 01:51:49,780
in Java. The other point is my loop. Let's
briefly see that as well. Once again, I'm
1183
01:51:49,780 --> 01:51:58,860
going to write the syntax of while loop and
we will talk about it after that.
1184
01:51:58,860 --> 01:52:03,610
This is the general syntax of a wild loop.
First of all, we type void. After that we
1185
01:52:03,610 --> 01:52:09,610
have a pair of parentheses inside the parentheses
we have our condition, in this case, a less
1186
01:52:09,610 --> 01:52:15,550
than 10. After that we have a pair of curly
braces and inside the curly braces we have
1187
01:52:15,550 --> 01:52:20,710
our code. In this case we are going to print
hello, notice that we are not changing the
1188
01:52:20,710 --> 01:52:25,880
value of a so this condition is always true.
And if I run my application right now, we
1189
01:52:25,880 --> 01:52:31,590
are going to stuck in an infinite loop. Let
me quickly run the application and see what
1190
01:52:31,590 --> 01:52:37,530
an infinite loop looks like. But before that,
let me comment this for loop, you can see
1191
01:52:37,530 --> 01:52:43,150
that no matter what happens, we are inside
an infinite loop we are printing Hello indefinitely.
1192
01:52:43,150 --> 01:52:49,600
In some cases, this might be our desired behavior.
But in most cases, we probably do not want
1193
01:52:49,600 --> 01:52:55,910
that we can exit our application by pressing
on this red button, we can also check our
1194
01:52:55,910 --> 01:53:00,940
exit code which is negative one in a normal
application, we would get zero. Okay, but
1195
01:53:00,940 --> 01:53:07,100
how can we exit a wild? There are multiple
ways for that the simplest way is to change
1196
01:53:07,100 --> 01:53:14,350
the value of a inside the void loop. For example,
after printing Hello, I can say a plus plus,
1197
01:53:14,350 --> 01:53:20,410
in order to increase the value of a so that
it won't be the same every time that we are
1198
01:53:20,410 --> 01:53:26,290
going to loop through this while loop. So
for example, for the first time a is five,
1199
01:53:26,290 --> 01:53:30,840
so our condition is true. So we are going
to execute these two line of code, we are
1200
01:53:30,840 --> 01:53:36,460
going to loop through the cycle until a is
10. when that's the case, this condition is
1201
01:53:36,460 --> 01:53:41,470
no longer met. And we are going to break out
of the Void loop. Let's run the application
1202
01:53:41,470 --> 01:53:47,720
one more time. This time you can see that
we have only printed Hello five time. And
1203
01:53:47,720 --> 01:53:53,250
also this exit code is zero which means that
we have exited our application successfully
1204
01:53:53,250 --> 01:53:59,660
without any error. The other way that break
out of avoid loop is to add a break. Let me
1205
01:53:59,660 --> 01:54:06,150
quickly add that this time after the first
loop we are going to break out of the while
1206
01:54:06,150 --> 01:54:12,030
loop it means that we are going to print hello
only once. Let's run the application. And
1207
01:54:12,030 --> 01:54:17,940
in here you can see that we have printed Hello
only once inside while loops we have another
1208
01:54:17,940 --> 01:54:23,340
keyword called continue. Let's quickly see
that as well. I'm going to delete this break
1209
01:54:23,340 --> 01:54:28,710
and before this print statement, I'm going
to put an if statement. Let's say if A is
1210
01:54:28,710 --> 01:54:36,500
equal to eight. Then let's continue. This
continuing here means that go to the next
1211
01:54:36,500 --> 01:54:41,920
record of your while loop go to the next round
of looping. It means that no matter what is
1212
01:54:41,920 --> 01:54:47,400
the rest of the code inside the viable break
out of the while loop and go to the next record,
1213
01:54:47,400 --> 01:54:53,840
so for example in this case, if A is five,
six or seven we are going to print hello.
1214
01:54:53,840 --> 01:54:59,010
But if a is eight you're not going to print
hello. After that we are going to print hello
1215
01:54:59,010 --> 01:55:04,580
in case if a is nice. If you don't want to
stuck in an infinite loop, you also need to
1216
01:55:04,580 --> 01:55:10,300
change the value of a software inside the
while loop. For example before this if statement,
1217
01:55:10,300 --> 01:55:16,010
I can say a plus was right now if I run my
application, I should say hello only four
1218
01:55:16,010 --> 01:55:21,100
times, because if a is eight, we are going
to continue the loop, it means that we are
1219
01:55:21,100 --> 01:55:26,150
going to jump to the next record and we are
not going to print hello, let's run the application
1220
01:55:26,150 --> 01:55:31,640
and see if we can see Hello four times. And
as you can see, we are seeing Hello four times.
1221
01:55:31,640 --> 01:55:37,670
So continue is a useful keyword for when you
want to jump to the next record. Okay, now
1222
01:55:37,670 --> 01:55:43,190
that we have talked about why loop let's also
talk about another kind of loops in Java,
1223
01:55:43,190 --> 01:55:50,550
the value of A in here is five. If we change
our condition to let's say a less than five,
1224
01:55:50,550 --> 01:55:56,350
we know that this condition is no longer true.
So we are not going to go inside the violin,
1225
01:55:56,350 --> 01:56:01,980
it means that we are not going to run the
code inside the void loop. Let's run the application,
1226
01:56:01,980 --> 01:56:06,860
we shouldn't see any Hello printed into our
console. And as you can see, we can't see
1227
01:56:06,860 --> 01:56:12,460
any Hello. But in some cases, you may want
to go inside the while loop at least once
1228
01:56:12,460 --> 01:56:18,381
no matter what the condition is. For those
situations, you can use do while loops. Let
1229
01:56:18,381 --> 01:56:24,350
me comment this while loop and let's create
a divide loop. It's a lot like via loops,
1230
01:56:24,350 --> 01:56:30,300
you need to type do inside the curly braces,
you need to put your court for example, we
1231
01:56:30,300 --> 01:56:36,490
can say System dot out dot print line. And
you can print hello, after the curly brace,
1232
01:56:36,490 --> 01:56:42,150
we need to say why. And inside the parentheses
you need to specify your condition for example,
1233
01:56:42,150 --> 01:56:48,770
we need to say a less than five, after the
parenthesis you need a semicolon, of course,
1234
01:56:48,770 --> 01:56:53,940
in this case, because A is equal to five,
this condition here is not true. But even
1235
01:56:53,940 --> 01:56:59,270
with that condition, we should see this Hello
at least once because we are using two wild
1236
01:56:59,270 --> 01:57:04,490
loops. Let's run the application. And let's
see if we can see Hello. As you can see, we
1237
01:57:04,490 --> 01:57:10,200
can see Hello only once. So do while loops
are useful for when you want to execute your
1238
01:57:10,200 --> 01:57:15,360
code, at least one no matter what the condition
is. We also have another kind of loops in
1239
01:57:15,360 --> 01:57:21,030
Java called for each loops. But I'm not going
to talk about that in here. Because I think
1240
01:57:21,030 --> 01:57:26,260
that if we know about different arrays and
collections, and in regard to that if we know
1241
01:57:26,260 --> 01:57:32,030
about object oriented programming, we will
understand it much better. So I'm going to
1242
01:57:32,030 --> 01:57:37,150
talk about for each loops after we have learned
about all those stuff. Okay, I think that's
1243
01:57:37,150 --> 01:57:42,470
enough for loops. Now we are going to have
a quick challenge. But before we have our
1244
01:57:42,470 --> 01:57:46,471
challenge, we need to know about two more
things, we need to know how to get the user's
1245
01:57:46,471 --> 01:57:52,340
input. And also we need to know how to generate
a random number. Later on, we will see that
1246
01:57:52,340 --> 01:57:57,510
how these two are going to be useful. In order
to get the users input, you can use something
1247
01:57:57,510 --> 01:58:02,650
called a scanner. The syntax in here might
not be familiar to you because we haven't
1248
01:58:02,650 --> 01:58:07,860
talked about object oriented programming.
But we are going to need that in our challenge.
1249
01:58:07,860 --> 01:58:12,660
Because we want to have an interactive challenge.
If you don't understand the syntax, just copy
1250
01:58:12,660 --> 01:58:17,090
and paste the code that I'm writing in here.
In order to define a scanner, you can type
1251
01:58:17,090 --> 01:58:21,840
something like this, you can see the scanner
with capital S, notice that it's coming from
1252
01:58:21,840 --> 01:58:27,150
Java dot util package. After that, you need
to name your scanner, which I'm going to say
1253
01:58:27,150 --> 01:58:33,550
a scanner, after that you can say is equal
to new scanner. Once again, with capital S
1254
01:58:33,550 --> 01:58:39,550
inside the parentheses you need to say system.in
we have seen system dot out which was useful
1255
01:58:39,550 --> 01:58:45,970
for outputting something system that is useful
for when you want to get the user's input.
1256
01:58:45,970 --> 01:58:53,590
Now you have a scanner, you can get a number
from user like this. You can say int answer
1257
01:58:53,590 --> 01:59:01,610
is equal to a scanner, dot next int this one
down here by saying a scanner dot next int
1258
01:59:01,610 --> 01:59:07,200
our scanner is going to wait for the user's
input until the user enter some number. We
1259
01:59:07,200 --> 01:59:12,840
will see that in action in a minute. But before
that, let's quickly output something. For
1260
01:59:12,840 --> 01:59:20,320
example, let's say answer was plus the value
of answer. Also, let's type something before
1261
01:59:20,320 --> 01:59:27,520
our scanner. Let's say please enter a number.
Let's run our application. And let's see what
1262
01:59:27,520 --> 01:59:32,960
these few lines of code are going to do. You
can see that we are saying please enter a
1263
01:59:32,960 --> 01:59:39,520
number text. And in here console is waiting
for the user to enter a number. For example,
1264
01:59:39,520 --> 01:59:46,140
I can say five by pressing Enter. You can
see that answer was five. And we have exited
1265
01:59:46,140 --> 01:59:52,470
our application successfully. So by using
a scanner, you can get the user's input. Besides
1266
01:59:52,470 --> 01:59:57,860
integers beside number, you can also get a
text from user if you want. Let's quickly
1267
01:59:57,860 --> 02:00:04,890
see that as well. First of all, let's go Something,
let's say enter your name. After that, I can
1268
02:00:04,890 --> 02:00:08,610
say something like this, I can say the string
name
1269
02:00:08,610 --> 02:00:14,840
is equal to scanner dot next, you can see
that this next method is going to return a
1270
02:00:14,840 --> 02:00:20,380
string. Don't worry about all of these new
stuff, method, scanner classes, object oriented
1271
02:00:20,380 --> 02:00:25,050
programming. We will talk about all of these
later on. But for now, because we are going
1272
02:00:25,050 --> 02:00:31,290
to need the user's input, we are going to
figure out a way to receive that. So by using
1273
02:00:31,290 --> 02:00:36,920
scanner dot, Next, you're going to get the
user's name. And after that, let's create
1274
02:00:36,920 --> 02:00:42,390
something. Let's say hello, close the name
of the user. Let's run our application once
1275
02:00:42,390 --> 02:00:48,650
again. In here, first of all, we need to enter
a number. Let's add four. After that the scanner
1276
02:00:48,650 --> 02:00:55,690
is waiting for my name, let's say Mesa. And
we can say hello Mesa message. So this way
1277
02:00:55,690 --> 02:01:01,380
we can get the user's input. Let's also see
how we can generate a random number. I'm going
1278
02:01:01,380 --> 02:01:06,200
to comment these lines of code for now, in
order to generate a random number, you can
1279
02:01:06,200 --> 02:01:11,980
say something like this, you can say random
with capital R, let's name it random, is equal
1280
02:01:11,980 --> 02:01:18,030
to new random. And we don't need to provide
anything inside the parentheses. After that,
1281
02:01:18,030 --> 02:01:23,530
in order to generate a random number, we can
say something like int number is equal to
1282
02:01:23,530 --> 02:01:31,270
random dot next int, this method in here is
going to generate a random number. let's print
1283
02:01:31,270 --> 02:01:39,390
that number. That's the number plus the number.
Let's run our application. Once again, you
1284
02:01:39,390 --> 02:01:45,480
can see that we have this random number. It's
also useful to specify a domain for your random
1285
02:01:45,480 --> 02:01:52,070
number. Right now, our random number is this
weird negative number, it might not be useful,
1286
02:01:52,070 --> 02:01:57,040
it's good to define a domain. In order to
define that domain, you can pass a number
1287
02:01:57,040 --> 02:02:03,360
to this next int method. For example, if I
put 20 in here, and if I run my application,
1288
02:02:03,360 --> 02:02:08,970
once again, the number should be somewhere
between zero and 19. If you want your random
1289
02:02:08,970 --> 02:02:15,710
number to be somewhere between one to 20,
you can simply add one in here, we can say
1290
02:02:15,710 --> 02:02:21,230
plus, but let's run the application. Once
again, we can see mine this time. This numbering
1291
02:02:21,230 --> 02:02:26,060
here is completely random. It means that every
time that we were on our application, we should
1292
02:02:26,060 --> 02:02:30,950
see a different number, you can see that we
are generating different numbers. Okay, now
1293
02:02:30,950 --> 02:02:36,530
that we know how to get a random number and
also how to get the user's input, it's time
1294
02:02:36,530 --> 02:02:42,360
for our challenge. Here is our challenge we
are going to create again, the concept behind
1295
02:02:42,360 --> 02:02:48,460
this game is to generate a random number and
ask the user to guess that number. continue
1296
02:02:48,460 --> 02:02:54,550
asking the user until you receive the correct
number. Also, to make everything more fun.
1297
02:02:54,550 --> 02:02:59,550
After five times of guessing gronke show a
game over message to the user. Here is how
1298
02:02:59,550 --> 02:03:04,770
our game is going to work. First of all, we
are going to show a welcome message. After
1299
02:03:04,770 --> 02:03:10,000
that we are going to ask for the user's name.
And we are going to say hello to the user.
1300
02:03:10,000 --> 02:03:15,050
After that, we are going to ask for the user's
permission to start the game. If you received
1301
02:03:15,050 --> 02:03:20,190
a positive answer, we are going to generate
a random number and we are going to ask for
1302
02:03:20,190 --> 02:03:25,460
the users guests. If the guest is correct,
we are going to show a congratulation message
1303
02:03:25,460 --> 02:03:31,330
and quit the game. If the guest is wrong,
we are going to ask again until we receive
1304
02:03:31,330 --> 02:03:36,680
the correct number. Also, as a hint to the
user beside the first time, every time that
1305
02:03:36,680 --> 02:03:42,180
you're asking for a number tell the user to
guess higher or lower. For example, if the
1306
02:03:42,180 --> 02:03:47,800
random number is 10. If the user has guessed
seven for the first time, tell him to guess
1307
02:03:47,800 --> 02:03:54,270
higher if the user failed after five times,
show a game over message and quit the game.
1308
02:03:54,270 --> 02:03:58,980
This challenging here is the combination of
everything that we have learned so far. So
1309
02:03:58,980 --> 02:04:03,490
make sure to practice before watching the
next video, it's going to help you a lot.
1310
02:04:03,490 --> 02:04:08,850
Okay, go solve the challenge. And after that,
come back to the next video so that you see
1311
02:04:08,850 --> 02:04:18,110
my solution for this challenge. See you in
the next video. I hope you solve the challenge
1312
02:04:18,110 --> 02:04:24,490
but if not, that's totally okay. After all,
it's your first encounter with Java. And it's
1313
02:04:24,490 --> 02:04:29,270
the first application that you're writing.
In this video, we are going to solve the challenge
1314
02:04:29,270 --> 02:04:36,260
together. Let's start by creating a new project.
Like before, I'm going to select Java next.
1315
02:04:36,260 --> 02:04:45,000
Once again next, let's name this project gets
me going. Let's create our project. And inside
1316
02:04:45,000 --> 02:04:50,970
our source folder, let's create our package
and Java file. Let's name this package or
1317
02:04:50,970 --> 02:04:51,970
that may code
1318
02:04:51,970 --> 02:04:59,750
that gives me a new Java class. Let's name
it main. Inside our main class, let's type
1319
02:04:59,750 --> 02:05:06,300
p SVM and now we have our main method. Okay,
first of all, we are going to show some welcome
1320
02:05:06,300 --> 02:05:13,890
message to the user. So let's say System dot
out dot print line, let's say welcome to Wonderland.
1321
02:05:13,890 --> 02:05:20,240
After that, we are going to ask for the user's
name. So let's say Please enter your name,
1322
02:05:20,240 --> 02:05:26,300
or marry have your name. After that, we need
to receive the user's name, in which we need
1323
02:05:26,300 --> 02:05:33,480
a scanner for that. Let's create our scanner.
Let's say scanner is equal to new scanner
1324
02:05:33,480 --> 02:05:41,670
system.me. After that, let's say string name
is the call to the scanner dot next, and let's
1325
02:05:41,670 --> 02:05:50,551
say hello to the user. Let's say hello, plus
the name. After that, let's ask for the user's
1326
02:05:50,551 --> 02:05:58,720
permission to start the game. Let's say Shall
we start? in here? I'm going to give the user
1327
02:05:58,720 --> 02:06:08,500
two option. And here are my two options. Let's
say one, yes. And the other, let's say no.
1328
02:06:08,500 --> 02:06:13,530
But in order to format my text a little bit
better, I'm going to add a special syntax
1329
02:06:13,530 --> 02:06:20,440
in here in which you haven't seen so far.
By adding backslash T. I'm adding a tab before
1330
02:06:20,440 --> 02:06:26,820
this. Yes. So this will be formatted with
a tab. Let's add one in here as well. Nothing
1331
02:06:26,820 --> 02:06:32,150
is special Just for formatting my text a little
bit better. After that, I'm going to save
1332
02:06:32,150 --> 02:06:41,220
the user's answer. Let's say eight, begin
answer is equal to the scanner dot next int.
1333
02:06:41,220 --> 02:06:47,660
And after that, I'm going to create a while
loop. Let's say vine begin answer is not equal
1334
02:06:47,660 --> 02:06:54,630
to one, we are going to continue asking our
question. So let's copy these three line of
1335
02:06:54,630 --> 02:07:00,470
code and paste them inside our while loop.
After that inside the void loop, we also need
1336
02:07:00,470 --> 02:07:06,740
to store the new users answer for that we
can say begin answer is equal to the scanner
1337
02:07:06,740 --> 02:07:12,800
dot next int. This way, if you are not going
to break out of this while loop until the
1338
02:07:12,800 --> 02:07:18,360
users answer is valid, when it's fun, we have
the permission to start the game. After the
1339
02:07:18,360 --> 02:07:23,660
void loop, it's time to generate a random
number, let's say random, random is equal
1340
02:07:23,660 --> 02:07:31,840
to a new random. After death, let's say int
x is equal to random dot next int for the
1341
02:07:31,840 --> 02:07:38,380
domain, I'm going to say 20. Because I want
my number to be from one to 20. So I need
1342
02:07:38,380 --> 02:07:44,400
to add a plus one in here. After that, it's
time to ask the user to guess a number. So
1343
02:07:44,400 --> 02:07:46,130
let's say
1344
02:07:46,130 --> 02:07:51,510
please get a number. And in here, we need
to save the user's answer.
1345
02:07:51,510 --> 02:08:00,430
Let's say int, user input is equal to a scanner
dot next int. In here, I'm going to define
1346
02:08:00,430 --> 02:08:06,560
three new variables and their usage as will
be clear in a minute. Let's quickly define
1347
02:08:06,560 --> 02:08:12,700
them. First of all, I'm going to save the
times that user has tried the game. So let's
1348
02:08:12,700 --> 02:08:20,070
say eight times tried is equal to zero initially,
after that, let's define a Boolean, let's
1349
02:08:20,070 --> 02:08:29,360
say Boolean. As well, initially, it's false.
And after that, a Boolean indicating if we
1350
02:08:29,360 --> 02:08:35,280
should finish the game. So let's say should
finish. Once again, it's going to be false.
1351
02:08:35,280 --> 02:08:39,830
Initially, After defining these three new
variables, I'm going to create another vial
1352
02:08:39,830 --> 02:08:45,230
of let's say, why should finish is not equal
to two.
1353
02:08:45,230 --> 02:08:46,560
Then,
1354
02:08:46,560 --> 02:08:52,170
inside the while loop, first of all, I'm going
to increase the times that user has right.
1355
02:08:52,170 --> 02:08:58,320
So let's say times three plus plus, after
that, I'm going to create an if statement.
1356
02:08:58,320 --> 02:09:05,780
Let's say if times three is less than five,
then we have the permission to continue. If
1357
02:09:05,780 --> 02:09:11,761
it's more than five, in the else case, we
need to break out of this while loop. For
1358
02:09:11,761 --> 02:09:17,640
that, I'm just going to say short finish is
equal to true. This is going to be the last
1359
02:09:17,640 --> 02:09:22,790
line of our while loop. So if we look once
again, because this condition is no longer
1360
02:09:22,790 --> 02:09:27,690
met, we are going to break out of the while
loop. But in case the time stride is less
1361
02:09:27,690 --> 02:09:33,600
than five, we are going to check that if the
users input is equal to the randomly generated
1362
02:09:33,600 --> 02:09:41,590
number for that I can say if user input is
equal to x, then we need to change the value
1363
02:09:41,590 --> 02:09:47,850
of husband to true because user has won the
game. And also we need to break out of this
1364
02:09:47,850 --> 02:09:53,610
while loop let's say short finish is equal
to true. Let's add an else if case to this
1365
02:09:53,610 --> 02:10:03,230
if statement. Let's say else. if let's say
if users input is greater than r x, which
1366
02:10:03,230 --> 02:10:09,010
is our randomly generated number, we are going
to tell the user to guess lower for that we
1367
02:10:09,010 --> 02:10:15,640
can say, guessed lower. And of course, we
need to wait for the new users inputs for
1368
02:10:15,640 --> 02:10:22,230
that we can say user input is equal to a scanner
dot next int. We also need another case in
1369
02:10:22,230 --> 02:10:29,100
here in case the number is less than x. If
that's the case, we are going to tell the
1370
02:10:29,100 --> 02:10:34,860
user to guess higher. And also if you're going
to wait for the new user input, let's say
1371
02:10:34,860 --> 02:10:41,320
user input is equal to scanner dot next int.
We are done with this void loop, we will review
1372
02:10:41,320 --> 02:10:46,410
what we are doing in here. But after the void
loop, we need another if statement, we can
1373
02:10:46,410 --> 02:10:54,360
say if the user has won the game. If that's
true, then we are going to show a message,
1374
02:10:54,360 --> 02:11:00,551
let's say congratulation. And also let's say
to the user, in which try he or she has won
1375
02:11:00,551 --> 02:11:12,040
the game. Let's say you have guessed in your
plus times tried plus guess. But in the else
1376
02:11:12,040 --> 02:11:18,360
case, first level, we are going to show a
game over message. Let's say game over. And
1377
02:11:18,360 --> 02:11:22,550
after that, we are going to say what was the
number,
1378
02:11:22,550 --> 02:11:30,280
let's say the number was plus x. This is our
entire application. Let's review what we are
1379
02:11:30,280 --> 02:11:35,840
doing in here. First of all, we are showing
a welcome message to the user. After that
1380
02:11:35,840 --> 02:11:41,510
we are asking for the user's name, we are
creating a scanner and after that we are waiting
1381
02:11:41,510 --> 02:11:47,050
for the user to enter his or her name. After
we received the name, we are waiting for the
1382
02:11:47,050 --> 02:11:53,050
user's permission to start the game, we are
saving the user's input in here, inside the
1383
02:11:53,050 --> 02:11:58,080
while loop we are making sure that we have
the permission. If we don't have the permission,
1384
02:11:58,080 --> 02:12:03,350
we are going to ask again and again until
we have the permission. After that, we made
1385
02:12:03,350 --> 02:12:08,900
sure that we have the permission, we are creating
a random number, the random number is between
1386
02:12:08,900 --> 02:12:15,320
one to 20. And after that, we are asking the
user to guess a number. We are saving the
1387
02:12:15,320 --> 02:12:21,170
user's input in here, we have defined three
new variables in here times through it has
1388
02:12:21,170 --> 02:12:27,350
one and short finish. Next, we are creating
this viral loop. It starts from here and ends
1389
02:12:27,350 --> 02:12:33,070
in here. The condition of this viral loop
is vile, we shouldn't finish the game continue
1390
02:12:33,070 --> 02:12:38,920
the loop. First thing inside this while loop
we are increasing the time stride. So if it's
1391
02:12:38,920 --> 02:12:44,860
the first time because the time stride is
initially zero, we are changing that to Vaughn.
1392
02:12:44,860 --> 02:12:50,210
After that we are checking that if times three
is less than five, we are comparing the user's
1393
02:12:50,210 --> 02:12:56,070
input with the randomly generated number.
If that's equal to a random number, we are
1394
02:12:56,070 --> 02:13:01,150
changing the value of this husband to true
and also we need to break out of this loop.
1395
02:13:01,150 --> 02:13:05,980
So we are changing the value of this short
finish to true. But in case the user input
1396
02:13:05,980 --> 02:13:11,160
is greater than our random number, we are
telling the user to guess lower and also we
1397
02:13:11,160 --> 02:13:17,780
are waiting for the user's input. But in case
the user's input is less than our random number,
1398
02:13:17,780 --> 02:13:23,390
we are telling the user to guess higher once
again, we are waiting for the users input
1399
02:13:23,390 --> 02:13:29,350
in case the time strike is greater than five,
we are just waiting out of the loop. If that's
1400
02:13:29,350 --> 02:13:35,530
the case, the husband is still false. So we
are checking that in here. If has one is equal
1401
02:13:35,530 --> 02:13:41,790
to true it means that user has won the game.
So we are showing this congratulation message.
1402
02:13:41,790 --> 02:13:47,270
Also we are showing that English try the user
has guessed the number correctly. But in case
1403
02:13:47,270 --> 02:13:52,710
if the user didn't want the game, we are showing
this game over message and also we are showing
1404
02:13:52,710 --> 02:13:58,730
the random number. Let's run the game and
see if everything is working fine. We are
1405
02:13:58,730 --> 02:14:05,530
seeing the welcome message. After that it's
asking for our name. Let's put Mesa we are
1406
02:14:05,530 --> 02:14:11,230
seeing Hello Mesa after that we are seeing
the message for our permission, shall we start
1407
02:14:11,230 --> 02:14:17,300
or it was better to say Shall we begin? Let's
say to know once again, we are seeing the
1408
02:14:17,300 --> 02:14:22,650
message. This is our first wide look. Also
this formatting here is because of that backslash
1409
02:14:22,650 --> 02:14:28,630
t that I have used when I have created this
text I'm talking about this one, this backslash
1410
02:14:28,630 --> 02:14:33,860
t in here. Also beside one and two. If you
enter any other number, we should see this
1411
02:14:33,860 --> 02:14:37,940
message once again. So let's say three for
example. You can see that we are seeing the
1412
02:14:37,940 --> 02:14:44,260
message. Okay, let's say Yes to continue.
The console is waiting for us to get a number.
1413
02:14:44,260 --> 02:14:50,470
Let's say 10 it says gets lower, let's say
five. It seems like I have won the game in
1414
02:14:50,470 --> 02:14:58,360
my second Troy. Okay, let's run the game once
again. May I have your name? Let's say Mason.
1415
02:14:58,360 --> 02:15:04,900
Yes. Let's begin. That's guess another number.
It says that gets lower, I'm going to guess
1416
02:15:04,900 --> 02:15:14,790
higher in order to debug all the features
of the game, let's say 1112 1315. You can
1417
02:15:14,790 --> 02:15:21,250
see that after five Troy, I can see the gameover
message. And I can see that the number was
1418
02:15:21,250 --> 02:15:26,710
nine. Okay, it seems like our application
is working perfect. Just before I finish off
1419
02:15:26,710 --> 02:15:34,080
this video, let's change this message in here
too. Shall we begin? I think that's better.
1420
02:15:34,080 --> 02:15:40,320
Okay, this was our challenge. I hope you solved
it if you did do celebrate, but if not, that's
1421
02:15:40,320 --> 02:15:45,560
okay. Later on in the course, we are going
to practice all of these many more times so
1422
02:15:45,560 --> 02:15:50,550
that we make sure that we have learned everything
that we see in the course. Okay, I think that's
1423
02:15:50,550 --> 02:15:54,720
a good point to stop the video in here. Just
before I do that, I'm going to say that in
1424
02:15:54,720 --> 02:15:58,610
the next video, we are going to talk about
arrays in Java.
1425
02:15:58,610 --> 02:16:07,770
See you in the next video. In this video,
we are going to talk about arrays in Java.
1426
02:16:07,770 --> 02:16:13,460
Up until now if we wanted to store some simple
data, for example, a number or maybe a text,
1427
02:16:13,460 --> 02:16:20,180
we could have used variables. For example,
we could have said int a is equal to five.
1428
02:16:20,180 --> 02:16:25,240
But what if we want to store a list of different
variables for example, a list of names or
1429
02:16:25,240 --> 02:16:31,630
a list of students. for storing a list of
variables, we have multiple options in Java.
1430
02:16:31,630 --> 02:16:36,309
The first one that we are going to talk about
in this video is called array, let's see how
1431
02:16:36,309 --> 02:16:42,059
we can have an array of for example strings.
For that we can say for string. After that,
1432
02:16:42,059 --> 02:16:47,170
we need a pair of square brackets. After that,
we need the name of our array. For example,
1433
02:16:47,170 --> 02:16:53,780
I can say students, next I can say new string
once again with a pair of square brackets.
1434
02:16:53,780 --> 02:16:59,340
This new keyboard in here is simple English,
it means that we are defining a new array
1435
02:16:59,340 --> 02:17:05,010
of strings inside this square bracket, we
can define the size of our array. For example,
1436
02:17:05,010 --> 02:17:09,840
if we want to serve five different student
names, I can say five in here, as simple as
1437
02:17:09,840 --> 02:17:15,940
that now we have an array of size five. After
defining your array, you can put some values
1438
02:17:15,940 --> 02:17:24,090
into it. For example, I can say the students
with an index of let's say, Vaughn is equal
1439
02:17:24,090 --> 02:17:29,719
to let's put my name in here It may sound
this way, we are assigning this value to the
1440
02:17:29,719 --> 02:17:34,990
element in which has the index of one in our
array, there is a very important point in
1441
02:17:34,990 --> 02:17:40,960
here and that indexes in Java starts from
zero. So in here, when I said the students
1442
02:17:40,960 --> 02:17:46,340
with an index of one, it means that we are
referring to the second element in our array.
1443
02:17:46,340 --> 02:17:51,050
If you want to assign some value to your first
element in your array, you can say students
1444
02:17:51,050 --> 02:17:59,050
with an index of zero. Let's see that as well,
let's say is equal to, for example, so we
1445
02:17:59,050 --> 02:18:05,000
can keep doing this for all of our elements
inside our array. Besides assigning values
1446
02:18:05,000 --> 02:18:10,400
to your array elements, you can also get the
value of those elements as well. For example,
1447
02:18:10,400 --> 02:18:14,780
in here, we can say print the value of a student
with an index of
1448
02:18:14,780 --> 02:18:16,260
two. You can
1449
02:18:16,260 --> 02:18:20,700
guess it in this case, it should be Tom. Let's
run the application and see if we can see
1450
02:18:20,700 --> 02:18:26,679
Tom. As you can see, Tom has been printed.
This is one way of assigning different values
1451
02:18:26,679 --> 02:18:31,810
to your array elements. The other way is like
this, you can assign different values at the
1452
02:18:31,810 --> 02:18:37,050
time of creating your area. For example, in
here I can see a string array, let's name
1453
02:18:37,050 --> 02:18:43,760
it employees is equal to instead of saying
new string array, I can pass a pair of curly
1454
02:18:43,760 --> 02:18:50,610
brace. And inside that curly brace, I can
pass my values. For example, I can say Mesa,
1455
02:18:50,610 --> 02:18:56,360
or every other value that I want. So these
are the two ways of assigning values to your
1456
02:18:56,360 --> 02:19:01,560
array elements, like a string array, you can
also have other kinds of arrays as well. For
1457
02:19:01,560 --> 02:19:06,559
example, if I want to put some numbers inside
an array, I can say int with a pair of square
1458
02:19:06,559 --> 02:19:13,440
brackets, let's say numbers is equal to, let's
say a pair of curly braces. And inside those
1459
02:19:13,440 --> 02:19:20,000
curly braces, I can pass my values, let's
say one, two, through six. When you're working
1460
02:19:20,000 --> 02:19:25,330
with different arrays, for loops can be very
helpful to access all of the elements inside
1461
02:19:25,330 --> 02:19:30,230
your array. For example, if I want to print
all of the values inside this numbers array,
1462
02:19:30,230 --> 02:19:35,250
I can say something like this, I can create
a folder. Let's say for int i is equal to
1463
02:19:35,250 --> 02:19:42,881
zero, i less than in this case, the size of
my numbers array is six. So let's say six.
1464
02:19:42,881 --> 02:19:48,760
After that, let's say i plus plus. Inside
the for loop, I can say print the value of
1465
02:19:48,760 --> 02:19:55,011
numbers with an index of I. This way, I am
going to print all of the numbers inside my
1466
02:19:55,011 --> 02:19:59,490
numbers array before running the application.
I'm going to comment this line of code so
1467
02:19:59,490 --> 02:20:05,570
that we Have a clean console. As you can see
numbers from one to six have been printed
1468
02:20:05,570 --> 02:20:10,630
into our console. So for loops are very helpful
when you're working with different kinds of
1469
02:20:10,630 --> 02:20:16,170
arrays. In here, we have hard coded these
six. But what if we don't know the size of
1470
02:20:16,170 --> 02:20:21,870
our array? For example, if we get some values
from our database or from our server in that
1471
02:20:21,870 --> 02:20:26,440
case, it might be possible that we don't know
the size of our array. How can we use for
1472
02:20:26,440 --> 02:20:30,980
loops with arrays, if you don't know the size
of our array for that, we can use a helpful
1473
02:20:30,980 --> 02:20:36,580
property of different areas. For example,
in this case, I can say numbers, dot length.
1474
02:20:36,580 --> 02:20:41,530
This way, we don't need to be worried about
the size of our array, we can just use it
1475
02:20:41,530 --> 02:20:46,980
like this. Let's run the application once
again. And as before, we have printed numbers
1476
02:20:46,980 --> 02:20:52,040
from one to six. Later on, we will see that
beside primitive data types and strings, we
1477
02:20:52,040 --> 02:20:56,410
can also have different kinds of arrays for
different objects, for example, for different
1478
02:20:56,410 --> 02:21:02,200
classes in Java, but more on that later on.
So using arrays is one way of having a list
1479
02:21:02,200 --> 02:21:08,750
of different items. But there are some limitations
to arrays. For example, the size of our array
1480
02:21:08,750 --> 02:21:13,561
after the definition is immutable. Let me
show you what I'm talking about. In here.
1481
02:21:13,561 --> 02:21:18,762
When we define our students array, we have
set the size to be five. But what if we want
1482
02:21:18,762 --> 02:21:24,110
to change the size of our array, for example,
what if we want to define a sixth element?
1483
02:21:24,110 --> 02:21:31,090
For example, if you say the students with
an index of five, let's say Brian, if you
1484
02:21:31,090 --> 02:21:36,980
hover over these five, you can see that array
index is out of bounds, it means that these
1485
02:21:36,980 --> 02:21:42,610
elements in here cannot fit in our array.
For example, if we try to reach to that element,
1486
02:21:42,610 --> 02:21:49,470
you should see a problem, let's say print
the students with an index of five. Before
1487
02:21:49,470 --> 02:21:55,090
that, let's comment this line of code. Let's
run our application. As you can see, we are
1488
02:21:55,090 --> 02:21:59,420
getting an exception in here. Once again,
when we get exceptions, it means that our
1489
02:21:59,420 --> 02:22:05,660
application crashes. And the exception type
in here is array index out of bounds exception,
1490
02:22:05,660 --> 02:22:11,630
which means that the sixth element does not
exist in our array. So arrays are immutable
1491
02:22:11,630 --> 02:22:17,260
after you define them, you cannot change their
size. This is one of the limitations of simple
1492
02:22:17,260 --> 02:22:21,670
arrays in Java, there are others as well.
For example, you cannot do that much with
1493
02:22:21,670 --> 02:22:26,460
your arrays. later on. When we use other kinds
of collections in Java, we will see that we
1494
02:22:26,460 --> 02:22:32,500
have all kinds of operations on our array
elements. But I'm not going to talk about
1495
02:22:32,500 --> 02:22:36,930
other kinds of collections in here because
I think that it's better to talk about them
1496
02:22:36,930 --> 02:22:42,220
after we know about object oriented programming.
Later on in the course, we will talk about
1497
02:22:42,220 --> 02:22:47,840
other kinds of collections like array lists
and maps. Just before I finish off this video,
1498
02:22:47,840 --> 02:22:52,820
I'm going to write a simple application in
which will help us to practice what we have
1499
02:22:52,820 --> 02:22:58,750
learned about arrays. Before that, let me
comment all of these lines of codes. And let's
1500
02:22:58,750 --> 02:23:03,970
start creating our application down in here.
In this application, I'm going to simulate
1501
02:23:03,970 --> 02:23:09,300
the contacts application on the phone, I'm
going to show a list of different contacts
1502
02:23:09,300 --> 02:23:14,520
names on a phone for to the user. And after
that by typing the contacts name, we are going
1503
02:23:14,520 --> 02:23:19,470
to give the user that contacts number. In
order to simulate that first of all, we are
1504
02:23:19,470 --> 02:23:26,300
going to need a string array, let's say a
string array names is equal to let's pass
1505
02:23:26,300 --> 02:23:33,420
our names directly. Let's say Mesa. After
that, we need a list of different numbers
1506
02:23:33,420 --> 02:23:40,000
for that I'm going to create that integer
array. Let's see numbers is equal to and let's
1507
02:23:40,000 --> 02:23:46,100
initialize our array. After that, I'm going
to show the name of all of the available contacts
1508
02:23:46,100 --> 02:23:54,120
on the phone. So let's create a for loop.
For int i is equal to zero, i less than names
1509
02:23:54,120 --> 02:24:01,570
that length, and i plus plus. let's print
the names inside our names array. Let's say
1510
02:24:01,570 --> 02:24:08,530
print names with an index of I. After the
for loop, I'm going to ask the user to enter
1511
02:24:08,530 --> 02:24:16,010
a name. So let's say print. Let's say please
enter a name. After that we need our scanner.
1512
02:24:16,010 --> 02:24:23,570
So let's create that is equal to new scanner
system that mean after that, let's get the
1513
02:24:23,570 --> 02:24:25,550
contacts name. Let's say
1514
02:24:25,550 --> 02:24:27,670
the string name
1515
02:24:27,670 --> 02:24:33,850
is equal to a scanner dot next. Then I'm going
to create a for loop and inside that loop
1516
02:24:33,850 --> 02:24:40,841
I'm going to find the index of the contact.
So let's say for int i is equal to zero. i
1517
02:24:40,841 --> 02:24:49,050
less than names, dot length, i plus plus.
Let's write an if statement in here. Let's
1518
02:24:49,050 --> 02:24:57,760
say if name is equal to names with an index
of joy. Then inside this if statement we are
1519
02:24:57,760 --> 02:25:05,510
going to print the contacts number for that
I can see print numbers with an index of pi.
1520
02:25:05,510 --> 02:25:10,220
But right now there is a problem with my code,
I'm not sure that if it's going to work fine
1521
02:25:10,220 --> 02:25:15,010
or not, and that's because of these two equal
signs in here. Let's run the application and
1522
02:25:15,010 --> 02:25:20,860
see if it's going to work. Let's enter our
name in here. Let's say Sarah. And as you
1523
02:25:20,860 --> 02:25:26,330
can see, nothing has been printed. Let's talk
about these two equal signs in here. If you
1524
02:25:26,330 --> 02:25:31,040
hover over the warning in here, it says that
the string values are compared using two equal
1525
02:25:31,040 --> 02:25:37,340
signs not equal with a pair of parentheses.
The problem in here is that when you're using
1526
02:25:37,340 --> 02:25:43,270
two equal signs, you are checking for the
equality of two strings by a reference, I'm
1527
02:25:43,270 --> 02:25:49,370
talking about the reference inside the devices
memory Ram. Whenever you are storing a variable,
1528
02:25:49,370 --> 02:25:55,190
that variable allocates some space inside
the device's memory RAM, and that allocated
1529
02:25:55,190 --> 02:26:01,131
a space has some address. And that allocated
the space has some reference in which we are
1530
02:26:01,131 --> 02:26:06,690
comparing those two references in here together.
So even if the value of two strings in here
1531
02:26:06,690 --> 02:26:11,800
can be the same, but the reference might not
be for that reason, it's not a good way to
1532
02:26:11,800 --> 02:26:17,591
compare two strings by using these two equal
signs. Instead of using these two equal signs,
1533
02:26:17,591 --> 02:26:22,240
there is a better option for comparing two
strings, and that's the equals method. Let's
1534
02:26:22,240 --> 02:26:28,490
see that as well. In here, I can say name,
dot equals, and inside the parentheses, I
1535
02:26:28,490 --> 02:26:35,000
can pass the other string. For example, I
can say names with an index of I. If you remember
1536
02:26:35,000 --> 02:26:39,511
from the previous videos, I said that the
strings are not primitive data type, and they
1537
02:26:39,511 --> 02:26:45,530
are objects in Java. And because they are
objects, we have some properties on our strings.
1538
02:26:45,530 --> 02:26:51,200
For example, this equals method. Once again,
we are going to talk about methods later on
1539
02:26:51,200 --> 02:26:56,070
in the course when we talk about object oriented
programming. But for now, just know that there
1540
02:26:56,070 --> 02:27:02,150
is a method called equals in our string class,
in which we can use to check the equality
1541
02:27:02,150 --> 02:27:08,650
of two strings by the values. So now, if you're
on our application, we should get Sarah's
1542
02:27:08,650 --> 02:27:16,000
number. Let's run the application. In here,
once again, let's see Sarah. And as you can
1543
02:27:16,000 --> 02:27:20,040
see, we are getting the number. Let's also
check that if you're getting the correct number,
1544
02:27:20,040 --> 02:27:26,170
Sarah is the second contact in our array.
And the second element in our numbers array
1545
02:27:26,170 --> 02:27:30,930
is this number in which we have printed into
our console, it seems like our application
1546
02:27:30,930 --> 02:27:36,070
is working fine. Okay, I think that's enough
talking about arrays in Java. In the next
1547
02:27:36,070 --> 02:27:40,721
video. Finally, we are going to start talking
about object oriented programming. That's
1548
02:27:40,721 --> 02:27:45,520
a very important topic, and it's going to
help you a lot to understand Java, so make
1549
02:27:45,520 --> 02:27:54,050
sure to Don't miss the next two or three videos.
See you in the next video.
1550
02:27:54,050 --> 02:27:58,220
As I said at the end of the previous video,
in this video, finally we are going to start
1551
02:27:58,220 --> 02:28:03,350
talking about object oriented programming.
Up until this point with the help of variables,
1552
02:28:03,350 --> 02:28:08,400
we could have defined simple data types. For
example, if we wanted to have numbers, we
1553
02:28:08,400 --> 02:28:15,180
could have say int number is equal to five,
four. Similarly, we could have defined characters,
1554
02:28:15,180 --> 02:28:19,840
texts, and all sorts of variables that we
have talked about in previous videos. But
1555
02:28:19,840 --> 02:28:25,180
what if we want to have a more complex data?
For example, how can we define a car in Java
1556
02:28:25,180 --> 02:28:30,320
or a phone or every other object that you
can think of? Well, that is possible with
1557
02:28:30,320 --> 02:28:35,670
the concept of object oriented programming.
In object oriented programming, you define
1558
02:28:35,670 --> 02:28:42,480
your own objects, the core component of object
oriented programming our classes. Right now
1559
02:28:42,480 --> 02:28:47,380
you can see that we are inside the class,
this class is named main. But if we want,
1560
02:28:47,380 --> 02:28:52,400
we can create our own class as well. Let me
quickly delete this line of code. And let's
1561
02:28:52,400 --> 02:28:58,750
create a new class. For example, inside my
source folder, inside my package on the package,
1562
02:28:58,750 --> 02:29:04,290
I can right click and by selecting new Java
class, I can create my class. In here I need
1563
02:29:04,290 --> 02:29:10,270
to name my class. For example, I can say phone,
notice that there is a convention, you need
1564
02:29:10,270 --> 02:29:16,390
to use upper cases for the first letter of
the name of your class. Also, you cannot have
1565
02:29:16,390 --> 02:29:22,020
spaces or some strange characters as the name
of your classes. So in here, I'm just going
1566
02:29:22,020 --> 02:29:27,000
to simply name my class form. Notice that
there are other options in here as well for
1567
02:29:27,000 --> 02:29:31,980
example interface and you know, later on we
will talk about them but for now, let's just
1568
02:29:31,980 --> 02:29:38,380
create a class the one that has the CI con.
By pressing Enter, we can create our class.
1569
02:29:38,380 --> 02:29:44,170
This is how a class looks like it lives inside
the package right now its package is altered.
1570
02:29:44,170 --> 02:29:50,410
maker.org is section one. Also if you take
a look at the left pane here you can see that
1571
02:29:50,410 --> 02:29:56,690
a new class has been added into our package.
Every class has the keyword class in its declaration.
1572
02:29:56,690 --> 02:30:03,540
Also every class has a name and besides This
too has an access modifier. We will talk about
1573
02:30:03,540 --> 02:30:08,220
access modifiers in few minutes. But for now
let's continue on. Every class has a pair
1574
02:30:08,220 --> 02:30:13,900
of curly braces in which we can put our code
inside them. A class is going to represent
1575
02:30:13,900 --> 02:30:18,840
an object. In this case, we are going to represent
a phone. If you think of different objects,
1576
02:30:18,840 --> 02:30:24,820
objects can have different properties. For
example, in the case of a phone, a phone can
1577
02:30:24,820 --> 02:30:30,620
have some amount of memory RAM, it can have
a screen size. Beside that it has a name,
1578
02:30:30,620 --> 02:30:35,640
and all sorts of other properties that you
can think of. When you define a class, you
1579
02:30:35,640 --> 02:30:40,330
can have properties for your classes as well.
For example, in this case, if we want to give
1580
02:30:40,330 --> 02:30:45,190
some properties to our phone class, we can
say something like this, we can say the string
1581
02:30:45,190 --> 02:30:52,410
name. Now we define a property called name
for our phone. Notice that I'm not initializing
1582
02:30:52,410 --> 02:30:57,330
the property of my class in here, I'm not
giving a name to my phone. And that's because
1583
02:30:57,330 --> 02:31:02,820
when we declare a class, we can reuse that
class, we can create multiple instance of
1584
02:31:02,820 --> 02:31:07,840
that class with different properties. For
example, if we define a new iPhone, the name
1585
02:31:07,840 --> 02:31:13,220
would be iPhone, if we define another phone,
the name of that phone might be different.
1586
02:31:13,220 --> 02:31:18,091
So for that reason, I'm not initializing the
properties of my class, let's add a few more
1587
02:31:18,091 --> 02:31:25,030
properties to our phone object, let's say
in screen size. Beside that, let's say in
1588
02:31:25,030 --> 02:31:32,630
memory Ram. After that, let's say int camera.
So now that we have created this simple class,
1589
02:31:32,630 --> 02:31:38,010
we can instantiate it, we know for sure that
the starting point of our application is this
1590
02:31:38,010 --> 02:31:43,320
main method inside our main class. So if I
need to instantiate my class, I need to do
1591
02:31:43,320 --> 02:31:48,570
it in here. And here is how you can create
an instance of your class you type the name
1592
02:31:48,570 --> 02:31:53,940
of your class, you can see that when I typed
the name of my class, I'm getting some suggestions
1593
02:31:53,940 --> 02:31:59,680
from IntelliJ. Sometimes you may get multiple
options. And that's because different classes
1594
02:31:59,680 --> 02:32:04,801
with different names can live in different
packages. For that reason, always take a look
1595
02:32:04,801 --> 02:32:09,650
at the package that your object lives in.
In this case, I am going to just import this
1596
02:32:09,650 --> 02:32:15,630
class. After that you need to name your instance
for example, I can say iPhone, next I can
1597
02:32:15,630 --> 02:32:22,000
say is equal to new phone. This way with the
help of this new keyword, we can create a
1598
02:32:22,000 --> 02:32:27,740
new instance of our object. Now that we have
created an instance of our class, we can have
1599
02:32:27,740 --> 02:32:32,920
access to the properties of our objects our
class, for example, if you want to define
1600
02:32:32,920 --> 02:32:39,590
some property for our class, we can say something
like this, we can say iPhone dot, let's say
1601
02:32:39,590 --> 02:32:42,851
name is equal to let's name it iPhone 11.
1602
02:32:42,851 --> 02:32:49,860
With the help of this dot operator, you can
have access to all of the properties or to
1603
02:32:49,860 --> 02:32:56,130
be precise all of the fields of your class.
Similar to assigning a value to your classes,
1604
02:32:56,130 --> 02:33:00,530
properties or fields, you can also get those
properties or fields. And here is how you
1605
02:33:00,530 --> 02:33:04,830
can get them. For example, if you want to
print the name of our iPhone, we can say something
1606
02:33:04,830 --> 02:33:10,830
like this, we can say print, iPhone dot name.
If you run our application, we should see
1607
02:33:10,830 --> 02:33:17,440
iPhone 11 printed into our console. And here
is iPhone 11. Similar to that you can have
1608
02:33:17,440 --> 02:33:22,840
access to all of the fields of your class.
For example, I can say iPhone, let's say memory
1609
02:33:22,840 --> 02:33:28,840
RAM is equal to eight. Beside properties,
your classes can also have some behaviors.
1610
02:33:28,840 --> 02:33:33,390
For example, in the case of a phone, you can
play some music with your phone, or you can
1611
02:33:33,390 --> 02:33:38,550
call somebody. If you want to define some
behavior for your class, you can use methods,
1612
02:33:38,550 --> 02:33:43,090
let's see them as built. Let's switch back
to our phone class. Here is how you can define
1613
02:33:43,090 --> 02:33:49,180
a method I'm going to write the syntax and
after that we will talk about it. First of
1614
02:33:49,180 --> 02:33:54,620
all, our method has a name in this case its
name is play music. After that a method can
1615
02:33:54,620 --> 02:33:59,530
have an access modifier like we did for the
declaration of our class, we will talk about
1616
02:33:59,530 --> 02:34:04,511
this public in a minute. After that every
method can have some inputs. For example,
1617
02:34:04,511 --> 02:34:09,591
in this case, we are getting a string called
trackman. Later on, when we use this method,
1618
02:34:09,591 --> 02:34:14,680
we need to pass some input to this method.
If you don't want to have any inputs for your
1619
02:34:14,680 --> 02:34:19,610
method, you can just pass a pair of empty
parentheses. But if you want, you can have
1620
02:34:19,610 --> 02:34:24,630
one or more inputs. If you want to have multiple
inputs for your method, you can separate those
1621
02:34:24,630 --> 02:34:30,890
inputs with a comma. For example in here I
can say is drink album name. But I'm not going
1622
02:34:30,890 --> 02:34:35,970
to do that in here. But you can have as many
inputs as you want. beside a name, an input
1623
02:34:35,970 --> 02:34:42,640
and also an access modifier. Every method
can return something in this case, it's void,
1624
02:34:42,640 --> 02:34:47,970
but if you want you can change it. For example,
we can say a string as the return type of
1625
02:34:47,970 --> 02:34:54,040
your method. You can also have integers or
other kinds of primitive data types. Even
1626
02:34:54,040 --> 02:34:59,230
you can return another class with your method.
But more on that later on. If you don't want
1627
02:34:59,230 --> 02:35:05,220
to return any From your method, you can use
the keyword void. Like we did before, we will
1628
02:35:05,220 --> 02:35:10,340
talk more about the return type of every method
later on. But for now, we are not going to
1629
02:35:10,340 --> 02:35:14,760
return anything from this method. So we are
going to pass void inside the curly braces
1630
02:35:14,760 --> 02:35:20,200
of my method, I can put my code for example,
in this case, I'm going to play some music,
1631
02:35:20,200 --> 02:35:26,730
or at least I'm going to print something.
In this case, I'm just going to say, playing
1632
02:35:26,730 --> 02:35:28,180
plus the track me.
1633
02:35:28,180 --> 02:35:34,590
Now that I have created this method, I can
use it in my main class where I have instantiated
1634
02:35:34,590 --> 02:35:40,890
my object. For example, in here I can see
iPhone dot play music. Once again, you can
1635
02:35:40,890 --> 02:35:46,480
see that by typing dots, I can have access
to my methods as well. Right now, I'm getting
1636
02:35:46,480 --> 02:35:51,850
error error in here. And that's because when
I have created my method, I have set an input
1637
02:35:51,850 --> 02:35:57,050
for it. So when I'm going to use my method,
I need to pass a tracking. For example, let's
1638
02:35:57,050 --> 02:36:02,771
say our beings are bearing the name of a track.
Let's run the application and see what would
1639
02:36:02,771 --> 02:36:08,800
happen, you can see that we are playing some
music. So by defining methods, you can have
1640
02:36:08,800 --> 02:36:14,220
behaviors for your classes, there are a few
reasons for using methods. The first one is
1641
02:36:14,220 --> 02:36:19,890
that you can reuse your method. For example,
in here, after this line of code, I can say
1642
02:36:19,890 --> 02:36:26,900
iPhone dot play music, and I can pass a different
track name, let's pass lamenting case this
1643
02:36:26,900 --> 02:36:33,590
time. If we run our application, you can see
that our methods are being executed one at
1644
02:36:33,590 --> 02:36:39,850
a time. So the first benefit of using methods
is that you can reuse them. The next one is
1645
02:36:39,850 --> 02:36:46,030
for organizing our code. For example, imagine
that you want to have some sort of calculation,
1646
02:36:46,030 --> 02:36:51,880
you can pass all the codes for that calculation
into a method. And by calling that method,
1647
02:36:51,880 --> 02:36:57,010
you can pass all of those calculations to
a method and later on, you can just call that
1648
02:36:57,010 --> 02:37:02,050
method in order to do the calculation, it's
going to help us a lot for organizing our
1649
02:37:02,050 --> 02:37:07,970
code. Also, by using methods, you can encapsulate
things we will talk about encapsulation in
1650
02:37:07,970 --> 02:37:14,061
later videos. But in here inside your class,
you can define your methods as private, and
1651
02:37:14,061 --> 02:37:19,011
that method will be accessible only from your
class and not from outside of your class.
1652
02:37:19,011 --> 02:37:23,631
If you go back to main class, you can see
that we are getting red warning. If you hover
1653
02:37:23,631 --> 02:37:30,620
over there, it says that this method has private
access. So by using private methods, you can
1654
02:37:30,620 --> 02:37:36,590
encapsulate different behaviors for your own
class. Okay, now let's talk about this access
1655
02:37:36,590 --> 02:37:41,650
modifier. You can have access modifiers, for
your methods for your classes. And also for
1656
02:37:41,650 --> 02:37:47,000
the fields of your classes, I can make this
name private, if I want as well. Basically,
1657
02:37:47,000 --> 02:37:53,820
we have three kind of access modifiers, we
have private we have public. And also we have
1658
02:37:53,820 --> 02:37:59,640
protected if you don't use anything as the
access modifier. It's equal to when you use
1659
02:37:59,640 --> 02:38:05,650
public. So there is no difference between
public string name or just a string name.
1660
02:38:05,650 --> 02:38:11,230
When you use public as the access modifier.
For example, if I use public in here, it means
1661
02:38:11,230 --> 02:38:16,970
that this field is going to be accessible
from other classes as well. But if I use private
1662
02:38:16,970 --> 02:38:22,430
in here, let's say private, if I go back to
my main class, you can see that we are getting
1663
02:38:22,430 --> 02:38:28,761
a red warning, it means that we can no longer
use this property. So by setting the access
1664
02:38:28,761 --> 02:38:35,090
modifier of our field or method, that field
or method is only accessible from inside our
1665
02:38:35,090 --> 02:38:40,101
class, beside public and private we also have
projects that we've been talking about protected
1666
02:38:40,101 --> 02:38:45,180
later on in the course both private and public
are the most access modifiers that we are
1667
02:38:45,180 --> 02:38:49,890
going to use in the course for the access
modifier of your class. You can also have
1668
02:38:49,890 --> 02:38:54,971
private admin group inside there. But usually
you wouldn't do that right now you can see
1669
02:38:54,971 --> 02:38:59,450
that we are getting a red warning in here.
And the warning says that modifier private
1670
02:38:59,450 --> 02:39:05,260
not allowed in here. Do you use private mostly
when you use inner classes we will talk about
1671
02:39:05,260 --> 02:39:11,160
inner classes later on in future videos. But
basically, they are some nested classes inside
1672
02:39:11,160 --> 02:39:16,930
other classes. When you define them as private
classes, you can have access to those nested
1673
02:39:16,930 --> 02:39:22,470
classes from the parent class. We will talk
about that later on when we talk about inner
1674
02:39:22,470 --> 02:39:26,970
classes. For now let's just change this one
to public. There are a few reasons for using
1675
02:39:26,970 --> 02:39:32,260
private as the access modifier of your fields
or methods. The first one is that you may
1676
02:39:32,260 --> 02:39:39,180
want to limit the access of your fields or
methods to the class itself and not other
1677
02:39:39,180 --> 02:39:44,090
classes. For example, imagine that you are
writing some code that you are going to publish
1678
02:39:44,090 --> 02:39:50,210
for other developers. In that case, you may
want to protect your classes against changes.
1679
02:39:50,210 --> 02:39:55,110
For that reason, you may want to change the
access modifiers of your fields and also methods
1680
02:39:55,110 --> 02:40:00,750
to private but now that we have defined the
access modifier of this field, as Private,
1681
02:40:00,750 --> 02:40:05,880
how can we have access to this name from outside
of this class, there is a way to that, let's
1682
02:40:05,880 --> 02:40:11,140
quickly talk about it, we can create public
methods. For example, down in here, I can
1683
02:40:11,140 --> 02:40:16,660
say public as the return type of this method,
I'm going to say void. For the name of this
1684
02:40:16,660 --> 02:40:22,091
method, I'm going to say set name. As you
can see, when I use the keyword set, I am
1685
02:40:22,091 --> 02:40:27,370
getting some suggestions from IntelliJ. Basically,
there is a convention among programmers to
1686
02:40:27,370 --> 02:40:33,271
set the name of the setter methods like this,
an intelligent is smart enough to know the
1687
02:40:33,271 --> 02:40:37,810
convention. But of course, you can name them
whatever you want. In this case, I'm going
1688
02:40:37,810 --> 02:40:42,680
to follow the convention. So I'm going to
say certainly, inside the parentheses as the
1689
02:40:42,680 --> 02:40:45,190
input of this method, I'm going to say,
1690
02:40:45,190 --> 02:40:52,520
the string, let's say name. Inside this method,
I'm going to change the value of this name
1691
02:40:52,520 --> 02:40:58,920
to whatever the name that I'm going to receive
via this method. For that I can say this dot
1692
02:40:58,920 --> 02:41:05,180
name is equal to name, the keyword, this is
referring to the current object that we are
1693
02:41:05,180 --> 02:41:10,680
in. For example, right now we are inside our
phone object, our phone class. So by using
1694
02:41:10,680 --> 02:41:17,130
this dot name, we are accessing the property
of this class called name. The second naming
1695
02:41:17,130 --> 02:41:22,630
here is the input of our method. So we are
setting the value of this name to whatever
1696
02:41:22,630 --> 02:41:28,550
we receive, we are the input of this method.
Now because the access modifier of this method
1697
02:41:28,550 --> 02:41:34,450
is public, we can use it inside other classes.
For example, from inside the main class, if
1698
02:41:34,450 --> 02:41:38,970
we want to set the name of our iPhone, we
can say something like this before that, let's
1699
02:41:38,970 --> 02:41:45,561
delete this line of code. We can say iPhone
dot, let's say set name. Inside the parentheses
1700
02:41:45,561 --> 02:41:51,681
of this method, I need to pass the name of
my phone, let's say iPhone 11. So this way,
1701
02:41:51,681 --> 02:41:58,230
I don't have access to the field directly.
But by using the method, I can change the
1702
02:41:58,230 --> 02:42:03,450
value of that field. But what if we want to
get the value of this field? Well, we can
1703
02:42:03,450 --> 02:42:09,091
write another method. And here is how we can
create that method. We can say public as the
1704
02:42:09,091 --> 02:42:14,640
return type of this method, I'm not going
to say void because I'm going to return this
1705
02:42:14,640 --> 02:42:19,971
name. So I'm going to say a string. Once again,
there is a convention in here, we can say
1706
02:42:19,971 --> 02:42:26,090
get name, we don't need anything as the input.
Inside this method, we are going to return
1707
02:42:26,090 --> 02:42:32,510
something. And here is how you can return
something from a method you type return. After
1708
02:42:32,510 --> 02:42:36,910
that, whatever you are going to return. In
this case, I am going to return this name.
1709
02:42:36,910 --> 02:42:43,221
So I'm going to say this dot name. Once again,
this refers to the current object, this dot
1710
02:42:43,221 --> 02:42:49,090
names means the name inside our phone object.
Now that we have created this method, inside
1711
02:42:49,090 --> 02:42:54,360
our main class, instead of saying iPhone dot
name, I can say something like this, I can
1712
02:42:54,360 --> 02:43:01,680
say, iPhone dot get name. If we run our application,
once again, we should see the name of our
1713
02:43:01,680 --> 02:43:07,880
form. Let's run the application. You can see
that iPhone 11 has been printed. Beside creating
1714
02:43:07,880 --> 02:43:13,390
these kinds of methods ourselves, we have
another option. If we come down in here, we
1715
02:43:13,390 --> 02:43:19,510
can use the help of our ID, we can right click
in here, we can go to this generate method.
1716
02:43:19,510 --> 02:43:24,229
Similarly, we can press ALT plus insert, you
can see that in here we have a list of options.
1717
02:43:24,229 --> 02:43:30,260
We have getter, we have surfer, and also we
have getter and setter, if we want to create
1718
02:43:30,260 --> 02:43:35,460
getters and setters, we can select that. After
that we can create the field that we want
1719
02:43:35,460 --> 02:43:41,310
to create getters and setters for for example,
if we select a camera, and press OK, you can
1720
02:43:41,310 --> 02:43:46,830
see that Id has created some codes for us,
you can see that these methods are almost
1721
02:43:46,830 --> 02:43:52,930
identical to the methods that we created above
in here. Because creating getters and setter
1722
02:43:52,930 --> 02:43:58,590
methods are very usual, Id can help us with
the creating of them. But why are we using
1723
02:43:58,590 --> 02:44:04,480
getters and setters? Well, there are a few
reasons for that. The first reason is encapsulation.
1724
02:44:04,480 --> 02:44:09,500
Let's quickly talk about that a bit. For example,
if you don't want to give others the permission
1725
02:44:09,500 --> 02:44:15,200
to change the value of our fields, we can
delete the setters. In that case, we only
1726
02:44:15,200 --> 02:44:21,240
give them the permission to get the value
of objects but not set the value of objects
1727
02:44:21,240 --> 02:44:27,080
or vice versa, we can create setter methods
for our class. In that case, we can set the
1728
02:44:27,080 --> 02:44:33,021
value of our properties from other classes,
but we cannot get the value of this properties.
1729
02:44:33,021 --> 02:44:38,780
The first reason is encapsulation. The other
reason is that sometimes in your setter and
1730
02:44:38,780 --> 02:44:44,340
getter methods, you may want to do some other
operations as well. For example, if you're
1731
02:44:44,340 --> 02:44:49,630
setting a name for your phone, you may want
to validate that name before setting it. In
1732
02:44:49,630 --> 02:44:55,330
that case, you can have some validation before
assigning the value to your property. Okay,
1733
02:44:55,330 --> 02:44:59,690
that's enough talking about getters and setters.
That's greatly changed the access modifiers
1734
02:44:59,690 --> 02:45:05,550
of have these two private, and let's create
getters and setters for the ones that we didn't
1735
02:45:05,550 --> 02:45:11,190
create any. down in here after everything,
I'm going to press ALT plus insert, let's
1736
02:45:11,190 --> 02:45:15,920
say getters and setters. And let's select
both of them, you can see that IntelliJ has
1737
02:45:15,920 --> 02:45:20,840
created these codes for us. But because we
have changed the access modifier of this memory
1738
02:45:20,840 --> 02:45:26,310
ram to private, now we have an error inside
our main class in here, instead of saying
1739
02:45:26,310 --> 02:45:31,060
iPhone dot memory is equal to eight, I can
say something like this, I can say iPhone
1740
02:45:31,060 --> 02:45:37,479
dot cert memory. And I can pass eight. What
we did in here is one way of instantiating
1741
02:45:37,479 --> 02:45:43,601
objects, we have defined our object. After
that we have set different values for our
1742
02:45:43,601 --> 02:45:48,560
object, there is another way of instantiating
your objects, let's quickly see that as well.
1743
02:45:48,560 --> 02:45:54,310
So if I switch back to my phone class, above
the play music method, I can create something
1744
02:45:54,310 --> 02:46:00,101
called constructor. Let's use the help of
ID for creating that constructor. Once again,
1745
02:46:00,101 --> 02:46:05,550
I'm going to go to generate menu. The first
item in here is constructor in here, I'm going
1746
02:46:05,550 --> 02:46:10,140
to select all of the fields. And I'm going
to press OK. And as you can see, this code
1747
02:46:10,140 --> 02:46:15,680
has been generated for us this is called a
constructor in object oriented programming.
1748
02:46:15,680 --> 02:46:22,050
Our constructor needs to have public as its
access modifier, it cannot be private. After
1749
02:46:22,050 --> 02:46:27,920
that the return type of this constructor is
a phone the exact item that we are in our
1750
02:46:27,920 --> 02:46:33,220
constructor does not have any name. And as
the input of this constructor we are receiving
1751
02:46:33,220 --> 02:46:38,970
all of the fields of our class we are receiving
name a screen size, memory and camera inside
1752
02:46:38,970 --> 02:46:44,310
our constructor we are assigning different
values that we have get for your the input
1753
02:46:44,310 --> 02:46:49,300
to the fields of our class. Now that we have
created a constructor if we switch back to
1754
02:46:49,300 --> 02:46:54,430
our main class, we can see that we are getting
a red warning in here. The warning says that
1755
02:46:54,430 --> 02:47:00,160
expected four arguments both found zero, it
means that now that we have created the constructor,
1756
02:47:00,160 --> 02:47:06,090
we need to pass the values directly to the
parentheses of this new form. And here is
1757
02:47:06,090 --> 02:47:09,800
how you can pass them. The first one was the
name of our phone,
1758
02:47:09,800 --> 02:47:16,910
let's say iPhone 11. I don't remember the
order of things. But the next one was a screen
1759
02:47:16,910 --> 02:47:22,250
size, let's say five, I believe the next one
was memory RAM, let's say eight, and the other
1760
02:47:22,250 --> 02:47:27,820
one was camera, let's say eight as well. Now
that we have instantiated our object like
1761
02:47:27,820 --> 02:47:33,720
this, we don't need to directly set fields
of our class, we can safely delete this also,
1762
02:47:33,720 --> 02:47:39,030
let's delete this as well. In this line, we
are printing the name of our iPhone. Let's
1763
02:47:39,030 --> 02:47:43,720
run the application and see if we can print
the name correctly. As you can see, iPhone
1764
02:47:43,720 --> 02:47:48,660
11 has been printed. So when you are using
constructors, you need to pass the values
1765
02:47:48,660 --> 02:47:54,830
of different properties at the time of instantiating
your objects your class can have multiple
1766
02:47:54,830 --> 02:48:00,030
constructors as well. For example, if I come
down in here, and once again, right click
1767
02:48:00,030 --> 02:48:06,380
and go to the Generate menu. If I select konstruktor,
this time, I can select different items. For
1768
02:48:06,380 --> 02:48:11,800
example, in this case, I'm going to select
only name and memory. Now if I press OK, you
1769
02:48:11,800 --> 02:48:17,070
can see that we have another constructor which
will only receive name and memory run. But
1770
02:48:17,070 --> 02:48:21,970
if you hover over this phone, you can see
that this constructor is never used. If we
1771
02:48:21,970 --> 02:48:27,470
switch back to our main class, we can create
another phone and we can use the second constructor.
1772
02:48:27,470 --> 02:48:33,270
Let's quickly do that. I'm going to do that
after these two line of code. Let's say phone.
1773
02:48:33,270 --> 02:48:38,540
And this is the beauty of object oriented
programming. When you create a class, you
1774
02:48:38,540 --> 02:48:43,430
can reuse that class as many times as you
want. For example, in here, I'm going to define
1775
02:48:43,430 --> 02:48:50,511
an entirely new font. Let's name this one
pizza, let's say is equal to new phone. This
1776
02:48:50,511 --> 02:48:55,360
time I'm going to use the second constructor
for the name I'm going to save pixel three,
1777
02:48:55,360 --> 02:49:01,830
and four the memory, I'm going to say 16 you
can see that the ID is happy with this way
1778
02:49:01,830 --> 02:49:08,320
of defining a new phone. Now we have two different
instances of our phone class having two different
1779
02:49:08,320 --> 02:49:14,110
kinds of constructors for a class is called
polymorphism in Java polymorphism has other
1780
02:49:14,110 --> 02:49:18,530
types as well. We will talk about it later
on. Okay, I think that's a good point to stop
1781
02:49:18,530 --> 02:49:23,920
the video in here. In the next video we are
going to continue our talk about object oriented
1782
02:49:23,920 --> 02:49:28,650
programming. There are many more things left
that we need to talk about. See you in the
1783
02:49:28,650 --> 02:49:38,180
next video. In the previous video, we have
talked briefly about classes what those are,
1784
02:49:38,180 --> 02:49:44,220
how can we create them and also what are the
most usages of a class in this video we are
1785
02:49:44,220 --> 02:49:50,130
going to talk about some important concepts
regarding to object oriented programming.
1786
02:49:50,130 --> 02:49:55,721
The first of those concepts is inheritance.
I'm going to talk about inheritance with an
1787
02:49:55,721 --> 02:50:01,460
example because I think that would make everything
easier to understand First of all, I'm going
1788
02:50:01,460 --> 02:50:07,760
to create a class called doc. For this doc
class, I'm going to define some properties.
1789
02:50:07,760 --> 02:50:14,640
For example, I'm going to define a name, let's
say private string name. After that, I'm going
1790
02:50:14,640 --> 02:50:22,820
to define a color, let's say private string
color. Let's also define the number of legs,
1791
02:50:22,820 --> 02:50:29,729
let's say private int, legs. And also I'm
going to define a Boolean indicating that
1792
02:50:29,729 --> 02:50:38,021
if a dog has a tail or not, so let's say private
Boolean, let's name it as tail. Let's also
1793
02:50:38,021 --> 02:50:43,551
create a constructor for our class by pressing
ALT plus insert, you can see the dialog in
1794
02:50:43,551 --> 02:50:49,690
here. Let's select all of the fields. Let's
also create some getters and setters for our
1795
02:50:49,690 --> 02:50:55,070
class as well, once again, by pressing ALT
plus insert. By selecting this getter and
1796
02:50:55,070 --> 02:51:00,630
setter, we can create getters and setters.
Let's also define a method for our class as
1797
02:51:00,630 --> 02:51:07,050
well. I'm going to say public void, let's
name this method eat, I'm going to indicate
1798
02:51:07,050 --> 02:51:13,480
the eating behavior of a dog. As the input
of this method, I'm going to receive a food.
1799
02:51:13,480 --> 02:51:19,670
So let's say let's drink food. And inside
this method, I'm just going to print something,
1800
02:51:19,670 --> 02:51:26,690
let's say eating class our food. Okay, nothing
special is in this class. First of all, we
1801
02:51:26,690 --> 02:51:32,010
have created these four fields. After that
we have created the constructor, we have created
1802
02:51:32,010 --> 02:51:37,690
our getters and setters. And we have created
this eat method, or we have talked about why
1803
02:51:37,690 --> 02:51:44,050
I'm doing all of this, let's also create another
class called bird. Let's say bird. For this
1804
02:51:44,050 --> 02:51:51,510
bird class, let's also have a name. Also,
let's have a color. Let's define two more
1805
02:51:51,510 --> 02:51:58,160
fields, one for the number of legs, private
int legs, and also another one indicating
1806
02:51:58,160 --> 02:52:04,040
that if our bird has a tail or not, so let's
say private Boolean,
1807
02:52:04,040 --> 02:52:11,080
let's say has, for this bird class, let's
also create a constructor, and some getters
1808
02:52:11,080 --> 02:52:17,680
and setters. I'm going to create my eath method
for this bird class as well, let's say public
1809
02:52:17,680 --> 02:52:18,700
void,
1810
02:52:18,700 --> 02:52:19,730
eat.
1811
02:52:19,730 --> 02:52:26,750
Let's get the name of our food. And inside
this method, let's create something. Let's
1812
02:52:26,750 --> 02:52:32,960
say eating that food. You probably can see
the pattern in here, there are a lot of similarities
1813
02:52:32,960 --> 02:52:39,180
between our bird and dog class. Both of those
classes have four fields, they have a constructor,
1814
02:52:39,180 --> 02:52:44,180
they have all of these getters and setters.
And both of them have this eat method. In
1815
02:52:44,180 --> 02:52:49,430
programming, this is not efficient, you basically
are copying and pasting yourself. Instead
1816
02:52:49,430 --> 02:52:55,560
of defining our bird and dot class like this,
we are going to use another concept in object
1817
02:52:55,560 --> 02:53:00,510
oriented programming called inheritance. For
that, first of all, I'm going to create an
1818
02:53:00,510 --> 02:53:06,471
animal class. And after that, I will extend
the animal class to the spirit and dog. Let's
1819
02:53:06,471 --> 02:53:12,030
see how we can do that inside my package,
I'm going to create my animal class, let's
1820
02:53:12,030 --> 02:53:17,850
say animal. First of all, let's add some fields,
I'm going to use the four previous fields.
1821
02:53:17,850 --> 02:53:24,290
Let's also copy them from here. And after
that, let's create a constructor and getters
1822
02:53:24,290 --> 02:53:30,470
and setters and our eat method. Don't worry,
we will reduce the amount of copying and pasting
1823
02:53:30,470 --> 02:53:39,181
in a minute, let's say public, void eat string
food as the input. And let's print the name
1824
02:53:39,181 --> 02:53:45,660
of our foot. Now that we have created this
animal class, creating bird and dot class
1825
02:53:45,660 --> 02:53:51,271
is going to be much simpler. For that I'm
going to delete all of the quotes inside this
1826
02:53:51,271 --> 02:53:57,130
bird class. Let's delete all of them. This
time, instead of defining all of the fields
1827
02:53:57,130 --> 02:54:03,720
and constructors and methods, I'm going to
say extends animal. This way we are saying
1828
02:54:03,720 --> 02:54:08,880
that our bird is an animal, we are extending
the animal class and later on if we want,
1829
02:54:08,880 --> 02:54:13,550
we can customize our birth class as well.
But right now you can see that we are getting
1830
02:54:13,550 --> 02:54:18,140
a red warning. And the warning in here it
says that there is no default constructor
1831
02:54:18,140 --> 02:54:23,010
available in this package. Basically, it says
that you need the constructor, let's press
1832
02:54:23,010 --> 02:54:28,142
ALT plus insert in here. And let's select
constructor. And as you can see, the ID has
1833
02:54:28,142 --> 02:54:32,990
generated this constructor you can also see
that even though we don't have any fields
1834
02:54:32,990 --> 02:54:38,601
in our birth class, we are receiving some
arguments in the constructor. But this constructor
1835
02:54:38,601 --> 02:54:44,770
has rather a strange syntax. We haven't seen
this super keyword so far. Basically, this
1836
02:54:44,770 --> 02:54:49,400
super keyword means that do whatever you're
doing with these parameters in the parent
1837
02:54:49,400 --> 02:54:55,890
class. In this case, our parent class is animal.
So this super is passing all of these arguments
1838
02:54:55,890 --> 02:55:02,380
to the constructor of our animal class. It
means that It setting all of the values to
1839
02:55:02,380 --> 02:55:07,910
the fields of our animal. Now that I have
created my bird class like this, if I want
1840
02:55:07,910 --> 02:55:12,601
to instantiate it, for example, inside the
main class, I can do as before, let's say
1841
02:55:12,601 --> 02:55:19,730
bird. Let's name it Phoenix, let's say is
equal to new bird. And as you can see, the
1842
02:55:19,730 --> 02:55:25,070
constructor is waiting for our arguments.
First of all, we need a name. That's past
1843
02:55:25,070 --> 02:55:31,570
Nina, we need the color, let's say golden.
After that, we need the number of legs, which
1844
02:55:31,570 --> 02:55:37,600
I'm going to pass to, and a Boolean indicating
if our bird has a tail or not, that's passed
1845
02:55:37,600 --> 02:55:43,020
through. Once again, remember that in our
bird class, we didn't have any extra methods.
1846
02:55:43,020 --> 02:55:48,820
But in my main class, if I want, I can use
some of the getter methods of our parent class,
1847
02:55:48,820 --> 02:55:53,400
in this case, animal. For example, if I want
to get the name of my bird, I can say something
1848
02:55:53,400 --> 02:55:58,911
like this, I can say Phoenix dot get name,
you can see that we have all of the getter
1849
02:55:58,911 --> 02:56:05,570
methods. Let's run the application. And let's
see if we can print the name of our birth,
1850
02:56:05,570 --> 02:56:10,260
you can see that the name has been printed
successfully. Beside getter and setter methods,
1851
02:56:10,260 --> 02:56:14,940
you can also have access to all of the other
methods that we have created. For example,
1852
02:56:14,940 --> 02:56:20,690
if you remember inside the animal class, we
have created this each method, let's see if
1853
02:56:20,690 --> 02:56:24,770
we can use it in our bird instance. For example,
in here, if I say
1854
02:56:24,770 --> 02:56:33,260
Pheonix, that it, I don't know what Phoenix
is, its birth, let's just pass meat. And let's
1855
02:56:33,260 --> 02:56:38,260
run our application. And let's see if we can
successfully execute the eat method. And as
1856
02:56:38,260 --> 02:56:43,460
you can see in here, Nina is eating meat.
Now that you have created your birth class,
1857
02:56:43,460 --> 02:56:48,280
you can also change the dog class as well.
For example, in here, I'm going to delete
1858
02:56:48,280 --> 02:56:54,770
all of these codes once again. Instead, I'm
going to extend the animal plus, once again,
1859
02:56:54,770 --> 02:56:59,920
let's say extends animal, you can see the
red warning. And that's because we need the
1860
02:56:59,920 --> 02:57:04,710
constructor, you can see that the amount of
similar codes that you were copying and pasting
1861
02:57:04,710 --> 02:57:10,610
is a lot less right now, the bird and dog
class are basically the same. But we know
1862
02:57:10,610 --> 02:57:16,260
for sure that a dog and a bird are not the
same, they may have other fields. And also
1863
02:57:16,260 --> 02:57:21,540
other behaviors as well. If you want, you
can customize your child classes as well.
1864
02:57:21,540 --> 02:57:27,470
For example, inside my birth class, I want
to add another field, I'm going to say private,
1865
02:57:27,470 --> 02:57:33,979
let's say ain't veins, we know that dogs do
not have wings. In here. I'm going to delete
1866
02:57:33,979 --> 02:57:38,351
this constructor and I'm going to create it
once again, because I'm going to use the help
1867
02:57:38,351 --> 02:57:44,510
of my ID, let's press ALT plus insert. In
here, I'm going to select Winx. And this time,
1868
02:57:44,510 --> 02:57:49,650
as you can see, the constructor has been changed
a little bit, the super statement is as the
1869
02:57:49,650 --> 02:57:54,960
same it means that do whatever you're doing
with all of these four fields. But because
1870
02:57:54,960 --> 02:58:00,550
inside our parent class inside the animal
class, we do not have a wings field, these
1871
02:58:00,550 --> 02:58:06,040
wings hasn't been passed to this super statement.
So we are setting the value of wings directly
1872
02:58:06,040 --> 02:58:11,340
inside the constructor of this bird class.
This way we can customize our child classes.
1873
02:58:11,340 --> 02:58:16,950
Right now the duck does not have any wings
field, but our bird does what's right now
1874
02:58:16,950 --> 02:58:21,490
if I switch back to my main class, I should
get a warning. And the warning in here says
1875
02:58:21,490 --> 02:58:26,920
that you need to pass a wink that's also fascinating
here as well. For example, I'm going to say
1876
02:58:26,920 --> 02:58:32,530
to we can also create getter and setter methods
for these wings as well. For example inside
1877
02:58:32,530 --> 02:58:38,561
our bird class down in here, let's press up
plus insert and add select getter and setters.
1878
02:58:38,561 --> 02:58:44,290
Now that we have created the getters and setters,
let's see if we can print the number of wings.
1879
02:58:44,290 --> 02:58:51,080
For example, down in here, I'm going to say
Phoenix dot get wings. Let's run the application.
1880
02:58:51,080 --> 02:58:56,970
And you can see that two has been printed
as the number of means beside getter and setter
1881
02:58:56,970 --> 02:59:01,890
methods. You can also have other methods inside
your child class as well. For example, inside
1882
02:59:01,890 --> 02:59:07,600
the spirit class, if I want, I can define
a fly method as well. Let's quickly see that
1883
02:59:07,600 --> 02:59:08,820
let's say public void
1884
02:59:08,820 --> 02:59:11,101
fly.
1885
02:59:11,101 --> 02:59:16,570
I'm not going to receive anything as the input
let's just print something. Let's say this
1886
02:59:16,570 --> 02:59:22,890
dot get name. Also if you remember from the
previous video, I said that this keyword refers
1887
02:59:22,890 --> 02:59:27,570
to the current object that you are in. In
this case inside the bird class, we don't
1888
02:59:27,570 --> 02:59:33,260
have any getname method. But because we are
extending the animal class, we can use the
1889
02:59:33,260 --> 02:59:39,710
start getting, let's say is flying. So besides
using fields, you can also customize your
1890
02:59:39,710 --> 02:59:45,040
child classes with different methods. Let's
see if we can call this method from inside
1891
02:59:45,040 --> 02:59:51,510
our main class, let's say Phoenix dot fly.
As you can see, Nina is flying. Also beside
1892
02:59:51,510 --> 02:59:57,110
the finding new methods inside your child
class. You can also change the behavior of
1893
02:59:57,110 --> 03:00:01,490
methods of your parent class. For example,
if you were Remember, inside the animal class,
1894
03:00:01,490 --> 03:00:06,330
we had this eat method, I'm talking about
this one, which inside this method we are
1895
03:00:06,330 --> 03:00:11,450
saying eating food inside our bird class.
If we want, we can change that. For example
1896
03:00:11,450 --> 03:00:16,960
right in here, I'm going to press Ctrl plus
Oh, you can see that we are seeing a list
1897
03:00:16,960 --> 03:00:22,690
of different methods, I'm going to override
this eat method. So by overriding, you're
1898
03:00:22,690 --> 03:00:28,490
going to say that I'm going to change the
behavior of this eat method from what is happening
1899
03:00:28,490 --> 03:00:33,479
inside the parent class. Once again, we are
seeing this super keyword, it means that do
1900
03:00:33,479 --> 03:00:37,880
whatever you're doing with this parameter
inside the parent class. If you want, you
1901
03:00:37,880 --> 03:00:42,870
can delete this super statement completely,
but let it be for now we will delete that
1902
03:00:42,870 --> 03:00:48,281
later on. After this super keyword. I'm going
to print another statement in here. Let's
1903
03:00:48,281 --> 03:00:53,760
just say eating finished. So this way, we
have changed the eat method
1904
03:00:53,760 --> 03:00:59,290
inside our child class from what it was inside
the animal class. Right now inside our main
1905
03:00:59,290 --> 03:01:04,160
class, we have executed this line of code
for Unix that eat let's comment the lines
1906
03:01:04,160 --> 03:01:09,660
after that, so that we can see what's going
to be the behavior of this eat method. Let's
1907
03:01:09,660 --> 03:01:14,570
run the application, you can see that we are
saying eating meats. And after that eating
1908
03:01:14,570 --> 03:01:19,200
finished, if you want to completely change
the behavior of your method inside the child
1909
03:01:19,200 --> 03:01:24,460
class, you can simply delete this super that
eat method. For example, in here, I'm going
1910
03:01:24,460 --> 03:01:31,170
to say chewing food instead of eating fruit.
So in both child and parent class, we have
1911
03:01:31,170 --> 03:01:36,000
this eighth method, but their behavior is
different. Let's run the application and see
1912
03:01:36,000 --> 03:01:41,130
if we can see the difference, we can see that
we are chewing meat. So what we did so far
1913
03:01:41,130 --> 03:01:47,520
is called inheritance in object oriented programming.
We have inherited the birth class from the
1914
03:01:47,520 --> 03:01:53,400
animal class. You can also continue this inheritance
multiple times. For example, if you want,
1915
03:01:53,400 --> 03:01:59,300
you can inherit the dog object. Let's quickly
create another class in here. For example,
1916
03:01:59,300 --> 03:02:06,240
let's say Shephard. Now in here, I can say
extends the dog class, you can see that once
1917
03:02:06,240 --> 03:02:11,550
again, we are getting this red warning. Let's
create our constructor in order to overcome
1918
03:02:11,550 --> 03:02:16,771
that problem, we can see that even though
inside the doc field, we do not have any field.
1919
03:02:16,771 --> 03:02:22,590
But inside our Shephard class in the constructor,
we need all of those arguments, because the
1920
03:02:22,590 --> 03:02:27,690
parent of this Shephard class, which is dog
is the child of another class, which is animal.
1921
03:02:27,690 --> 03:02:33,300
So you can do this inheritance as many times
as you want, you can also create another class
1922
03:02:33,300 --> 03:02:38,340
and extend the shefford class. But I don't
think that mean anything in here, I just wanted
1923
03:02:38,340 --> 03:02:43,479
to say that you have that option. Okay, I
think that's enough talking about inheritance.
1924
03:02:43,479 --> 03:02:48,640
The other concept that I'm going to talk about
is called polymorphism. We have talked a bit
1925
03:02:48,640 --> 03:02:53,690
about polymorphism in the previous video,
but let's talk more about it in here. So in
1926
03:02:53,690 --> 03:03:00,190
previous video, I said that one kind of polymorphism
is that for a class to have multiple constructors,
1927
03:03:00,190 --> 03:03:06,180
for example, inside our animal class, if we
had another constructor in here, which will
1928
03:03:06,180 --> 03:03:12,181
accept different number of fields. For example,
the first three now that we have two constructor,
1929
03:03:12,181 --> 03:03:17,229
this is one kind of polymorphism. Now we can
create this animal class in two different
1930
03:03:17,229 --> 03:03:23,360
ways. Basically, polymorphism means having
multiple forms. But there are two more kinds
1931
03:03:23,360 --> 03:03:28,470
of polymorphism. But before we talk about
that, let's delete this constructor, I'm not
1932
03:03:28,470 --> 03:03:33,540
sure that if you're going to need that the
other kind of polymorphism that we have used
1933
03:03:33,540 --> 03:03:39,100
so far is about this eat method. So inside
this animal class, we had this eight method,
1934
03:03:39,100 --> 03:03:43,960
but also we had it inside the child class,
which is birth. But these two eight methods
1935
03:03:43,960 --> 03:03:48,460
have different forms, it means that we are
doing different things inside each one of
1936
03:03:48,460 --> 03:03:54,520
these methods. This is another kind of polymorphism
two methods inside a parent and child class
1937
03:03:54,520 --> 03:04:00,570
that are doing the same job differently. We
also have another kind of polymorphism. And
1938
03:04:00,570 --> 03:04:06,020
that's with the methods themselves. We don't
have this following method inside our animal
1939
03:04:06,020 --> 03:04:12,360
class. But in here, we can also have another
method called fly as well, let's quickly see
1940
03:04:12,360 --> 03:04:16,970
how we can create another method called flight.
I'm going to say public void
1941
03:04:16,970 --> 03:04:19,240
fly. But this
1942
03:04:19,240 --> 03:04:24,850
time inside the parentheses, I'm going to
receive some arguments for example, I'm going
1943
03:04:24,850 --> 03:04:30,910
to receive a speed as you can see, the compiler
is happy with this way of defining two different
1944
03:04:30,910 --> 03:04:36,160
methods with the same name. Now, if I switch
back to my main class, you can see that we
1945
03:04:36,160 --> 03:04:40,690
are not getting an error but before that,
I think I should uncomment this line of code.
1946
03:04:40,690 --> 03:04:45,670
Even though we are not getting error warning
this in here it means that we are using the
1947
03:04:45,670 --> 03:04:50,770
first flow method Let me close all of the
unnecessary classes. Inside the bird class
1948
03:04:50,770 --> 03:04:55,770
we are using the first method, but if we want
to use the second method, we just need to
1949
03:04:55,770 --> 03:05:00,860
pass some arguments in here. For example,
let's pass 100 we can have methods with the
1950
03:05:00,860 --> 03:05:06,229
same name when we have different number of
arguments, or alternatively, when we have
1951
03:05:06,229 --> 03:05:11,920
different kinds of arguments, for example,
in this case, we are passing an integer. But
1952
03:05:11,920 --> 03:05:17,810
if we had another method, for example, in
which we pass a string, that method can have
1953
03:05:17,810 --> 03:05:24,500
the fly name as well. Let's see that briefly,
let's say public void fly. And inside the
1954
03:05:24,500 --> 03:05:29,860
parentheses, I don't know, let's pass a string.
Let's just say name, you can see that the
1955
03:05:29,860 --> 03:05:35,440
compiler is happy with this way of defining
a method with the same name. So besides the
1956
03:05:35,440 --> 03:05:41,190
difference in the number of parameters, the
kind of parameter is also important. This
1957
03:05:41,190 --> 03:05:46,900
in here is also called polymorphism. using
methods with the same name in different ways,
1958
03:05:46,900 --> 03:05:51,110
okay, let's delete these two, I don't think
we are going to need them. And I think it's
1959
03:05:51,110 --> 03:05:55,370
a good point to stop the video in here. In
the next video, first thing we are going to
1960
03:05:55,370 --> 03:06:00,170
start talking about composition in object
oriented programming. See you in the next
1961
03:06:00,170 --> 03:06:09,530
video. In the previous video, we have talked
about inheritance and polymorphism. In this
1962
03:06:09,530 --> 03:06:14,860
video, we are going to talk about composition
in object oriented programming. In previous
1963
03:06:14,860 --> 03:06:20,260
videos, I said that classes are useful for
when you want to define your customized data
1964
03:06:20,260 --> 03:06:25,780
type. For example, imagine that you are going
to create a car in that car you have multiple
1965
03:06:25,780 --> 03:06:31,180
systems, for example, you have a system for
a stereotype, your car also may have some
1966
03:06:31,180 --> 03:06:37,181
engine, it can also have a fuel system. If
you want to simulate a car. In programming,
1967
03:06:37,181 --> 03:06:42,850
you can create different classes, for example,
for engine for a stereo system, and also for
1968
03:06:42,850 --> 03:06:48,351
field system. And after that inside your car
class, you can compose all of those classes
1969
03:06:48,351 --> 03:06:54,490
to have a car, let's see how we can use composition
in Java. In my package, I'm going to create
1970
03:06:54,490 --> 03:07:00,511
a class called engine. For this engine, I'm
going to have two fields. First of all, I'm
1971
03:07:00,511 --> 03:07:05,940
going to define a model. So let's say private
string model. And after that, let's have an
1972
03:07:05,940 --> 03:07:13,171
integer, private int. I'm going to name this
integer, rpm, rpm stands for round per minute,
1973
03:07:13,171 --> 03:07:17,790
it's just a property of different engines.
I'm going to keep it really simple. So I think
1974
03:07:17,790 --> 03:07:22,630
these two would be enough. After that, let's
quickly create our constructor and getters
1975
03:07:22,630 --> 03:07:28,150
and setters. Let's create a car class. And
let's see how we can use this engine inside
1976
03:07:28,150 --> 03:07:34,330
that, let's say new Java class. Let's name
it car. For this car class, I'm going to define
1977
03:07:34,330 --> 03:07:37,540
four fields. First of all, let's pass a name,
1978
03:07:37,540 --> 03:07:44,370
private string name. After that, let's have
an integer for the number of doors. After
1979
03:07:44,370 --> 03:07:51,150
that, let's define the color of our car private
string color. And after that, I'm going to
1980
03:07:51,150 --> 03:07:56,860
include or compose an engine in here. For
that I can simply say private engine with
1981
03:07:56,860 --> 03:08:02,430
capital E. And in here, you can see that in
my package, I have an engine class, I can
1982
03:08:02,430 --> 03:08:07,690
import that into this class. Let's name it
engine. For the sake of simplicity, I have
1983
03:08:07,690 --> 03:08:12,820
composed only one class inside this class.
But if you want, you can create another class
1984
03:08:12,820 --> 03:08:18,660
for a stereo system and your system and everything
else that you want. But I think you will get
1985
03:08:18,660 --> 03:08:25,210
my point. If I use only one class after death
like other classes, I can create my constructor,
1986
03:08:25,210 --> 03:08:30,520
you can see that this engine has been passed
to our constructor the same way that we have
1987
03:08:30,520 --> 03:08:35,800
passed other data types like integers and
strings, we can also have getters and setters
1988
03:08:35,800 --> 03:08:41,770
the same way that we had for previous classes.
Now that we have created this car, we can
1989
03:08:41,770 --> 03:08:46,820
instantiate it. For example, inside our main
class. For example, if I want to have a car
1990
03:08:46,820 --> 03:08:53,960
object in here, I can say car, let's name
it Mercedes, is equal to new car. As you can
1991
03:08:53,960 --> 03:08:59,979
see, the constructor in here requires four
fields and name doors color, and also an engine
1992
03:08:59,979 --> 03:09:06,870
for the name. I'm going to pass Mercedes AMG
for the number of doors that's passed to for
1993
03:09:06,870 --> 03:09:12,070
the color, let's say silver. What What should
we pass as the engine here? Well, we can pass
1994
03:09:12,070 --> 03:09:17,670
our engine in two ways. First of all, we can
create our engine before this court class.
1995
03:09:17,670 --> 03:09:23,771
For example, in here I can say engine, let's
name it engine is equal to new engine. And
1996
03:09:23,771 --> 03:09:28,790
I can pass a model and RPM for this engine
to instantiated for example, I'm going to
1997
03:09:28,790 --> 03:09:37,310
say three note for the RPM, let's say 8000.
After creating our engine objects, we can
1998
03:09:37,310 --> 03:09:42,340
pass it to the constructor of our car for
example, in here I can say engine and like
1999
03:09:42,340 --> 03:09:48,970
that we can create our car but the other way
is to pass your engine directly to the constructor
2000
03:09:48,970 --> 03:09:54,300
of your car. For example, I can see a new
engine in here. And I can't instantiate my
2001
03:09:54,300 --> 03:10:02,110
engine objects directly inside the constructor.
Let's say Renault Let's fast 1000. Now that
2002
03:10:02,110 --> 03:10:06,900
we have passed it directly, we don't need
the first line. Of course, we know that Mercedes,
2003
03:10:06,900 --> 03:10:12,729
we don't use Renault engine, but I think it
doesn't matter in here. Now that we have instantiated
2004
03:10:12,729 --> 03:10:18,301
our car objects, we can have access to all
of the fields like we did before. For example,
2005
03:10:18,301 --> 03:10:22,920
if you want to print the name of our car,
we can say something like this, we can say
2006
03:10:22,920 --> 03:10:30,300
Mercedes dot name, or that gets named to riversides.
But what if we want to get the model of the
2007
03:10:30,300 --> 03:10:36,030
engine of this car? Well, for that, we can
say something like this, let's print engine
2008
03:10:36,030 --> 03:10:40,660
model. Plus, we can say Mercedes dot
2009
03:10:40,660 --> 03:10:46,580
get engine, this method in here. After that,
we can once again say dot get model. So this
2010
03:10:46,580 --> 03:10:52,790
get engine method will return an engine object
in which we can perform another dot operator
2011
03:10:52,790 --> 03:10:57,540
on that, let's run the application and see
what would be the result. First of all, we
2012
03:10:57,540 --> 03:11:03,011
can see the name of our car, and after that,
you can see the engine model, which is renamed.
2013
03:11:03,011 --> 03:11:08,550
Also instead of using the methods of your
engine, you can get the engine object directly.
2014
03:11:08,550 --> 03:11:15,050
For example, in here before the printer statement,
I'm going to say engine, let's name it engine
2015
03:11:15,050 --> 03:11:21,660
is equal to let's say Mercedes dot get engine.
You can see the return type in here. But whenever
2016
03:11:21,660 --> 03:11:27,110
you are not sure about the return type, you
can press down the control key. And by hover
2017
03:11:27,110 --> 03:11:31,610
over in your method, you can see the return
type, you can see the declaration of your
2018
03:11:31,610 --> 03:11:36,630
method which says public engine get engine,
it means that this method is going to return
2019
03:11:36,630 --> 03:11:42,920
an engine object. Now that you have your instance
of engine, you can perform all kinds of operations
2020
03:11:42,920 --> 03:11:48,391
on that, instead of pressing down the control
key and hover over your method, there is another
2021
03:11:48,391 --> 03:11:53,720
option to get the return type and for that
matter the declaration of your method. And
2022
03:11:53,720 --> 03:11:57,910
here is how you can do that, you can click
on the name of your method, and you can press
2023
03:11:57,910 --> 03:12:03,110
down the control key. This way, you can see
some information about the declaration of
2024
03:12:03,110 --> 03:12:08,330
your method. These kinds of documentation
are especially useful for when you are using
2025
03:12:08,330 --> 03:12:13,980
a third party library or some built in methods.
For example, if we click on this print line
2026
03:12:13,980 --> 03:12:20,150
method and press Ctrl Q, you can see that
the documentation is giving us much more information.
2027
03:12:20,150 --> 03:12:25,160
For example, in this case, it says that this
method is printing a string and then terminate
2028
03:12:25,160 --> 03:12:30,350
the line, you can see some information about
the method itself. Also, if you want, you
2029
03:12:30,350 --> 03:12:35,660
can have this kind of information on the methods
that you yourself create. For example, on
2030
03:12:35,660 --> 03:12:41,811
this get engine method. If we switch to our
car class, we can create some sort of documentation.
2031
03:12:41,811 --> 03:12:46,800
Let's find that method. And here is how you
can do that you need to add a comment in here.
2032
03:12:46,800 --> 03:12:51,770
In previous videos, we have seen how to create
a new comment by using two slashes we could
2033
03:12:51,770 --> 03:12:57,230
have create our comment. But these kinds of
comments are not useful in here. Instead,
2034
03:12:57,230 --> 03:13:03,320
we can use a single slash. And after that,
we can have to start by pressing and enter.
2035
03:13:03,320 --> 03:13:08,550
You can see that we have these sorts of comments
as well. These kinds of comments are useful
2036
03:13:08,550 --> 03:13:14,410
for when you have multiple lines. And in here
you can have some sort of information in which
2037
03:13:14,410 --> 03:13:20,240
later on will be shown when you press Ctrl
q on the name of your method. For example,
2038
03:13:20,240 --> 03:13:27,021
in here, let's say returns the engine of our
car. Now that we have provided this information
2039
03:13:27,021 --> 03:13:32,640
in here, if we switch back to our main class
on the declaration of our method on the main,
2040
03:13:32,640 --> 03:13:38,380
if we press Ctrl plus Q, you can see that
in here we can see that information. Sometimes
2041
03:13:38,380 --> 03:13:42,980
these kinds of commenting can be useful as
well. But there are more to these kinds of
2042
03:13:42,980 --> 03:13:48,110
comments. For example, we can provide some
links, but more on that later on in future
2043
03:13:48,110 --> 03:13:54,850
videos. So this way of using different classes
inside other classes is called composition
2044
03:13:54,850 --> 03:13:59,820
in object oriented programming. Once again,
if you have created some other classes, for
2045
03:13:59,820 --> 03:14:05,320
example, a stereo system class and also a
fuel system class, you could have add them
2046
03:14:05,320 --> 03:14:09,971
one by one in here as well. But for the sake
of simplicity, I've just included this engine,
2047
03:14:09,971 --> 03:14:15,400
this way we can compose different objects
inside one class. Okay, I think that's enough
2048
03:14:15,400 --> 03:14:20,860
talking about composition. Let's talk about
a keyword in Java called nom. Before that,
2049
03:14:20,860 --> 03:14:26,640
let's close this car class. And let's comment
all of these lines of codes. There are some
2050
03:14:26,640 --> 03:14:32,521
times that you don't want to instantiate your
objects immediately. For example, down in
2051
03:14:32,521 --> 03:14:40,220
here I can say car Mercedes is equal to I
don't want to instantiate my car object in
2052
03:14:40,220 --> 03:14:45,640
here like we did before. Instead, I want to
post one that too few lines after this for
2053
03:14:45,640 --> 03:14:52,280
that you can pass null values for your objects.
Let's see now in here as well. This null keyword
2054
03:14:52,280 --> 03:14:58,590
in Java means nothing. It means that this
car Mercedes is nothing when the value of
2055
03:14:58,590 --> 03:15:04,610
an object is now You cannot perform any kind
of operation on that. For example, in here,
2056
03:15:04,610 --> 03:15:10,960
I cannot say Mercedes dot get name, for example,
we are not getting a compile time error, it
2057
03:15:10,960 --> 03:15:16,141
means that our application is going to be
created. But if you take a look at the highlighting
2058
03:15:16,141 --> 03:15:21,860
here, it says that this method is going to
produce no pointer exception. If you remember
2059
03:15:21,860 --> 03:15:27,220
from previous videos, I said that whenever
nullpointerexception happens, your application
2060
03:15:27,220 --> 03:15:32,740
is going to crash. So the error in here is
going to be a runtime error and not a compile
2061
03:15:32,740 --> 03:15:37,530
time error. Let's run our application. And
let's see what would happen. You can see that
2062
03:15:37,530 --> 03:15:42,900
in here we are getting a red warning in our
console says Java dot Lang dot nullpointerexception.
2063
03:15:42,900 --> 03:15:48,850
In general, we should always avoid exceptions
in our application. Later on, we will talk
2064
03:15:48,850 --> 03:15:54,330
about how can we do that, but for now, let's
just assume that we know whenever an object
2065
03:15:54,330 --> 03:15:59,930
is now we cannot perform any operation on
that, for example, we cannot use the methods
2066
03:15:59,930 --> 03:16:05,511
inside that object. So whenever you're not
sure about null values, for example, whenever
2067
03:16:05,511 --> 03:16:10,950
you are retrieving some data from a web server,
or from your local database, first of all,
2068
03:16:10,950 --> 03:16:16,261
you need to check that if your object is null
or not. And here is how you can do that. For
2069
03:16:16,261 --> 03:16:22,200
example, you can create an if statement before
this Mercedes dot get name, I can say if let's
2070
03:16:22,200 --> 03:16:23,771
say Mercedes,
2071
03:16:23,771 --> 03:16:30,590
is not equal to Now, I'm going to put an if
statement in here in case our object is not
2072
03:16:30,590 --> 03:16:36,870
now we are going to call this method but in
the else case, I'm going to print something.
2073
03:16:36,870 --> 03:16:42,110
Let's say the car was now let's run the application
once again, this time, you can see that we
2074
03:16:42,110 --> 03:16:47,200
are not seeing the exception, which is good.
But right now, we are not doing anything helpful.
2075
03:16:47,200 --> 03:16:53,370
With our value being now we are just printing
something, sometimes this is the desired behavior.
2076
03:16:53,370 --> 03:16:58,910
But sometimes you may want to check that what
was the cause of your objects to be no, so
2077
03:16:58,910 --> 03:17:05,400
not means nothing in Java? Okay, let's comment
all of these. And let's talk about another
2078
03:17:05,400 --> 03:17:11,960
keyword in Java called final. So up to this
point when we want to instantiate our object
2079
03:17:11,960 --> 03:17:17,020
or variable, because I've said something like
this, because I said int a is equal to five.
2080
03:17:17,020 --> 03:17:22,510
For example, later on, we could have changed
this a for example, because I have said, A
2081
03:17:22,510 --> 03:17:29,060
is equal to five plus five for a plus fun.
This way, we could have changed the value
2082
03:17:29,060 --> 03:17:34,170
of our variable. But there are some times
that you need to be sure that the value of
2083
03:17:34,170 --> 03:17:39,260
your variable wouldn't change. In those cases,
you can use the final keyword. And here is
2084
03:17:39,260 --> 03:17:44,590
how you can do that before the type of your
variable you can say final final int a is
2085
03:17:44,590 --> 03:17:50,070
equal to five. Now if I try to change the
value of my variable, for example, like before,
2086
03:17:50,070 --> 03:17:55,380
if I say a is equal to a plus one, you can
see that we are getting a warning in here
2087
03:17:55,380 --> 03:18:01,370
the warning says that cannot assign a value
to a final variable. So whenever you are declaring
2088
03:18:01,370 --> 03:18:07,670
your variables and classes as final, you cannot
change the value of that variable or class.
2089
03:18:07,670 --> 03:18:12,760
Let's also check the case when we define our
classes as final, I'm going to comment these
2090
03:18:12,760 --> 03:18:18,840
two line of code. And then in here, I'm going
to say final, let's say engine, let's name
2091
03:18:18,840 --> 03:18:27,189
it engine is equal to new engine. Let's pass
a model and rpm. Now if I try to change the
2092
03:18:27,189 --> 03:18:33,941
value of this engine, for example, if I say
engine is equal to new engine, with other
2093
03:18:33,941 --> 03:18:40,560
parameters, for example, let's change the
RPM, let's say 7000. Once again, you can see
2094
03:18:40,560 --> 03:18:46,070
the error, the error says that cannot assign
a value to final variable. But there is a
2095
03:18:46,070 --> 03:18:52,040
point in here. When you declare your objects
as final, you cannot change the whole instance
2096
03:18:52,040 --> 03:18:58,280
of that object. But you can change the properties
of your object. For example, in here, if you
2097
03:18:58,280 --> 03:19:02,890
remember, inside our engine class, we had
two setter methods which with the help of
2098
03:19:02,890 --> 03:19:07,790
them, we could have changed the value of two
properties. For example, if we wanted to change
2099
03:19:07,790 --> 03:19:15,110
the RPM, because I said something like engine
dot set RPM to let's say 10,000. You can see
2100
03:19:15,110 --> 03:19:21,260
that even though we declared our engine as
final, we can change its properties, we just
2101
03:19:21,260 --> 03:19:27,070
cannot change the whole instance we can change
its properties using Final keywords can be
2102
03:19:27,070 --> 03:19:31,450
useful from time to time. Okay, I think that's
enough for this video. In the next video,
2103
03:19:31,450 --> 03:19:36,540
we are going to have a quick challenge to
make sure that we have understand the concepts
2104
03:19:36,540 --> 03:19:46,881
of object oriented programming. See you in
the next video. This is our challenge. Imagine
2105
03:19:46,881 --> 03:19:51,930
that you are a doctor and you want to check
on your patient. In this application. First
2106
03:19:51,930 --> 03:19:57,620
of all, you're showing the name and age of
the user to the doctor. And after that you
2107
03:19:57,620 --> 03:20:02,850
can see a list of different organs in which
A doctor can select the first organ, his left
2108
03:20:02,850 --> 03:20:08,750
eye. Let's select that. In here, you can see
that we are seeing some details about the
2109
03:20:08,750 --> 03:20:13,890
organ, for example, the name, the medical
condition, and also the color of the oil.
2110
03:20:13,890 --> 03:20:19,430
After that, we gave the doctor the option
to close the oil. And also if the eye is closed,
2111
03:20:19,430 --> 03:20:24,350
we are going to open that let's select one
in here to close the eyes, you can see that
2112
03:20:24,350 --> 03:20:29,830
left eye closed. Once again, we are seeing
the least of our organs. Let's select two
2113
03:20:29,830 --> 03:20:36,390
in this case. This time, if I don't want to
close or open the eye, I can put any number
2114
03:20:36,390 --> 03:20:41,530
beside one for example, if I put two in here,
you can see that once again, we are seeing
2115
03:20:41,530 --> 03:20:47,770
the list of different organs. Let's select
three to see the heart. Once again, you can
2116
03:20:47,770 --> 03:20:52,090
see that we are seeing some details about
the heart. After that we have the ability
2117
03:20:52,090 --> 03:20:57,979
to change the heart rate. In a normal doctor
patient situation, this wouldn't be the available
2118
03:20:57,979 --> 03:21:05,380
option. But in here, we just want to demonstrate
some behavior for our heart organ. So if we
2119
03:21:05,380 --> 03:21:13,150
select Vani, here, the scanner is asking for
our new heart rate, which I can put 75. And
2120
03:21:13,150 --> 03:21:15,970
you can see that heart rate change to 75.
2121
03:21:15,970 --> 03:21:22,729
Let's select a stomach. Once again, you can
see the details of the stomach, we have one
2122
03:21:22,729 --> 03:21:28,980
behavior in here, which is digest. If we select
that, we can see that digesting has begun.
2123
03:21:28,980 --> 03:21:34,720
Once again, this is a hypothetical situation.
And in a normal situation, you cannot order
2124
03:21:34,720 --> 03:21:41,650
the patient to digest the food. Let's also
select five in here to see the scheme. This
2125
03:21:41,650 --> 03:21:46,600
time for the skin, you can see that we are
not seeing any behavior instead, you're just
2126
03:21:46,600 --> 03:21:53,650
seeing some details. After that, once again,
we have our list, in which if we put six or
2127
03:21:53,650 --> 03:21:58,891
for that matter any other number, we will
close the application. Let's put six in here.
2128
03:21:58,891 --> 03:22:04,780
And as you can see application has been exited
successfully. by solving this challenge, I
2129
03:22:04,780 --> 03:22:10,460
want you to practice the object oriented programming
concepts that we have talked about in the
2130
03:22:10,460 --> 03:22:16,590
past three videos. So make sure to use them.
Okay, pause the video in here and go solve
2131
03:22:16,590 --> 03:22:21,290
the challenge. After that, come back to the
video. And let's see my solution for the challenge
2132
03:22:21,290 --> 03:22:28,890
as well. Here is my solution for the challenge.
First of all, I am going to create a package
2133
03:22:28,890 --> 03:22:39,910
inside my source folder. Let's name it or
dot maker dot o p challenge. Inside this package,
2134
03:22:39,910 --> 03:22:49,250
I am going to create my organ class. Inside
this organ class, I'm going to have two fields,
2135
03:22:49,250 --> 03:22:57,880
let's say private string, name and also private
string medical condition. I'm going to keep
2136
03:22:57,880 --> 03:23:04,770
it simple. So I think these two would be fine.
After that, let's create the constructor.
2137
03:23:04,770 --> 03:23:09,620
And after that, let's create all of the getters
and setters. I'm not sure that we are going
2138
03:23:09,620 --> 03:23:14,870
to use all of these getters and setters. later
on. If we didn't use them, we just simply
2139
03:23:14,870 --> 03:23:20,810
delete them. Beside these, I'm going to have
another method inside this class. Named get
2140
03:23:20,810 --> 03:23:31,590
details. Let's see that as well. Let's say
public void, get details. And let's print
2141
03:23:31,590 --> 03:23:42,110
some details about this Oregon. First of all,
let's say name. Plus, this stuff gets named.
2142
03:23:42,110 --> 03:23:51,720
After that, let's print a medical condition
of this organ as well. medical condition plus
2143
03:23:51,720 --> 03:23:58,490
these that get medical condition. That's all
we need inside this organ class. Let's quickly
2144
03:23:58,490 --> 03:24:06,260
create our organs one by one. First of all,
I'm going to create an organ called I. Let's
2145
03:24:06,260 --> 03:24:14,770
say I inside this iclass I'm going to have
a string called color. And I after that let's
2146
03:24:14,770 --> 03:24:24,750
have a Boolean indicating if the eye is open
or not. So let's say private Boolean is opened.
2147
03:24:24,750 --> 03:24:31,680
After that, it's time to extend the Oregon
class, let's say extends Oregon. In here we
2148
03:24:31,680 --> 03:24:37,430
are using inheritance. Now it's the time to
create our constructor. Let's select both
2149
03:24:37,430 --> 03:24:42,631
of these two fields. And after that, let's
create all of the getters and setters. Once
2150
03:24:42,631 --> 03:24:47,631
again, I'm not sure that we are going to use
all of these getters and setters. later on.
2151
03:24:47,631 --> 03:24:53,150
If we didn't use them, we just simply delete
them. Okay, let's override the get details
2152
03:24:53,150 --> 03:24:59,680
method. I can do that by pressing Ctrl plus
or by selecting get details method. Now we
2153
03:24:59,680 --> 03:25:04,690
can change Use this method. For example, in
this case, let's also print the color of the
2154
03:25:04,690 --> 03:25:15,980
eye. Let's say color. Plus this dot get color.
Inside this iclass, I'm going to have two
2155
03:25:15,980 --> 03:25:25,290
other methods as well, let's see them, let's
say public void, open. Inside this open method,
2156
03:25:25,290 --> 03:25:32,220
I'm going to change the value of this Boolean
to true. So let's say this dot set opened.
2157
03:25:32,220 --> 03:25:37,570
And let's fast through. After that, let's
bring something indicating the AI has been
2158
03:25:37,570 --> 03:25:45,800
opened. Let's say this that gets name, you
can see that we don't have any getname method.
2159
03:25:45,800 --> 03:25:50,770
Inside this class, we are using the getname
from the parent class, which is Oregon, let's
2160
03:25:50,770 --> 03:25:59,740
say plus opened. Let's have another method
called close. I'm going to save public void,
2161
03:25:59,740 --> 03:26:06,840
close. Once again, first of all, I'm going
to change the value of the Boolean to false.
2162
03:26:06,840 --> 03:26:13,880
Let's say this dot set opened, and let's pass
false. After that, let's print something.
2163
03:26:13,880 --> 03:26:16,479
Let's say this dot get name,
2164
03:26:16,479 --> 03:26:23,141
plus close. That's all we need inside this
iclass. Let's quickly create another class
2165
03:26:23,141 --> 03:26:32,210
for the heart. Let's say heart. For this heart
class, I'm going to add another field called
2166
03:26:32,210 --> 03:26:42,729
rate, let's say private int rate. It's time
to extend the Oregon class extends Oregon.
2167
03:26:42,729 --> 03:26:49,740
And after that, we need to create our constructor.
After that, we need to create our getters
2168
03:26:49,740 --> 03:26:55,320
and setters. I'm pressing ALT plus insert
to get that dialog. In case that's confusing
2169
03:26:55,320 --> 03:27:01,650
for you. After that, it's time to override
the get details method by pressing Ctrl plus
2170
03:27:01,650 --> 03:27:07,700
Oh, and by selecting get details method, we
have that in here, I'm just going to print
2171
03:27:07,700 --> 03:27:18,729
the heart rate, let's say heart rate. Plus
this dot get heart rate, or get rate. That's
2172
03:27:18,729 --> 03:27:24,840
all we need to do inside this heart class.
Let's quickly create a stomach class. Let's
2173
03:27:24,840 --> 03:27:32,150
say stomache. Inside this Islamic class, I'm
going to have a Boolean that's a private Boolean,
2174
03:27:32,150 --> 03:27:39,870
these empty indicating if the Islamic is empty
or not. After that, it's time to extend the
2175
03:27:39,870 --> 03:27:49,010
Oregon class extends Oregon. And after that,
we need to create our constructor. Let's create
2176
03:27:49,010 --> 03:27:55,690
our getters and setters. And after that, let's
override the get details method. Inside this
2177
03:27:55,690 --> 03:28:00,570
get details method. First of all, I'm going
to check that if the stomach is empty or not.
2178
03:28:00,570 --> 03:28:07,950
So let's say if this.is empty, I'm going to
use the method in here. If the stomach is
2179
03:28:07,950 --> 03:28:16,979
empty, I'm going to print something I'm going
to say need to be fat. But in the else case,
2180
03:28:16,979 --> 03:28:24,270
let's just print the stomach is full. Let's
also create another method for this class
2181
03:28:24,270 --> 03:28:34,290
called digest. Let's say public void digest.
Let's just print something let's say digesting
2182
03:28:34,290 --> 03:28:41,280
begin. Okay, we are done with our stomach
class. Let's quickly create the skin class
2183
03:28:41,280 --> 03:28:48,960
as well. For this Scan Class, I'm going to
define two more fields. First of all, I'm
2184
03:28:48,960 --> 03:28:56,310
going to define a string for the color of
the skin. After that, I'm going to define
2185
03:28:56,310 --> 03:29:03,930
an integer for the softness of their skin,
let's say private int softness. This integer
2186
03:29:03,930 --> 03:29:10,180
is going to be some number from zero to 100,
indicating the softness of the skin. After
2187
03:29:10,180 --> 03:29:18,140
that it's time to extend the organic class.
After that, we need to create our constructor.
2188
03:29:18,140 --> 03:29:25,280
Next, we need to create our getters and setters.
And after that, let's override the get details
2189
03:29:25,280 --> 03:29:35,650
method. Inside this method, let's also print
a skin color. Let's say skin color. Plus this
2190
03:29:35,650 --> 03:29:43,280
dot get color. That's all of our organs. Now
we can create our patient class. Inside my
2191
03:29:43,280 --> 03:29:49,910
package. Let's create a patient class. For
this patient, first of all, I'm going to have
2192
03:29:49,910 --> 03:29:55,630
a string for its name. After that, we are
going to have an integer for the age of the
2193
03:29:55,630 --> 03:30:02,780
patient. After that we can create our organs.
For example, I'm going To save private eye,
2194
03:30:02,780 --> 03:30:15,141
let's say left eye. Let's do the same for
the right eye. Let's also have a heart. After
2195
03:30:15,141 --> 03:30:25,330
that may be a stomach and a skin. Okay, that's
all of our fields for the patient. Let's create
2196
03:30:25,330 --> 03:30:28,170
the constructor.
2197
03:30:28,170 --> 03:30:36,610
And let's create all of the getters and setters.
We don't need any special method inside this
2198
03:30:36,610 --> 03:30:43,170
patient class, inside our main class, which
we need to create, we can instantiate it.
2199
03:30:43,170 --> 03:30:49,280
Let's create our main class. First of all
in here, we are going to create our main method.
2200
03:30:49,280 --> 03:30:53,530
After that, we can create our patient object.
But before that, let's minimize this project
2201
03:30:53,530 --> 03:31:01,181
main. Let's say patient, let's name the patient
is equal to new patient. First of all, we
2202
03:31:01,181 --> 03:31:07,890
need a naming here, let's say Brad. After
that, we need the age, let's say 28. After
2203
03:31:07,890 --> 03:31:14,130
that, we need the left eye which I can say
new I. For this, I first of all, we need a
2204
03:31:14,130 --> 03:31:20,360
name, let's say left eye. After that, we need
the medical condition, which I'm going to
2205
03:31:20,360 --> 03:31:30,150
say short sighted. For the color, let's say
blue. For the is open Boolean, let's say true,
2206
03:31:30,150 --> 03:31:41,070
we need the same thing for the right I knew
I let's just change the name to right i. Also,
2207
03:31:41,070 --> 03:31:50,890
let's change the medical condition to normal.
After that, we need the heart, let's say new
2208
03:31:50,890 --> 03:31:59,321
heart. For the medical condition, let's say
normal. And for the rate of the heart, let's
2209
03:31:59,321 --> 03:32:08,280
just say 65. I think the next organ was a
stomach. So let's say new stomach, the main
2210
03:32:08,280 --> 03:32:13,721
would be Islamic. The medical condition, I'm
just going to enter a name of some medical
2211
03:32:13,721 --> 03:32:20,690
condition, I'm going to say p UD public services
in case if you're wondering. And for the ease
2212
03:32:20,690 --> 03:32:27,250
empty, let's say false. After that, we need
a skin let's say new skin for the color of
2213
03:32:27,250 --> 03:32:33,851
their skin. Or First of all, we need the name,
let's say a skin for the medical condition,
2214
03:32:33,851 --> 03:32:41,220
let's say burnt. After that, what do we need,
we need the color and also softness for the
2215
03:32:41,220 --> 03:32:48,570
color, let's say right? For the softness,
let's say 40 out of 100. Okay, that's our
2216
03:32:48,570 --> 03:32:54,000
patient object. Now we need to create the
logic for our application. First of all, I'm
2217
03:32:54,000 --> 03:33:02,420
going to print the name and age of the patient.
So let's say print name. Plus patients that
2218
03:33:02,420 --> 03:33:12,860
gets me after that do the same thing for the
age. We are going to receive the user's input.
2219
03:33:12,860 --> 03:33:19,650
So we need a scanner. Let's say a scanner
scanner is equal to new scanner. Let's fast
2220
03:33:19,650 --> 03:33:26,650
system that. Also, now that we know about
object oriented programming, we know how this
2221
03:33:26,650 --> 03:33:31,280
scanner is working. This is just the class
with this new keyword we are instantiating
2222
03:33:31,280 --> 03:33:36,940
it inside the constructor of this scanner,
we need this system.in whatever that is. So
2223
03:33:36,940 --> 03:33:43,190
this way we have created an instance of this
scanner. After that I'm going to define a
2224
03:33:43,190 --> 03:33:49,520
Boolean, let's say Boolean. Let's name it
short finish. And initially I'm going to set
2225
03:33:49,520 --> 03:33:55,110
it to false. Later on, we will see that how
it This one is going to be useful. After that
2226
03:33:55,110 --> 03:34:01,350
I'm going to create a while loop let's say
while should finish is not equal to true,
2227
03:34:01,350 --> 03:34:07,110
continue looping. Inside the while loop, I'm
going to show a list of different organs.
2228
03:34:07,110 --> 03:34:15,741
For that I can say choose an organ. In here
I have two options. I can use this system
2229
03:34:15,741 --> 03:34:22,330
dot out dot print lines line after line. Or
the better option is to use backslash n. Let's
2230
03:34:22,330 --> 03:34:28,229
see that as well. First of all, let's go to
the next line in order to have a cleaner code.
2231
03:34:28,229 --> 03:34:34,400
In here I can say backslash n, this backslash
n moves the cursor to the next line. The other
2232
03:34:34,400 --> 03:34:40,630
one that I'm going to use in here is called
backslash t. This backslash T will add a tab
2233
03:34:40,630 --> 03:34:46,100
to our text. Later on we will see exactly
what these two are doing. Okay, the first
2234
03:34:46,100 --> 03:34:49,689
organ is left eye
2235
03:34:49,689 --> 03:35:03,220
The next one is right I
After showing this list to the user, I'm going
2236
03:35:03,220 --> 03:35:09,840
to receive the user's input. So I'm going
to say int choice is equal to a scanner dot
2237
03:35:09,840 --> 03:35:15,561
next int. We have seen this in previous videos.
So I'm not going to explain in here, you're
2238
03:35:15,561 --> 03:35:20,979
just going to receive the user's input. After
that, depending on this choice, I'm going
2239
03:35:20,979 --> 03:35:26,610
to create a switch statement, let's say switch
on choice. And let's create our different
2240
03:35:26,610 --> 03:35:33,229
cases. In case the user's choice is one, first
of all, we are going to print the details
2241
03:35:33,229 --> 03:35:42,020
about the left eye. So let's say patient that
gets left I don't get details. Next, I'm going
2242
03:35:42,020 --> 03:35:47,229
to give the user the option to open or close
the eye. For that, I need to check that if
2243
03:35:47,229 --> 03:35:54,460
the eye is opened or closed. So let's say
if patient that get left eye that is opened,
2244
03:35:54,460 --> 03:36:00,320
we need to close it, or at least we need to
give the option to close by, let's bring something
2245
03:36:00,320 --> 03:36:06,760
in here. Let's say backslash, D, backslash
D in order to have two tabs. After that, let's
2246
03:36:06,760 --> 03:36:15,971
say close or close the next we need to get
the user's input in case if it's one, we are
2247
03:36:15,971 --> 03:36:26,081
going to close the eyes. So let's say if a
scanner dot next int is equal to one, then
2248
03:36:26,081 --> 03:36:34,180
we need to say patient dot get left eye dot,
close the eyes or close. But in the else case,
2249
03:36:34,180 --> 03:36:40,130
we are just going to continue to the next
record. In the else case of the first if statement,
2250
03:36:40,130 --> 03:36:45,790
this finding here in case the eye is not opened,
we are going to show the option to open the
2251
03:36:45,790 --> 03:36:52,610
eyes. So let's say else, we need the same
logic, we can print something backslash t
2252
03:36:52,610 --> 03:37:02,010
backslash t Vaughn opened it. Next, we need
to get the user's input. Let's say if scanner
2253
03:37:02,010 --> 03:37:11,990
dot next int is equal to one, we are going
to save patients that get left eye dot open.
2254
03:37:11,990 --> 03:37:17,860
In the else case, we are just going to continue.
By continue I mean, once again, we are going
2255
03:37:17,860 --> 03:37:23,650
to show the list of different organs. Okay,
that's our first case. We also need a break
2256
03:37:23,650 --> 03:37:30,880
in here. But let's write the second case.
The second case is the right eye. We basically
2257
03:37:30,880 --> 03:37:38,370
can copy and paste all of these logic. But
we just need to change the left eye to right.
2258
03:37:38,370 --> 03:37:49,211
So for example, in here, let's say get right.
Okay, we are done with our second case. Let's
2259
03:37:49,211 --> 03:37:55,550
also write the case for the heart. Let's say
case three. First of all, let's show the details
2260
03:37:55,550 --> 03:38:04,740
of the heart, let's say patient that get heart
dot get details. After that, we are going
2261
03:38:04,740 --> 03:38:10,660
to give the user to change the heart rate.
for that. Let's say backslash t backslash
2262
03:38:10,660 --> 03:38:17,590
T one, change the heart rate. Once again,
we need to listen for the user's input. So
2263
03:38:17,590 --> 03:38:26,050
let's say if the scanner dot next int is equal
to one, we need to ask for the new heart rate.
2264
03:38:26,050 --> 03:38:31,390
So let's say enter a new heart rate. After
that, we need to save that heart rate. So
2265
03:38:31,390 --> 03:38:40,280
let's say int is equal to a scanner dot next.
After that, we can say patients that get heart
2266
03:38:40,280 --> 03:38:47,070
dot, let's say set rate. And let's pass our
new heart rate. But in the else case, like
2267
03:38:47,070 --> 03:38:52,920
before, we are going to continue. We also
need a break in here. Let's quickly add that
2268
03:38:52,920 --> 03:38:59,689
and also after changing the heart rate, it's
better to show some message. Let's say heart
2269
03:38:59,689 --> 03:39:12,610
rate change to plus patients that get heart
dot get rate. Okay, that's our third case.
2270
03:39:12,610 --> 03:39:17,960
The fourth one was for the stomach. So let's
say case four. First of all, we need to print
2271
03:39:17,960 --> 03:39:25,820
the details of the stomach patients that get
the stomach that get details. For the stomach,
2272
03:39:25,820 --> 03:39:32,100
we had the digest option. So in here, let's
show that option. backslash D, backslash D.
2273
03:39:32,100 --> 03:39:41,220
Let's see digest. Let's see if the scanner
dot next int is equal to one.
2274
03:39:41,220 --> 03:39:49,360
If that's the case, let's say patient dot
get stomach dot digest. But in the else case,
2275
03:39:49,360 --> 03:39:55,750
let's just continue. We also need a break
in here. Let's quickly add that the fifth
2276
03:39:55,750 --> 03:40:02,990
case is for the scheme. Let's say case five
For the scheme, we didn't have any option,
2277
03:40:02,990 --> 03:40:10,670
we just need to print the details, let's say
patient dot get scheme, dot get details. Of
2278
03:40:10,670 --> 03:40:16,021
course after that we need to add a break.
In here, I'm going to add a default case.
2279
03:40:16,021 --> 03:40:21,950
And if the user enters any other number than
these five, we are going to create the application.
2280
03:40:21,950 --> 03:40:27,840
For that we are going to change the value
of this short finish Boolean to true. After
2281
03:40:27,840 --> 03:40:36,260
that, we need a break. Okay, I think our application
is ready to test let's see how did we do.
2282
03:40:36,260 --> 03:40:41,720
In here, we can see the name and age of the
patient which is good, we can also see the
2283
03:40:41,720 --> 03:40:48,320
list of our organs. Once again, it's good.
Let's select left eye, we can see the details
2284
03:40:48,320 --> 03:40:54,130
of the left eye. And also we have this close
the eye option. Let's put another number beside
2285
03:40:54,130 --> 03:41:00,890
this one, let's say to once again, we can
see the list of our organs. Let's see if we
2286
03:41:00,890 --> 03:41:06,470
can close the left eye. Let's put one in here.
And as you can see, the left eye has been
2287
03:41:06,470 --> 03:41:12,521
closed. Okay, let's check the right eye. It's
working the same. Let's enter two in here
2288
03:41:12,521 --> 03:41:18,560
in order to show the list of organs. And let's
select our heart, we can see the hearts details,
2289
03:41:18,560 --> 03:41:24,130
which is good. Let's see if we can change
the heart rate. Let's select one in here,
2290
03:41:24,130 --> 03:41:30,180
the console is waiting for the new heart rates,
let's say 75. And as you can see, heart rate
2291
03:41:30,180 --> 03:41:35,350
has been changed to 75. Let's check it once
again, in order to make sure of that, let's
2292
03:41:35,350 --> 03:41:41,351
say three. And as you can see, the heart rate
is 75. Let's go to in order to show the list
2293
03:41:41,351 --> 03:41:47,230
of organs. And let's select four in order
to see their stomach, we can see all of the
2294
03:41:47,230 --> 03:41:54,141
details of the stomach. Let's digest some
food. And as you can see digesting has begun.
2295
03:41:54,141 --> 03:41:59,390
Let's check the skin which is five, you can
see that we are getting the details of the
2296
03:41:59,390 --> 03:42:05,189
skin. But we don't have any option. That seems
to be good. And if you enter any other number,
2297
03:42:05,189 --> 03:42:10,660
then these five we should create the application.
Let's enter seven in here, for example. And
2298
03:42:10,660 --> 03:42:16,060
as you can see application has been finished
successfully. Okay, it seems like our application
2299
03:42:16,060 --> 03:42:22,350
is working fine. Let's just delete the getter
and setter methods that we said that we will
2300
03:42:22,350 --> 03:42:27,670
delete if we didn't use them. Let's check
the classes one by one. First of all inside
2301
03:42:27,670 --> 03:42:33,380
the iclass. Everything that is grayed out
It means that we never used that, for example,
2302
03:42:33,380 --> 03:42:39,070
we never used this set color method. Okay,
it seems to be good. Inside the heart class.
2303
03:42:39,070 --> 03:42:47,460
I think we have used all of them. Okay, that's
fine. Inside the organ class, we didn't need
2304
03:42:47,460 --> 03:42:53,550
this set medical condition method. And also
we didn't need this setname method inside
2305
03:42:53,550 --> 03:43:00,350
the patient class. Let's see what do we have
in there? And I think we didn't use any of
2306
03:43:00,350 --> 03:43:13,430
the setters of this patient class. Let's just
delete them. Okay, that seems to be good.
2307
03:43:13,430 --> 03:43:18,689
Let's see, what do you have inside a scheme
class. And in here, we never use these setters
2308
03:43:18,689 --> 03:43:24,939
and this get softness method. Let's delete
them inside the stomach class. Let's delete
2309
03:43:24,939 --> 03:43:30,479
this set empty method. And in order to make
sure of everything, let's just run our application
2310
03:43:30,479 --> 03:43:36,900
one more time. Okay, it seems like we don't
have any compile time error, which seems to
2311
03:43:36,900 --> 03:43:42,420
be fine. Okay, that was our challenge about
object oriented programming concepts. I will
2312
03:43:42,420 --> 03:43:46,340
upload the source code at the links that you
can see on the screen, feel free to check
2313
03:43:46,340 --> 03:43:51,350
them and also, I'm more than happy to see
your feedback about the code. In the next
2314
03:43:51,350 --> 03:44:01,530
video, we will talk about collections in Java.
See you in the next video. In previous videos,
2315
03:44:01,530 --> 03:44:06,771
we have seen simple arrays in Java. For example,
if we wanted to store a list of different
2316
03:44:06,771 --> 03:44:11,970
names, we could have said something like this
because I've set a string with a pair of a
2317
03:44:11,970 --> 03:44:16,740
square bracket. After that, we could have
named our array. And after that we could have
2318
03:44:16,740 --> 03:44:22,150
initialized our string array like this inside
a square bracket in here, we needed to pass
2319
03:44:22,150 --> 03:44:27,439
the size of our array for example five. The
other way was to pass our elements at the
2320
03:44:27,439 --> 03:44:32,500
time of instantiating our array for example,
we could have said a pair of curly braces
2321
03:44:32,500 --> 03:44:37,070
and inside those curly braces because I've
passed our elements. Let's pass few names
2322
03:44:37,070 --> 03:44:39,030
in here.
2323
03:44:39,030 --> 03:44:43,130
After that, if we wanted to have access to
these elements, we could have say something
2324
03:44:43,130 --> 03:44:49,771
like this, we could have said print, let's
say names with an index of let's say two for
2325
03:44:49,771 --> 03:44:54,450
the third element, which is Brad in this case,
if you're on our application right now we
2326
03:44:54,450 --> 03:45:00,101
should see Brad. Let's run it. You can see
that Brad has been printed but if you remember
2327
03:45:00,101 --> 03:45:06,310
I said that using these kinds of simple arrays
in Java has a lot of limitations. For example,
2328
03:45:06,310 --> 03:45:11,800
the first limitation is that their size is
immutable, it means that their size cannot
2329
03:45:11,800 --> 03:45:17,430
be changed. Right now we have five elements
inside our names array. If we want to add
2330
03:45:17,430 --> 03:45:21,899
another item inside this array, we cannot
do that. For example, we cannot say something
2331
03:45:21,899 --> 03:45:28,030
like names with an index of five, which indicates
the sixth element, we cannot assign a value
2332
03:45:28,030 --> 03:45:33,790
to that, you can see that in here we have
a red warning. And if we try to run our application,
2333
03:45:33,790 --> 03:45:39,870
we get an exception. Let's see the exception.
The exception in here is array index out of
2334
03:45:39,870 --> 03:45:44,640
bounds exception. Also, when working with
simple arrays in Java, you don't have that
2335
03:45:44,640 --> 03:45:49,320
much options. Later on, we will see that when
we use different kinds of collections, we
2336
03:45:49,320 --> 03:45:54,740
can operate all sorts of operations on our
collections in which we do not have them in
2337
03:45:54,740 --> 03:46:01,150
simple areas. If we want to add a sixth element
to our array, we can create a new array, and
2338
03:46:01,150 --> 03:46:06,410
after that, we can copy all of the elements
from inside this array. Right now for the
2339
03:46:06,410 --> 03:46:12,170
situation that we have in here. If we want
to add another item to our array, we can create
2340
03:46:12,170 --> 03:46:16,940
another array with the size of six. After
that, we can copy all of these elements to
2341
03:46:16,940 --> 03:46:22,500
that array. And after everything, we can add
this new item. For example, in here, I can
2342
03:46:22,500 --> 03:46:29,440
say a string array. Let's name it new names,
is equal to new string array with the size
2343
03:46:29,440 --> 03:46:35,860
of six. After that I can create a for loop
let's say for int i is equal to zero i less
2344
03:46:35,860 --> 03:46:41,930
than names dot length, and i plus plus. And
inside that for loop, first of all, I need
2345
03:46:41,930 --> 03:46:47,930
to add all of the names to this new names
array, I can say new names with an index of
2346
03:46:47,930 --> 03:46:53,680
i is equal to names with an index of oil.
This way, we can copy all of the elements
2347
03:46:53,680 --> 03:46:59,390
from inside this names array to this new names
array. After copying all of the elements.
2348
03:46:59,390 --> 03:47:06,940
After the for loop, I can say new names with
an index of five is equal to Jerry. This way
2349
03:47:06,940 --> 03:47:12,320
we can get around this problem. For example,
if I comment this line and run my application,
2350
03:47:12,320 --> 03:47:19,030
maybe after that, print the six element inside
our new names, we have a solution. But as
2351
03:47:19,030 --> 03:47:24,510
you can see, this solution is not that much
effective. Let's quickly print this. You can
2352
03:47:24,510 --> 03:47:29,340
see Jerry has been printed in here. But as
you can see, there are a lot of codes involved.
2353
03:47:29,340 --> 03:47:35,410
And beside that if your array has a lot of
elements, for example, a million elements,
2354
03:47:35,410 --> 03:47:40,470
this process can be really time consuming.
Besides that it can be resource consuming
2355
03:47:40,470 --> 03:47:45,880
if you are downloading this array from the
internet. So this solution in here is not
2356
03:47:45,880 --> 03:47:51,850
effective. Instead of copy and pasting the
whole array, we are going to use collections.
2357
03:47:51,850 --> 03:47:56,410
Let's delete all of these codes. In Java,
we have multiple kinds of collections. The
2358
03:47:56,410 --> 03:48:00,660
first of which I'm going to talk about in
here is called ArrayList. Let's begin to see
2359
03:48:00,660 --> 03:48:08,330
that so we type array list with capsule a.
After that inside a pair of anchor bracket,
2360
03:48:08,330 --> 03:48:14,090
or as some might call them diamonds, we need
to define the type of our ArrayList object
2361
03:48:14,090 --> 03:48:19,120
for example, I can say is string. After that
we need to name our ArrayList. For example,
2362
03:48:19,120 --> 03:48:25,580
I can say names, is equal to new ArrayList.
As simple as that we have an ArrayList This
2363
03:48:25,580 --> 03:48:30,390
is one way of defining your ArrayList. There
is also another way, let's quickly see that
2364
03:48:30,390 --> 03:48:35,651
so instead of saying ArrayList string, we
can say list, you can see that the icon in
2365
03:48:35,651 --> 03:48:40,870
here is different. It's an interface. We will
talk about interfaces later on in the course.
2366
03:48:40,870 --> 03:48:45,380
But for now just know that there are two ways
of defining your ArrayList. Let's see how
2367
03:48:45,380 --> 03:48:52,091
can we use this list to create an ArrayList
we can say list of strings for example. Once
2368
03:48:52,091 --> 03:48:58,960
again, let's name our list. For example, let's
see students is equal to new ArrayList. There
2369
03:48:58,960 --> 03:49:03,750
is a slight difference in here. But I just
wanted to show you both ways of defining your
2370
03:49:03,750 --> 03:49:09,300
ArrayList. Okay, let's delete this line for
now. Right now our ArrayList is empty. And
2371
03:49:09,300 --> 03:49:14,340
if we want to add some elements to this ArrayList,
we can simply say names.ad,
2372
03:49:14,340 --> 03:49:19,680
we can use this add method in order to add
elements into our ArrayList. For example,
2373
03:49:19,680 --> 03:49:24,990
let's add one. You can use this add method
as many times as you want. If I want to add
2374
03:49:24,990 --> 03:49:31,729
another element, I can simply do that. So
the first benefit of using array list is clear.
2375
03:49:31,729 --> 03:49:37,960
Now the size of array list is mutable, it
means that we can change the size of array
2376
03:49:37,960 --> 03:49:43,180
list. After adding some elements to your array
list. You can get those elements by using
2377
03:49:43,180 --> 03:49:48,810
the get method. Let's quickly say that, for
example, let's print the first element I can
2378
03:49:48,810 --> 03:49:55,370
see names that get you can see that this get
method requires an index. Once again, indexes
2379
03:49:55,370 --> 03:50:01,270
in Java start from zero so if I pass zero
in here, it means that I'm going to get the
2380
03:50:01,270 --> 03:50:06,630
first element. Right now if I run my application,
I should see my Sam printed into the console,
2381
03:50:06,630 --> 03:50:11,710
you can see that Mason has been printed. Also
when using array lists, you can also get the
2382
03:50:11,710 --> 03:50:16,351
size of your array list. For example, if I
want to print the size of my ArrayList, I
2383
03:50:16,351 --> 03:50:21,950
can say something like this, I can say names
dot size. Let's run the application and see
2384
03:50:21,950 --> 03:50:26,740
what would be the result, you can see that
the size of our ArrayList is two, you have
2385
03:50:26,740 --> 03:50:32,610
all sorts of options when using array lists,
you can also clear all of the elements inside
2386
03:50:32,610 --> 03:50:39,080
your ArrayList. For example, I can say names
dot clear. This clear method removes all of
2387
03:50:39,080 --> 03:50:44,229
the elements inside your ArrayList. After
clearing, if I once again get the size of
2388
03:50:44,229 --> 03:50:49,689
my ArrayList this time the size should be
zero. And as you can see down in here, it's
2389
03:50:49,689 --> 03:50:54,590
zero. If you want, you can also remove one
element from your ArrayList. Let's quickly
2390
03:50:54,590 --> 03:51:01,680
see that I can say names dot remove this remove
method requires an object, it means that we
2391
03:51:01,680 --> 03:51:07,701
need to pass the whole element. So let's say
may sound for example. But before that, let
2392
03:51:07,701 --> 03:51:13,190
me quickly comment this line because we don't
want to clear our array list. And after removing
2393
03:51:13,190 --> 03:51:21,100
my son, let's print the first element inside
our array list. Let's say names that get and
2394
03:51:21,100 --> 03:51:26,250
let's pass zero as the index. As you can see,
the first element right now is Sarah, it means
2395
03:51:26,250 --> 03:51:30,940
that my Sam has been removed successfully.
Let's see what other options do we have on
2396
03:51:30,940 --> 03:51:36,860
our ArrayList. Let's like names by typing
dots, we can see the list of options, we can
2397
03:51:36,860 --> 03:51:42,600
also check that if an option exists in our
ArrayList. For example, if I use this contains
2398
03:51:42,600 --> 03:51:48,399
method, once again, it requires an object,
let's say Mesa, let's bring this whole line
2399
03:51:48,399 --> 03:51:54,439
of code. This contains method returns a Boolean.
So right now if I run my application, because
2400
03:51:54,439 --> 03:51:59,650
we have removed my sample we should see false,
we can see that that's good. But if we change
2401
03:51:59,650 --> 03:52:06,430
this to Sarah, we should see through. And
through this, you can also check that if your
2402
03:52:06,430 --> 03:52:13,271
ArrayList is empty or not. For that you can
print something for example, you can say names.is
2403
03:52:13,271 --> 03:52:19,510
empty, we have this method once again, it's
returning a Boolean. Right now we have zero
2404
03:52:19,510 --> 03:52:24,860
in our ArrayList. So it shouldn't be empty.
And as you can see we are receiving false.
2405
03:52:24,860 --> 03:52:28,900
But if we remove Sara before this line of
code we should see through, let's quickly
2406
03:52:28,900 --> 03:52:36,290
do that names dot remove. And let's pass this
time you can see that through has been printed,
2407
03:52:36,290 --> 03:52:41,610
you can also get the index of some element
from inside your ArrayList as well. Let's
2408
03:52:41,610 --> 03:52:45,729
quickly see that before everything I'm going
to comment all of these lines of code because
2409
03:52:45,729 --> 03:52:48,920
I want to have a clear console.
2410
03:52:48,920 --> 03:52:56,770
down in here. Let's say print names that index
of and in here I can pass my object for example,
2411
03:52:56,770 --> 03:53:02,670
I can pass Mesa. Right now Mason is the first
element so we should see zero printed, you
2412
03:53:02,670 --> 03:53:08,430
can see that it's zero. But if we don't have
that element inside our ArrayList. For example,
2413
03:53:08,430 --> 03:53:14,220
if we pass Brad in here, we should see negative
one. And as you can see negative one has been
2414
03:53:14,220 --> 03:53:20,620
printed. When you're using ArrayList you can
only have objects as the type of elements
2415
03:53:20,620 --> 03:53:26,240
inside your ArrayList. For example, in here,
I cannot say integer, you cannot use primitive
2416
03:53:26,240 --> 03:53:32,061
data types when you are creating a new ArrayList.
As you can see in the error in here, the error
2417
03:53:32,061 --> 03:53:37,510
says that type argument cannot be primitive
type. If you want to have an ArrayList of
2418
03:53:37,510 --> 03:53:43,149
different numbers, you can use the equivalence
Java class. For example, in here I can say
2419
03:53:43,149 --> 03:53:48,880
integer with a capital I. And now I have an
ArrayList of integer. Right now I am getting
2420
03:53:48,880 --> 03:53:55,000
an error because I'm trying to add some strings
to this integer array list. But you get the
2421
03:53:55,000 --> 03:54:01,110
idea of we can use this integer class in order
to have an ArrayList. So in Java equivalent
2422
03:54:01,110 --> 03:54:06,541
to every primitive data type, you have a class
as well, we have seen this integer. Let's
2423
03:54:06,541 --> 03:54:14,630
also check others, we can say Boolean similar
to that we can say long, we can also say double.
2424
03:54:14,630 --> 03:54:19,120
And you can check the others yourself. But
let's change this one to a string right now.
2425
03:54:19,120 --> 03:54:24,250
The other option that you have when you're
using ArrayList is sorting different items.
2426
03:54:24,250 --> 03:54:28,760
For example, if you want to sort different
elements inside your array list alphabetically,
2427
03:54:28,760 --> 03:54:34,110
you have that option. And the way to do that
is like this. Let's see that down in here
2428
03:54:34,110 --> 03:54:40,670
we can see names that sort you can see that
this sort method requires some comparateur
2429
03:54:40,670 --> 03:54:45,680
later on in the course we will see this comparateur
In fact, we will use this sort method in order
2430
03:54:45,680 --> 03:54:50,780
to source different elements in our array
list. But for now, I'm not going to talk about
2431
03:54:50,780 --> 03:54:55,540
this I just wanted to say that you have this
option. Okay, let's delete this and let's
2432
03:54:55,540 --> 03:55:00,030
move on from this part. Similar to simple
arrays in Java. If you want to eat
2433
03:55:00,030 --> 03:55:05,080
To read through all of the elements in your
array list, you can use for loops. Let's quickly
2434
03:55:05,080 --> 03:55:12,510
see that as well. You can say for int i is
equal to zero, i less than named that size,
2435
03:55:12,510 --> 03:55:21,800
this time, i plus plus. Inside this for loop,
we can say, print names that get with the
2436
03:55:21,800 --> 03:55:26,811
index of AI. This way, we can print all of
the elements in our array list. But before
2437
03:55:26,811 --> 03:55:32,021
that, let's comment this line. And let's run
our application, you can see that we can see
2438
03:55:32,021 --> 03:55:37,050
both of our elements printed. Okay, that's
enough talking about array lists. Let's also
2439
03:55:37,050 --> 03:55:42,830
talk about another kind of collection in Java
called maps. maps are useful for when you
2440
03:55:42,830 --> 03:55:48,460
have some key value pair data. Let's define
a map and we will talk about what I mean by
2441
03:55:48,460 --> 03:55:53,430
key value pairs. You can define a map like
this, you can say map you can see that map
2442
03:55:53,430 --> 03:55:57,700
is an interface. We will talk about interfaces
later on in the course I believe in the next
2443
03:55:57,700 --> 03:56:02,830
video. But for now, let's move on. You can
also see that inside these anchor brackets
2444
03:56:02,830 --> 03:56:08,460
inside these diamonds, we have these K and
V these two stands for key and values. So
2445
03:56:08,460 --> 03:56:13,440
in here, if we say anchor bracket, for the
first element, we need to pass the kind of
2446
03:56:13,440 --> 03:56:19,080
key the data type of our keys. For example,
I can say string. And after that, we need
2447
03:56:19,080 --> 03:56:23,979
to provide the data type of our values. For
example, I can say a string once again, for
2448
03:56:23,979 --> 03:56:29,170
the data type of my values. Right now I'm
getting a red warning. And that's because
2449
03:56:29,170 --> 03:56:36,399
it seems like the ID hasn't imported maps
into my class, I can press ALT plus Enter.
2450
03:56:36,399 --> 03:56:41,439
And if we take a look at above, in here, it
seems like we have imported maps into our
2451
03:56:41,439 --> 03:56:47,030
class successfully. After defining the data
type of your keys and values inside your map,
2452
03:56:47,030 --> 03:56:54,189
you need to name your map. For example, I
can say contacts. In here, I can say new maps.
2453
03:56:54,189 --> 03:56:58,380
But if I do that, you will see that there
are a lot of methods that I need to be worried
2454
03:56:58,380 --> 03:57:04,560
about, there is a better way of defining your
maps, let's press Ctrl Z in order to undo
2455
03:57:04,560 --> 03:57:11,720
the change, I can say new hashmap. This time,
you can see that this hash map is a class.
2456
03:57:11,720 --> 03:57:16,800
And if you want we can instantiate our map
this way, this time, we do not have all of
2457
03:57:16,800 --> 03:57:23,310
those extra methods. I don't intend to talk
about hash maps in here, the topic is a bit
2458
03:57:23,310 --> 03:57:28,490
technical, it's about the way that we are
going to iterate through our maps elements.
2459
03:57:28,490 --> 03:57:33,150
It's way above the talk that we have in here,
I just wanted to say that you can instantiate
2460
03:57:33,150 --> 03:57:38,630
your maps as different hash maps. Okay, now
you have an empty map, which the data type
2461
03:57:38,630 --> 03:57:44,340
of different keys is a string. And the data
type of different values is a string as well.
2462
03:57:44,340 --> 03:57:49,391
If you want to put some elements to this map,
you can say something like this, you can say
2463
03:57:49,391 --> 03:57:54,320
contacts that put this is the method that
we are going to use in order to add some elements
2464
03:57:54,320 --> 03:57:59,271
to our map, you can see that right now this
port method is waiting for a key and also
2465
03:57:59,271 --> 03:58:06,240
a value for the key. For example, I can pass
a name, let's say me, Sam, and for the value,
2466
03:58:06,240 --> 03:58:13,171
let's pass his email, let's say me sam@maker.org.
This way we can add elements to our map, I
2467
03:58:13,171 --> 03:58:18,250
think if I change the name of my contacts
map to let's say email list, that would be
2468
03:58:18,250 --> 03:58:23,810
much more better. In order to change the name
of some variable or class in Java, you have
2469
03:58:23,810 --> 03:58:29,340
an option, you can select the whole name,
you can right click on the name of your variable,
2470
03:58:29,340 --> 03:58:34,570
you can go to this refactor in here, and you
can use this rename or alternatively, you
2471
03:58:34,570 --> 03:58:39,979
can use the shortcut, which is shift plus
f6. Let's select that. And in here, I'm going
2472
03:58:39,979 --> 03:58:43,820
to delete this whole name, you can see that
once I'm deleting this, it's deleting the
2473
03:58:43,820 --> 03:58:49,850
name in both places. So let's say email list.
And let's press enter. I think this name is
2474
03:58:49,850 --> 03:58:56,280
much more suitable. Let's add another element
to our map. Let's say email list dot put,
2475
03:58:56,280 --> 03:59:04,660
let's say Brad. And let's say brad@gmail.com.
Now that I have some elements in my map, if
2476
03:59:04,660 --> 03:59:09,860
I want to have access to those elements, I
can say something like this. For example,
2477
03:59:09,860 --> 03:59:15,500
let's print this statement. Let's say email
list dot. And once again, you can see that
2478
03:59:15,500 --> 03:59:20,439
we have this get method which requires an
object for the object in here we need to pass
2479
03:59:20,439 --> 03:59:26,490
the key. For example, if I pass Mesa, as the
result, I should see the value of Mesa. In
2480
03:59:26,490 --> 03:59:31,180
this case, I should see the email. Let's run
the application. And let's see if we can print
2481
03:59:31,180 --> 03:59:36,270
the email of maysa. You can see that the email
has been printed successfully, like array
2482
03:59:36,270 --> 03:59:40,770
lists. When you are using maps, you have all
sorts of options. For example, if you want
2483
03:59:40,770 --> 03:59:47,760
to get the size of your map, you can say something
like this you can say email list dot size.
2484
03:59:47,760 --> 03:59:52,321
Let's run the application and let's see if
we can get to two has been printed. If you
2485
03:59:52,321 --> 03:59:58,840
want to remove an item from your map you can
say email list that remove once again this
2486
03:59:58,840 --> 04:00:04,690
remove required As an object, in this case,
we need to pass the key. Let's pass Brad.
2487
04:00:04,690 --> 04:00:10,150
And let's print the size of our map once again.
This time after removing Brad, you can see
2488
04:00:10,150 --> 04:00:15,890
that the size is one. You can also check that
if some key exists in your map or not. The
2489
04:00:15,890 --> 04:00:21,540
way to do that is like this. Let's bring that
first of all, we can say email lists that
2490
04:00:21,540 --> 04:00:27,460
contain keys. Similarly, we have contains
value, which you can guess it will check that
2491
04:00:27,460 --> 04:00:33,070
if some value exists in your map. Let's try
the first one contains key. And let's pass
2492
04:00:33,070 --> 04:00:38,270
Brad in here, because we have removed breath,
we should see faults. And as you can see,
2493
04:00:38,270 --> 04:00:43,430
false has been printed. But if I change this
to Mesa, we should see through. Okay, that
2494
04:00:43,430 --> 04:00:48,130
seems to work fine. We can do the same thing
for the values.
2495
04:00:48,130 --> 04:00:56,820
Let's say email list dot contains value. Let's
pass the May Sam's email may sam@miko.org.
2496
04:00:56,820 --> 04:01:02,189
Because it does exist in our map we should
see through and through it is you can check
2497
04:01:02,189 --> 04:01:08,200
other options. yourself. If you want. We can
say email list dot once again, you can see
2498
04:01:08,200 --> 04:01:13,410
that we have this clear option, it will clear
all of the elements of your map you have this
2499
04:01:13,410 --> 04:01:18,490
is empty method, it will check that if your
map is empty or not. The other option that
2500
04:01:18,490 --> 04:01:25,420
you have in here is this values, you can see
that this values method will return the collection.
2501
04:01:25,420 --> 04:01:30,560
If you want to copy all of the values of your
map, you can use these values and you can
2502
04:01:30,560 --> 04:01:35,740
save it inside a collection. But I'm not going
to do that in here. But to just show you I'm
2503
04:01:35,740 --> 04:01:41,240
going to say that there is an interface in
Java called collection this morning here in
2504
04:01:41,240 --> 04:01:46,970
which is the generic type of all of the collections
available in Java. But more on that later
2505
04:01:46,970 --> 04:01:53,439
on when we source different items inside our
ArrayList like ArrayList, you cannot have
2506
04:01:53,439 --> 04:01:58,870
a primitive data type as the data type of
your key or your values when you are using
2507
04:01:58,870 --> 04:02:03,721
maps. For example, in here, I cannot say int
if you take a look at the warning, it says
2508
04:02:03,721 --> 04:02:09,070
that type argument cannot be of primitive
data type. Similarly for the values, let's
2509
04:02:09,070 --> 04:02:15,150
say int, once again, you can use the equivalent
Java class for these primitive data types.
2510
04:02:15,150 --> 04:02:20,010
But you cannot just use primitive data types.
Okay, let's roll back everything. Besides
2511
04:02:20,010 --> 04:02:25,200
maps and array lists, you have other kinds
of collections in Java as well, I don't want
2512
04:02:25,200 --> 04:02:30,210
to talk about them in here, because I think
that for the Android course, these two are
2513
04:02:30,210 --> 04:02:35,399
enough and you don't need to know about others.
But if you're curious, you can always check
2514
04:02:35,399 --> 04:02:41,070
this Oracle webpage, you can see that we have
sets, lists and other kinds of collections.
2515
04:02:41,070 --> 04:02:46,479
But honestly, these are not going to be helpful
for our Android course maps. And array lists
2516
04:02:46,479 --> 04:02:53,790
are just fine. As the data type of key values
inside maps, and also values inside the ArrayList.
2517
04:02:53,790 --> 04:02:59,189
You can also have different classes. Let's
quickly see that as well. In my source folder
2518
04:02:59,189 --> 04:03:03,750
inside the package, I'm going to create a
new class called student. Let's quickly see
2519
04:03:03,750 --> 04:03:09,800
that inside this student class, I'm going
to define two new fields, let's say private
2520
04:03:09,800 --> 04:03:12,530
string name, and private integer
2521
04:03:12,530 --> 04:03:14,021
ID.
2522
04:03:14,021 --> 04:03:21,050
Let's have a constructor and some getters
and setters. Now that I have created this
2523
04:03:21,050 --> 04:03:26,689
class, I can pass it as the data type of my
ArrayList. Let's quickly create another array
2524
04:03:26,689 --> 04:03:32,860
list down in here. I can say ArrayList of
different students This time, let's name this
2525
04:03:32,860 --> 04:03:40,180
ArrayList students, let's say is equal to
new ArrayList. Now you can put as many students
2526
04:03:40,180 --> 04:03:48,080
as you want inside this ArrayList. For example,
let's say students that add let's say a new
2527
04:03:48,080 --> 04:03:53,643
student, let's say may sound with the idea
of 15, maybe. So beside the strings, other
2528
04:03:53,643 --> 04:03:59,610
kind of customized datatype is agreeable with
the collections in Java. Okay, just before
2529
04:03:59,610 --> 04:04:04,320
I finish off this video I'm going to talk
about for each loops in here. If you remember
2530
04:04:04,320 --> 04:04:09,149
when we talked about loops in Java, I said
that there is another kind of loops called
2531
04:04:09,149 --> 04:04:14,310
for each loop, I said that we are going to
talk about for each loops when we know about
2532
04:04:14,310 --> 04:04:19,610
object oriented programming and also collections
in Java. So I think now it's a good time to
2533
04:04:19,610 --> 04:04:24,400
talk about that you can create a for each
loop like this, you can say for instead of
2534
04:04:24,400 --> 04:04:30,540
saying int i is equal to zero, I'm going to
pass the datatype of the list that I'm going
2535
04:04:30,540 --> 04:04:35,620
to iterate over. For example, in this case,
I'm going to say a student. After net I need
2536
04:04:35,620 --> 04:04:41,570
a name. For example, I'm going to say s and
after the name, I need a column. After that
2537
04:04:41,570 --> 04:04:46,851
I need the list or ArrayList that I'm going
to iterate. For example, in this case, I'm
2538
04:04:46,851 --> 04:04:52,660
going to say students, let's review everything
once again. In here. First of all we have
2539
04:04:52,660 --> 04:04:57,530
passed the data type of the object that we
are going to look into the name of that specific
2540
04:04:57,530 --> 04:05:02,110
object is as you can name it, whatever Do
you want after the name, you should have a
2541
04:05:02,110 --> 04:05:07,260
colon. And after that, you need to pass the
collection that you're going to look into
2542
04:05:07,260 --> 04:05:12,240
inside this for loop, you can use this s in
order to have some sort of operation on your
2543
04:05:12,240 --> 04:05:19,030
object. For example, if I want to print the
name of my students, I can say, print s, that
2544
04:05:19,030 --> 04:05:25,439
gets me right now our ArrayList has only one
student, let's quickly add another, let's
2545
04:05:25,439 --> 04:05:33,820
say students that add a new student. Let's
say Sarah, with the idea of maybe 18. Let's
2546
04:05:33,820 --> 04:05:38,180
run the application and see what would be
the result. As you can see down in here, Mason,
2547
04:05:38,180 --> 04:05:44,189
and Sarah has been preempted. So this is in
here is like I in a for loop. But instead
2548
04:05:44,189 --> 04:05:50,280
of being the index of the item, it's the item
itself from time to times for each loops can
2549
04:05:50,280 --> 04:05:54,610
be useful and this is how you can create them.
Okay, I think that's a good point to stop
2550
04:05:54,610 --> 04:05:59,360
the video in here. In the next video we are
going to talk about some small concepts. For
2551
04:05:59,360 --> 04:06:04,660
example, we will talk about this static keyword
that we have seen in the declaration of our
2552
04:06:04,660 --> 04:06:09,610
main method. Besides that, if we had time,
we will talk about inner classes, we will
2553
04:06:09,610 --> 04:06:18,840
talk about interfaces and abstract classes
See you in the next video. In this video,
2554
04:06:18,840 --> 04:06:24,240
we are going to talk about some small concepts
that we need to know about. The first of those
2555
04:06:24,240 --> 04:06:29,760
concepts is this static keyword that we have
seen so far in the declaration of our main
2556
04:06:29,760 --> 04:06:34,490
method. I'm going to talk about that in another
class. So let's create another class in my
2557
04:06:34,490 --> 04:06:41,930
package. Let's name this class test class.
You have seen inside the main class that we
2558
04:06:41,930 --> 04:06:46,990
can have this static keyword for our methods.
But besides that, we can also declare our
2559
04:06:46,990 --> 04:06:53,950
variables as a static as well. For example,
in here, I can say public, let's say static.
2560
04:06:53,950 --> 04:06:59,340
After that I can say a string. Let's say name,
you can see that it's working fine as well.
2561
04:06:59,340 --> 04:07:04,760
I'm not sure if you have noticed or not. But
as soon as I declare this a string as a static,
2562
04:07:04,760 --> 04:07:10,900
the style of this name has changed to italic.
If I remove that you can see the difference.
2563
04:07:10,900 --> 04:07:16,060
But what does it mean to have a static variable
or a static method? Well, when you define
2564
04:07:16,060 --> 04:07:22,210
your variables and methods as static, those
methods and variables belong to the object
2565
04:07:22,210 --> 04:07:27,660
itself and not to the instances of that object,
we will see what I'm talking about. But before
2566
04:07:27,660 --> 04:07:32,520
that, let's quickly have other variables in
here. For example, I can say public
2567
04:07:32,520 --> 04:07:41,420
int, age, maybe another one public, let's
say a string, a skin color. These variable
2568
04:07:41,420 --> 04:07:46,270
doesn't mean anything, I just want to have
some variables. Let's also create a constructor
2569
04:07:46,270 --> 04:07:53,290
in here. Notice that when I use the intelligent
generator to create my constructor, I do not
2570
04:07:53,290 --> 04:07:58,250
see my static variable in this list. Once
again, that's because the static variables
2571
04:07:58,250 --> 04:08:03,470
do not belong to the instances of the object
ID belongs to the object itself. Let's select
2572
04:08:03,470 --> 04:08:09,470
these two. And let's create getters and setters.
But as you can see, we do have a getter and
2573
04:08:09,470 --> 04:08:15,110
setter for this static variable. Okay, now
that we have this class, if we want to create
2574
04:08:15,110 --> 04:08:20,760
an instance of that class, for example, from
inside our main class, if you say test class,
2575
04:08:20,760 --> 04:08:25,710
let's name it test class is equal to new test
class. The constructor in here is waiting
2576
04:08:25,710 --> 04:08:32,770
for an age and also a skin color, let's say
25. Right? This way, we can create our instance
2577
04:08:32,770 --> 04:08:38,470
of the class but how do we assign a value
to the name variable farming site artists
2578
04:08:38,470 --> 04:08:44,330
class? Well, you can use the setter methods.
For example, you can say test class dot set
2579
04:08:44,330 --> 04:08:49,470
name, this method and you can pass a name
for example, Mason, but there is a warning
2580
04:08:49,470 --> 04:08:55,010
in here this highlighted warning. What does
that mean is that you can set a value for
2581
04:08:55,010 --> 04:09:00,660
this name variable even without instantiating
your class. For example, in here, I can say
2582
04:09:00,660 --> 04:09:07,030
test class with a capital T dot, let's say
name. Or alternatively, because the setter
2583
04:09:07,030 --> 04:09:12,149
method for this name is static as well, I
can say set name. Let's use the first one,
2584
04:09:12,149 --> 04:09:18,550
I can say test class dot name is equal to
for example, Mesa, we can see that even without
2585
04:09:18,550 --> 04:09:22,580
instantiating this object, we can assign a
value to this variable. In order to make sure
2586
04:09:22,580 --> 04:09:28,250
of that let's move these two line of code
to after assigning a value to the name variable.
2587
04:09:28,250 --> 04:09:33,330
I can also delete this setname method safely.
Now, if I want to have access to the name
2588
04:09:33,330 --> 04:09:39,891
variable of this test class, I can say something
like this. I can say name plus test class
2589
04:09:39,891 --> 04:09:46,490
this time with flirty. That let's say getname.
Let's run the application and let's see what
2590
04:09:46,490 --> 04:09:52,010
would be the result. You can see that message
has been printed even though we have set the
2591
04:09:52,010 --> 04:09:59,170
value of this name variable before instantiating
our object so whenever you use a static variables
2592
04:09:59,170 --> 04:10:04,750
that variable will be the same in all instances
of your class. For example, if I have another
2593
04:10:04,750 --> 04:10:09,570
instance of this test class, for example,
if I say test class,
2594
04:10:09,570 --> 04:10:16,880
second test class is equal to new test class,
maybe another age and another skin color,
2595
04:10:16,880 --> 04:10:23,649
let's say 30. Black. If I print the name variable
of the second test class, the result would
2596
04:10:23,649 --> 04:10:30,359
be the same. Let's say second test class dot
get name, or for that matter, we can say name.
2597
04:10:30,359 --> 04:10:36,170
Let's run our application, you can see that
my Sam has been printed. Besides variables,
2598
04:10:36,170 --> 04:10:41,740
methods can be as static as well, let's quickly
define a static method inside our test class,
2599
04:10:41,740 --> 04:10:49,740
let's say public static void. Let's name it
print something. And inside this method, let's
2600
04:10:49,740 --> 04:10:56,130
just print something. This static method like
a static variable belong to the object itself
2601
04:10:56,130 --> 04:11:02,649
and not to the instances of this object. For
example, in here, once again, I can say test
2602
04:11:02,649 --> 04:11:08,240
class with a capital T dot print something,
you can see that we do not need to instantiate
2603
04:11:08,240 --> 04:11:13,760
our object. If you're on our application,
you can see that something is printing. Okay,
2604
04:11:13,760 --> 04:11:18,681
now we know that static variables and methods
belong to the object itself and not to the
2605
04:11:18,681 --> 04:11:23,609
instances of that object. But why would we
want to use a static variables and methods
2606
04:11:23,609 --> 04:11:28,370
there are a few reasons for that. First of
all, you may want to have a constant variable
2607
04:11:28,370 --> 04:11:33,780
in all instances of your class. For example,
imagine that you are creating a class for
2608
04:11:33,780 --> 04:11:39,260
simulating different employees of a company
inside that class you may have some information
2609
04:11:39,260 --> 04:11:43,820
about the employee, for example, the age,
the name, email, and everything. But besides
2610
04:11:43,820 --> 04:11:48,790
all of those, you may have a field called
company name, you know that in all instances
2611
04:11:48,790 --> 04:11:53,740
of your employee class, the company name should
be the same for that matter, you may want
2612
04:11:53,740 --> 04:11:59,780
to declare your company name field as a static
also, some say that using a static variables
2613
04:11:59,780 --> 04:12:05,780
and methods is very memory friendly, because
there can be only one instance of that variable
2614
04:12:05,780 --> 04:12:11,670
or method in all of your application. So no
matter if you have 1000 employees in your
2615
04:12:11,670 --> 04:12:17,840
application, the company name field in all
of those instances will occupy only some spaces
2616
04:12:17,840 --> 04:12:24,080
as a string can so using a static variables
and methods is very memory friendly. But there
2617
04:12:24,080 --> 04:12:29,170
is a downside when you're working with this
static keyword. Let's quickly see that inside
2618
04:12:29,170 --> 04:12:35,950
our test class as well. Let's quickly create
another method in here, let's say public void.
2619
04:12:35,950 --> 04:12:41,020
Let's name it print, notice that this print
method is not a static and because this is
2620
04:12:41,020 --> 04:12:46,130
not a static now, we cannot have access to
that method from inside this print something
2621
04:12:46,130 --> 04:12:51,710
method for example, in here I cannot say print,
you can see we have a red warning in here.
2622
04:12:51,710 --> 04:12:57,570
It says that non static method print cannot
be referenced from a static context similar
2623
04:12:57,570 --> 04:13:03,330
to non static methods, you cannot have access
to non static fields inside the static method
2624
04:13:03,330 --> 04:13:09,141
as well. For example, in here if I try to
print the age of this test class, you can
2625
04:13:09,141 --> 04:13:14,310
see that we are getting a red warning in here.
Once again, the warning says that non static
2626
04:13:14,310 --> 04:13:20,450
field cannot be referenced from a static context.
So you cannot use non static fields and methods
2627
04:13:20,450 --> 04:13:25,330
inside a static method. Okay, I think that's
enough talking about this static keyword.
2628
04:13:25,330 --> 04:13:30,180
Let's quickly delete this class. Also, we
need to delete all of these lines of code.
2629
04:13:30,180 --> 04:13:35,300
Okay, now we know everything about this main
method. First of all, its access modifier
2630
04:13:35,300 --> 04:13:41,370
is public. It's a static method, its return
type is void, its name is main. And also as
2631
04:13:41,370 --> 04:13:46,470
the input of this method, we are receiving
a string array called arguments. Because this
2632
04:13:46,470 --> 04:13:51,850
main method is a static, we know that there
can only be one instance of this main method
2633
04:13:51,850 --> 04:13:57,149
in our entire application. We are not concerned
about the voice in here, but we know that
2634
04:13:57,149 --> 04:14:02,270
in order to run Java applications, we need
a main method, which is a static with this
2635
04:14:02,270 --> 04:14:06,760
specific syntax. Okay, that's enough. Let's
move on. The next topic that I'm going to
2636
04:14:06,760 --> 04:14:13,820
talk about in here is inner class. Let's quickly
see that in another class called test class.
2637
04:14:13,820 --> 04:14:19,310
Inside our classes, besides variables and
methods, we can also have another class and
2638
04:14:19,310 --> 04:14:24,660
here is how we can define that class. We can
say private, let's say class, I'm going to
2639
04:14:24,660 --> 04:14:31,520
name this class test inner class. And here
we have this inner class like before, inside
2640
04:14:31,520 --> 04:14:37,200
this in our class, we can have other methods
and variables. For example, I can say private
2641
04:14:37,200 --> 04:14:42,410
string name in here. I can have a constructor
for this class as well. Let's quickly see
2642
04:14:42,410 --> 04:14:47,760
that you can see we can create the constructor
we can create all sorts of getters and setters.
2643
04:14:47,760 --> 04:14:53,630
And we can treat this class as any other class
in Java. But there are a few differences between
2644
04:14:53,630 --> 04:14:59,820
a class and an inner class. For example, if
we have some fields in our parent class, that's
2645
04:14:59,820 --> 04:15:07,149
Let's define some, let's say private int,
age, and private string color. No matter what
2646
04:15:07,149 --> 04:15:12,410
the access modifier of these fields are, we
can have access to these inside our tests
2647
04:15:12,410 --> 04:15:18,880
in our class. But we need to do that inside
another method, let's say private, void, print
2648
04:15:18,880 --> 04:15:26,590
the page. Inside this method, I can say print,
let's say age plus age, you can see that it's
2649
04:15:26,590 --> 04:15:31,760
working fine. Even though the access modifier
of this age is private, we can have access
2650
04:15:31,760 --> 04:15:36,380
to that from inside our inner class. The same
is for different methods of your parents.
2651
04:15:36,380 --> 04:15:41,490
Plus, no matter what the access modifier of
those methods are, you can have access to
2652
04:15:41,490 --> 04:15:45,890
them from inside your inner class, there are
some times that you may want to use inner
2653
04:15:45,890 --> 04:15:51,270
classes for example, when you use inner classes,
your code might be much more easier for us
2654
04:15:51,270 --> 04:15:56,030
to follow. The other reason for using inner
classes is that you can have this private
2655
04:15:56,030 --> 04:16:01,120
access modifier at the declaration of your
class if you remember, we cannot change this
2656
04:16:01,120 --> 04:16:05,810
public access modifier when we create our
parent class for example, in here I cannot
2657
04:16:05,810 --> 04:16:11,260
say private you can see an error in here and
if you hover over that you can see that modifier
2658
04:16:11,260 --> 04:16:16,760
privates not allowed in here beside private
access modifier You can also have a static
2659
04:16:16,760 --> 04:16:22,530
classes. That is another benefit of using
inner classes you didn't have this option
2660
04:16:22,530 --> 04:16:28,640
when you created your parent class let's quickly
see that if I say static in here once again
2661
04:16:28,640 --> 04:16:34,930
modifier static not allowed in here. So within
our classes, you can have private and static
2662
04:16:34,930 --> 04:16:39,689
classes in this course, we are not going to
use inner classes that much. In fact, the
2663
04:16:39,689 --> 04:16:44,891
first time that we are going to use it is
when we know about async tasks. In most cases,
2664
04:16:44,891 --> 04:16:50,170
we are going to define our async tasks as
inner classes. We will talk about that later
2665
04:16:50,170 --> 04:16:54,430
on in the course. Okay, I think that's a good
point to stop the video in here. I did want
2666
04:16:54,430 --> 04:16:59,761
to talk about interfaces and abstract classes
in this video. But I think that's better to
2667
04:16:59,761 --> 04:17:07,810
have that discussion in the next video. So
see you in the next video.
2668
04:17:07,810 --> 04:17:12,189
As I said at the end of the previous video,
In this video, we are going to talk about
2669
04:17:12,189 --> 04:17:19,110
interfaces in Java interfaces are like a contract
between two parts of your application. Let's
2670
04:17:19,110 --> 04:17:24,020
see what I mean by that you can create an
interface like you have created a Java class,
2671
04:17:24,020 --> 04:17:30,520
you can right click on your package by selecting
new Java class. And then in here, by selecting
2672
04:17:30,520 --> 04:17:37,180
interface, you can have an interface Let's
name this interface card interface. If you
2673
04:17:37,180 --> 04:17:41,870
take a look at your package, you can see that
we have created this interface. And if you
2674
04:17:41,870 --> 04:17:46,780
take a closer look, you can see that the icon
for this interface is somehow different from
2675
04:17:46,780 --> 04:17:54,060
the icon of classes in Java. This is the general
schematic of an interface in Java, like classes,
2676
04:17:54,060 --> 04:17:59,880
interfaces, lives inside the package, they
have this public access modifier, we can name
2677
04:17:59,880 --> 04:18:05,330
our interfaces, whatever we want. And also
they have this interface keyword in their
2678
04:18:05,330 --> 04:18:11,930
declaration. Inside interfaces, we can define
our abstract methods, let's quickly see an
2679
04:18:11,930 --> 04:18:18,939
abstract method. I can say public, let's say
void. After that, let's say print name. As
2680
04:18:18,939 --> 04:18:23,970
the input of this method, let's just receive
a name, let's say a string name. And that's
2681
04:18:23,970 --> 04:18:29,160
it, you cannot have a body for your abstract
methods. For example, in here, if I put a
2682
04:18:29,160 --> 04:18:34,050
pair of curly braces, you can see that I'm
getting a red warning. And the warning says
2683
04:18:34,050 --> 04:18:39,770
that interface abstract methods cannot have
body. So I can delete these two curly braces
2684
04:18:39,770 --> 04:18:46,130
and I can finish my sentence in here. By using
abstract methods you are hiding the functionality
2685
04:18:46,130 --> 04:18:50,760
of your methods. For example, in this case,
we don't know what this print name method
2686
04:18:50,760 --> 04:18:57,290
does. Instead, we are just defining some sort
of contract. The contract in here is the signature
2687
04:18:57,290 --> 04:19:02,050
of our method, we know that the name of this
method is print name, we know that we are
2688
04:19:02,050 --> 04:19:07,220
receiving a string called name. And also we
know that the return type of this method is
2689
04:19:07,220 --> 04:19:13,140
void. The signature of this method is a contract
in which ferrovie implements this interface,
2690
04:19:13,140 --> 04:19:17,730
we are going to use this signature. Also if
you take a look at this public keyword, you
2691
04:19:17,730 --> 04:19:22,420
can see that it's somehow grayed out. If you
hover over that keyword, you can see that
2692
04:19:22,420 --> 04:19:27,960
modifier public is redundant for interface
methods, it means that it's not necessary
2693
04:19:27,960 --> 04:19:33,899
in here. And the reason for that is because
all of the abstract methods are public inside
2694
04:19:33,899 --> 04:19:39,990
an interface. It means that you cannot have
private or protected abstract methods. Inside
2695
04:19:39,990 --> 04:19:44,830
your interface. You can have as many abstract
methods as you want. For example, if I want
2696
04:19:44,830 --> 04:19:51,530
to define another one in here, I can say void.
Let's name this one to start. And I'm not
2697
04:19:51,530 --> 04:19:56,070
going to receive anything as the input of
this method. Let's define another one. Let's
2698
04:19:56,070 --> 04:20:02,780
name the spawn move. And also let's receive
an integer call. Speed, I created the first
2699
04:20:02,780 --> 04:20:07,330
method so that I can show you how you can
create abstract methods. And I'm going to
2700
04:20:07,330 --> 04:20:13,220
delete that in here. So this car interface
has two methods start and move. But how can
2701
04:20:13,220 --> 04:20:18,450
we use this interface? Let's quickly see that
as well. Inside my package, I'm going to create
2702
04:20:18,450 --> 04:20:25,030
a Java class called electric car. And inside
this electric car, I'm going to implement
2703
04:20:25,030 --> 04:20:29,880
that interface that I just created. In order
to implement that interface, I am going to
2704
04:20:29,880 --> 04:20:35,530
say implements at the declaration of my class.
And after that, I need to provide the name
2705
04:20:35,530 --> 04:20:40,590
of my interface. In this case, it's called
interface. But once we do that, you can see
2706
04:20:40,590 --> 04:20:45,460
that I'm getting a red warning in here. And
for this red warning, we need to implement
2707
04:20:45,460 --> 04:20:50,311
the two methods that we have created inside
our interface. In order to implement those
2708
04:20:50,311 --> 04:20:55,990
two methods, we have multiple options. First
of all, we can click on this red light bar.
2709
04:20:55,990 --> 04:21:01,570
By selecting implement methods, we can implement
the necessary methods. Or alternatively, if
2710
04:21:01,570 --> 04:21:06,570
you don't see this red light, well, you can
click on the error and by pressing ALT plus
2711
04:21:06,570 --> 04:21:12,080
enter, you can see the same dialog or the
other option is to come inside the declaration
2712
04:21:12,080 --> 04:21:17,851
of your class. And by pressing Ctrl plus i,
you can see the list of necessary methods.
2713
04:21:17,851 --> 04:21:23,609
In this case, we have a start and move methods.
Also, if you press Ctrl plus or you can see
2714
04:21:23,609 --> 04:21:27,899
the list of all of the methods that you can
override. In here, you need to be a little
2715
04:21:27,899 --> 04:21:32,979
bit careful because all of these methods are
none necessary. The methods that are necessary
2716
04:21:32,979 --> 04:21:37,729
are these two methods. If you take a closer
look at the icon of these two methods, you
2717
04:21:37,729 --> 04:21:43,150
can see that the icon is somehow not completed.
These are the two methods that we need to
2718
04:21:43,150 --> 04:21:49,320
implement or override. Let's select them.
And in here we have a body for this start
2719
04:21:49,320 --> 04:21:54,051
and move method, it means that we can define
the functionality for these two methods in
2720
04:21:54,051 --> 04:21:59,280
here. For example, in the start case, I'm
just going to print something. Because it's
2721
04:21:59,280 --> 04:22:05,740
an electrical car, I'm going to say electricity
flow started. Let's also define a field for
2722
04:22:05,740 --> 04:22:13,010
this class. I'm going to save private string
name. We can also have a constructor by pressing
2723
04:22:13,010 --> 04:22:19,020
ALT plus insert, let's create that constructor.
And let's also create getters and setters.
2724
04:22:19,020 --> 04:22:24,550
down in here, let's also define some functionality
for this move method. Once again, I'm going
2725
04:22:24,550 --> 04:22:31,840
to print something, let's say this that getname
plus moves at some speed.
2726
04:22:31,840 --> 04:22:37,280
Okay, now that we have created this class
in which implements our interface, how can
2727
04:22:37,280 --> 04:22:43,360
we instantiate our interface for that we can
switch to our main class. And inside this
2728
04:22:43,360 --> 04:22:50,141
main class, we can say car interface. Let's
name it car interface is equal to in here
2729
04:22:50,141 --> 04:22:56,430
I have two options. First of all, I can say
new car interface like this for Java classes.
2730
04:22:56,430 --> 04:23:01,471
But if I do that, you can see that we are
overriding these two methods. Sometimes this
2731
04:23:01,471 --> 04:23:05,624
might be our desired behavior. But in here,
I'm not going to use this way. Instead, I'm
2732
04:23:05,624 --> 04:23:06,624
going to say is equal to new electric car.
This one, let's also pass a name, let's say
2733
04:23:06,624 --> 04:23:07,624
Tesla, you can see that we are assigning a
class to an interface. But how is it working?
2734
04:23:07,624 --> 04:23:08,624
Well, because when we have created our class,
we implemented this interface. Now Java compiler
2735
04:23:08,624 --> 04:23:09,624
is happy with assigning a class to this interface.
Now that we have instantiated this interface,
2736
04:23:09,624 --> 04:23:10,624
we can use its methods. For example, I can
say car interface dots, let's say start. And
2737
04:23:10,624 --> 04:23:11,624
also let's say car interface dot move, we
also need to pass a speed, let's pass 60.
2738
04:23:11,624 --> 04:23:12,624
Let's run the application, you can see that
electricity flow started and Tesla is moving
2739
04:23:12,624 --> 04:23:13,624
at 60 miles per hour. You can see that even
though inside our interface, we didn't have
2740
04:23:13,624 --> 04:23:14,624
any body. But we are printing some functionality.
When we call the start method on our interface.
2741
04:23:14,624 --> 04:23:15,624
If you want to make sure that this start method
is happening inside your class. You can also
2742
04:23:15,624 --> 04:23:16,624
print something else in here for example,
inside this start method, let's print the
2743
04:23:16,624 --> 04:23:17,624
name of this class. I'm going to pass this
if you remember I said that this refers to
2744
04:23:17,624 --> 04:23:18,624
the current object. In this case, it's referring
to this class and right now if I print this,
2745
04:23:18,624 --> 04:23:19,624
it will print the address of our class. Let's
run the application. You can see the address
2746
04:23:19,624 --> 04:23:20,624
of our electric car class. It means that this
start method is happening inside our electric
2747
04:23:20,624 --> 04:23:21,624
car class and not inside the interface. If
you remember when we have talked about collections,
2748
04:23:21,624 --> 04:23:22,624
we have defined array lists in two ways. One
of those ways was to define our array list
2749
04:23:22,624 --> 04:23:23,624
as list for example, because I've said list
which once again you can see that is an interface.
2750
04:23:23,624 --> 04:23:24,624
For example, the stuff a string, let's name
it names is equal to new ArrayList Probably
2751
04:23:24,624 --> 04:23:25,624
now you understand this syntax in here, it
means that we have this ArrayList class in
2752
04:23:25,624 --> 04:23:26,624
which implements this list interface. If you
want to make sure of that and see it for yourself,
2753
04:23:26,624 --> 04:23:27,624
you can press down the CTRL key on Windows,
I believe it's Command key on Mac. And by
2754
04:23:27,624 --> 04:23:28,624
clicking on this ArrayList, you can see the
documentation for your ArrayList class. Let's
2755
04:23:28,624 --> 04:23:29,624
scroll a bit above in here inside the declaration
of our ArrayList class, you can see that we
2756
04:23:29,624 --> 04:23:30,624
are implementing this list. Don't worry about
this in here, it means that we are going to
2757
04:23:30,624 --> 04:23:31,624
accept any kind of object but as you can see
if you are implementing this Okay, let's move
2758
04:23:31,624 --> 04:23:32,624
on from here. Now that we have our interface,
we can also have another class in which implements
2759
04:23:32,624 --> 04:23:33,624
this interface. Let's quickly create another
class, let's say new Java class. Let's name
2760
04:23:33,624 --> 04:23:34,624
this phone fossil fuel car. Once again, in
here, I'm going to say implements. Let's say
2761
04:23:34,624 --> 04:23:35,624
car interface, we need to implement two methods.
Let's press Ctrl I, and let's select our two
2762
04:23:35,624 --> 04:23:36,624
methods. Once again, let's define some functionalities
for these two methods. For example, let's
2763
04:23:36,624 --> 04:23:37,624
bring something in here. Let's say explosion
in cylinder cause the engine to start. Before
2764
04:23:37,624 --> 04:23:38,624
I define some functionality for this move
method, let's quickly add a field above in
2765
04:23:38,624 --> 04:23:39,624
here like before, I'm going to say private,
the string name. Let's create the constructor
2766
04:23:39,624 --> 04:23:40,624
and also getters and setters. After that insight,
the move method, I'm going to say this car
2767
04:23:40,624 --> 04:23:41,624
is moving at some speed. Now that we have
created this class, we can instantiate our
2768
04:23:41,624 --> 04:23:42,624
interface inside our main class, like this,
let's delete this line of code. In here I
2769
04:23:42,624 --> 04:23:43,624
can say car interface. Once again, let's name
this one fossil car interface.
2770
04:23:43,624 --> 04:23:44,624
Like before, I can say new fossil fuel car.
Let's pass Mercedes in here, for example.
2771
04:23:44,624 --> 04:23:45,624
Once again, you can see that Java compiler
is happy with this way of instantiating our
2772
04:23:45,624 --> 04:23:46,624
interface because we have implemented this
car interface inside both electric car class
2773
04:23:46,624 --> 04:23:47,624
and also fossil fuel car class, we can instantiate
our car interface in both of these ways. And
2774
04:23:47,624 --> 04:23:48,624
after that, I can say fosu car interface dot
start. And let's see fossil car interface
2775
04:23:48,624 --> 04:23:49,624
dot move. Let's also pass some speed in here
and let's run our application. You can see
2776
04:23:49,624 --> 04:23:50,624
that both of these methods have been called
in here, I have a point by defining two classes
2777
04:23:50,624 --> 04:23:51,624
and implement the same interface in both of
these classes. My point is that when you create
2778
04:23:51,624 --> 04:23:52,624
an interface, that interface is a contract
between different parts of your application.
2779
04:23:52,624 --> 04:23:53,624
For example, in this case, I'm defining multiple
points of cars. No matter what kind the car
2780
04:23:53,624 --> 04:23:54,624
is, the contract says that discourse should
have a start and also a move method. So probably
2781
04:23:54,624 --> 04:23:55,624
by now you can see the usage of interfaces.
But there are a lot more two interfaces. For
2782
04:23:55,624 --> 04:23:56,624
example, we can use something called callback
interfaces. callback interfaces are extremely
2783
04:23:56,624 --> 04:23:57,624
useful for when you create event listeners
like clique listeners. And also they are useful
2784
04:23:57,624 --> 04:23:58,624
for when you want to create some sort of connection
between two different threads. We will talk
2785
04:23:58,624 --> 04:23:59,624
about threads in the next video, don't worry
about that, we will see the use of interfaces
2786
04:23:59,624 --> 04:24:00,624
when we want to create a communication between
two different threads. Also, it's worth noticing
2787
04:24:00,624 --> 04:24:01,624
that you can implement multiple interfaces
at the declaration of your class. For example,
2788
04:24:01,624 --> 04:24:02,624
let's quickly create another interface in
here, I'm going to name this one test interface.
2789
04:24:02,624 --> 04:24:03,624
For this, let's just have a method in here.
Let's say sorry, for private, the abstract
2790
04:24:03,624 --> 04:24:04,624
methods cannot be private, let's say void
print name.
2791
04:24:04,624 --> 04:24:05,624
And let's receive a name.
2792
04:24:05,624 --> 04:24:06,624
After I have created this interface inside,
for example, my fossil fuel car, after implementing
2793
04:24:06,624 --> 04:24:07,624
the first interface, I can add a comma. And
after that I can provide the name of my second
2794
04:24:07,624 --> 04:24:08,624
interface. In this case, it's test interface.
But once again, you can see that we are getting
2795
04:24:08,624 --> 04:24:09,624
the error somewhere inside the declaration
of our class, I can press Control plus i and
2796
04:24:09,624 --> 04:24:10,624
i can implement my method. So you can have
multiple interfaces implemented at the declaration
2797
04:24:10,624 --> 04:24:11,624
of your class. Okay, I think that's enough
talking about interfaces. Later on in the
2798
04:24:11,624 --> 04:24:12,624
course we are going to use interfaces a lot
and Dave users will be much more clear. So
2799
04:24:12,624 --> 04:24:13,624
hang in there if something is vague. Okay,
let's delete all of these extra classes and
2800
04:24:13,624 --> 04:24:14,624
extra interfaces. Let's also delete these
lines of codes. Now let's talk about abstract
2801
04:24:14,624 --> 04:24:15,624
methods. For that I'm going to create a new
class inside my package. Let's say new Java
2802
04:24:15,624 --> 04:24:16,624
class, I'm going to name this fun test abstract
class. If you want to make your class abstract,
2803
04:24:16,624 --> 04:24:17,624
you need to add another keyboard between the
access modifier and the keyword class. Let's
2804
04:24:17,624 --> 04:24:18,624
add abstract in here you can see public abstract
class. And immediately after I've added this
2805
04:24:18,624 --> 04:24:19,624
abstract keyword, if you take a look at your
package, you can see that the icon for this
2806
04:24:19,624 --> 04:24:20,624
class has been changed. This means that this
is an abstract class. inside an abstract class
2807
04:24:20,624 --> 04:24:21,624
like interfaces. You can have abstract methods,
let's quickly see them. For example, I can
2808
04:24:21,624 --> 04:24:22,624
say public abstract, let's say void, and let's
name this font print name. Let's receive a
2809
04:24:22,624 --> 04:24:23,624
name string name. Once again, like interfaces.
When you create abstract methods, you cannot
2810
04:24:23,624 --> 04:24:24,624
have a body. For example, if I put a pair
of curly braces once again, I can see the
2811
04:24:24,624 --> 04:24:25,624
warning in here like before I can finish my
sentence with the semicolon. The use of abstract
2812
04:24:25,624 --> 04:24:26,624
classes is a lot like interfaces. But there
are a few slight differences. For example,
2813
04:24:26,624 --> 04:24:27,624
let's see the first difference in here inside
my package, I'm going to define a new class,
2814
04:24:27,624 --> 04:24:28,624
let's name this one test class. When we wanted
to implement some interface, we could have
2815
04:24:28,624 --> 04:24:29,624
said implements. And after that the name of
our interface. But when we want to use abstract
2816
04:24:29,624 --> 04:24:30,624
classes, we can say extends, let's say extends
test abstract class. Right now we are getting
2817
04:24:30,624 --> 04:24:31,624
the warning again by pressing Ctrl plus if
you can see the necessary methods. So the
2818
04:24:31,624 --> 04:24:32,624
first difference is between this extends and
implement keyword. The other difference is
2819
04:24:32,624 --> 04:24:33,624
that when you want to use abstract classes,
you cannot extend multiple abstract classes.
2820
04:24:33,624 --> 04:24:34,624
For example, in here, I cannot add a comma
and add another abstract class in another
2821
04:24:34,624 --> 04:24:35,624
word abstract classes does not support multiple
inheritance. So that's the second difference
2822
04:24:35,624 --> 04:24:36,624
between an interface and abstract class. The
other difference is that inside your abstract
2823
04:24:36,624 --> 04:24:37,624
class, you can also have non abstract methods.
For example, in here, I can say public, let's
2824
04:24:37,624 --> 04:24:38,624
say void. Let's name this method print. Let's
receive something in here. For example, let's
2825
04:24:38,624 --> 04:24:39,624
say a string text. And because this is not
an abstract method, we can have a body for
2826
04:24:39,624 --> 04:24:40,624
this method. We didn't have this option inside
an interface, we can have public non abstract
2827
04:24:40,624 --> 04:24:41,624
methods inside an abstract class. Like every
other class, we can also have fields and maybe
2828
04:24:41,624 --> 04:24:42,624
a constructor as well. For example, let's
say private string name. Let's create a constructor
2829
04:24:42,624 --> 04:24:43,624
for this class, you can see that all is working
well. So the other difference is that you
2830
04:24:43,624 --> 04:24:44,624
can have fields constructors and non abstract
fields. If you want to see the use of abstract
2831
04:24:44,624 --> 04:24:45,624
classes like before we can switch to our main
class. And in here, I can say test abstract
2832
04:24:45,624 --> 04:24:46,624
class. Let's name it test abstract class is
equal to new test class, the class that extends
2833
04:24:46,624 --> 04:24:47,624
artist abstract class. After that, we can
use the methods for example, I can say this
2834
04:24:47,624 --> 04:24:48,624
abstract class dot, let's say print name.
In here, we need to provide a name, let's
2835
04:24:48,624 --> 04:24:49,624
say Mesa, but we didn't have any functionality
inside artists class, let's quickly define
2836
04:24:49,624 --> 04:24:50,624
some functionality in here. Also know that
I've switched to this test class. Because
2837
04:24:50,624 --> 04:24:51,624
we have created a constructor inside our test
abstract class, we need to create the constructor
2838
04:24:51,624 --> 04:24:52,624
in here as well. But for the sake of simplicity,
I'm going to delete all three of these, because
2839
04:24:52,624 --> 04:24:53,624
I just wanted to show that these three options
are available inside an abstract class. Let's
2840
04:24:53,624 --> 04:24:54,624
bring something inside our test class inside
this print method. Let's just print the name.
2841
04:24:54,624 --> 04:24:55,624
Let's run the application and let's see if
we can see Mason printed into our console.
2842
04:24:55,624 --> 04:24:56,624
You can see my Sam has been printed. So once
again you can see that inside artist abstract
2843
04:24:56,624 --> 04:24:57,624
class, we have this abstract method which
does not have anybody instead we have an extended
2844
04:24:57,624 --> 04:24:58,624
this class inside our test class. And we have
declared the functionality inside this test
2845
04:24:58,624 --> 04:24:59,624
class, you can see that there are a lot of
similarities between interfaces and abstract
2846
04:24:59,624 --> 04:25:00,624
classes. Sometimes the decision for using
an abstract class or an interface can be hard
2847
04:25:00,624 --> 04:25:01,624
for that let me show you an Oracle web page
in which may help you to decide better you
2848
04:25:01,624 --> 04:25:02,624
can see the address in here. I think if you
check this webpage, it might be helpful. They
2849
04:25:02,624 --> 04:25:03,624
say that use abstract classes when your classes
are related to each other. Or the other case
2850
04:25:03,624 --> 04:25:04,624
is that when you think that the class that
is going to extend the abstract class may
2851
04:25:04,624 --> 04:25:05,624
use some common methods or fields or also
it may require some access modifiers other
2852
04:25:05,624 --> 04:25:06,624
than public such as private and protected
or the other case is that when you want to
2853
04:25:06,624 --> 04:25:07,624
declare some fields that are non static and
non final, this option is also not available
2854
04:25:07,624 --> 04:25:08,624
in interfaces, but they say that Use interfaces
when your classes are not related to each
2855
04:25:08,624 --> 04:25:09,624
other, or in cases that you want to use multiple
inheritance we can see that when we use interfaces,
2856
04:25:09,624 --> 04:25:10,624
we can implement multiple interfaces. Once
again, I suggest you take a look at this webpage,
2857
04:25:10,624 --> 04:25:11,624
it might be useful. Okay, I think that's enough
for this video. In the next video we are going
2858
04:25:11,624 --> 04:25:12,624
to talk about concurrency and threading in
Java See you in the next video.
2859
04:25:12,624 --> 04:25:13,624
In this video, we are going to talk about
concurrency. In Java. concurrency means doing
2860
04:25:13,624 --> 04:25:14,624
things simultaneously and at the same time.
For example, imagine that you're working with
2861
04:25:14,624 --> 04:25:15,624
your phone and you're reading a book at the
same time, you may want to listen to some
2862
04:25:15,624 --> 04:25:16,624
music simultaneously, you may be downloading
some files from internet This is called concurrency
2863
04:25:16,624 --> 04:25:17,624
or doing things at the same time in programming.
In Java. The first option that we have in
2864
04:25:17,624 --> 04:25:18,624
order to create a concurrent programming is
to create a thread a thread is a unit of execution
2865
04:25:18,624 --> 04:25:19,624
in your device's central processing unit or
CPU. Basically, different threads come together
2866
04:25:19,624 --> 04:25:20,624
inside something called a process. All of
these processes will be handled by your device's
2867
04:25:20,624 --> 04:25:21,624
CPU. By default, when you run your Java application
or for that matter, your Android application,
2868
04:25:21,624 --> 04:25:22,624
a thread called the main thread will be created.
Also, some might call this main thread the
2869
04:25:22,624 --> 04:25:23,624
UI thread or the user interface thread as
well. Later on, if you want to do some background
2870
04:25:23,624 --> 04:25:24,624
task, for example, downloading a file, you
will create a worker thread inside your main
2871
04:25:24,624 --> 04:25:25,624
thread. The first option in Java that you
have in order to create a thread is like this.
2872
04:25:25,624 --> 04:25:26,624
You can say thread, let's name this thread
is equal to new threat. Like the finding any
2873
04:25:26,624 --> 04:25:27,624
other Java class. Let's finish our sentence
in here. The constructor of this threat requires
2874
04:25:27,624 --> 04:25:28,624
an interface and that interface is the runnable
interface, we can pass that like this, we
2875
04:25:28,624 --> 04:25:29,624
can say new runnable, you can see that this
runnable is an interface. When you pass this
2876
04:25:29,624 --> 04:25:30,624
runnable interface, this run method will be
created. Basically this run method is the
2877
04:25:30,624 --> 04:25:31,624
place that you put the code that you want
to be handled inside of worker thread. For
2878
04:25:31,624 --> 04:25:32,624
example, let's print something in here. And
let's see if we can have a complete application.
2879
04:25:32,624 --> 04:25:33,624
For example, let's say hello from another
thread. In order to start this thread, we
2880
04:25:33,624 --> 04:25:34,624
need to come down after the declaration of
our thread and say thread dot start, let's
2881
04:25:34,624 --> 04:25:35,624
run our application. And let's see if we can
see any result, you can see that our message
2882
04:25:35,624 --> 04:25:36,624
has been printed into the console. But how
do we see the difference between the main
2883
04:25:36,624 --> 04:25:37,624
thread and this worker thread for that I am
going to write a piece of logic in here. For
2884
04:25:37,624 --> 04:25:38,624
example, inside this run method, I'm going
to create a for loop. Let's say for int i
2885
04:25:38,624 --> 04:25:39,624
is equal to zero, i less than, let's say five,
and i plus plus. Inside this for loop, I'm
2886
04:25:39,624 --> 04:25:40,624
going to print the value of this I and after
that I'm going to asleep the thread we will
2887
04:25:40,624 --> 04:25:41,624
see how we can asleep the current thread.
First of all, let's say print printing, let's
2888
04:25:41,624 --> 04:25:42,624
say plus I plus, let's say in a worker thread.
In order to asleep your thread, you have multiple
2889
04:25:42,624 --> 04:25:43,624
options. The first one and I believe the easiest
one is to say something like this you can
2890
04:25:43,624 --> 04:25:44,624
say thread with a capital T dot sleep. Inside
the parentheses of this sleep method you need
2891
04:25:44,624 --> 04:25:45,624
to pass some numbers in milliseconds. For
example, if I want to sleep my tread for one
2892
04:25:45,624 --> 04:25:46,624
second, I need to pass 1000 milliseconds this
asleep method is going to cause our loop to
2893
04:25:46,624 --> 04:25:47,624
wait one second for the next record for example
in here we are going to print zero after that
2894
04:25:47,624 --> 04:25:48,624
we are going to wait one second. And after
that we are going to print one but right now
2895
04:25:48,624 --> 04:25:49,624
as you can see, we are getting a warning in
here and the warning says that unhandled exception
2896
04:25:49,624 --> 04:25:50,624
Java dot Lang dot interrupted exception. In
the previous videos briefly, we have talked
2897
04:25:50,624 --> 04:25:51,624
about exceptions. We will talk about them
in this video later on. But for now, you just
2898
04:25:51,624 --> 04:25:52,624
need to know that whenever an exception occurs,
your application would crash. So because of
2899
04:25:52,624 --> 04:25:53,624
that, we need to do something for this error
in here. The easiest way to overcome this
2900
04:25:53,624 --> 04:25:54,624
error in here is to click on the error and
press ALT plus insert. After that, select
2901
04:25:54,624 --> 04:25:55,624
this surround with try catch, you can see
that this code has been added and our thread
2902
04:25:55,624 --> 04:25:56,624
dot sleep method has been moved to this try
block later on. We will talk about this. But
2903
04:25:56,624 --> 04:25:57,624
for now let's move on from this part. And
let's write the same logic inside our main
2904
04:25:57,624 --> 04:25:58,624
thread so that we can see that the code inside
the worker thread and the main thread can
2905
04:25:58,624 --> 04:25:59,624
be executed at the same time. So basically,
I'm going to copy this for loop. Let's copy
2906
04:25:59,624 --> 04:26:00,624
it from here and inside the main thread outside
of this run method. Let's base the button
2907
04:26:00,624 --> 04:26:01,624
here. I'm just going to change this message
to main thread. Let's run the application
2908
04:26:01,624 --> 04:26:02,624
and let's see If this code is going to work,
as you can see, we are printing the value
2909
04:26:02,624 --> 04:26:03,624
of oil two times. Once in a worker thread,
and once in the main thread, it means that
2910
04:26:03,624 --> 04:26:04,624
the code inside the worker thread and the
code inside the main thread are happening
2911
04:26:04,624 --> 04:26:05,624
at the same time. In this situation, we say
that our application is asynchronous, or it
2912
04:26:05,624 --> 04:26:06,624
is concurrent. So once again, when you run
your Java or Android application, one thread
2913
04:26:06,624 --> 04:26:07,624
will be created by default, in which we call
that thread the main thread or the UI thread.
2914
04:26:07,624 --> 04:26:08,624
After that inside that main thread, you can
create as many worker thread as you want.
2915
04:26:08,624 --> 04:26:09,624
The simplest way to create a thread is to
say thread thread is equal to new thread.
2916
04:26:09,624 --> 04:26:10,624
And after that, we can pass a new runnable
to the constructor of your thread in which
2917
04:26:10,624 --> 04:26:11,624
we will create this run method that will be
executed in a background thread. But by this
2918
04:26:11,624 --> 04:26:12,624
way of defining your thread, there are a lot
of considerations that you need to be worried
2919
04:26:12,624 --> 04:26:13,624
about. For example, you need to be worried
about cancelling your thread when the work
2920
04:26:13,624 --> 04:26:14,624
is done. Also, there are all sorts of new
concepts, for example, thread pools, deadlocks
2921
04:26:14,624 --> 04:26:15,624
and interrupting your thread. I do not intend
to talk about all of those concepts in here,
2922
04:26:15,624 --> 04:26:16,624
because in Android, we have all sorts of options
available for handling background tasks. In
2923
04:26:16,624 --> 04:26:17,624
fact, later on in the course, we have a complete
section dedicated to handling background tasks.
2924
04:26:17,624 --> 04:26:18,624
In Android, we have options like async, task
services, Job Scheduler, and work manager,
2925
04:26:18,624 --> 04:26:19,624
we are going to talk about all of them. But
more on that in future videos. Okay, now let's
2926
04:26:19,624 --> 04:26:20,624
talk about this interrupted exception that
is happening in here. Basically, whenever
2927
04:26:20,624 --> 04:26:21,624
an exception occurs, your application will
crash and your application will be closed
2928
04:26:21,624 --> 04:26:22,624
by the operating system. There are all sorts
of exceptions that can occur. For example,
2929
04:26:22,624 --> 04:26:23,624
the ones that we have seen previously, were
arithmetic exception, null pointer exception,
2930
04:26:23,624 --> 04:26:24,624
and the one that we can see in here is interrupted
exception. So in order to avoid crashes, you
2931
04:26:24,624 --> 04:26:25,624
need to do something about the exception.
Let's comment all of these and let's see what
2932
04:26:25,624 --> 04:26:26,624
we can do about the exceptions.
2933
04:26:26,624 --> 04:26:27,624
Let's create an application in which we will
create an arithmetic exception. In here I'm
2934
04:26:27,624 --> 04:26:28,624
going to define two new integers. For example,
let's say int, a is equal to two, and int
2935
04:26:28,624 --> 04:26:29,624
b is equal to zero. Let's bring something
in here. Let's say print a divided by b. If
2936
04:26:29,624 --> 04:26:30,624
we run our application, right, now, we should
see an arithmetic exception. You can see that
2937
04:26:30,624 --> 04:26:31,624
down in here. The thing that we can do about
this exception is that to catch it whenever
2938
04:26:31,624 --> 04:26:32,624
it happens, and we can do that by using a
try catch block. Let's quickly see that for
2939
04:26:32,624 --> 04:26:33,624
example, I can say try. Let's move this line
of code to inside our try block. We can say
2940
04:26:33,624 --> 04:26:34,624
try this in case some exception happens catch
that exception. The kind of exception was
2941
04:26:34,624 --> 04:26:35,624
arithmetic exception. So let's say arithmetic
exception, let's name this exception E. And
2942
04:26:35,624 --> 04:26:36,624
inside the catch block, we can do something
else with our code. For example, we can print
2943
04:26:36,624 --> 04:26:37,624
something. In this case, let's say B was zero.
Now, if we run our application, we wouldn't
2944
04:26:37,624 --> 04:26:38,624
get an exception. Instead, we are printing
this message. Sometimes this message might
2945
04:26:38,624 --> 04:26:39,624
not to be useful. And in fact, in most cases,
this message will be useful for debugging
2946
04:26:39,624 --> 04:26:40,624
purposes, but it's much better than a crash.
The kind of exception that we are catching
2947
04:26:40,624 --> 04:26:41,624
in here is arithmetic exception. If we do
not know the kind of exception, we can pass
2948
04:26:41,624 --> 04:26:42,624
the generic term exception. If you pass this
exception, it's going to accept all of the
2949
04:26:42,624 --> 04:26:43,624
exceptions. If we run our application, once
again, we should see the same result. And
2950
04:26:43,624 --> 04:26:44,624
here is the same result. The other kind of
exception that we have seen so far was null
2951
04:26:44,624 --> 04:26:45,624
pointer exception that would occur when we
do some operation on a null object reference.
2952
04:26:45,624 --> 04:26:46,624
For example, if we had a string in here, let's
say a string name is equal to Now, if we do
2953
04:26:46,624 --> 04:26:47,624
some operation on our name inside the try
block, for example, if we say name, dot equals,
2954
04:26:47,624 --> 04:26:48,624
let's say, Mesa, if we do that, we are going
to get a nullpointerexception. Because we
2955
04:26:48,624 --> 04:26:49,624
have done this dot equals method on a null
object inside the catch block this time, instead
2956
04:26:49,624 --> 04:26:50,624
of saying B was zero, let's say name was no.
If you run the application, it's going to
2957
04:26:50,624 --> 04:26:51,624
work fine because we are passing the generic
term exception, but we can safely pass null
2958
04:26:51,624 --> 04:26:52,624
pointer exception. Let's run the application.
You can see that name was no the other kind
2959
04:26:52,624 --> 04:26:53,624
of exception that we just saw was this interrupted
exception. It's going to happen whenever we
2960
04:26:53,624 --> 04:26:54,624
interrupt the current thread. For example,
imagine that inside a worker thread, you are
2961
04:26:54,624 --> 04:26:55,624
downloading some file at the same time, you
may receive a phone call. If you don't catch
2962
04:26:55,624 --> 04:26:56,624
the interrupted exception, you may get a crash.
We have all sorts of exceptions. For example,
2963
04:26:56,624 --> 04:26:57,624
we have audio exception, which stands for
input output exception We have class cast
2964
04:26:57,624 --> 04:26:58,624
exception, we have all sorts of other exceptions
in which we are going to talk about them whenever
2965
04:26:58,624 --> 04:26:59,624
we face them. Also on our exception, we have
a useful method for debugging purposes. And
2966
04:26:59,624 --> 04:27:00,624
that method is called print stack trace, we
can see the name of our exception dot print
2967
04:27:00,624 --> 04:27:01,624
a stack trace, we will talk about this stack
in future videos when we talk about activities
2968
04:27:01,624 --> 04:27:02,624
and fragments in Android. But for now, just
know that this line of code in here is going
2969
04:27:02,624 --> 04:27:03,624
to print something into our console that might
be useful when you're debugging your application.
2970
04:27:03,624 --> 04:27:04,624
Let's run the application in here. You can
see the stack trace of your exception in here
2971
04:27:04,624 --> 04:27:05,624
it says that the exception occurred in line
34, which is this line, and you're also seeing
2972
04:27:05,624 --> 04:27:06,624
the name of your exception, this is not an
actual exception, you're just seeing some
2973
04:27:06,624 --> 04:27:07,624
information about your exception, this in
here might be useful for when you're debugging
2974
04:27:07,624 --> 04:27:08,624
your application. Okay, I think that's enough
talking about concurrency and exceptions in
2975
04:27:08,624 --> 04:27:09,624
Java. In the next video, we are going to talk
about Singleton pattern in Java. See you in
2976
04:27:09,624 --> 04:27:10,624
the next video. In this video, we are going
to talk about Singleton pattern. Basically,
2977
04:27:10,624 --> 04:27:11,624
we use Singleton pattern when we want to make
sure that we have only one instance of some
2978
04:27:11,624 --> 04:27:12,624
class in our entire application. In some cases,
we may want such a thing. And here is how
2979
04:27:12,624 --> 04:27:13,624
we can implement that pattern. For example,
one case of using Singleton pattern is when
2980
04:27:13,624 --> 04:27:14,624
you are using a database in your application.
Don't worry about the databases, we have a
2981
04:27:14,624 --> 04:27:15,624
complete section dedicated to them. But in
here, we are just going to create a simple
2982
04:27:15,624 --> 04:27:16,624
Java class named database. Let's quickly create
that inside our source folder inside our package.
2983
04:27:16,624 --> 04:27:17,624
And we will see how we can implement that
patter. Let's name this class database. For
2984
04:27:17,624 --> 04:27:18,624
this database class, I'm going to define a
field let's say private string name. Also,
2985
04:27:18,624 --> 04:27:19,624
let's create a constructor and also getters
and setters.
2986
04:27:19,624 --> 04:27:20,624
Right now, because we have this public constructor,
we can create as many instance as we want
2987
04:27:20,624 --> 04:27:21,624
from this database class. But if you want
to make sure that you have only one instance
2988
04:27:21,624 --> 04:27:22,624
of this class in your entire application,
you can create another field in here, for
2989
04:27:22,624 --> 04:27:23,624
example, I'm going to say private, static
as the data type of this field, I'm going
2990
04:27:23,624 --> 04:27:24,624
to specify the class that we are in for example,
in this case, I'm going to say database. So
2991
04:27:24,624 --> 04:27:25,624
let's say private static database, make sure
to import the one that comes from our package
2992
04:27:25,624 --> 04:27:26,624
or dot maker dot Singleton pattern. For the
name of this field, it's convention to name
2993
04:27:26,624 --> 04:27:27,624
it instance. And that's it. Now we can create
a getter method for this field. And here is
2994
04:27:27,624 --> 04:27:28,624
how we can create that field, we can say public,
because we are going to use this instance
2995
04:27:28,624 --> 04:27:29,624
inside this field, I'm going to say static,
the return type of this getter method is going
2996
04:27:29,624 --> 04:27:30,624
to be the class itself. So I'm going to say
database. Let's name this method. Get instance,
2997
04:27:30,624 --> 04:27:31,624
as you can see the suggestion in here and
let's have a body for this method. In here,
2998
04:27:31,624 --> 04:27:32,624
instead of just simply passing this instance,
I'm going to create an if statement, I'm going
2999
04:27:32,624 --> 04:27:33,624
to say if let's say now, is equal to our instance,
if that's the case, I'm going to create a
3000
04:27:33,624 --> 04:27:34,624
new instance, I'm going to say instance, is
equal to new database. Notice that we need
3001
04:27:34,624 --> 04:27:35,624
a name for the constructor of this database.
So I'm going to receive that name via the
3002
04:27:35,624 --> 04:27:36,624
input of this method in here, let's say the
string name. And later on, let's first this
3003
04:27:36,624 --> 04:27:37,624
naming here. So if our instance is now we
are going to create a new instance, and after
3004
04:27:37,624 --> 04:27:38,624
creating that we are going to return it, we
can say return instance. But in the other
3005
04:27:38,624 --> 04:27:39,624
case, in case if our instance is not not in
the else case, I'm just going to simply return
3006
04:27:39,624 --> 04:27:40,624
instance, right now if we use this get instance
method, instead of this constructor, we are
3007
04:27:40,624 --> 04:27:41,624
sure that we have only one instance of our
database class in our entire application.
3008
04:27:41,624 --> 04:27:42,624
For example, if we go to the main class, and
if we try to instantiate our database, we
3009
04:27:42,624 --> 04:27:43,624
can have only one instance we can say database,
let's name the database is equal to database
3010
04:27:43,624 --> 04:27:44,624
with capital D dot get instance, of course,
we need the naming here. Let's pass a name.
3011
04:27:44,624 --> 04:27:45,624
For example, let's say DB music. Notice that
because we have declared this get instance
3012
04:27:45,624 --> 04:27:46,624
method as a static, now we can use this database
class with this capital D, it means that we
3013
04:27:46,624 --> 04:27:47,624
are not relying on the instance of this database
class. Instead, we are using the object itself.
3014
04:27:47,624 --> 04:27:48,624
But right now someone can declare the database
class as we did for any other class. For example,
3015
04:27:48,624 --> 04:27:49,624
in here, someone can say database. Let's name
this one test database
3016
04:27:49,624 --> 04:27:50,624
is equal to new database because in our database
class, we have the constructor, we can pass
3017
04:27:50,624 --> 04:27:51,624
a name in here and if we do that, we have
another instance of this database class. For
3018
04:27:51,624 --> 04:27:52,624
example, let's say DB movies. In order to
restrict users from using the constructor,
3019
04:27:52,624 --> 04:27:53,624
I'm going to change the access modifier of
this constructor inside my database class,
3020
04:27:53,624 --> 04:27:54,624
I'm going to change this public to private,
so that this constructor would be available
3021
04:27:54,624 --> 04:27:55,624
only from inside this class and not from inside
any other class like this main class. Right
3022
04:27:55,624 --> 04:27:56,624
now we have an error in here and the error
says that the constructor is private. So by
3023
04:27:56,624 --> 04:27:57,624
changing the access modifier from public to
private, now, we are restricting others from
3024
04:27:57,624 --> 04:27:58,624
creating multiple instances of our database
class. But because our constructor inside
3025
04:27:58,624 --> 04:27:59,624
our database is private, we can use this constructor
inside this get instance method, as you can
3026
04:27:59,624 --> 04:28:00,624
see in line 10, in here, right now, because
of this static keyword. And also because of
3027
04:28:00,624 --> 04:28:01,624
this, if statement that we have put in here,
we are sure that we have only one instance
3028
04:28:01,624 --> 04:28:02,624
of our database class. But there is also another
point that we need to be worried about. And
3029
04:28:02,624 --> 04:28:03,624
that's when someone tries to create multiple
instances of our database from multiple threads.
3030
04:28:03,624 --> 04:28:04,624
In some cases, if someone tries to create
multiple instances of our class asynchronously,
3031
04:28:04,624 --> 04:28:05,624
they might be successful in doing such a thing.
In order to make our get instance method thread
3032
04:28:05,624 --> 04:28:06,624
safe, we have an option in here after this
static keyword, we can add another keyword
3033
04:28:06,624 --> 04:28:07,624
in which we haven't seen so far, we can say
synchronized, this synchronized keyword will
3034
04:28:07,624 --> 04:28:08,624
make our get instance method thread safe,
it means that no more than one thread can
3035
04:28:08,624 --> 04:28:09,624
call this get instance at the same time. So
this is the whole Singleton pattern. First
3036
04:28:09,624 --> 04:28:10,624
of all, you declare a static field of the
kind of your class for example, in this case
3037
04:28:10,624 --> 04:28:11,624
database, after that, you create a get instance
method in which is a static once again and
3038
04:28:11,624 --> 04:28:12,624
also synchronized. Inside that get instance
method you are checking that if the instance
3039
04:28:12,624 --> 04:28:13,624
of your database is now or not, if it is not,
it means that it's the first time that you
3040
04:28:13,624 --> 04:28:14,624
are calling this get instance method. So you
need to create a new instance like we are
3041
04:28:14,624 --> 04:28:15,624
doing in line 10. After that, we are returning
the instance but in the else case, we are
3042
04:28:15,624 --> 04:28:16,624
not going to create a new instance, we are
just going to return the existing instance
3043
04:28:16,624 --> 04:28:17,624
beside this get instance method. In order
to restrict others from using the constructor,
3044
04:28:17,624 --> 04:28:18,624
you need to change the access modifier of
your constructor to private so that it would
3045
04:28:18,624 --> 04:28:19,624
be usable only inside your database class.
We can also make these if statements shorter
3046
04:28:19,624 --> 04:28:20,624
for example, in here, I can delete this whole
else case. And I can delete this return instance
3047
04:28:20,624 --> 04:28:21,624
from line 11 in here, and after the if statement,
I can return that instance. So by the time
3048
04:28:21,624 --> 04:28:22,624
we reach to this line 13 in which we are returning
the instance, we are sure that our instance
3049
04:28:22,624 --> 04:28:23,624
is not not and we are returning either a new
instance or the existing instance. Okay, I
3050
04:28:23,624 --> 04:28:24,624
think that's enough talking about the singleton
pattern later on in the course, you will see
3051
04:28:24,624 --> 04:28:25,624
that this pattern might be useful from time
to time. Before I finish off this video, I'm
3052
04:28:25,624 --> 04:28:26,624
going to talk about one more methods available
in every class in Java. And that method is
3053
04:28:26,624 --> 04:28:27,624
called to string right now we don't have any
to string method inside our database class.
3054
04:28:27,624 --> 04:28:28,624
But if we switch back to the main class, and
if we say print database.to string, you can
3055
04:28:28,624 --> 04:28:29,624
see that we have such a method. And if we
run our application, we should see some information
3056
04:28:29,624 --> 04:28:30,624
about our database class. Let's quickly run
it and let's see if we can see any result.
3057
04:28:30,624 --> 04:28:31,624
You can see that we are getting the address
of our database class. If you want, you can
3058
04:28:31,624 --> 04:28:32,624
change this for example inside your database
class, you can override the two string methods
3059
04:28:32,624 --> 04:28:33,624
down in here inside the scope of our class,
we can press Ctrl plus or and from here we
3060
04:28:33,624 --> 04:28:34,624
can select this two string method. Right now
we are returning the super statement. If you
3061
04:28:34,624 --> 04:28:35,624
want to change that we can delete this whole
statement. And we can prepare a string in
3062
04:28:35,624 --> 04:28:36,624
here. For example, we can say a string text
is equal to let's say database class. After
3063
04:28:36,624 --> 04:28:37,624
that, let's add a line. And after that, let's
print the name of our database.
3064
04:28:37,624 --> 04:28:38,624
Let's say name plus the start name. After
creating this text, we can return for example,
3065
04:28:38,624 --> 04:28:39,624
we can say return text. Now that we have overrided,
this two string method, if we run our application,
3066
04:28:39,624 --> 04:28:40,624
we should see some other results printed into
our console. From time to time this two string
3067
04:28:40,624 --> 04:28:41,624
method can be useful as well. It's available
in every Java class and basically it will
3068
04:28:41,624 --> 04:28:42,624
print some detail about that class. Okay,
I think that's enough for this video. And
3069
04:28:42,624 --> 04:28:43,624
for that method. I think that's enough talking
about Java. Our Java section is done and hopefully
3070
04:28:43,624 --> 04:28:44,624
we have learned a lot in the next video we
are going to have a quick challenge in order
3071
04:28:44,624 --> 04:28:45,624
to make sure that we have learned everything
that we have talked so far. But before I finish
3072
04:28:45,624 --> 04:28:46,624
off this video, I'm going to say that with
Java, you can do a lot have things for example,
3073
04:28:46,624 --> 04:28:47,624
besides writing Android applications, you
can use Java in order to create desktop applications
3074
04:28:47,624 --> 04:28:48,624
for Windows, Linux and Mac OS operating systems.
And also with the help of spring framework,
3075
04:28:48,624 --> 04:28:49,624
you can use Java in order to create enterprise
web applications. There are much more to Java
3076
04:28:49,624 --> 04:28:50,624
language. For example, you can use Java in
order to create applications for quantum computers.
3077
04:28:50,624 --> 04:28:51,624
You can also use it for data analysis purposes,
and literally everything else that you want.
3078
04:28:51,624 --> 04:28:52,624
In this course we have touched the surface
of Java language. But if you want to know
3079
04:28:52,624 --> 04:28:53,624
more, I'm currently recording a new course
for Java language. And I'm sure that you will
3080
04:28:53,624 --> 04:28:54,624
find that course helpful beside the Java course
I plan to record a complete course for a spring
3081
04:28:54,624 --> 04:28:55,624
framework in order to create web applications.
By the time you're watching this video, those
3082
04:28:55,624 --> 04:28:56,624
courses may be released, so make sure to check
them. Okay, I think that's enough. In the
3083
04:28:56,624 --> 04:28:57,624
next video, as I said, we are going to have
a quick challenge See you in the next video.
3084
04:28:57,624 --> 04:28:58,624
Here is our final challenge for the Java section,
I want you to simulate your phone's contacts
3085
04:28:58,624 --> 04:28:59,624
and messages applications. Here is how our
application is going to work. First of all,
3086
04:28:59,624 --> 04:29:00,624
we are going to greet the user with some message.
After that, we are going to show three options
3087
04:29:00,624 --> 04:29:01,624
to the user, one for managing the contacts,
another one for handling the messages, and
3088
04:29:01,624 --> 04:29:02,624
another one for creating the application.
In case if the user selects the first option,
3089
04:29:02,624 --> 04:29:03,624
which is managing the contacts, we are going
to show these options, one for showing all
3090
04:29:03,624 --> 04:29:04,624
of the contacts another one for adding a new
contact. Another one for searching for a specific
3091
04:29:04,624 --> 04:29:05,624
contact. One more for deleting a contact.
And the last one is to go back to the previous
3092
04:29:05,624 --> 04:29:06,624
menu, which was these three options. But in
case if the user selects the second option,
3093
04:29:06,624 --> 04:29:07,624
which is handling messages, we are going to
show these three options. The first one is
3094
04:29:07,624 --> 04:29:08,624
to see a list of all of the messages. The
other one is to send a new message and one
3095
04:29:08,624 --> 04:29:09,624
last one in order to go back to the previous
menu in which is our initial menu, which has
3096
04:29:09,624 --> 04:29:10,624
three options. In case if the user selects
these third option, we are going to create
3097
04:29:10,624 --> 04:29:11,624
the application. We haven't talked about databases
and permanent data storage. But as a hint,
3098
04:29:11,624 --> 04:29:12,624
if you want to store contacts and messages,
you can use a static variables. Okay, pause
3099
04:29:12,624 --> 04:29:13,624
the video in here and go solve the challenge.
After that, come back. And let's see my solution
3100
04:29:13,624 --> 04:29:14,624
for this application. Okay, let's go to see
how can we create this simple application.
3101
04:29:14,624 --> 04:29:15,624
First of all, I have created my project, and
I have created this package inside my source
3102
04:29:15,624 --> 04:29:16,624
folder. Inside this package I have created
this main class in which contains this main
3103
04:29:16,624 --> 04:29:17,624
method. Let's continue from here. First of
all, I'm going to create a new Java class
3104
04:29:17,624 --> 04:29:18,624
called message. Inside this message class,
I'm going to have three fields, let's say
3105
04:29:18,624 --> 04:29:19,624
private string text. Another one, let's say
private string recipient. This recipient in
3106
04:29:19,624 --> 04:29:20,624
here is going to be the name of our contact.
Also in here, I'm going to define an integer
3107
04:29:20,624 --> 04:29:21,624
called ID, let's say private int ID. This
ID is not necessary for this application in
3108
04:29:21,624 --> 04:29:22,624
here. But because in most cases, when you're
working with databases, you have a column
3109
04:29:22,624 --> 04:29:23,624
called ID in order to identify every unique
message. I want to be as close as I can to
3110
04:29:23,624 --> 04:29:24,624
a real world application. I just wanted you
to see this ID field inside a model in which
3111
04:29:24,624 --> 04:29:25,624
is a message. Okay, now that we have these
three fields, let's quickly create a constructor.
3112
04:29:25,624 --> 04:29:26,624
And let's accept all of these three fields.
After that, let's create getters and setters.
3113
04:29:26,624 --> 04:29:27,624
I'm not sure that we are going to need all
of these getters and setters later on. If
3114
04:29:27,624 --> 04:29:28,624
we didn't use them, we will delete them. Also
inside this message class, let's quickly create
3115
04:29:28,624 --> 04:29:29,624
another method called get details. Basically,
we are going to print the details of every
3116
04:29:29,624 --> 04:29:30,624
message. So let's say public void. Let's name
it get details.
3117
04:29:30,624 --> 04:29:31,624
Let's say print. First of all, let's say contact
name. And let's print the recipient. After
3118
04:29:31,624 --> 04:29:32,624
that, let's print the text itself. Let's say
message plus the text. We also need to add
3119
04:29:32,624 --> 04:29:33,624
a plus in here. Sorry about that. And I think
if we add a backslash n in here, it would
3120
04:29:33,624 --> 04:29:34,624
be much better. After the text. Let's also
print the ID Let's say backslash n, Id plus
3121
04:29:34,624 --> 04:29:35,624
our ID. That's it for our message class, let's
quickly create the contact class as well.
3122
04:29:35,624 --> 04:29:36,624
New Java class, let's name this phone contact.
Inside this contact class, I'm going to have
3123
04:29:36,624 --> 04:29:37,624
four fields, let's say private string, name,
private string. Number, you may think that
3124
04:29:37,624 --> 04:29:38,624
this number should be an integer, or maybe
a long, but because different numbers can
3125
04:29:38,624 --> 04:29:39,624
have different formats. For example, they
can have some parentheses or maybe some hyphens.
3126
04:29:39,624 --> 04:29:40,624
I am saving this number as a string. But if
you want, you can always use integers or long.
3127
04:29:40,624 --> 04:29:41,624
After name and number, I'm going to save another
field in here, let's say private string email.
3128
04:29:41,624 --> 04:29:42,624
After all of these, I'm going to have another
field in here, let's say private ArrayList
3129
04:29:42,624 --> 04:29:43,624
of different messages. Let's name this phone
messages. So basically, I'm going to save
3130
04:29:43,624 --> 04:29:44,624
different messages for every contact inside
an ArrayList. Okay, now that we have our four
3131
04:29:44,624 --> 04:29:45,624
fields, let's quickly create our constructor.
But in here, I'm going to create two constructors,
3132
04:29:45,624 --> 04:29:46,624
because at the time of creating a new contact,
that contact does not have any messages. For
3133
04:29:46,624 --> 04:29:47,624
that, I'm going to create another constructor
in here, which will not accept an ArrayList
3134
04:29:47,624 --> 04:29:48,624
of different messages. This time, let's select
the first three items. But because if I don't
3135
04:29:48,624 --> 04:29:49,624
do anything about this messages, this ArrayList
will be now inside this constructor, I'm going
3136
04:29:49,624 --> 04:29:50,624
to see these dots messages is equal to new
ArrayList. So at the time of creating a new
3137
04:29:50,624 --> 04:29:51,624
contact, I'm saving the messages array list
as an empty array list. Okay, now that we
3138
04:29:51,624 --> 04:29:52,624
have two constructors in here, let's quickly
create getters and setters. And also after
3139
04:29:52,624 --> 04:29:53,624
that, let's create a get details method. Let's
say public void, get details. We can also
3140
04:29:53,624 --> 04:29:54,624
use two string methods. But in here, I'm just
more comfortable with this get details method.
3141
04:29:54,624 --> 04:29:55,624
let's print all of the information about each
contact. Let's say name plus the staff name.
3142
04:29:55,624 --> 04:29:56,624
After that, let's print the number and email.
Okay, that's it for our contact class. Let's
3143
04:29:56,624 --> 04:29:57,624
switch to main class. And let's write the
logic for our application. First of all in
3144
04:29:57,624 --> 04:29:58,624
here, I'm going to create an ArrayList of
different contacts. But I'm not going to do
3145
04:29:58,624 --> 04:29:59,624
that inside this main method because we are
going to have multiple methods and we need
3146
04:29:59,624 --> 04:30:00,624
to have access to our contacts are released
in different methods for that I'm going to
3147
04:30:00,624 --> 04:30:01,624
define my ArrayList above in here in the fields
of this main class. I can say private, static,
3148
04:30:01,624 --> 04:30:02,624
because the methods that I'm going to use
later on are static methods, I need to create
3149
04:30:02,624 --> 04:30:03,624
this ArrayList as a static, so let's say private
static array list of different contact. Let's
3150
04:30:03,624 --> 04:30:04,624
name it contacts. But this is not a good place
to initialize our contacts ArrayList because
3151
04:30:04,624 --> 04:30:05,624
this is not the place that our code will be
started. We can initialize our contacts ArrayList
3152
04:30:05,624 --> 04:30:06,624
for example, inside this main method. But
before I do that, I'm going to also define
3153
04:30:06,624 --> 04:30:07,624
a scanner in here because we are going to
need the scanner in multiple methods. So let's
3154
04:30:07,624 --> 04:30:08,624
say private static scanner, let's name it
the scanner. After that inside the main method,
3155
04:30:08,624 --> 04:30:09,624
let's initialize our contacts ArrayList. Let's
say contact is equal to new ArrayList. As
3156
04:30:09,624 --> 04:30:10,624
you can see in here, I'm going to greet the
user with some message, let's just print something.
3157
04:30:10,624 --> 04:30:11,624
After that, I'm going to show these three
different options to the user. Because I want
3158
04:30:11,624 --> 04:30:12,624
to have a clean code. I'm going to show these
three options inside another method. Let's
3159
04:30:12,624 --> 04:30:13,624
name that method show initial options. But
as you can see, right now I'm getting a red
3160
04:30:13,624 --> 04:30:14,624
warning and that's because I need to create
this method down in here. So let's say private,
3161
04:30:14,624 --> 04:30:15,624
static void, show initial options. Let's bring
the options in here. After that, we need to
3162
04:30:15,624 --> 04:30:16,624
get the user's input. And for that we can
use the scanner. So let's initialize our scanner.
3163
04:30:16,624 --> 04:30:17,624
Right now we have declared our scanner above
in here, but we never initialize that. It's
3164
04:30:17,624 --> 04:30:18,624
a good place to initialize our scanner in
here. We can say a scanner is equal to new
3165
04:30:18,624 --> 04:30:19,624
scanner. Let's pass the system that after
that, we can say it's choice is equal to a
3166
04:30:19,624 --> 04:30:20,624
scanner dot next int. Let's create a switch
upon this choice. Let's say switch on choice.
3167
04:30:20,624 --> 04:30:21,624
In case the choice is one, we are going to
manage the contacts. Once again, let's do
3168
04:30:21,624 --> 04:30:22,624
that in another method, let's say manage contacts.
Don't worry about this read warning, we will
3169
04:30:22,624 --> 04:30:23,624
create this method later on. Let's also add
a break in here. But in case and the choice
3170
04:30:23,624 --> 04:30:24,624
is to, we are going to handle the messages.
So let's say manage messages. In case the
3171
04:30:24,624 --> 04:30:25,624
choice is three, or for that matter, any other
number, we are going to create the application.
3172
04:30:25,624 --> 04:30:26,624
So in here, I can say default. In the default
case, I'm just going to break out of the application.
3173
04:30:26,624 --> 04:30:27,624
Let's minimize this project main for now.
And then in here, let's create our two methods.
3174
04:30:27,624 --> 04:30:28,624
First of all, let's manage our contacts. Let's
say private, static void, manage contacts.
3175
04:30:28,624 --> 04:30:29,624
Once again, in here, I'm going to show a list
of options. So let's print those options.
3176
04:30:29,624 --> 04:30:30,624
One, once again, we need to get the user's
input for that we can use the scanner, let's
3177
04:30:30,624 --> 04:30:31,624
say int choice is equal to a scanner dot next
int. Notice that we are not initializing our
3178
04:30:31,624 --> 04:30:32,624
scanner in here. And that's because we have
done that inside this short initial options
3179
04:30:32,624 --> 04:30:33,624
method. So by this time, we have an instance
of our scanner. After that, once again, we
3180
04:30:33,624 --> 04:30:34,624
need to create a switch on our choice. In
case it's one, we are going to show all of
3181
04:30:34,624 --> 04:30:35,624
the contacts, let's create that method, show
all contacts. And also we need to break. Let's
3182
04:30:35,624 --> 04:30:36,624
also write the rest of the cases. In the default
case, I'm going to go back to the previous
3183
04:30:36,624 --> 04:30:37,624
menu. So I'm going to once again run this
show initial options method, so that we can
3184
04:30:37,624 --> 04:30:38,624
see all of these options once again. So in
the default case, I'm going to say show initial
3185
04:30:38,624 --> 04:30:39,624
options. After that in here, we need a default
as well. I'm saying default in here, because
3186
04:30:39,624 --> 04:30:40,624
I'm going to go back to the previous menu
in case if the user enters any other number.
3187
04:30:40,624 --> 04:30:41,624
Okay, let's create these methods one by one.
First of all, let's show a list of all of
3188
04:30:41,624 --> 04:30:42,624
the contacts. I'm going to do that down in
here. Also, instead of writing the declaration
3189
04:30:42,624 --> 04:30:43,624
of your method, you can use the help of your
ID. For that, you can click on the red arrow
3190
04:30:43,624 --> 04:30:44,624
in here and by pressing ALT plus insert, the
first option is to create this method, you
3191
04:30:44,624 --> 04:30:45,624
can see that we have created this method successfully,
the void is fine for the return type. And
3192
04:30:45,624 --> 04:30:46,624
down in here, I can't create the logic for
this method. In order to show a list of all
3193
04:30:46,624 --> 04:30:47,624
of the contacts, I'm going to simply create
a for each loop. Let's say for contact, let's
3194
04:30:47,624 --> 04:30:48,624
name it see inside our contacts, or at least,
let's just say C dot get details the method
3195
04:30:48,624 --> 04:30:49,624
that we have created inside our contact class.
After the for loop once again, I'm going to
3196
04:30:49,624 --> 04:30:50,624
show the initial options. For that I'm going
to save show initial options. We are going
3197
04:30:50,624 --> 04:30:51,624
to quit the application when the user tells
us that's all we need to do inside this method.
3198
04:30:51,624 --> 04:30:52,624
Let's just minimize it for now. And let's
create the second method which is add new
3199
04:30:52,624 --> 04:30:53,624
contacts. Once again, we can use the help
of our ID. Inside this add new contact method
3200
04:30:53,624 --> 04:30:54,624
we are going to receive some users input.
But before that, let's print something you
3201
04:30:54,624 --> 04:30:55,624
Let's say adding a new contact. After that,
let's say please enter the contacts name.
3202
04:30:55,624 --> 04:30:56,624
Once again, we are going to use our scanner,
let's say a string name is equal to a scanner
3203
04:30:56,624 --> 04:30:57,624
dot next, after we get the contacts name,
we are going to get the contact number. So
3204
04:30:57,624 --> 04:30:58,624
once again, let's bring something in here.
Let's say please enter contacts number. Let's
3205
04:30:58,624 --> 04:30:59,624
see string number is equal to a scanner dot
next, let's also get the contacts email string
3206
04:30:59,624 --> 04:31:00,624
he made is equal to a scanner dot next. After
that, we need to make sure that the user enter
3207
04:31:00,624 --> 04:31:01,624
something as the name number and email of
our contact. And don't just press enter and
3208
04:31:01,624 --> 04:31:02,624
leave us with empty strings. For that, I'm
going to create an if statement in here, let's
3209
04:31:02,624 --> 04:31:03,624
say if
3210
04:31:03,624 --> 04:31:04,624
name dot equals an empty string. Let's say
or if number that equals an empty string.
3211
04:31:04,624 --> 04:31:05,624
Or if email is equal to an empty string. If
each one of these is true, we are going to
3212
04:31:05,624 --> 04:31:06,624
show some message to the user we are going
to ask to enter all of the required information.
3213
04:31:06,624 --> 04:31:07,624
Let's say please enter all of the information.
After printing this line of code, I'm going
3214
04:31:07,624 --> 04:31:08,624
to recall this whole method I'm going to start
from the beginning of this method. For that
3215
04:31:08,624 --> 04:31:09,624
I'm going to say add new contact. I'm calling
this method from inside the method. This is
3216
04:31:09,624 --> 04:31:10,624
called a recursive call, you can see the icon
at the left side in here. If you hover over
3217
04:31:10,624 --> 04:31:11,624
that you can see the recursive call calling
a method from inside that method. So by this
3218
04:31:11,624 --> 04:31:12,624
way of calling our method, we are starting
once again from the beginning of this method,
3219
04:31:12,624 --> 04:31:13,624
but in the else case, we are going to create
our contact object, I'm going to say contact,
3220
04:31:13,624 --> 04:31:14,624
let's name it contact is equal to new contact.
And after that, I'm going to pass name, number
3221
04:31:14,624 --> 04:31:15,624
and email. If you remember inside our contact
class, we had two constructors. And in here
3222
04:31:15,624 --> 04:31:16,624
we are using the second constructor, we didn't
pass any messages ArrayList to this constructor
3223
04:31:16,624 --> 04:31:17,624
in here. After creating our contact object,
we can say contact our ArrayList dot add.
3224
04:31:17,624 --> 04:31:18,624
Let's pass our contact. As simple as that.
Now we have added a new contact after this
3225
04:31:18,624 --> 04:31:19,624
if statement. Once again, I'm going to show
the initial options. So let's say show initial
3226
04:31:19,624 --> 04:31:20,624
options. Because once again, we are not going
to create the application unless the user
3227
04:31:20,624 --> 04:31:21,624
tells us Ok, we are done with our second method.
Let's see what else do we have in here. Let's
3228
04:31:21,624 --> 04:31:22,624
create this search for contact method. The
Void is fine for the return type. Inside this
3229
04:31:22,624 --> 04:31:23,624
method first of all we are going to ask for
the name of the contact. So let's bring something
3230
04:31:23,624 --> 04:31:24,624
let's say please enter the contact name. Let's
say string name is equal to the scanner dot
3231
04:31:24,624 --> 04:31:25,624
next. After that we need to make sure that
we have received any so let's say if name
3232
04:31:25,624 --> 04:31:26,624
dot equals an empty string. We are going to
print some message we are going to ask for
3233
04:31:26,624 --> 04:31:27,624
the contact name once again. Let's say please
enter the name and after that once again we
3234
04:31:27,624 --> 04:31:28,624
are going to use a recursive call. We are
going to say search for contacts once again.
3235
04:31:28,624 --> 04:31:29,624
But in the else case, we are going to search
for this contact inside our contacts ArrayList.
3236
04:31:29,624 --> 04:31:30,624
For that once again I'm going to create a
for each loop. Let's say for contact. Let's
3237
04:31:30,624 --> 04:31:31,624
name it see inside our contacts are a list.
Let's say if c dot get name is equal to the
3238
04:31:31,624 --> 04:31:32,624
name that we just received. If that's the
case, let's print c dot get details. Let's
3239
04:31:32,624 --> 04:31:33,624
also define a Boolean before this for loop.
We will see why I am defining this Boolean.
3240
04:31:33,624 --> 04:31:34,624
Let's say Boolean. Let's name it does exist.
Let's initialize it to false in this if state
3241
04:31:34,624 --> 04:31:35,624
Wherever you have found the contact first
of all I'm going to change the value of this
3242
04:31:35,624 --> 04:31:36,624
does exist to true, let's say it does exist
is equal to true. And after the for loop,
3243
04:31:36,624 --> 04:31:37,624
I'm going to create an if statement, I'm going
to say if does exist is not equal to true.
3244
04:31:37,624 --> 04:31:38,624
Then we are going to print something I'm going
to save there is no such contact.
3245
04:31:38,624 --> 04:31:39,624
After all of these logics, we are going to
show the list of options once again. So let's
3246
04:31:39,624 --> 04:31:40,624
say show initial options. Okay, that's it
for our search for contact method. Let's also
3247
04:31:40,624 --> 04:31:41,624
create this delete contact method. Once again,
in here, we are going to ask for the contacts
3248
04:31:41,624 --> 04:31:42,624
name. So let's say please enter the name.
Like before, let's say a string name is equal
3249
04:31:42,624 --> 04:31:43,624
to a scanner dot next, next, we need to create
our if statement. If named, that equals an
3250
04:31:43,624 --> 04:31:44,624
empty string, we are going to ask for the
name once again. And we are going to start
3251
04:31:44,624 --> 04:31:45,624
from the beginning of this method. In the
else case, we are going to look into our contacts
3252
04:31:45,624 --> 04:31:46,624
ArrayList. So let's say for contact see inside
our contacts list. Before I write the logic,
3253
04:31:46,624 --> 04:31:47,624
let's also define a Boolean above in here
like we did for the search for contacts method.
3254
04:31:47,624 --> 04:31:48,624
Let's say Boolean does exist. Let's initialize
it to false. So in here, let's say if I don't
3255
04:31:48,624 --> 04:31:49,624
know why I said as in here, let's quickly
change this one to see. So let's say if see,
3256
04:31:49,624 --> 04:31:50,624
that gets name is equal to the name that we
have received. If that's the case, first of
3257
04:31:50,624 --> 04:31:51,624
all, we are going to change the value of this
does exist. Let's say it does exist is equal
3258
04:31:51,624 --> 04:31:52,624
to true. After that, we are going to remove
this contact from our contacts ArrayList we
3259
04:31:52,624 --> 04:31:53,624
can save contact dot remove. This remove method
requires an object which I'm going to pass
3260
04:31:53,624 --> 04:31:54,624
see the object that we are currently looking
in this iteration. After the for loop, I'm
3261
04:31:54,624 --> 04:31:55,624
going to say if does exist is not equal to
true then we are going to say there is no
3262
04:31:55,624 --> 04:31:56,624
such contact. And after all of these logics,
we are going to show the list of all of the
3263
04:31:56,624 --> 04:31:57,624
initial options. Okay, that's it for this
method. Let's minimize this one. So far, we
3264
04:31:57,624 --> 04:31:58,624
have created the logic for the first option
in our initial options, we have created the
3265
04:31:58,624 --> 04:31:59,624
whole logic for this manage contacts. But
right now in the logic for this manage contacts
3266
04:31:59,624 --> 04:32:00,624
option, we have one or two possible points
for multiple bugs, we are going to fix those
3267
04:32:00,624 --> 04:32:01,624
two and also we are going to write the rest
of the logic for this application in the next
3268
04:32:01,624 --> 04:32:02,624
video. See you in the next video.
3269
04:32:02,624 --> 04:32:03,624
In the previous video, we have created the
logic for this manage contacts option. But
3270
04:32:03,624 --> 04:32:04,624
if you remember I said that we have some issues
right now with this manage contacts logic.
3271
04:32:04,624 --> 04:32:05,624
And in this video, first of all, we are going
to fix those issues. The first issue is inside
3272
04:32:05,624 --> 04:32:06,624
this add new contact method. In here we are
adding the contact into our contacts array
3273
04:32:06,624 --> 04:32:07,624
list. But before that, we need to make sure
that we do not have a contact with this name
3274
04:32:07,624 --> 04:32:08,624
saved on our device for that before adding
this contact to my contacts array list. I'm
3275
04:32:08,624 --> 04:32:09,624
going to check that if I have such a contact
on my device. So first of all, let's create
3276
04:32:09,624 --> 04:32:10,624
a Boolean in here. Let's say Boolean does
exist. Let's initialize it to false. After
3277
04:32:10,624 --> 04:32:11,624
that, I'm going to create a for each loop.
Let's say for contact see inside my contacts
3278
04:32:11,624 --> 04:32:12,624
I have a list. Let's see if c dot get name
is equal to the name that we just received.
3279
04:32:12,624 --> 04:32:13,624
Inside this if statement, I'm going to change
the value of this does exist to true so let's
3280
04:32:13,624 --> 04:32:14,624
say does exist is equal to true. After the
for loop I'm going to check that if this does
3281
04:32:14,624 --> 04:32:15,624
exist is equal to true or not. Let's say if
does exist is equal to true. Then we are going
3282
04:32:15,624 --> 04:32:16,624
to print something we are going to say we
have a contact with this name. After that
3283
04:32:16,624 --> 04:32:17,624
we are going to start from the beginning of
this method. So I'm going to call this add
3284
04:32:17,624 --> 04:32:18,624
new contact method once again. But in the
else case, I'm going to add the contact to
3285
04:32:18,624 --> 04:32:19,624
my account. Tech's ArrayList, so I'm going
to call this line of code from here. And I'm
3286
04:32:19,624 --> 04:32:20,624
going to paste them inside this else case.
Also, after adding the contact to our contacts
3287
04:32:20,624 --> 04:32:21,624
array list, I'm going to show some message
to the user. So let's say name, added successfully.
3288
04:32:21,624 --> 04:32:22,624
Okay, that's the fix that we needed inside
our add new contact method. Let's also change
3289
04:32:22,624 --> 04:32:23,624
the show all contacts method a little bit
as well. So inside this for each loop, after
3290
04:32:23,624 --> 04:32:24,624
printing the details of a contact, I'm going
to print another line. And this line is going
3291
04:32:24,624 --> 04:32:25,624
to be just a separator that will indicate
the difference between different contacts.
3292
04:32:25,624 --> 04:32:26,624
Nothing special in here. Okay, let's move
on. And let's create the method that we haven't
3293
04:32:26,624 --> 04:32:27,624
created in here, this manage messages method,
by pressing ALT plus insert, we can create
3294
04:32:27,624 --> 04:32:28,624
that method. First of all, we are going to
show a list of options. So let's print something.
3295
04:32:28,624 --> 04:32:29,624
After that, we need to receive the user's
input, I can say int choice is the call to
3296
04:32:29,624 --> 04:32:30,624
a scanner dot next int. And let's create our
switch statement on this choice. In case it's
3297
04:32:30,624 --> 04:32:31,624
Vaughn, we are going to show a list of all
of the messages. So let's say show all messages,
3298
04:32:31,624 --> 04:32:32,624
we will create that method in a minute. But
in here, let's add a break. And let's write
3299
04:32:32,624 --> 04:32:33,624
the other cases. In case it's true, we are
going to send a new message. So let's say
3300
04:32:33,624 --> 04:32:34,624
send a new message. And let's add a break.
In the default case, I'm just going to show
3301
04:32:34,624 --> 04:32:35,624
the initial options. After that, in here,
we need a break as well. Okay, let's create
3302
04:32:35,624 --> 04:32:36,624
this show all messages method. In here, first
of all, I'm going to save all of the messages
3303
04:32:36,624 --> 04:32:37,624
from all of the contacts into an array list.
So let's say array list of different messages.
3304
04:32:37,624 --> 04:32:38,624
Let's name it all messages is equal to new
ArrayList. After that, I'm going to create
3305
04:32:38,624 --> 04:32:39,624
a for each loop. For contact, see inside my
contacts or in this. Let's see all messages
3306
04:32:39,624 --> 04:32:40,624
that at all, we haven't used this add all
method, but in here, we are going to do that.
3307
04:32:40,624 --> 04:32:41,624
I'm going to say C dot get messages. So by
using this add all method, we are adding all
3308
04:32:41,624 --> 04:32:42,624
of the messages of one contact to our all
messages ArrayList. After the for loop, I'm
3309
04:32:42,624 --> 04:32:43,624
going to check that if the size of these all
messages is zero or not. So let's say if all
3310
04:32:43,624 --> 04:32:44,624
messages that size is greater than zero, I'm
going to show the details of every message.
3311
04:32:44,624 --> 04:32:45,624
So for that, I'm going to create another for
each loop in here. Let's say for message,
3312
04:32:45,624 --> 04:32:46,624
let's name it m inside out all messages, at
least. let's print m dot get details. And
3313
04:32:46,624 --> 04:32:47,624
after that, let's also print some separator.
But in the else case, I'm going to say that
3314
04:32:47,624 --> 04:32:48,624
you do not have any message. After everything
after the if statement, I am going to show
3315
04:32:48,624 --> 04:32:49,624
the initial options. Okay, that's it for this
show all messages method. Let's also create
3316
04:32:49,624 --> 04:32:50,624
the other method, which is send a new message.
In here, first of all, I'm going to get the
3317
04:32:50,624 --> 04:32:51,624
contact or recipients name. So let's ask the
user that, let's say Who are you going to
3318
04:32:51,624 --> 04:32:52,624
send a message. After that we need to get
the name of our contact. So I can say string
3319
04:32:52,624 --> 04:32:53,624
name is equal to scanner dot next. After that,
we need to check that if this name is empty
3320
04:32:53,624 --> 04:32:54,624
or not for that, I can say if name that equals
an empty string. We are going to ask the user
3321
04:32:54,624 --> 04:32:55,624
to enter a name. So let's say please enter
the name of the contact. After that we are
3322
04:32:55,624 --> 04:32:56,624
going to have a recursive call. So in here,
I'm going to say send a new message. But in
3323
04:32:56,624 --> 04:32:57,624
the else case, I'm going to check that if
we have such a contact in our contacts ArrayList.
3324
04:32:57,624 --> 04:32:58,624
For that I'm going to define a Boolean in
here Boolean does exist, is equal to false.
3325
04:32:58,624 --> 04:32:59,624
After that, I'm going to create a for each
loop. Let's say for contact see inside our
3326
04:32:59,624 --> 04:33:00,624
contacts or at least if see that gets name,
dot equals the name that we just received.
3327
04:33:00,624 --> 04:33:01,624
Let's say it does exist is equal to true.
After the for loop, I'm going to check that
3328
04:33:01,624 --> 04:33:02,624
if does exist is equal to true or not. First
of all, let's write the else case. In the
3329
04:33:02,624 --> 04:33:03,624
else case, I'm going to say there is no such
contact. But inside the if block, if we do
3330
04:33:03,624 --> 04:33:04,624
have such a contact, we are going to ask for
the message itself. So let's say what are
3331
04:33:04,624 --> 04:33:05,624
you going to say? Let's say a string text
is equal to a scanner dot next. After that,
3332
04:33:05,624 --> 04:33:06,624
we need to make sure that this text is empty
or not. Let's say if text that equals an empty
3333
04:33:06,624 --> 04:33:07,624
string. If that's the case, let's say please
enter some message.
3334
04:33:07,624 --> 04:33:08,624
And after that, we are going to start from
the beginning of this method. So let's say
3335
04:33:08,624 --> 04:33:09,624
send me a message, which once again is a recursive
call. But in the else case, we are going to
3336
04:33:09,624 --> 04:33:10,624
create our message object. And after that,
we are going to add it to the contact that
3337
04:33:10,624 --> 04:33:11,624
exists in our contacts ArrayList. But if you
remember, inside our message class, we had
3338
04:33:11,624 --> 04:33:12,624
a field called ID. Basically, whenever you
are receiving these messages from a database,
3339
04:33:12,624 --> 04:33:13,624
or maybe a web server, this ID is unique to
every message. In here, I'm trying to be as
3340
04:33:13,624 --> 04:33:14,624
close as I can, and to a real world application.
So I'm going to make this ID unique. In order
3341
04:33:14,624 --> 04:33:15,624
to have a unique ID for every message, I'm
going to define a field above in here inside
3342
04:33:15,624 --> 04:33:16,624
my main class I am talking about here. And
after that, we will increment the number of
3343
04:33:16,624 --> 04:33:17,624
this ID by one every time that we create a
new message. For that I'm going to say private,
3344
04:33:17,624 --> 04:33:18,624
static integer. Let's name this phone ID.
And let's also initialize it to zero. After
3345
04:33:18,624 --> 04:33:19,624
that down in here, when we create our message
object inside this else case, down in here,
3346
04:33:19,624 --> 04:33:20,624
I can say ID plus plus. So because this ID
is a static integer, whenever we increment
3347
04:33:20,624 --> 04:33:21,624
its value, it's going to be unique for every
message. So right now we have the name of
3348
04:33:21,624 --> 04:33:22,624
our recipient, which we have received above
in here. And also we have the message body,
3349
04:33:22,624 --> 04:33:23,624
which is this text. And also we have generated
this ID which is unique. These are the three
3350
04:33:23,624 --> 04:33:24,624
things that we need in order to create our
message object, I can say message, let's name
3351
04:33:24,624 --> 04:33:25,624
it new message is equal to new message we
need the text. After that we need the recipient,
3352
04:33:25,624 --> 04:33:26,624
which is name. And after that, we need the
ID. Next, I need to find the proper contact
3353
04:33:26,624 --> 04:33:27,624
to add this message to his or her array list
of different messages. For that I'm going
3354
04:33:27,624 --> 04:33:28,624
to create a for loop. Let's say for contact
see inside our contacts array list. Let's
3355
04:33:28,624 --> 04:33:29,624
say if see that get name is equal to the name
that we received. If that's the case, first
3356
04:33:29,624 --> 04:33:30,624
of all, we need to get the array list of different
messages of this contact. For that I can say
3357
04:33:30,624 --> 04:33:31,624
array list of messages. Let's call this ArrayList
new messages is equal to c dot get messages.
3358
04:33:31,624 --> 04:33:32,624
After that, we need to add this new message
to this new messages ArrayList so let's say
3359
04:33:32,624 --> 04:33:33,624
new messages that add new message Sorry, I
have used this add all methods went to remove
3360
04:33:33,624 --> 04:33:34,624
this all. After that we are going to save
the current contact that we are looking into.
3361
04:33:34,624 --> 04:33:35,624
because later on we are going to remove this
contact from our contacts array list. And
3362
04:33:35,624 --> 04:33:36,624
we are going to update his or her array list
of messages. And after that, we are going
3363
04:33:36,624 --> 04:33:37,624
to add it once again to our contacts ArrayList.
So for that I can say contact. Let's name
3364
04:33:37,624 --> 04:33:38,624
it current contact is equal to see. Next we
need to say current contact dot set messages.
3365
04:33:38,624 --> 04:33:39,624
And we need to pass our new messages are released.
After that, it's time to remove the C from
3366
04:33:39,624 --> 04:33:40,624
our contacts or at least we can say contacts
that remove and we can pass the C object.
3367
04:33:40,624 --> 04:33:41,624
Now that we have removed the previous contact,
we can add the new one by saying contacts
3368
04:33:41,624 --> 04:33:42,624
dot add. Let's say current contact. So this
way we have added this new message to our
3369
04:33:42,624 --> 04:33:43,624
specific contact and we have updated our contacts
ArrayList Okay, that's the entire logic that
3370
04:33:43,624 --> 04:33:44,624
we need in order to add a new message just
after everything after we are done with this
3371
04:33:44,624 --> 04:33:45,624
method. We need to show the initial options
once again. Let's minimize this method. And
3372
04:33:45,624 --> 04:33:46,624
let's see what else do we have? I think we
have covered everything. And we are ready
3373
04:33:46,624 --> 04:33:47,624
to test our application. So let's run the
application.
3374
04:33:47,624 --> 04:33:48,624
In here we are seeing the greeting message,
which is fine. After that we are seeing the
3375
04:33:48,624 --> 04:33:49,624
initial list of options. If we select one,
we can see the options for managing different
3376
04:33:49,624 --> 04:33:50,624
contacts. Right now we do not have any contacts
in our ArrayList. So if we select one, you
3377
04:33:50,624 --> 04:33:51,624
can see that we are not seeing anything, it's
better to print some message in here in case
3378
04:33:51,624 --> 04:33:52,624
if we do not have any contact in our phone,
for example, that says, you do not have any
3379
04:33:52,624 --> 04:33:53,624
contact on your device, but we will write
that later on. For now let's just see the
3380
04:33:53,624 --> 04:33:54,624
other options. Once again, let's select Manage
contact. This time, let's try to add a new
3381
04:33:54,624 --> 04:33:55,624
contact, you can see that the console is waiting
for a name, let's say may Sam, the console
3382
04:33:55,624 --> 04:33:56,624
is waiting for a number. Let's add some number.
After that, we need to provide some email,
3383
04:33:56,624 --> 04:33:57,624
let's say may sam@gmail.com. And as you can
see Mason added successfully. If you want
3384
04:33:57,624 --> 04:33:58,624
to make sure of that, once again, you can
check the list of all of your contacts by
3385
04:33:58,624 --> 04:33:59,624
saying show all contacts. And in here you
can see the details of Mason. Let's also add
3386
04:33:59,624 --> 04:34:00,624
another contact. Let's say Sarah, this thing.
Let's add a random number. After that an email.
3387
04:34:00,624 --> 04:34:01,624
This time let's try searching for a new contact.
So in here, if I say three, you can see that
3388
04:34:01,624 --> 04:34:02,624
the console is waiting for the name of our
contact. Let's type Brad in here, you can
3389
04:34:02,624 --> 04:34:03,624
see that we are seeing this message there
is no such contact in your phone. Once again,
3390
04:34:03,624 --> 04:34:04,624
let's try searching for contact this time.
Let's try Sarah. You can see that we are seeing
3391
04:34:04,624 --> 04:34:05,624
the details about Sarah. Let's also try removing
one of the contacts. Let's say for in order
3392
04:34:05,624 --> 04:34:06,624
to delete a contact, let's say me Sam. In
here we are not seeing any message indicating
3393
04:34:06,624 --> 04:34:07,624
if that we have deleted Maxim successfully
or not. But if we check the list of our contacts,
3394
04:34:07,624 --> 04:34:08,624
we should see only Sarah. And as you can see,
we have only Sarah in our contacts list. Let's
3395
04:34:08,624 --> 04:34:09,624
also try this go back as well. So let's say
manage contacts once again. This time if I
3396
04:34:09,624 --> 04:34:10,624
put any number beside one through four, for
example, if I say six, you can see that we
3397
04:34:10,624 --> 04:34:11,624
are going back to the main menu. Also, let's
try adding a new contact with empty values.
3398
04:34:11,624 --> 04:34:12,624
Sorry about that, I need to go back and select
one. Let's say to in order to add a new contact.
3399
04:34:12,624 --> 04:34:13,624
Let's don't provide anything for the name.
You can see that the scanner is waiting for
3400
04:34:13,624 --> 04:34:14,624
some input. But even if the scanner moves
to the next line, we have write the logic
3401
04:34:14,624 --> 04:34:15,624
in order to prevent adding an empty contact.
Okay, that's good. Let's add many samples
3402
04:34:15,624 --> 04:34:16,624
again. Let's add some number. And some you
mean. Let's see what we have inside the messages
3403
04:34:16,624 --> 04:34:17,624
option. In order to see a list of all of the
messages. Let's press one, you can see this
3404
04:34:17,624 --> 04:34:18,624
message you don't have any message. Once again,
let's go to the messages option. I've entered
3405
04:34:18,624 --> 04:34:19,624
two in here and let's try sending a new message.
Who are you going to send a message? Let's
3406
04:34:19,624 --> 04:34:20,624
say Sarah, what are you going to say? Let's
say hello. It seems like we are getting a
3407
04:34:20,624 --> 04:34:21,624
crash and the crash happened because of this
exception. Concurrent modification exception.
3408
04:34:21,624 --> 04:34:22,624
Okay, let's take a deeper look into our code.
And let's debug our application for the first
3409
04:34:22,624 --> 04:34:23,624
time. It seems like the exception occurred
when we try to add the new message. Also,
3410
04:34:23,624 --> 04:34:24,624
you can see the line 99 in here, if you click
on that, you will be navigated to that line.
3411
04:34:24,624 --> 04:34:25,624
Yes, we have this exception inside the add
new message or send a new message inside this
3412
04:34:25,624 --> 04:34:26,624
for loop. I think the error in here is happening
because first of all, we are using this C
3413
04:34:26,624 --> 04:34:27,624
to assign a new value to this current contact.
And immediately after that we are trying to
3414
04:34:27,624 --> 04:34:28,624
remove this contact from our contacts ArrayList.
So this causes the concurrent modification
3415
04:34:28,624 --> 04:34:29,624
exception. If you want to get over that exception,
you can simplify this if statement. For example
3416
04:34:29,624 --> 04:34:30,624
in here I'm going to delete all these three
lines of code. So by this point, we have this
3417
04:34:30,624 --> 04:34:31,624
new messages are released. We have added our
new message to this ArrayList and after that,
3418
04:34:31,624 --> 04:34:32,624
in order to update the array list inside this
contact, I can say see dot set messages. And
3419
04:34:32,624 --> 04:34:33,624
I can pass my new messages ArrayList. So no
need for removing and adding a new contact
3420
04:34:33,624 --> 04:34:34,624
into our ArrayList. concurrently. Let's run
the application once again. And let's see
3421
04:34:34,624 --> 04:34:35,624
if we are seeing the exception once again.
First of all, we need to add a contact. So
3422
04:34:35,624 --> 04:34:36,624
let's say manage contact, add a new contact.
Let's Provide a name and number after that
3423
04:34:36,624 --> 04:34:37,624
some email. And now we can send a new message,
let's say messages. Send a new message. Who
3424
04:34:37,624 --> 04:34:38,624
are you going to send a message? Let's say
Mesa, what are you going to say, after this
3425
04:34:38,624 --> 04:34:39,624
line of code, we got the exception. Let's
say hello. And this time, it seems like we
3426
04:34:39,624 --> 04:34:40,624
have added or we have sent a new message.
If you want to make sure of that, you can
3427
04:34:40,624 --> 04:34:41,624
go to the messages option. And you can show
the list of all of your messages, you can
3428
04:34:41,624 --> 04:34:42,624
see that we have one message in which we have
sent it to Mesa. Okay, that seems better.
3429
04:34:42,624 --> 04:34:43,624
Once again, let's go to this messages option.
And let's try going back to the main menu.
3430
04:34:43,624 --> 04:34:44,624
So if I type three or any other number beside
this one and two, we should go back to the
3431
04:34:44,624 --> 04:34:45,624
main menu. For example, let's say for and
as you can see, we are seeing the initial
3432
04:34:45,624 --> 04:34:46,624
options. For the last thing, let's try creating
our application. And as you can see, we have
3433
04:34:46,624 --> 04:34:47,624
created the application successfully, let's
just fix the few errors that we had. In the
3434
04:34:47,624 --> 04:34:48,624
show all contacts method. If you remember
I said that we are going to show some message
3435
04:34:48,624 --> 04:34:49,624
in case if we don't have any contacts in our
ArrayList. So for that, first of all, I'm
3436
04:34:49,624 --> 04:34:50,624
going to check that if we do have any contact.
For that, I'm going to say if contacts that
3437
04:34:50,624 --> 04:34:51,624
size is greater than zero. First of all, let's
write the else case. In the else case, I'm
3438
04:34:51,624 --> 04:34:52,624
just going to print you do not have any contact.
But in the if block view are going to do the
3439
04:34:52,624 --> 04:34:53,624
same thing that we were doing previously.
Let's run the application one more time in
3440
04:34:53,624 --> 04:34:54,624
order to see that these changes are effective
or not.
3441
04:34:54,624 --> 04:34:55,624
So right now, we do not have any contacts
in our contacts list. Also, you may be wondering
3442
04:34:55,624 --> 04:34:56,624
why we are losing the contacts that we are
keep adding into our application. That's because
3443
04:34:56,624 --> 04:34:57,624
we are not using any permanent data storage
like a database, we are using a static array
3444
04:34:57,624 --> 04:34:58,624
list and that ArrayList will be cleared when
we close our application. For that reason
3445
04:34:58,624 --> 04:34:59,624
all of the contacts are being removed whenever
we stop the application. So let's say manage
3446
04:34:59,624 --> 04:35:00,624
contacts. And let's say show all contacts,
you can see the message in here you do not
3447
04:35:00,624 --> 04:35:01,624
have any contacts. But it's better to show
the list of initial options once again after
3448
04:35:01,624 --> 04:35:02,624
we print this line of code. So down in here
after this print statement, let's say show
3449
04:35:02,624 --> 04:35:03,624
initial options. Okay, this was the last video
of this section. And this was my solution
3450
04:35:03,624 --> 04:35:04,624
for this challenge. Your solution is probably
different than me. And that's okay, because
3451
04:35:04,624 --> 04:35:05,624
there are multiple ways of creating the same
application. Hopefully you have practiced
3452
04:35:05,624 --> 04:35:06,624
and learned everything that we have talked
about in this section. In the next section
3453
04:35:06,624 --> 04:35:07,624
of the course, we are going to switch back
to Android Studio, and we are going to talk
3454
04:35:07,624 --> 04:35:08,624
about user interface and how we can design
a beautiful layout for our application. See
3455
04:35:08,624 --> 04:35:09,624
you in the next section. What's up everyone,
I hope you're having fun. Before we start
3456
04:35:09,624 --> 04:35:10,624
our talk about user interface, I just wanted
to remind you that there is an extended version
3457
04:35:10,624 --> 04:35:11,624
of the scores that you can watch. by enrolling
in the extended course you will have lifetime
3458
04:35:11,624 --> 04:35:12,624
access to more than 60 hours of videos, you
can ask your questions directly from me and
3459
04:35:12,624 --> 04:35:13,624
I will come back to you within hours, you
will have access to all of the source codes
3460
04:35:13,624 --> 04:35:14,624
that I write in the videos. And also you can
have access to all of the new videos that
3461
04:35:14,624 --> 04:35:15,624
I upload. Just check out miko.org for more
details about the course. And don't forget
3462
04:35:15,624 --> 04:35:16,624
to use Free Code Camp as the coupon code to
get 20% discount. If you want to get serious
3463
04:35:16,624 --> 04:35:17,624
in Android app development. Taking the extended
course is almost a necessity. Okay, everyone,
3464
04:35:17,624 --> 04:35:18,624
I just wanted to remind you that there is
an extended version of this course that you
3465
04:35:18,624 --> 04:35:19,624
can take without further ado, let's just start
our talk about user interface. Have fun learning.
3466
04:35:19,624 --> 04:35:20,624
Hello everyone and welcome back. This is the
user interface section of our course and we
3467
04:35:20,624 --> 04:35:21,624
are going to talk about a lot of concepts
and tools that we have related to user interface.
3468
04:35:21,624 --> 04:35:22,624
We are going to talk about a lot of UI elements
and they are attributes. We are going To talk
3469
04:35:22,624 --> 04:35:23,624
about different kinds of event listeners,
for example, click listeners on different
3470
04:35:23,624 --> 04:35:24,624
UI elements. We will talk about how can we
create applications that is compatible with
3471
04:35:24,624 --> 04:35:25,624
different screen sizes, and also different
languages. Besides that, we will also talk
3472
04:35:25,624 --> 04:35:26,624
about material design and all sorts of stuff
in this section of the course. Without further
3473
04:35:26,624 --> 04:35:27,624
ado, let's jump into our project. And let's
implement some UI elements. As you can see,
3474
04:35:27,624 --> 04:35:28,624
I'm no longer using IntelliJ ID, and I've
switched to Android Studio. We needed IntelliJ
3475
04:35:28,624 --> 04:35:29,624
ID for the Java section of the course. But
in here, we can switch back to Android Studio,
3476
04:35:29,624 --> 04:35:30,624
just in case if you forget how to create a
new project in Android Studio, I'm going to
3477
04:35:30,624 --> 04:35:31,624
start from scratch. Let's start a new Android
Studio project. In here, we needed to select
3478
04:35:31,624 --> 04:35:32,624
a template. If you remember, I said that these
templates are some applications return to
3479
04:35:32,624 --> 04:35:33,624
some level. Later on in the course, specifically,
after we talked about activities, we will
3480
04:35:33,624 --> 04:35:34,624
see that how can we create all of these from
scratch, but for now, let's just select this
3481
04:35:34,624 --> 04:35:35,624
empty activity. After that in here, we needed
to name our project. For example, for this
3482
04:35:35,624 --> 04:35:36,624
application, I'm going to say UI basics. I'm
going to stay with this package name, I think
3483
04:35:36,624 --> 04:35:37,624
it's fine. We needed a save location. Once
again, the default is fine. For the language,
3484
04:35:37,624 --> 04:35:38,624
I'm going to say Java for the minimum API
level. As I said in previous videos, in the
3485
04:35:38,624 --> 04:35:39,624
most videos for this course, I'm going to
save with API level 19. After selecting all
3486
04:35:39,624 --> 04:35:40,624
of these, let's just create our project by
clicking on this Finish button down in here.
3487
04:35:40,624 --> 04:35:41,624
It seems like our project has been created
successfully. Now that we know about Java,
3488
04:35:41,624 --> 04:35:42,624
let's have another look at this class that
we have created in here. But before that,
3489
04:35:42,624 --> 04:35:43,624
let's minimize this Ron pane.
3490
04:35:43,624 --> 04:35:44,624
As you can see, this is a Java class in our
package, you can see that it's a class called
3491
04:35:44,624 --> 04:35:45,624
main activity. This class is extending the
app compat activity, whatever that is. Inside
3492
04:35:45,624 --> 04:35:46,624
this class, we have this onCreate method which
is overriding you can guess it probably because
3493
04:35:46,624 --> 04:35:47,624
of this app compat activity. And also above
in here we have some imports. Right now we
3494
04:35:47,624 --> 04:35:48,624
are importing this app compat activity and
also this bond. Don't worry about this, we
3495
04:35:48,624 --> 04:35:49,624
will talk about them later on in the course.
But for now you can see the general syntax
3496
04:35:49,624 --> 04:35:50,624
of a simple Java class in here as well. If
you remember I said that when you create an
3497
04:35:50,624 --> 04:35:51,624
activity, a Java class and also XML file will
be created. The Java part is responsible for
3498
04:35:51,624 --> 04:35:52,624
the behavior of your application. For example,
in your layout file, you may receive the user's
3499
04:35:52,624 --> 04:35:53,624
input in the Java file, you may decide to
what to do with the user's input. Hopefully,
3500
04:35:53,624 --> 04:35:54,624
now that we know about Java, we can understand
all of these much better. This onCreate method
3501
04:35:54,624 --> 04:35:55,624
is a part of something called the activity
lifecycle. We will talk about that in future
3502
04:35:55,624 --> 04:35:56,624
videos. But for now, just know that this onCreate
method is somehow like the main method inside
3503
04:35:56,624 --> 04:35:57,624
a simple Java application. This is the start
point of every activity. And right now, because
3504
04:35:57,624 --> 04:35:58,624
our application has only one activity, this
is the start point of our entire application.
3505
04:35:58,624 --> 04:35:59,624
So this line 13 in here would be the start
point of our code. You can see this super
3506
04:35:59,624 --> 04:36:00,624
statement in here. Once again, it means that
do whatever you are doing inside the parent
3507
04:36:00,624 --> 04:36:01,624
class. In this case, the parent class is this
app compat activity. For the time being we
3508
04:36:01,624 --> 04:36:02,624
are not concerned about that. But as you can
see, we are passing this save instance state
3509
04:36:02,624 --> 04:36:03,624
which is a bonded to the super statement.
Later on, we will talk about that. Also, it's
3510
04:36:03,624 --> 04:36:04,624
important to note that inside activities,
you have some inner methods, we didn't have
3511
04:36:04,624 --> 04:36:05,624
this option in Java. For example, you can
see this set content view method, you don't
3512
04:36:05,624 --> 04:36:06,624
see the declaration of this method anywhere
inside this class. But you can see that by
3513
04:36:06,624 --> 04:36:07,624
using it, you are not getting any red warning.
Similar to this setcontentview. If you have
3514
04:36:07,624 --> 04:36:08,624
a lot of other inner methods inside an activity,
we will talk about them later on in the course.
3515
04:36:08,624 --> 04:36:09,624
Okay, let's move on from this part. And let's
start working on our user interface. I just
3516
04:36:09,624 --> 04:36:10,624
wanted to have another look at Our application
and specifically this jar file. Now that we
3517
04:36:10,624 --> 04:36:11,624
know about Java, okay, let's close this java
file. And let's start talking about this activity
3518
04:36:11,624 --> 04:36:12,624
main dot XML file. If you remember in the
first videos of this course, I said that XML
3519
04:36:12,624 --> 04:36:13,624
is a markup language. Once again, if you are
a web developer, you know the concept of markup
3520
04:36:13,624 --> 04:36:14,624
languages. Those are just helpful for defining
some elements that are going to be shown to
3521
04:36:14,624 --> 04:36:15,624
the user. Before everything. Let's scripting
locate this file in our project. Right now
3522
04:36:15,624 --> 04:36:16,624
we are inside the Android view. And if you
want to find this activity main dot XML file,
3523
04:36:16,624 --> 04:36:17,624
you can come to this Resources folder. down
in here inside the layout folder, you can
3524
04:36:17,624 --> 04:36:18,624
see the file. And if you switch to projects
view, you can go to your application folder,
3525
04:36:18,624 --> 04:36:19,624
inside the app folder. Inside the source folder,
you have this main folder. And in here you
3526
04:36:19,624 --> 04:36:20,624
have the Resources folder. Once again, inside
the layout file, you have this activity main
3527
04:36:20,624 --> 04:36:21,624
dot XML file. Okay, let's switch back to Android
view. And let's minimize this project pane
3528
04:36:21,624 --> 04:36:22,624
for now, if you remember from the previous
videos of this course, I said that whenever
3529
04:36:22,624 --> 04:36:23,624
you want to define a layout file, you have
two options. First of all, you can use this
3530
04:36:23,624 --> 04:36:24,624
design view for example, by dragging some
items into your design view.
3531
04:36:24,624 --> 04:36:25,624
You can have different elements and later
on, you can define some attributes for your
3532
04:36:25,624 --> 04:36:26,624
elements. This is one way but the other way
is to switch to text view and manually type
3533
04:36:26,624 --> 04:36:27,624
everything that you need. In my opinion, when
you are an Android developer, most of the
3534
04:36:27,624 --> 04:36:28,624
times you are going to work with the TextView.
So in here, we are going to start from this
3535
04:36:28,624 --> 04:36:29,624
TextView. First of all, if you're not seeing
this preview in here, you can always enable
3536
04:36:29,624 --> 04:36:30,624
it and disable it from this preview option
in here. If you have a low memory RAM, my
3537
04:36:30,624 --> 04:36:31,624
suggestion is to disable this review, because
it's going to render the layout file in real
3538
04:36:31,624 --> 04:36:32,624
time. And that's going to load a lot of pressures
on your device's memory RAM and also CPU.
3539
04:36:32,624 --> 04:36:33,624
For that if you have low memory RAM, disable
this preview, it will speed everything up.
3540
04:36:33,624 --> 04:36:34,624
Right now inside this simple layout file,
we have this TextView you can see that when
3541
04:36:34,624 --> 04:36:35,624
I click on this TextView this TextView inside
our XML file has been highlighted, it means
3542
04:36:35,624 --> 04:36:36,624
that these are the same. I'm going to delete
this text view for now. Let's select all of
3543
04:36:36,624 --> 04:36:37,624
it. And let's press Delete. The next thing
that I'm going to do is that I'm going to
3544
04:36:37,624 --> 04:36:38,624
change this constraint layout. In future videos,
we will talk about what the constraint layout
3545
04:36:38,624 --> 04:36:39,624
is. But for now, for a starter, I think it's
better to start with relative layout. Let's
3546
04:36:39,624 --> 04:36:40,624
select that. Also, if you have noticed, you
can see that when we have changed the opening
3547
04:36:40,624 --> 04:36:41,624
tag in here to relative layout, the closing
tag has been changed to relative layout as
3548
04:36:41,624 --> 04:36:42,624
well. Inside these opening and closing tags,
I can have different UI elements. For example,
3549
04:36:42,624 --> 04:36:43,624
if I want to define a TextView, I can open
an anchor bracket and by typing TextView.
3550
04:36:43,624 --> 04:36:44,624
In capital T, we have a list of options. Let's
select the first one TextView. Immediately
3551
04:36:44,624 --> 04:36:45,624
after pressing Enter, we are getting some
attributes. So for every UI elements, you
3552
04:36:45,624 --> 04:36:46,624
need at least two attributes a width and also
a height. The values for these two attributes
3553
04:36:46,624 --> 04:36:47,624
can be some numbers. But Alternatively, you
can see that we have other options in here
3554
04:36:47,624 --> 04:36:48,624
as well. We will talk about this match parent
and wrap content in a minute. But for now,
3555
04:36:48,624 --> 04:36:49,624
let's just select wrap content. Basically,
it's going to wrap the content of this text
3556
04:36:49,624 --> 04:36:50,624
view for the height. Let's also select wrap
content. If you want you can finish creating
3557
04:36:50,624 --> 04:36:51,624
your text view by adding a slashing here.
And that's if you can have a text view. But
3558
04:36:51,624 --> 04:36:52,624
if you want to have more attributes, you have
a lot of options. For example, before this
3559
04:36:52,624 --> 04:36:53,624
slash I'm going to say let's say text, you
can see that we are getting a suggestion and
3560
04:36:53,624 --> 04:36:54,624
recall that text let's select that. And in
here I can pass a text for example, let's
3561
04:36:54,624 --> 04:36:55,624
say hello world.
3562
04:36:55,624 --> 04:36:56,624
As soon as I type this, you can see that text
in the preview as well. So text is one of
3563
04:36:56,624 --> 04:36:57,624
the attributes of this text view. Let's talk
about the other attributes found by Vaughn.
3564
04:36:57,624 --> 04:36:58,624
For example, right now this text is at the
corner of my screen I don't like this, I'm
3565
04:36:58,624 --> 04:36:59,624
going to change the position of this TextView
to the middle of this line. For that I have
3566
04:36:59,624 --> 04:37:00,624
multiple options First of all, I can add some
margin in which will add a space between this
3567
04:37:00,624 --> 04:37:01,624
TextView at the edge of the screen. For example,
I can say margin and I can pass 50 for example
3568
04:37:01,624 --> 04:37:02,624
50 dP dp is the unit when you're working with
your layout files, we can see that we have
3569
04:37:02,624 --> 04:37:03,624
some margin, but we can be more specific for
example, we can say margin top margin left
3570
04:37:03,624 --> 04:37:04,624
margin right and also margin bottom. For example,
I can say margin left let's say 100 Db this
3571
04:37:04,624 --> 04:37:05,624
time, margins are extremely useful but in
here they are not going to help us because
3572
04:37:05,624 --> 04:37:06,624
this is not accurate, we want our TextView
to be exactly in the center of our line. For
3573
04:37:06,624 --> 04:37:07,624
that, instead of using a margin, I have a
better option, I can say, center horizontal
3574
04:37:07,624 --> 04:37:08,624
in order to center my text view horizontally.
As you can see, I can pass through or false
3575
04:37:08,624 --> 04:37:09,624
in here, let's pass through. And this time
and the TextView has been centered horizontally.
3576
04:37:09,624 --> 04:37:10,624
If you want, you can also center it vertically
by saying center vertical and passing a value
3577
04:37:10,624 --> 04:37:11,624
of true, you can see that it has been centered
both horizontally and vertically. Beside these
3578
04:37:11,624 --> 04:37:12,624
two, you also have another option. For example,
in this case, I'm going to delete both of
3579
04:37:12,624 --> 04:37:13,624
these attributes. Instead, I'm going to say
center in parent, this one down in here. Let's
3580
04:37:13,624 --> 04:37:14,624
pass through as the value of this attribute.
This center parent is going to center our
3581
04:37:14,624 --> 04:37:15,624
elements inside its parent element. an XML
file is like a tree, we have these Child Elements.
3582
04:37:15,624 --> 04:37:16,624
And above them, we have the parent elements.
For example, in this case, the parent element
3583
04:37:16,624 --> 04:37:17,624
is a relative layout. If we want, we can have
another relative layout in here or maybe some
3584
04:37:17,624 --> 04:37:18,624
other UI elements. And inside that element,
we can have this TextView we can do these
3585
04:37:18,624 --> 04:37:19,624
kind of inheritance as many times as we want.
So in this case, the TextView is our child
3586
04:37:19,624 --> 04:37:20,624
element. And this relative layout is our parent
element. And when we use this center implant
3587
04:37:20,624 --> 04:37:21,624
attribute, it's going to center our TextView
in its parent element. Okay, let's move on
3588
04:37:21,624 --> 04:37:22,624
from this part. If you want, you can also
increase the size of your text for that you
3589
04:37:22,624 --> 04:37:23,624
can say text size, this attribute in here,
and you can pass a number for example, you
3590
04:37:23,624 --> 04:37:24,624
can say 20 SB. When you're working with the
size of your fonts, the unit is SP, we can
3591
04:37:24,624 --> 04:37:25,624
see that it's larger now. Also, if you want
you can have some styles on this text as well.
3592
04:37:25,624 --> 04:37:26,624
For that we can say text style. This attribute
you can see three styles bold, italic, and
3593
04:37:26,624 --> 04:37:27,624
normal. For example, let's fast bold. If you
want your attribute to be both bold and italic,
3594
04:37:27,624 --> 04:37:28,624
you can add a pipeline in here. And after
that you can see italic. This time you can
3595
04:37:28,624 --> 04:37:29,624
see both of these attributes in the same element.
If you want, you can also change the color
3596
04:37:29,624 --> 04:37:30,624
of your text for that we can say text color.
And we can pass a color what that color should
3597
04:37:30,624 --> 04:37:31,624
be defined inside another XML file called
color. Right now we have three colors, this
3598
04:37:31,624 --> 04:37:32,624
color, accent, color, primary and color primary
dark. Basically, these are the names of some
3599
04:37:32,624 --> 04:37:33,624
corners. Let's select the first one. And as
you can see the color has been changed successfully.
3600
04:37:33,624 --> 04:37:34,624
But where does this color dot XML file locates
in our project? Well, if you want to take
3601
04:37:34,624 --> 04:37:35,624
a look at that, you can open the project pane.
Inside the Resources folder, inside the values
3602
04:37:35,624 --> 04:37:36,624
folder, we have this colors dot XML, you can
see that we have some colors in here with
3603
04:37:36,624 --> 04:37:37,624
the exact name that we just saw. Don't worry
about this, we are going to talk about different
3604
04:37:37,624 --> 04:37:38,624
kinds of XML files in future videos. But for
now, I just wanted you to see this XML file.
3605
04:37:38,624 --> 04:37:39,624
So instead of passing a color from inside
the color dot XML file, you can also pass
3606
04:37:39,624 --> 04:37:40,624
the hexadecimal value of that color. For that
you can search the internet for something
3607
04:37:40,624 --> 04:37:41,624
like HTML color picker.
3608
04:37:41,624 --> 04:37:42,624
Let's search that. For example. Let's select
the green color in here. This one maybe, and
3609
04:37:42,624 --> 04:37:43,624
in here, you can see the hexadecimal value.
Let's copy that. And instead of facing a color
3610
04:37:43,624 --> 04:37:44,624
from inside our color dot XML file, I'm going
to paste the value of that color, you can
3611
04:37:44,624 --> 04:37:45,624
see that the color of the TextView has been
changed to the color that we just selected.
3612
04:37:45,624 --> 04:37:46,624
If you remember from the previous videos of
this course, we have seen that when we wanted
3613
04:37:46,624 --> 04:37:47,624
to have access to all of these UI elements
inside our Java file, we needed an ID. Let's
3614
04:37:47,624 --> 04:37:48,624
add an ID attribute for this TextView as well,
let's say ID, the convention is to say add
3615
04:37:48,624 --> 04:37:49,624
plus ID slash. Let's select that. And let's
define some ID. Let's say txt welcome. The
3616
04:37:49,624 --> 04:37:50,624
ID attribute should be unique inside every
XML file. It means that two different elements
3617
04:37:50,624 --> 04:37:51,624
inside the same XML file cannot have the same
ID. Later on we will use this ID in our Java
3618
04:37:51,624 --> 04:37:52,624
file in order to have access to this TextView
there are much more attributes to text us
3619
04:37:52,624 --> 04:37:53,624
but I think if we continue we are going to
get word for that. I'm going to move on from
3620
04:37:53,624 --> 04:37:54,624
this TextView. And I'm going to define a button
after this TextView. For that, once again,
3621
04:37:54,624 --> 04:37:55,624
I can open an anchor bracket. And in here
I can say button. With this capital B, let's
3622
04:37:55,624 --> 04:37:56,624
select the first option. Once again, we have
these two mandatory attributes. And in here,
3623
04:37:56,624 --> 04:37:57,624
let's talk about the difference between this
wrap content and match band. Right now, if
3624
04:37:57,624 --> 04:37:58,624
I select match parent, let's also select match
parent for this height, you can see that this
3625
04:37:58,624 --> 04:37:59,624
button is covering all of this screen. And
that's because we have set the width and height
3626
04:37:59,624 --> 04:38:00,624
to match parent. If you want, you can change
this height for example to wrap content. And
3627
04:38:00,624 --> 04:38:01,624
here you can see the difference the width
is matching the parent but the height is wrapping
3628
04:38:01,624 --> 04:38:02,624
the buttons content. Let's also change the
width to wrap content. Beside wrap wanted
3629
04:38:02,624 --> 04:38:03,624
and match parent, you can always pass the
values manually, for example, I can say 150
3630
04:38:03,624 --> 04:38:04,624
DPI, you can see that the width has been changed.
But this is highly discouraged because our
3631
04:38:04,624 --> 04:38:05,624
application is going to be installed on devices
with different screen sizes. Sometimes if
3632
04:38:05,624 --> 04:38:06,624
we put some numbers for these, if we pass
the values manually, we may not know the exact
3633
04:38:06,624 --> 04:38:07,624
position of every element. And also sometimes
our elements might not fit in the screen in
3634
04:38:07,624 --> 04:38:08,624
some devices. So for that matter, I'm going
to change this font to wrap content in here.
3635
04:38:08,624 --> 04:38:09,624
Also, now that I have talked about different
screen sizes, I'm going to say that in here
3636
04:38:09,624 --> 04:38:10,624
inside this preview, you have this option
called devices for preview. If you click on
3637
04:38:10,624 --> 04:38:11,624
that, you can see a drop down. And from here,
you can select different screen sizes. For
3638
04:38:11,624 --> 04:38:12,624
example, let's select this font, and you can
check the looks of your application in different
3639
04:38:12,624 --> 04:38:13,624
screen sizes. For example, let's take a tablet,
you can see that it's somehow different. Don't
3640
04:38:13,624 --> 04:38:14,624
worry about this. Later on in the course we
are going to cover how we can have a consistent
3641
04:38:14,624 --> 04:38:15,624
look for our application in different screen
sizes. I just wanted to mention these options
3642
04:38:15,624 --> 04:38:16,624
above in here. Like for the text view, we
have a text option for the button as well.
3643
04:38:16,624 --> 04:38:17,624
Let's say text. And let's say Say hello. Similarly,
we have the option to add some margins. But
3644
04:38:17,624 --> 04:38:18,624
I'm not going to do that right now. Instead,
I'm going to move this button to below this
3645
04:38:18,624 --> 04:38:19,624
TextView. For that we have an attribute called
below this one here layouts below. And we
3646
04:38:19,624 --> 04:38:20,624
need to pass the ID of the other element.
For example, in this case, we need to pass
3647
04:38:20,624 --> 04:38:21,624
the ID of this TextView I can say at plus
ID slash. And I can say txt welcome, you can
3648
04:38:21,624 --> 04:38:22,624
see that the button has been moved to below
this TextView. But this is not entirely but
3649
04:38:22,624 --> 04:38:23,624
we want, we also need to center it horizontally.
Or that once again, we can see center horizontal
3650
04:38:23,624 --> 04:38:24,624
and we can pass through. Let's also add a
margin top.
3651
04:38:24,624 --> 04:38:25,624
Let's say margin top. And let's pass for example
15 dP, that's much better. For both of these
3652
04:38:25,624 --> 04:38:26,624
texts here and button you have another attribute
called background. Let's also see that you
3653
04:38:26,624 --> 04:38:27,624
can say background. And for example, we can
pass a color, let's pass the first color,
3654
04:38:27,624 --> 04:38:28,624
you can see that the background color of this
button has been changed somehow, you have
3655
04:38:28,624 --> 04:38:29,624
the same option for the TextView. If you want
you can apply that. I think this is an ugly
3656
04:38:29,624 --> 04:38:30,624
color. So I'm going to change that. Let's
see the other colors. Let's select this primary
3657
04:38:30,624 --> 04:38:31,624
dark. Nope, this is not my color. Okay, this
is better, you can always add the hexadecimal
3658
04:38:31,624 --> 04:38:32,624
value of the color that you want in here as
well. For different UI elements, we also have
3659
04:38:32,624 --> 04:38:33,624
another attribute called on click we have
seen the use of this athlete. When we have
3660
04:38:33,624 --> 04:38:34,624
created our first Android application, you
can guess it, we are going to set an event
3661
04:38:34,624 --> 04:38:35,624
listener for this button and we are going
to see what happens when we click on this
3662
04:38:35,624 --> 04:38:36,624
button. So let's say on click, and let's pass
the name of some method. For example, I'm
3663
04:38:36,624 --> 04:38:37,624
going to see on Hello btn clicked. Right now
you can see that we are getting this red warning
3664
04:38:37,624 --> 04:38:38,624
and that's because we need to create this
method inside our Java file. Let's finish
3665
04:38:38,624 --> 04:38:39,624
creating our button by using this slash. And
let's create this method inside our main activity
3666
04:38:39,624 --> 04:38:40,624
dot java file. So let's open that file. In
order to open some files, I can always switch
3667
04:38:40,624 --> 04:38:41,624
to this project thing. Or alternatively I
can press the Shift key two times You can
3668
04:38:41,624 --> 04:38:42,624
see that when I do that I have a search menu.
For example, in here, I can search for main
3669
04:38:42,624 --> 04:38:43,624
activity. And if I press Enter on the first
option, we can open our main activity dot
3670
04:38:43,624 --> 04:38:44,624
java file. This is just a helpful shortcut
that we can use from time to time. Okay, we
3671
04:38:44,624 --> 04:38:45,624
need to create the onClick method inside the
scope of our class and outside of the scope
3672
04:38:45,624 --> 04:38:46,624
of this onCreate method. If you remember the
signature of that method, it was something
3673
04:38:46,624 --> 04:38:47,624
like this, we needed to set the access modifier
to public, the return type was void, the name
3674
04:38:47,624 --> 04:38:48,624
of the method is going to be exactly the name
that we have passed in our XML file, let's
3675
04:38:48,624 --> 04:38:49,624
say on Hello btn clicked. For the inputs of
this method, if you remember, we had a view
3676
04:38:49,624 --> 04:38:50,624
object with capital V. This view object is
not important right now we just need to know
3677
04:38:50,624 --> 04:38:51,624
that we need to receive it as the input of
this method. Let's fast view as the name of
3678
04:38:51,624 --> 04:38:52,624
this input. And in here, we can have the logic
to handle the click listener on our button.
3679
04:38:52,624 --> 04:38:53,624
Also, if you have noticed, when I added this
view, and your import has been added above
3680
04:38:53,624 --> 04:38:54,624
in here, let's also minimize these imports
for now. This is one way of creating event
3681
04:38:54,624 --> 04:38:55,624
listeners for buttons. And also for that matter
for different UI elements. We have other options
3682
04:38:55,624 --> 04:38:56,624
in which may be more useful from time to time.
We'll take a look at them in a minute. But
3683
04:38:56,624 --> 04:38:57,624
in here, let's quickly write some logic. For
example, in here, I'm going to change the
3684
04:38:57,624 --> 04:38:58,624
text of my text view. For that, first of all,
I need to have access to my text view. I can
3685
04:38:58,624 --> 04:38:59,624
say text view, if you take a look at your
inputs, you will see that this TextView from
3686
04:38:59,624 --> 04:39:00,624
the widget package has been added to our class.
If you remember I said that equivalent to
3687
04:39:00,624 --> 04:39:01,624
every UI element. You have a class in Java.
Okay, let's name this TextView. txt welcome.
3688
04:39:01,624 --> 04:39:02,624
And let's initialize it, we can initialize
it like this, we can say find view by Id like
3689
04:39:02,624 --> 04:39:03,624
this set content view method, this find view
by ID is also another inner method inside
3690
04:39:03,624 --> 04:39:04,624
every activity. Right now this find view by
ID method is waiting for some integer, and
3691
04:39:04,624 --> 04:39:05,624
the integer is going to be the ID of our elements.
And here is how we can pass our ID for example,
3692
04:39:05,624 --> 04:39:06,624
we can say our.id. Once again, if you remember,
I said that these are in here stands for resources.
3693
04:39:06,624 --> 04:39:07,624
It's a special class in Java, and it will
give us the access to all of the static contents
3694
04:39:07,624 --> 04:39:08,624
in our project. So in here, I can say our.id
dot, let's pass the ID of our text here, which
3695
04:39:08,624 --> 04:39:09,624
was txt. Welcome, I believe now that we have
access to this txt, welcome. We can do some
3696
04:39:09,624 --> 04:39:10,624
operation on that. For example, I can say
txt. Welcome. That's it. txt. We have seen
3697
04:39:10,624 --> 04:39:11,624
this method previously. And inside this method,
I can pass a text. Let's say hello again.
3698
04:39:11,624 --> 04:39:12,624
Let's run the application. And let's see if
we can change the text of this TextView. I'm
3699
04:39:12,624 --> 04:39:13,624
going to run the application on pixel three
API level 29, you can select whichever device
3700
04:39:13,624 --> 04:39:14,624
that you want. Okay, right now we can see
the layout that we have created so far, which
3701
04:39:14,624 --> 04:39:15,624
is honestly oddly, but don't worry about that
we are going to work on on our designing power.
3702
04:39:15,624 --> 04:39:16,624
If I click on this button, you can expect
what would happen we are going to change the
3703
04:39:16,624 --> 04:39:17,624
text from hello to Hello again. Okay, there
was nothing special. In this video, I just
3704
04:39:17,624 --> 04:39:18,624
wanted you to have another look at what we
have done in the first section of this course,
3705
04:39:18,624 --> 04:39:19,624
which was to create your first Android application,
I just wanted you to have a better understanding
3706
04:39:19,624 --> 04:39:20,624
on what you have done in that video. Also,
we have learned about some other attributes
3707
04:39:20,624 --> 04:39:21,624
in here as well. Okay, in the next video,
we are going to continue our talk on different
3708
04:39:21,624 --> 04:39:22,624
UI elements. For example, we will talk about
edit texts. And also we will talk about different
3709
04:39:22,624 --> 04:39:23,624
ways that we can implement an onclicklistener
for our button. Okay, see you in the next
3710
04:39:23,624 --> 04:39:24,624
video. In the previous video, we have seen
one way of creating an onclicklistener for
3711
04:39:24,624 --> 04:39:25,624
our button. There are two more ways and in
here we are going to see them. For example,
3712
04:39:25,624 --> 04:39:26,624
in our layout file, let's quickly create a
button. But before that, I'm going to change
3713
04:39:26,624 --> 04:39:27,624
this constraint layout to a relative layout.
Let's delete this text and let's create a
3714
04:39:27,624 --> 04:39:28,624
button. That's a wrap content and wrap content
for the bits Android. Let's give it a text.
3715
04:39:28,624 --> 04:39:29,624
For example, let's say Say hello once again.
Let's say center and parent. And let's pass
3716
04:39:29,624 --> 04:39:30,624
through, we have seen these in the previous
video, so I'm not going to discuss them. Also,
3717
04:39:30,624 --> 04:39:31,624
if you're going to need an ID, let's say btn.
Hello, you can pass whatever you want. So
3718
04:39:31,624 --> 04:39:32,624
in the previous video, when we wanted to create
an onclicklistener, we would have add this
3719
04:39:32,624 --> 04:39:33,624
onClick attribute. In here, we need to pass
the name of the equivalent method inside our
3720
04:39:33,624 --> 04:39:34,624
Java file. But in this case, I'm going to
use another approach. And for that, I'm going
3721
04:39:34,624 --> 04:39:35,624
to delete this attribute. Okay, let's switch
back to our Java file. And let's define this
3722
04:39:35,624 --> 04:39:36,624
button. Inside my onCreate method, I'm going
to say button. Let's name it btn. Hello, is
3723
04:39:36,624 --> 04:39:37,624
equal to find view by ID dot btn. Hello. For
setting an onclicklistener for your button,
3724
04:39:37,624 --> 04:39:38,624
you can use the equivalent method you can
say btn Hello, dot set onclicklistener. You
3725
04:39:38,624 --> 04:39:39,624
can see that we have this method inside the
parentheses we need to pass an interface.
3726
04:39:39,624 --> 04:39:40,624
In this case it can be new view dot onclicklistener.
If you take a closer look, you can see that
3727
04:39:40,624 --> 04:39:41,624
this is an interface. Okay, now that I have
passed this new interface in here, this onClick
3728
04:39:41,624 --> 04:39:42,624
method has been overrided. And inside this
onClick method, I can write my code. For example,
3729
04:39:42,624 --> 04:39:43,624
if I want to print something into my console,
I can say print. Let's just say hello, let's
3730
04:39:43,624 --> 04:39:44,624
run the application. And let's see if we can
see any result. I have opened this Ron paneling
3731
04:39:44,624 --> 04:39:45,624
here and if I click on this say hello button,
you can see that Hello is being printed in
3732
04:39:45,624 --> 04:39:46,624
here. So this way, by using this set onclicklistener
method on our button, we can define the onClick
3733
04:39:46,624 --> 04:39:47,624
listener. Inside the parentheses of this method,
we have passed this new view dot onclicklistener
3734
04:39:47,624 --> 04:39:48,624
method. But there is also another way, let's
quickly see that. So instead of passing a
3735
04:39:48,624 --> 04:39:49,624
new interface in here, I'm going to implement
this exact interface in the declaration of
3736
04:39:49,624 --> 04:39:50,624
my class. If you remember from the Java session,
we can implement interfaces like this, we
3737
04:39:50,624 --> 04:39:51,624
can say implement, let's say onClick listener.
There are two options in here. The first one
3738
04:39:51,624 --> 04:39:52,624
is coming from Android dot view that view
package and the next one is from another one,
3739
04:39:52,624 --> 04:39:53,624
we need the first one. When we implement interfaces,
we also need to implement the methods, we
3740
04:39:53,624 --> 04:39:54,624
can do that by pressing Ctrl plus by we need
to implement this onClick method. As the input
3741
04:39:54,624 --> 04:39:55,624
of this onClick method, we are getting a view
object.
3742
04:39:55,624 --> 04:39:56,624
And now we can use this view in order to create
different cases. For example, we can say switch
3743
04:39:56,624 --> 04:39:57,624
on view, or V in this case, it's his name.
Let's say that get ID, we are going to create
3744
04:39:57,624 --> 04:39:58,624
our switch depending on the ID of this view
object. And after that I can say case, in
3745
04:39:58,624 --> 04:39:59,624
case it's our.id dot btn. Hello, the exact
ID that we have passed in here. In case this
3746
04:39:59,624 --> 04:40:00,624
is the ID let's write some code. For example,
I'm going to copy this line of code from here.
3747
04:40:00,624 --> 04:40:01,624
Let's also add a break and also a default
case. Now that I have implemented this method
3748
04:40:01,624 --> 04:40:02,624
at the declaration of point class, and also
I've implemented this onClick method, I can
3749
04:40:02,624 --> 04:40:03,624
remove this btn hollow dot set onclicklistener
method. Let's write it once again, let's say
3750
04:40:03,624 --> 04:40:04,624
btn Hello dot set onclicklistener. This time
instead of saying new view dot onclicklistener
3751
04:40:04,624 --> 04:40:05,624
I can say this. If you remember from the Java
session, this refers to the current object.
3752
04:40:05,624 --> 04:40:06,624
Right now we are inside this class which implements
this interface. So this method down in here
3753
04:40:06,624 --> 04:40:07,624
is happy with accepting this as the input.
So how's the follow things in here? First
3754
04:40:07,624 --> 04:40:08,624
of all, we are setting the onclicklistener
and we are passing the class itself as the
3755
04:40:08,624 --> 04:40:09,624
interface. Whenever the user clicks on this
button, this onClick method will be triggered
3756
04:40:09,624 --> 04:40:10,624
a view will be passed to this method in this
case it's a button later on we will talk about
3757
04:40:10,624 --> 04:40:11,624
that why a view and a button are equivalent.
We are getting the idea of this view and we
3758
04:40:11,624 --> 04:40:12,624
are creating a switch statement. In case the
ID of this view is our.id dot btn Hello the
3759
04:40:12,624 --> 04:40:13,624
ID of our button. We are printing this Hello
in here. If we had other buttons or for that
3760
04:40:13,624 --> 04:40:14,624
matter other views, we can create different
cases for them in here as well. Let's run
3761
04:40:14,624 --> 04:40:15,624
the application once again and let's see if
we can see the same result. Once again if
3762
04:40:15,624 --> 04:40:16,624
I click on this say hello bata we can see
that Hello has been printed into are on Council.
3763
04:40:16,624 --> 04:40:17,624
Also, I don't like to come down in here inside
this round in order to test my application
3764
04:40:17,624 --> 04:40:18,624
means that I'm going to show something to
the user in our application. For that, instead
3765
04:40:18,624 --> 04:40:19,624
of using this system dot out dot print line,
I'm going to use another option called toast
3766
04:40:19,624 --> 04:40:20,624
messages. Let's search for those messages.
And let's see what those are like. So basically,
3767
04:40:20,624 --> 04:40:21,624
a toast message is something like this. This
is not a permanent message, and it will be
3768
04:40:21,624 --> 04:40:22,624
disappear after a period of time. Let's quickly
see that how can we define this in our code.
3769
04:40:22,624 --> 04:40:23,624
So in here, I'm going to delete this line
of code. Instead, I'm going to say toast,
3770
04:40:23,624 --> 04:40:24,624
you can see that I have two options in here.
The first one is a Java class, but the second
3771
04:40:24,624 --> 04:40:25,624
one is a template. Basically, templates are
provided by the ID that we are using. For
3772
04:40:25,624 --> 04:40:26,624
example, in here, if I select this template,
you can see that this whole line of code has
3773
04:40:26,624 --> 04:40:27,624
been created. So we are using this test class
in which has this static method. Inside this
3774
04:40:27,624 --> 04:40:28,624
static method, we are passing a context whatever
that is, we will talk about context later
3775
04:40:28,624 --> 04:40:29,624
on in the course. But for now, so we are using
this toast class English has this static method
3776
04:40:29,624 --> 04:40:30,624
called make text, we have three inputs for
this make text method. The first is a context,
3777
04:40:30,624 --> 04:40:31,624
don't worry about the context yet. We will
talk about that later on in the course. But
3778
04:40:31,624 --> 04:40:32,624
for now, just know that every activity in
Android is a context. And because right now
3779
04:40:32,624 --> 04:40:33,624
we are inside an activity, we can pass this
as our context. The next input is a text.
3780
04:40:33,624 --> 04:40:34,624
Basically, this is going to be the text that
we are going to show for example, let's say
3781
04:40:34,624 --> 04:40:35,624
hello button clicked.
3782
04:40:35,624 --> 04:40:36,624
After that, we have this constant in here,
which is toast dot length short, basically,
3783
04:40:36,624 --> 04:40:37,624
this is just a constant for determining how
long do we want our toast message. For example,
3784
04:40:37,624 --> 04:40:38,624
if you want to see the other time periods,
you can see that we have length long and length
3785
04:40:38,624 --> 04:40:39,624
short. Let's select length short. And after
this make text methods, we have this dot show
3786
04:40:39,624 --> 04:40:40,624
method, which you can guess is going to show
our toast message. So before this, we have
3787
04:40:40,624 --> 04:40:41,624
created our toast message. And after that,
with the help of this show method, we are
3788
04:40:41,624 --> 04:40:42,624
showing it to the user. In here we have used
the template. But if you don't want to use
3789
04:40:42,624 --> 04:40:43,624
the template, you can always create your own
toast message. You can say toast This time,
3790
04:40:43,624 --> 04:40:44,624
let's select the first one. Let's say make
text for the context list past these, we need
3791
04:40:44,624 --> 04:40:45,624
the text in here, let's say second text. And
after that, we need the constant, let's say
3792
04:40:45,624 --> 04:40:46,624
toast that last long dot show. Basically,
these two lines of code are the same. We just
3793
04:40:46,624 --> 04:40:47,624
can't create them with the help of the ID.
Okay, this time, let's run the application.
3794
04:40:47,624 --> 04:40:48,624
And let's see what would be the result. So
this time, if we click on this, say hello
3795
04:40:48,624 --> 04:40:49,624
butter, you can see that some toast message
is showing down in our screen. And after that
3796
04:40:49,624 --> 04:40:50,624
it will be disappeared. I think those messages
is much better. For our testing purposes.
3797
04:40:50,624 --> 04:40:51,624
It's much better than this round pain down
in here. Okay, let's close the application
3798
04:40:51,624 --> 04:40:52,624
in here. And let's talk about this view that
we are receiving in here in a logical way.
3799
04:40:52,624 --> 04:40:53,624
Instead of this view, we need to receive a
button because we are creating our switch
3800
04:40:53,624 --> 04:40:54,624
statement based on the ID of our button. But
there is a point in here, if you press down
3801
04:40:54,624 --> 04:40:55,624
the control key. And if you click on this
button class, once again, if you press down
3802
04:40:55,624 --> 04:40:56,624
the CTRL key and if you click on the class
that this button is extending, in this case,
3803
04:40:56,624 --> 04:40:57,624
this text view, you can see that this text
view class is extending the view class. It
3804
04:40:57,624 --> 04:40:58,624
means that after two times of inheritance,
our button is somehow a view view just extended
3805
04:40:58,624 --> 04:40:59,624
the view class two times. So at the end, a
button is a view. If you remember from the
3806
04:40:59,624 --> 04:41:00,624
Java session when we have talked about inheritance.
For example, when we extended the animal class
3807
04:41:00,624 --> 04:41:01,624
and we created our dog class, we said that
a dog is an animal. In this case, a button
3808
04:41:01,624 --> 04:41:02,624
is also a view. For that matter. Every UI
element is a view, like text views, edit text,
3809
04:41:02,624 --> 04:41:03,624
and also other UI elements that we will talk
about later on in the course. So this in here
3810
04:41:03,624 --> 04:41:04,624
proves that we can create an onclicklistener
for our TextView and other UI elements as
3811
04:41:04,624 --> 04:41:05,624
well. So this onClick method is not limited
to our button. It's also worth mentioning
3812
04:41:05,624 --> 04:41:06,624
that when you create your own listener this
way, by implementing the interface in your
3813
04:41:06,624 --> 04:41:07,624
class declaration, you just need to create
one onClick method for all of your UI elements.
3814
04:41:07,624 --> 04:41:08,624
For example, if I had another method down
in here, I could have passed the ID of that
3815
04:41:08,624 --> 04:41:09,624
button to another case. And inside that case,
I could have write my logic. So I can have
3816
04:41:09,624 --> 04:41:10,624
as many methods and for that matter, as many
onClick listeners for my view elements, also,
3817
04:41:10,624 --> 04:41:11,624
this onClick listener is one of the available
events, listeners, we have others as well,
3818
04:41:11,624 --> 04:41:12,624
if you want, you can take a look at them by
saying btn Hello dot set, let's say set listener.
3819
04:41:12,624 --> 04:41:13,624
And you can see a lot of these options we
have onClick listener we have on direct listener,
3820
04:41:13,624 --> 04:41:14,624
you can guess the meaning of each one of these,
we have set on hover listener in case if we
3821
04:41:14,624 --> 04:41:15,624
hover over our button, we also have another
useful one. on long click listener, it means
3822
04:41:15,624 --> 04:41:16,624
that if we like press on a button, that's
great. Let's see this one. This time, instead
3823
04:41:16,624 --> 04:41:17,624
of implementing the on long click listener
in my class declaration, I'm going to use
3824
04:41:17,624 --> 04:41:18,624
the easier way by saying new view dot online
click listener.
3825
04:41:18,624 --> 04:41:19,624
This one, you can see that in here we have
a Boolean as the return type of this online
3826
04:41:19,624 --> 04:41:20,624
click method. This Boolean is just the callback
indicating if we have long press our button.
3827
04:41:20,624 --> 04:41:21,624
If you want your code inside this method to
work fine, you need to pass through. Let's
3828
04:41:21,624 --> 04:41:22,624
write some logic. In here. For example, I'm
going to say toast. Once again, we can see
3829
04:41:22,624 --> 04:41:23,624
this context in here. But the format is somehow
different. Because right now we are inside
3830
04:41:23,624 --> 04:41:24,624
an interface, we are inside this online click
listener interface. If we pass this, this
3831
04:41:24,624 --> 04:41:25,624
will refer to the current object, which in
this case is an interface. But interfaces
3832
04:41:25,624 --> 04:41:26,624
are not context. And we need to pass our activity.
For that matter, we can say main activity
3833
04:41:26,624 --> 04:41:27,624
dot this, don't worry about this main activity
that this if you didn't get that I will talk
3834
04:41:27,624 --> 04:41:28,624
about that later on in the course when we
have talked about activities. But for now,
3835
04:41:28,624 --> 04:41:29,624
just let's pass this as our context. And let's
create a text. Let's also change the time
3836
04:41:29,624 --> 04:41:30,624
period in here to Lance long. I think I need
to delete this case from here as well. Okay,
3837
04:41:30,624 --> 04:41:31,624
let's run the application. So like before,
if I click on this button, you can see that
3838
04:41:31,624 --> 04:41:32,624
we are seeing this message Hello button clicked,
which is a simple onclicklistener for our
3839
04:41:32,624 --> 04:41:33,624
button. But if I press my button and hold
the key, you can see that we are seeing this
3840
04:41:33,624 --> 04:41:34,624
long press message. So this time, instead
of this onClick, we have executed this set
3841
04:41:34,624 --> 04:41:35,624
online click listener method. You can also
check the other event listeners for your view
3842
04:41:35,624 --> 04:41:36,624
items. Later on in the course we will check
a few more of them. But in general, you can
3843
04:41:36,624 --> 04:41:37,624
say btn Hello dot set listener. And you can
use each one of these that you want. Okay,
3844
04:41:37,624 --> 04:41:38,624
let's move on from the event listeners. And
let's define another UI element in our layout
3845
04:41:38,624 --> 04:41:39,624
file. Before this button, I am going to define
an edit text. Let's say Edit Text. For the
3846
04:41:39,624 --> 04:41:40,624
width, that's a match fine this time. And
for the height, that's a wrap content, you
3847
04:41:40,624 --> 04:41:41,624
can see that it's something like this, if
you click on that this is our edit text. Let's
3848
04:41:41,624 --> 04:41:42,624
also add a text in here. Let's say name and
name has been added. That seems to do better.
3849
04:41:42,624 --> 04:41:43,624
Let's also center our edit text, I'm going
to say centering parent, and I'm going to
3850
04:41:43,624 --> 04:41:44,624
pass through. But right now we have a conflict
between this edit text and our button. For
3851
04:41:44,624 --> 04:41:45,624
that I'm going to move this button to below
this edit text. But before that, I need to
3852
04:41:45,624 --> 04:41:46,624
give an ID to this edit text. Let's say ID.
Let's just name it edit text name. Let's finish
3853
04:41:46,624 --> 04:41:47,624
creating this edit text. And then in here
inside our button element, I can say below
3854
04:41:47,624 --> 04:41:48,624
or to be a specific layout below. And I can
pass the ID of my edit text before that I
3855
04:41:48,624 --> 04:41:49,624
need this ad plus ID slash. That's the Edit
Text name. So this way, we have moved this
3856
04:41:49,624 --> 04:41:50,624
button to below our edit text. I think it's
better to have some margins. That's a margin
3857
04:41:50,624 --> 04:41:51,624
top. And let's pass 20 dP, that seems better.
Okay, let's switch back to our edit text.
3858
04:41:51,624 --> 04:41:52,624
I don't like the text inside my edit text.
Because if I run the application, and if I
3859
04:41:52,624 --> 04:41:53,624
try to type something in here, first of all
I need to delete this text for that instead
3860
04:41:53,624 --> 04:41:54,624
of this text attribute. I'm going to use another
attribute called hint. I think we have hint
3861
04:41:54,624 --> 04:41:55,624
at the first videos of this course. Let's
just pass name. This time it's much better.
3862
04:41:55,624 --> 04:41:56,624
So whenever I click on this edit text, and
if Try to type something, this name hint will
3863
04:41:56,624 --> 04:41:57,624
be disappeared. Also, if we want to decrease
the width of our edit text, we can pass a
3864
04:41:57,624 --> 04:41:58,624
number in here, for example, we can say 250
dP, which is better in this case. But if you
3865
04:41:58,624 --> 04:41:59,624
remember I said that it's never a good idea
to pass your width and height manually, because
3866
04:41:59,624 --> 04:42:00,624
in different screen sizes, you cannot predict
the size of your different elements. For that,
3867
04:42:00,624 --> 04:42:01,624
once again, I'm going to change this font
to match that I believe it was. And after
3868
04:42:01,624 --> 04:42:02,624
that's done in here, I'm going to add two
margins, let's say margin left 250 dP, for
3869
04:42:02,624 --> 04:42:03,624
example, I think it's better to change it
to 100. And also, let's add a margin right
3870
04:42:03,624 --> 04:42:04,624
of 100.
3871
04:42:04,624 --> 04:42:05,624
Adding different margins is one way but we
also have another way, let's we can see that
3872
04:42:05,624 --> 04:42:06,624
as well. So I'm going to delete both of these
margins. Instead, at the declaration of my
3873
04:42:06,624 --> 04:42:07,624
relative layout inside the opening tag. As
you can see, this is our closing tag, and
3874
04:42:07,624 --> 04:42:08,624
this is our opening tag. Inside the opening
tag, I can define an attribute called padding,
3875
04:42:08,624 --> 04:42:09,624
once again, like margins, and we have multiple
paddings. We have padding bottom padding left,
3876
04:42:09,624 --> 04:42:10,624
right top, let's just use padding in here.
And let's pass 100 dP. So as you can see,
3877
04:42:10,624 --> 04:42:11,624
the size of our edit text has been changed,
the padding and the margin are a lot like
3878
04:42:11,624 --> 04:42:12,624
each other. Just there is one difference.
When you are adding a margin. For example,
3879
04:42:12,624 --> 04:42:13,624
in the previous case, for our edit text, the
margin would be added from the end of our
3880
04:42:13,624 --> 04:42:14,624
element, for example, from here to the end
of our screen. But when you're adding a padding,
3881
04:42:14,624 --> 04:42:15,624
depth padding, we'll add some space from inside
your element. For example, in this case, the
3882
04:42:15,624 --> 04:42:16,624
element is this relative layout itself. And
we have added the padding from inside the
3883
04:42:16,624 --> 04:42:17,624
relative layout. So once again, margin adds
the space from the outside of the elements,
3884
04:42:17,624 --> 04:42:18,624
but the padding will add the space from the
inside of the element, you can use both of
3885
04:42:18,624 --> 04:42:19,624
them depending on your need. Let's see what
other attributes do we have for our edit text,
3886
04:42:19,624 --> 04:42:20,624
like a TextView, we can change the color for
this edit text as well. For example, I can
3887
04:42:20,624 --> 04:42:21,624
say text color. And let's pass this color
that we have in our color resources. Right
3888
04:42:21,624 --> 04:42:22,624
now we do not have any text inside our edit
text. But as soon as we type something, the
3889
04:42:22,624 --> 04:42:23,624
color will be changed to this color that we
can see in here. We can also style the text
3890
04:42:23,624 --> 04:42:24,624
by saying text style this one, we can say
italic bold normal. Or if we want to use two
3891
04:42:24,624 --> 04:42:25,624
sides at the same time, we can add a pipeline.
There is another attribute that we didn't
3892
04:42:25,624 --> 04:42:26,624
have in text views. And that's called lines.
For example, there are times that you may
3893
04:42:26,624 --> 04:42:27,624
need a bigger input from the user, you can
specify the number of lines in here. For example,
3894
04:42:27,624 --> 04:42:28,624
if I say four, for example, now our edit text
is going to accept four lines of inputs. In
3895
04:42:28,624 --> 04:42:29,624
this case, I'm not going to use these lines.
So I'm just going to delete that the default
3896
04:42:29,624 --> 04:42:30,624
number for different lines is one. With edit
text, you can also define the kind of input
3897
04:42:30,624 --> 04:42:31,624
that you're going to receive. For that you
can say input type this one, and you can pass
3898
04:42:31,624 --> 04:42:32,624
one of these for example, if it's a date that
I'm going to receive, I can pass a date or
3899
04:42:32,624 --> 04:42:33,624
maybe this date time. If I'm going to receive
a number password, I can pass this number
3900
04:42:33,624 --> 04:42:34,624
password. If it's just a simple text, I can
pass this text, we have other options, for
3901
04:42:34,624 --> 04:42:35,624
example, for different emails, and all sorts
of options that you can see in here. For example,
3902
04:42:35,624 --> 04:42:36,624
if I select this text password in here, and
if I add some text instead of this hint, let's
3903
04:42:36,624 --> 04:42:37,624
say text. You can see that when I typed the
text, it will change it to these dots. Probably
3904
04:42:37,624 --> 04:42:38,624
you have seen this on your phone when you
are trying to log into some application or
3905
04:42:38,624 --> 04:42:39,624
some website, this will add some level of
security. Also when you define it to email,
3906
04:42:39,624 --> 04:42:40,624
this text email address when the user clicks
on your edit text, the keyword will be prepared
3907
04:42:40,624 --> 04:42:41,624
to type the email address. I'm not going to
define any specific type of input so I'm going
3908
04:42:41,624 --> 04:42:42,624
to delete this one in here as well. And let's
also change this one to him. Okay, I think
3909
04:42:42,624 --> 04:42:43,624
that's enough about edit text. We have all
sorts of different options in which you can
3910
04:42:43,624 --> 04:42:44,624
check them from here. As you can see, there
are 1000 of these attributes. Okay, below
3911
04:42:44,624 --> 04:42:45,624
this button. Let's also define a TextView.
I'm going to say TextView wrap content and
3912
04:42:45,624 --> 04:42:46,624
wrap content. Let's give it an ID, let's say.
txt Hello. After that may be some placeholder
3913
04:42:46,624 --> 04:42:47,624
text. Let's just say hello. Let's also move
it to below this button, I'm going to say
3914
04:42:47,624 --> 04:42:48,624
layout below. Also, it's worth noticing that
if you are not using a relative layout, for
3915
04:42:48,624 --> 04:42:49,624
example, if you're using a constraint layout,
you do not have this layout builder option.
3916
04:42:49,624 --> 04:42:50,624
This is one of the features of this relative
layout. We will talk about that in future
3917
04:42:50,624 --> 04:42:51,624
videos. Okay, in here, let's pass the idea
of our button, let's say btn. Hello. Also,
3918
04:42:51,624 --> 04:42:52,624
let's move it to center horizontal.
3919
04:42:52,624 --> 04:42:53,624
And let's add some margins. Margin top of
maybe 30 dP, let's increase the size of this
3920
04:42:53,624 --> 04:42:54,624
text by saying text size. And by passing I
believe 20 would be fine. I'm almost done
3921
04:42:54,624 --> 04:42:55,624
with this layout file. Let's just go top in
here into this code. And let's select this
3922
04:42:55,624 --> 04:42:56,624
reformat code in order to rearrange our code
so that if I publish the code, you will have
3923
04:42:56,624 --> 04:42:57,624
the same code that I'm writing in here. Okay,
let's switch to the main activity. And let's
3924
04:42:57,624 --> 04:42:58,624
define all these three items. Let's say Edit
Text. Let's name it. EDT. txt, I believe it
3925
04:42:58,624 --> 04:42:59,624
was name is equal to find view by ID r.id
dot edit text name. Let's also define our
3926
04:42:59,624 --> 04:43:00,624
text view. Let's name it txt name is equal
to find view by ID dot txt name, or txt Hello
3927
04:43:00,624 --> 04:43:01,624
was it I think it's better to change the name
to txt Hello. But right now there is a problem
3928
04:43:01,624 --> 04:43:02,624
with our code. We are instantiating our UI
elements inside this I create method, which
3929
04:43:02,624 --> 04:43:03,624
once again is the starting point of our application.
But we are going to need this to UI elements
3930
04:43:03,624 --> 04:43:04,624
inside this onClick method. Because we are
going to change the value of this TextView
3931
04:43:04,624 --> 04:43:05,624
whenever the user clicks on our button. And
if I try to have access to this txt, hello,
3932
04:43:05,624 --> 04:43:06,624
for example, from inside this method, if I
type txt Hello, you can see that I'm getting
3933
04:43:06,624 --> 04:43:07,624
a red warning, you can see that the ID cannot
resolve this symbol, it means that inside
3934
04:43:07,624 --> 04:43:08,624
this method, we do not have access to this
TextView. I'm not sure if I've mentioned this
3935
04:43:08,624 --> 04:43:09,624
in the Java section. But the problem in here
is occurring because of the scope in Java.
3936
04:43:09,624 --> 04:43:10,624
So right now the scope of this TextView is
inside this onCreate method. And outside of
3937
04:43:10,624 --> 04:43:11,624
that we cannot use that if we want to have
access to this TextView. Inside both of these
3938
04:43:11,624 --> 04:43:12,624
methods, we can say something like this, we
can declare this TextView in the declaration
3939
04:43:12,624 --> 04:43:13,624
of our class as different fields. For example,
above in here, I can say private, let's say
3940
04:43:13,624 --> 04:43:14,624
TextView this one. And let's name it. txt
Hello. But right now in here, if I try to
3941
04:43:14,624 --> 04:43:15,624
instantiate my TextView, for example, if I
say find view by ID and I pass my ID, because
3942
04:43:15,624 --> 04:43:16,624
this part in here is not a part of this activity
lifecycle. We are not going to instantiate
3943
04:43:16,624 --> 04:43:17,624
our TextView at all, we need to instantiate
our element inside this onCreate method, which
3944
04:43:17,624 --> 04:43:18,624
is once again the starting point of our application.
So this way, we are separating the declaration
3945
04:43:18,624 --> 04:43:19,624
and instantiating of our UI element, I can
put a semicolon down in here. And this time,
3946
04:43:19,624 --> 04:43:20,624
instead of defining the whole TextView I can
use this field I can say txt Hello response
3947
04:43:20,624 --> 04:43:21,624
is equal to find view by ID r.id dot txt Hello.
Now that I have declared this txt Hello as
3948
04:43:21,624 --> 04:43:22,624
a field, I can use it in both of these methods.
For example, in here, I can say txt Hello
3949
04:43:22,624 --> 04:43:23,624
dot txt. Inside the set text method, I'm going
to need the text of this edit text. For that
3950
04:43:23,624 --> 04:43:24,624
I'm going to define this edit text as a field
as well. So let's say private edit text. Let's
3951
04:43:24,624 --> 04:43:25,624
name it really txt name. And then in here,
let's remove this edit text. Now I can pass
3952
04:43:25,624 --> 04:43:26,624
this edit text for example to the set text
method. Let's say hello plus the Edit Text
3953
04:43:26,624 --> 04:43:27,624
dot get text. If you remember from the first
videos of this course, I said that we need
3954
04:43:27,624 --> 04:43:28,624
a two string method as well. We have seen
this two string method in different Java classes.
3955
04:43:28,624 --> 04:43:29,624
It's just going to generate some text. In
this case, if we delete this to string and
3956
04:43:29,624 --> 04:43:30,624
if we just pass this edit text name that get
text if you press down the CTRL key and if
3957
04:43:30,624 --> 04:43:31,624
you hover over this get text method you can
see The return type of this get text method
3958
04:43:31,624 --> 04:43:32,624
is an editable. We do not know what that is
yet. But we just know that this is not a string,
3959
04:43:32,624 --> 04:43:33,624
and in here, we are going to need a string.
For that we need another level of conversion,
3960
04:43:33,624 --> 04:43:34,624
we need to convert the editable to a string.
So we can use this two string method. Okay,
3961
04:43:34,624 --> 04:43:35,624
I think after a lot of talking, our application
is ready, let's just run the application.
3962
04:43:35,624 --> 04:43:36,624
And let's see if we can see the behavior that
we wanted. Let's type something in here. Let's
3963
04:43:36,624 --> 04:43:37,624
say Mesa. And let's press this pattern alone
based on our application is working fine.
3964
04:43:37,624 --> 04:43:38,624
And hopefully, we understand each piece of
the code that we have wrote so far, few minutes
3965
04:43:38,624 --> 04:43:39,624
earlier, I said that different UI elements
extend the view class, we have seen that for
3966
04:43:39,624 --> 04:43:40,624
this button, that's also check the case for
this edit text and text, you can press down
3967
04:43:40,624 --> 04:43:41,624
the control key, and you can click on this
edit text class, we can see that it's extending
3968
04:43:41,624 --> 04:43:42,624
the text view, if you click on this text view,
once again, by pressing down the control key,
3969
04:43:42,624 --> 04:43:43,624
you can see that it's extending the view class.
The same is for the TextView itself.
3970
04:43:43,624 --> 04:43:44,624
If you press down the control key, and if
we click on the text view, we can see that
3971
04:43:44,624 --> 04:43:45,624
it's extending the view class. Now that we
know both of these are extending the view
3972
04:43:45,624 --> 04:43:46,624
class, we can create an onclicklistener for
each one of them. And let's run inside this
3973
04:43:46,624 --> 04:43:47,624
onClick method. If you want, we can create
another case for each one of them. For example,
3974
04:43:47,624 --> 04:43:48,624
down in here, for the edit text, I can say
Edit Text name, dot set onclicklistener. You
3975
04:43:48,624 --> 04:43:49,624
can see that we have this option in here.
And because we are implementing the onclicklistener
3976
04:43:49,624 --> 04:43:50,624
interface inside our class declaration, I
can pass this in here. Later on inside the
3977
04:43:50,624 --> 04:43:51,624
onClick method, I can create another case.
For example, let's say case r.id dot, let's
3978
04:43:51,624 --> 04:43:52,624
say Edit Text name. If that's the case, let's
toast some message. Let's say attempting to
3979
04:43:52,624 --> 04:43:53,624
type something. Before I run my application,
I'm going to delete this toast because I'm
3980
04:43:53,624 --> 04:43:54,624
not going to have a conflict between different
hosts. So this time, if I click on the btn
3981
04:43:54,624 --> 04:43:55,624
Hello, the text is going to be changed to
Hello plus the text inside our edit text.
3982
04:43:55,624 --> 04:43:56,624
And if we click on the edit text itself, we
are going to see this toast message. By clicking
3983
04:43:56,624 --> 04:43:57,624
on the text, we can see the toast message
down in here that seems to be perfect. Okay,
3984
04:43:57,624 --> 04:43:58,624
I think that's enough for this video. In the
next video, we are going to talk about some
3985
04:43:58,624 --> 04:43:59,624
other UI elements like checkboxes, radio buttons,
and radio groups and also progress bar. Just
3986
04:43:59,624 --> 04:44:00,624
before I finish off this video, I'm going
to say that I will upload the source code
3987
04:44:00,624 --> 04:44:01,624
at my website@miko.org slash code. So feel
free to check that if you need. Okay, see
3988
04:44:01,624 --> 04:44:02,624
you in the next video. The first UI element
that we are going to talk about in this video
3989
04:44:02,624 --> 04:44:03,624
is checkbox. Let's quickly see that in our
layout file. Before everything, I'm going
3990
04:44:03,624 --> 04:44:04,624
to delete this TextView. And also I'm going
to change the constraint layout to relative
3991
04:44:04,624 --> 04:44:05,624
layout. Then in here, we can create our checkbox
like this. We can say checkbox for the width
3992
04:44:05,624 --> 04:44:06,624
and height, let's say wrapped content. You
can see that this little checkbox has been
3993
04:44:06,624 --> 04:44:07,624
added in here. If you want we can add a text
to this check box as well. Let's say text,
3994
04:44:07,624 --> 04:44:08,624
I'm going to enter the name of a movie. So
let's say Harry Potter. We can have an ID
3995
04:44:08,624 --> 04:44:09,624
as well as let's say ID. Let's name it checkbox
hiring. So basically a checkbox is a box that
3996
04:44:09,624 --> 04:44:10,624
we can check or uncheck. For example, in here
we have an attribute called checked. We can
3997
04:44:10,624 --> 04:44:11,624
pass through or false for this for example,
if we say true, you can see the checkbox will
3998
04:44:11,624 --> 04:44:12,624
be checked. Let's quickly add two more checkboxes.
Basically, I'm going to copy and paste this
3999
04:44:12,624 --> 04:44:13,624
one two more times. But this time, first of
all, I'm going to change the text to let's
4000
04:44:13,624 --> 04:44:14,624
say the matrix. And also let's change the
idea as well. Also, I'm going to add another
4001
04:44:14,624 --> 04:44:15,624
attribute I'm going to move this new checkbox
to the right of my previous checkbox. For
4002
04:44:15,624 --> 04:44:16,624
that we have an attribute called to write
of layout to write of. Let's pass the ID of
4003
04:44:16,624 --> 04:44:17,624
our first checkbox, let's say checkbox, Harry.
Let's also add a margin left. We can say margin
4004
04:44:17,624 --> 04:44:18,624
left and let's pass 15 If you For example,
let's copy and paste this one once again,
4005
04:44:18,624 --> 04:44:19,624
let's change the text to let's say Joker.
Let's also change the ID, let's say checkbox,
4006
04:44:19,624 --> 04:44:20,624
Joker. And also let's move it to the right
of checkbox matrix.
4007
04:44:20,624 --> 04:44:21,624
So this way, as you can see, we can have multiple
checkboxes, you can check and uncheck them
4008
04:44:21,624 --> 04:44:22,624
by default by setting an attribute of checked,
let's change the value for the Joker to let's
4009
04:44:22,624 --> 04:44:23,624
say, false. If we want to center this checkbox
horizontally, we can add a margin for the
4010
04:44:23,624 --> 04:44:24,624
first one. For example, we can say, margin
or margin left. And we can pass 25, for example.
4011
04:44:24,624 --> 04:44:25,624
But as you can see, this is not accurate.
Instead, what we can do is that we can create
4012
04:44:25,624 --> 04:44:26,624
another relative layout and move all of these
three checkboxes into that relative layout.
4013
04:44:26,624 --> 04:44:27,624
Let's quickly see how we can do that. So inside
my first relative layout, I'm going to create
4014
04:44:27,624 --> 04:44:28,624
another relative layout. Let's say relative
layout, for the width and height, let's say
4015
04:44:28,624 --> 04:44:29,624
wrap content. Let's also give it an ID. Let's
say ID. I'm going to name this fun movies,
4016
04:44:29,624 --> 04:44:30,624
relative layouts. Now, if I want, I can center
this relative left, I can say center horizontal,
4017
04:44:30,624 --> 04:44:31,624
let's pass through. Beside that let's add
a margin top, let's say margin top of 20 dP.
4018
04:44:31,624 --> 04:44:32,624
In other UI elements normally in here, we
could have finished creating our elements
4019
04:44:32,624 --> 04:44:33,624
by adding a slashing here. But in this case,
I'm not going to do that. Instead, I'm going
4020
04:44:33,624 --> 04:44:34,624
to finish creating my first tag my opening
tag. For that I can add a left anchor bracket.
4021
04:44:34,624 --> 04:44:35,624
And as you can see, this closing tag has been
created automatically. Now I can put my three
4022
04:44:35,624 --> 04:44:36,624
checkboxes inside these two tags, the opening
tag and the closing tag. Let's move all of
4023
04:44:36,624 --> 04:44:37,624
them to inside those two tags. If you noticed,
you can see that these three checkboxes are
4024
04:44:37,624 --> 04:44:38,624
now centered horizontally, and also they have
a margin top of 20 dP. That's because the
4025
04:44:38,624 --> 04:44:39,624
parents layout which is this relative layout
has these two attributes. So inside XML file,
4026
04:44:39,624 --> 04:44:40,624
you can have relative layout nested inside
another relative layout. Okay, now let's see
4027
04:44:40,624 --> 04:44:41,624
what can we do with these checkboxes inside
our Java code. For that, I'm going to open
4028
04:44:41,624 --> 04:44:42,624
my main activity dot java file. If you're
wondering, I'm pressing double shift in order
4029
04:44:42,624 --> 04:44:43,624
to see the search menu. First of all in here,
let's define our checkboxes. Once again, I'm
4030
04:44:43,624 --> 04:44:44,624
going to define them as fields, let's say
private checkbox. For the first one, let's
4031
04:44:44,624 --> 04:44:45,624
say checkbox, Harry. In here I have two options.
First of all, I can create my checkboxes line
4032
04:44:45,624 --> 04:44:46,624
after line. For example, I can say private
checkbox, and for the second one, we can name
4033
04:44:46,624 --> 04:44:47,624
it checkbox matrix. Or alternatively, because
the kind of these objects are the same, I
4034
04:44:47,624 --> 04:44:48,624
can add a comma in here and I can define my
second checkbox. For example, I can say checkbox
4035
04:44:48,624 --> 04:44:49,624
matrix. Let's also define the checkbox Joker.
And later on inside our on create method,
4036
04:44:49,624 --> 04:44:50,624
let's instantiate them. Let's say checkbox
Harry is equal to find view by ID r.id dot
4037
04:44:50,624 --> 04:44:51,624
checkbox sorry. Similarly for the next two.
If you want, you can have a click listener
4038
04:44:51,624 --> 04:44:52,624
for each one of these checkboxes For example,
I can say checkbox hiree dot set on check
4039
04:44:52,624 --> 04:44:53,624
change listener, this is the listener that
I need. Let's pass new unchecked change listener.
4040
04:44:53,624 --> 04:44:54,624
Once again, you can see that this is an interface.
The exact name is compound button that unchecked
4041
04:44:54,624 --> 04:44:55,624
change listener. Inside this interface, we
have this unchecked change method in which
4042
04:44:55,624 --> 04:44:56,624
we receive two elements. The compound button
itself, which is our checkbox in this case,
4043
04:44:56,624 --> 04:44:57,624
and also this is checked Boolean. So in here,
I can create an if statement, I can say if
4044
04:44:57,624 --> 04:44:58,624
is checked. Let's show a toast message. Let's
say you have watched Harry Potter. But in
4045
04:44:58,624 --> 04:44:59,624
the else case, let's show another source message.
This time I'm going to say you need to watch
4046
04:44:59,624 --> 04:45:00,624
Harry Potter. Before I write the logic for
the next two checkboxes, let's run our application
4047
04:45:00,624 --> 04:45:01,624
and let's see what would be the result.
4048
04:45:01,624 --> 04:45:02,624
Right now Harry Potter is checked but we are
not seeing any toast message and that's because
4049
04:45:02,624 --> 04:45:03,624
the onclicklistener in here is going to react
when we click on this checkbox. For example,
4050
04:45:03,624 --> 04:45:04,624
if I uncheck this checkbox, we can see that
you need to watch Harry Potter. Let's also
4051
04:45:04,624 --> 04:45:05,624
check it. You have watched Harry Potter. But
what if we want to see the toast message when
4052
04:45:05,624 --> 04:45:06,624
we run the application without any clicking
on our checkbox. For that we have an option
4053
04:45:06,624 --> 04:45:07,624
here before setting this onclicklistener.
We can check that if our checkbox is checked
4054
04:45:07,624 --> 04:45:08,624
or not. For example, for that I can create
an if statement, I can say if checkbox hiree.is
4055
04:45:08,624 --> 04:45:09,624
checked. As you can see, this method is going
to return a Boolean. And as its name applies,
4056
04:45:09,624 --> 04:45:10,624
it's going to check that if our checkbox is
checked or not. So if it's checked, I'm going
4057
04:45:10,624 --> 04:45:11,624
to show the toast message. Let's copy it from
here. But in the else case, I'm going to show
4058
04:45:11,624 --> 04:45:12,624
another toast message. Once again, we can
copy it from down here. Let's run the application.
4059
04:45:12,624 --> 04:45:13,624
As you can see, when I ran the application
without any clicking, I saw that was message.
4060
04:45:13,624 --> 04:45:14,624
So from time to time, this is check method
is going to be useful. I'm not going to define
4061
04:45:14,624 --> 04:45:15,624
an onclicklistener for the next two checkboxes
because I'm sure that you got my point. Also,
4062
04:45:15,624 --> 04:45:16,624
like in the previous video, instead of passing
this new compound button that unchecked change
4063
04:45:16,624 --> 04:45:17,624
listener, you can implement this listener
at the declaration of your class for example,
4064
04:45:17,624 --> 04:45:18,624
in here, and after that you need to override
this unchecked, change the method. After that
4065
04:45:18,624 --> 04:45:19,624
you can pass this instead of this new listener
in here into your set on check change listener
4066
04:45:19,624 --> 04:45:20,624
method. But once again, I'm not going to go
through that. Instead, let's switch back to
4067
04:45:20,624 --> 04:45:21,624
our activity main dot XML file. And let's
talk about another UI element. But before
4068
04:45:21,624 --> 04:45:22,624
that, I'm going to minimize this relative
layouts. The next UI element that I'm going
4069
04:45:22,624 --> 04:45:23,624
to talk about is a radio button. That's a
radio button, this one down in here. For the
4070
04:45:23,624 --> 04:45:24,624
width and height, let's say wrap content,
you can see that this is this circular button.
4071
04:45:24,624 --> 04:45:25,624
Let's move on to below our relative layout.
I can say below or layout below. I think it
4072
04:45:25,624 --> 04:45:26,624
was named movies relative layout. Let's also
add two margins, margin left of let's say
4073
04:45:26,624 --> 04:45:27,624
25 dP and also margin top of maybe 20 db,
like checkboxes, we can also have text, that's
4074
04:45:27,624 --> 04:45:28,624
a text. For the text, I'm just going to say
married. This is going to determine the marital
4075
04:45:28,624 --> 04:45:29,624
status of the user. So that's what this text
is about. Let's also give it an ID. Let's
4076
04:45:29,624 --> 04:45:30,624
say radio button married. Like checkboxes.
If you want to check this married radio button,
4077
04:45:30,624 --> 04:45:31,624
you can say checked and you can pass through.
If you want to have multiple radio buttons,
4078
04:45:31,624 --> 04:45:32,624
you can say something like this, you can copy
and paste this radio button, but there is
4079
04:45:32,624 --> 04:45:33,624
going to be some problem with our code. Before
that, let's change the text and ID of this
4080
04:45:33,624 --> 04:45:34,624
radio button. For example in here, let's say
syngo. And after that, let's change the ID
4081
04:45:34,624 --> 04:45:35,624
to RV singer. Let's also move this one to
write off our radio button married, let's
4082
04:45:35,624 --> 04:45:36,624
say to write off radio button marriage. So
the purpose of radio buttons in programming
4083
04:45:36,624 --> 04:45:37,624
is that among multiple radio buttons, you
can only select one. This is the difference
4084
04:45:37,624 --> 04:45:38,624
between radio button and checkbox. When you
have checkboxes, you can check multiple checkboxes,
4085
04:45:38,624 --> 04:45:39,624
both when you have radio buttons, you can
only have one radio button checked. But as
4086
04:45:39,624 --> 04:45:40,624
you can see, our program is not functioning
right in here. And that's because we need
4087
04:45:40,624 --> 04:45:41,624
to group these radio buttons. And we can do
that by creating another item in here called
4088
04:45:41,624 --> 04:45:42,624
radio group. Before that, I'm going to delete
these two radio buttons. Let's say radio group.
4089
04:45:42,624 --> 04:45:43,624
This one. Once again, for the width and height
I'm going to say wrap content. Let's move
4090
04:45:43,624 --> 04:45:44,624
it below our movies relative layout.
4091
04:45:44,624 --> 04:45:45,624
Layout below ID movies rely on a margin top
maybe of 20 dP. And also let's center it horizontally,
4092
04:45:45,624 --> 04:45:46,624
center horizontal to true. Once again, because
I'm going to put some radio buttons inside
4093
04:45:46,624 --> 04:45:47,624
this radio group, I'm not going to finish
creating this radio group. Instead, I'm going
4094
04:45:47,624 --> 04:45:48,624
to finish creating this opening tag by adding
a left anchor bracket like a relative layout,
4095
04:45:48,624 --> 04:45:49,624
you can see that now we have an opening tag
and also a closing tag. Inside this radio
4096
04:45:49,624 --> 04:45:50,624
group. I can have my radio buttons, let's
say radio button, wrap content and wrap content.
4097
04:45:50,624 --> 04:45:51,624
Let's give it an ID let's say radio button
married a text maybe. And also, let's change
4098
04:45:51,624 --> 04:45:52,624
It's default value to checked, let's say checked
through. Now we can have multiple radio buttons.
4099
04:45:52,624 --> 04:45:53,624
Let's copy and paste this one. So as you can
see, I never moved this radio button to below
4100
04:45:53,624 --> 04:45:54,624
our first radio button, but it's moved automatically.
If we change the ID to, for example, RV single,
4101
04:45:54,624 --> 04:45:55,624
you can see that even though both of these
radio buttons are checked, but only one of
4102
04:45:55,624 --> 04:45:56,624
them is showing the checked value, so when
you are using radio buttons, only one of them
4103
04:45:56,624 --> 04:45:57,624
can have a checked value. For that, I'm going
to delete this checked attribute from the
4104
04:45:57,624 --> 04:45:58,624
second radio button. And also I'm going to
change the text to single. Let's also add
4105
04:45:58,624 --> 04:45:59,624
another one. For the ID, let's say radio button
in REL. And let's change the text to in a
4106
04:45:59,624 --> 04:46:00,624
relationship. Let's also delete this check
attribute. Right now our radio buttons are
4107
04:46:00,624 --> 04:46:01,624
vertical. If you want, you can have horizontal
radio buttons as well for that inside your
4108
04:46:01,624 --> 04:46:02,624
radio group. You can have an attribute called
orientation in which you can pass horizontal
4109
04:46:02,624 --> 04:46:03,624
or vertical. If I pass horizontal, you can
see that now we have our radio buttons horizontally
4110
04:46:03,624 --> 04:46:04,624
ordered, I think it's better to have some
margins between these radio buttons. So let's
4111
04:46:04,624 --> 04:46:05,624
quickly add some margins. Let's say margin
left of maybe 15 dP, I guess. Let's add another
4112
04:46:05,624 --> 04:46:06,624
one down in here. Okay, now let's see how
we can use these radio buttons, our radio
4113
04:46:06,624 --> 04:46:07,624
group inside our Java file. For that I do
not need to define my radio groups. Instead,
4114
04:46:07,624 --> 04:46:08,624
I just need to instantiate my radio group,
let's say radio group. Let's name it radio
4115
04:46:08,624 --> 04:46:09,624
group marital status. But I believe I need
to give an ID to this radio group. And I think
4116
04:46:09,624 --> 04:46:10,624
I didn't do that. Yes, in here I can see ID
let's say radio group marital status. In our
4117
04:46:10,624 --> 04:46:11,624
I create method that's also minimize this
if statement and also this onClick listener.
4118
04:46:11,624 --> 04:46:12,624
And after the checkboxes, let's instantiate
our radio group. We can say radio group, marital
4119
04:46:12,624 --> 04:46:13,624
status, fine view by ID r.id dot, radio group
marital status, we can have an onclicklistener
4120
04:46:13,624 --> 04:46:14,624
for this radio group by saying radio group
dot set unchecked change listener. So this
4121
04:46:14,624 --> 04:46:15,624
method is going to be called whenever we change
the radio button selected, and inside the
4122
04:46:15,624 --> 04:46:16,624
parentheses, you can guess I can pass new
unchecked change listener, radio group dot
4123
04:46:16,624 --> 04:46:17,624
unchecked change listener, which is an interface
and it will create this object change method.
4124
04:46:17,624 --> 04:46:18,624
One of the inputs of this method is this check
ID in which we can use that for example, we
4125
04:46:18,624 --> 04:46:19,624
can create the switch statement on this check
ID. And we can create our cases we can say
4126
04:46:19,624 --> 04:46:20,624
in case it's our.id dot, let's say radio button
married. So basically this checked ID is the
4127
04:46:20,624 --> 04:46:21,624
ID of our radio button, whichever is selected.
So in case if the user selects this radio
4128
04:46:21,624 --> 04:46:22,624
button married, I'm going to show a toast
message. Let's just say married. But in case
4129
04:46:22,624 --> 04:46:23,624
it's our that Id dot, let's say radio button
single. Let's show another toast message.
4130
04:46:23,624 --> 04:46:24,624
Let's also add another case for RV in well.
As usual, let's also create our default case.
4131
04:46:24,624 --> 04:46:25,624
And let's add a break. Let's run our application.
And let's see if we can see any result.
4132
04:46:25,624 --> 04:46:26,624
Right now this married is selected. If we
select the single you can see that the married
4133
04:46:26,624 --> 04:46:27,624
one has been unchecked. And also we have saw
the toast message. Let's select another one.
4134
04:46:27,624 --> 04:46:28,624
You can see the toast message which is perfect.
Right now we have an onclicklistener for our
4135
04:46:28,624 --> 04:46:29,624
different radio buttons inside this radio
group. And depending on that button, we are
4136
04:46:29,624 --> 04:46:30,624
doing something for example, in this case,
we are showing a toast message. But if we
4137
04:46:30,624 --> 04:46:31,624
want to do something without clicking on these
buttons, we can do something like this in
4138
04:46:31,624 --> 04:46:32,624
our code. So before this set on check change
listener. I can say radio group marital status
4139
04:46:32,624 --> 04:46:33,624
dot get checked radio button ID. As you can
guess this will return the ID of the checked
4140
04:46:33,624 --> 04:46:34,624
radio button. And I can save that inside an
integer. For example, I can say me checked
4141
04:46:34,624 --> 04:46:35,624
button. After that, I can create a similar
switch statement based on the checked button.
4142
04:46:35,624 --> 04:46:36,624
So let's say switch on the checked button.
And let's create our cases, I'm going to copy
4143
04:46:36,624 --> 04:46:37,624
and paste the cases from here. Let's run the
application. And let's see if we can see any
4144
04:46:37,624 --> 04:46:38,624
result. As you can see, we are not clicking
on any of these radio buttons, but we just
4145
04:46:38,624 --> 04:46:39,624
saw the Maritimes that seems to be perfect.
In some cases, we might need to check for
4146
04:46:39,624 --> 04:46:40,624
the checked radio button before clicking on
them. I'm going to talk about another UI elements
4147
04:46:40,624 --> 04:46:41,624
in this video. And that's called a progress
bar. Let's quickly see that as well. Before
4148
04:46:41,624 --> 04:46:42,624
that, I'm going to minimize this radio group.
After that down in here, let's say progress
4149
04:46:42,624 --> 04:46:43,624
bar, draft content and wrap content. Let's
give it an ID, let's say progress bar. Let's
4150
04:46:43,624 --> 04:46:44,624
also center it, let's say center and parent.
And let's pass through. Let's run the application.
4151
04:46:44,624 --> 04:46:45,624
And let's see what our application is going
to look like with this progress bar. As you
4152
04:46:45,624 --> 04:46:46,624
can see, an infinite progress bar is constantly
circulating, this might be useful, for example,
4153
04:46:46,624 --> 04:46:47,624
when we are downloading something from the
internet. In order to make our progress bar
4154
04:46:47,624 --> 04:46:48,624
visible or on visible, we have an option.
For example, in our XML file, if we want it
4155
04:46:48,624 --> 04:46:49,624
to be not visible, we can say visibility.
And we can pass on. Also we have this visibility
4156
04:46:49,624 --> 04:46:50,624
attribute for almost all of the other UI elements.
If we want, we can change it to gone. But
4157
04:46:50,624 --> 04:46:51,624
by default, it's visible. Let's change it
back to Bob. Later on in our Java code, we
4158
04:46:51,624 --> 04:46:52,624
need to change this visibility to visible
and whenever we are done with it, we are going
4159
04:46:52,624 --> 04:46:53,624
to change it to gone. Let's see how can we
do that in our Java code. Before everything,
4160
04:46:53,624 --> 04:46:54,624
let's minimize all of the extra methods and
codes. And let's instantiate our progress
4161
04:46:54,624 --> 04:46:55,624
bar. Above in here, I'm going to say private
progress bar. Let's name it progress bar.
4162
04:46:55,624 --> 04:46:56,624
And then in here inside the onCreate method,
let's say progress bar is equal to find view
4163
04:46:56,624 --> 04:46:57,624
by ID r.id dot progress bar. I do not have
any button in here in order to change the
4164
04:46:57,624 --> 04:46:58,624
visibility of this progress bar. Instead,
I'm going to change its visibility whenever
4165
04:46:58,624 --> 04:46:59,624
the user clicks on the radio button singer
for that insight the radio group marital status
4166
04:46:59,624 --> 04:47:00,624
listener inside the switch case, in case if
it's always single, instead of showing the
4167
04:47:00,624 --> 04:47:01,624
toast message or after the toast message,
I'm going to say progress bar dot set visibility.
4168
04:47:01,624 --> 04:47:02,624
And I can say visible means that whenever
the user clicks on this radio button in Braille,
4169
04:47:02,624 --> 04:47:03,624
I'm going to change the visibility of my progress
bar to gone. Let's copy this line of code.
4170
04:47:03,624 --> 04:47:04,624
And after the toast message. let's paste that.
And let's just change this constant to God
4171
04:47:04,624 --> 04:47:05,624
view.com. Let's run our application.
4172
04:47:05,624 --> 04:47:06,624
Right now we cannot see any progress bar because
in our XML file, we have added the visibility
4173
04:47:06,624 --> 04:47:07,624
attribute and we have set the gun value in
case if we click on this single, you can see
4174
04:47:07,624 --> 04:47:08,624
that we are seeing the progress bar. In case
if I click on this in a relationship, you
4175
04:47:08,624 --> 04:47:09,624
can guess the visibility will be changed to
gone. There is also another form of progress
4176
04:47:09,624 --> 04:47:10,624
bar. Let's quickly see that as well. So in
my activity main dot XML file, I can define
4177
04:47:10,624 --> 04:47:11,624
a style for my progress. But before that,
I'm going to change the visibility once again
4178
04:47:11,624 --> 04:47:12,624
to visible in order to see the progress bar
in our preview. And after that, let's say
4179
04:47:12,624 --> 04:47:13,624
style. If I type horizontal, we can see this
styling here widget dot appcompat that progress
4180
04:47:13,624 --> 04:47:14,624
bar dot horizontal. Let's change the width
to match bands so that we can see it better.
4181
04:47:14,624 --> 04:47:15,624
And also in here I have another option I can
say progress. And I can pass an integer For
4182
04:47:15,624 --> 04:47:16,624
example, I can say 30 you can see that the
progress of this progress bar has been changed
4183
04:47:16,624 --> 04:47:17,624
to 30. This type of progress bar is extremely
useful when you want to show the progress
4184
04:47:17,624 --> 04:47:18,624
of something for example, downloading a file.
Sometimes you may want to show that how much
4185
04:47:18,624 --> 04:47:19,624
of the file has been downloaded. You can also
set a maximum for your progress. For example,
4186
04:47:19,624 --> 04:47:20,624
you can see Max and can pass 100. Let's also
add some margin left and right margin left
4187
04:47:20,624 --> 04:47:21,624
of 20 dP margin right after ntdp. Okay, now
let's switch back to our Java file. And let's
4188
04:47:21,624 --> 04:47:22,624
see what we can do with this progress bar.
Before everything, I'm going to delete these
4189
04:47:22,624 --> 04:47:23,624
two lines of code, because I'm not going to
make my progress bar visible or on visible.
4190
04:47:23,624 --> 04:47:24,624
Let's also minimize this switch statement
and this whole listener for our radio group,
4191
04:47:24,624 --> 04:47:25,624
after instantiating, our progress bar, I'm
going to create a threat. If you remember
4192
04:47:25,624 --> 04:47:26,624
from the Java session, this is the simplest
way that we can create a threat. Let's say
4193
04:47:26,624 --> 04:47:27,624
threat threat is equal to new threat, we needed
to pass a runnable, let's say new runnable.
4194
04:47:27,624 --> 04:47:28,624
Inside this run method, I'm going to create
a for loop. Let's say for int i is equal to
4195
04:47:28,624 --> 04:47:29,624
zero, i less than 10, i plus plus, for every
time of looping, I'm going to increment the
4196
04:47:29,624 --> 04:47:30,624
progress of this progress bar by 10. So let's
say progress bar, dot increments progress
4197
04:47:30,624 --> 04:47:31,624
by as you can guess we need to pass an integer
in here, for example, let's pass that. After
4198
04:47:31,624 --> 04:47:32,624
that. Let's freeze this thread for half a
second. If you remember from the Java session,
4199
04:47:32,624 --> 04:47:33,624
we have used this way we said thread dot sleep.
But this way, there is a probability that
4200
04:47:33,624 --> 04:47:34,624
we create interrupted exceptions. For example,
you can see that we have an error in here.
4201
04:47:34,624 --> 04:47:35,624
And the error says that unhandled exception.
There is also another way in Android, I'm
4202
04:47:35,624 --> 04:47:36,624
going to use that in here. I can say system
clock dot sleep. So this way, we don't need
4203
04:47:36,624 --> 04:47:37,624
to create a try catch block. This wasn't available
in Java, this system clock is coming from
4204
04:47:37,624 --> 04:47:38,624
Android dot voice, you can check that by take
a look at these imports. This one in here,
4205
04:47:38,624 --> 04:47:39,624
Android dot o s that system collect is the
one that we just imported. After creating
4206
04:47:39,624 --> 04:47:40,624
our thread, we just need to start it we can
do that after the definition of our trade,
4207
04:47:40,624 --> 04:47:41,624
we can say thread dot start. So in here, we
have created this worker thread in which inside
4208
04:47:41,624 --> 04:47:42,624
that we are creating a for loop. And for each
time of looping, we are incrementing the progress
4209
04:47:42,624 --> 04:47:43,624
of our progress bar by 10. Also, after each
time of looping, we are sleeping the worker
4210
04:47:43,624 --> 04:47:44,624
thread for half a second. Let's quickly switch
back to our layout file. And let's delete
4211
04:47:44,624 --> 04:47:45,624
this progress attribute it was just for demonstration.
So if I remove this one, you can see that
4212
04:47:45,624 --> 04:47:46,624
the progress will change to zero. Let's run
the application. And let's see if we can see
4213
04:47:46,624 --> 04:47:47,624
the progress of our progress bar. As you can
see, we are changing the value of this progress
4214
04:47:47,624 --> 04:47:48,624
bar for every half a second. If you want,
you can also get the progress of your progress
4215
04:47:48,624 --> 04:47:49,624
bar at any moment that you want. For example,
in here you can say progress bar dot get progress.
4216
04:47:49,624 --> 04:47:50,624
As you can see this get progress is returning
an integer which is the current progress of
4217
04:47:50,624 --> 04:47:51,624
your progress bar. Sometimes you may need
that and here is how you can get that.
4218
04:47:51,624 --> 04:47:52,624
Okay, I think that's enough for this video.
Once again, like previous videos, I'm going
4219
04:47:52,624 --> 04:47:53,624
to upload the source code at my website@maker.org
slash quotes. Feel free to check that if you
4220
04:47:53,624 --> 04:47:54,624
need and also in the next video. Finally,
we are going to talk about the differences
4221
04:47:54,624 --> 04:47:55,624
between a relative layout, a constraint layout
and also a linear layout. I think it's going
4222
04:47:55,624 --> 04:47:56,624
to help you a lot with understanding the layout
files better. Okay, see you in the next video.
4223
04:47:56,624 --> 04:47:57,624
In this video, we will talk about different
layouts in Android. Specifically, we will
4224
04:47:57,624 --> 04:47:58,624
talk about relative layout, we will talk about
linear layout, and also constraint layout.
4225
04:47:58,624 --> 04:47:59,624
Let's start by talking about the relative
layout. Like before, I'm going to switch to
4226
04:47:59,624 --> 04:48:00,624
text view in my layout file. Let's also minimize
this project thing. And let's change this
4227
04:48:00,624 --> 04:48:01,624
constraint layout to relative layout. I'm
also going to enable this preview so that
4228
04:48:01,624 --> 04:48:02,624
we can see what we are doing. First of all,
let's delete this TextView. And let's start
4229
04:48:02,624 --> 04:48:03,624
from the beginning. So inside the relative
layout, all of our UI elements are somehow
4230
04:48:03,624 --> 04:48:04,624
related to each other, it means that the place
of every UI element is relative to another
4231
04:48:04,624 --> 04:48:05,624
layout, or maybe its parent, let's quickly
create a TextView and let's see the relativity
4232
04:48:05,624 --> 04:48:06,624
in action. Like before, I'm going to say wrap
content for the width and height. Let's also
4233
04:48:06,624 --> 04:48:07,624
specify a text. Let's just say hello world.
As you can see right now the TextView is placed
4234
04:48:07,624 --> 04:48:08,624
at the left corner of my screen if I want
I can change that. For example you have seen
4235
04:48:08,624 --> 04:48:09,624
in the previous videos that I Can center this
TextView vertically or horizontally. For that
4236
04:48:09,624 --> 04:48:10,624
I can say center horizontal. Let's pass through,
you can see that it's being centered. And
4237
04:48:10,624 --> 04:48:11,624
this is the relativity that I was talking
about. When we set this layout center horizontal
4238
04:48:11,624 --> 04:48:12,624
attribute to true. We are defining relativity
between this TextView and its parent. Let's
4239
04:48:12,624 --> 04:48:13,624
also center it vertically. Or instead of these
two attributes, I can simply say centering
4240
04:48:13,624 --> 04:48:14,624
parent. Let's also give an ID to this TextView.
Let's say txt Hello. Now if I define another
4241
04:48:14,624 --> 04:48:15,624
UI element, I can place that related to this
TextView. Let's quickly create a button in
4242
04:48:15,624 --> 04:48:16,624
here. Let's say wrap content and wrap content
for the width and height. Let's define a text.
4243
04:48:16,624 --> 04:48:17,624
Like before, let's say Say hello. Also, let's
define an ID. Now if I want to move this button
4244
04:48:17,624 --> 04:48:18,624
to below this TextView, I can simply say layout
below. And let's pass the ID of our TextView.
4245
04:48:18,624 --> 04:48:19,624
txt Hello, I can center this button as well.
For example, I can say center horizontally.
4246
04:48:19,624 --> 04:48:20,624
And let's pass through, we can also have a
margin for example, we can say margin top
4247
04:48:20,624 --> 04:48:21,624
layout margin top and we can pass some number
for example 15 db. Inside a relatively at
4248
04:48:21,624 --> 04:48:22,624
you can also have another relative layout.
Let's quickly see that for example in here,
4249
04:48:22,624 --> 04:48:23,624
I'm going to define a relative layout and
inside that relative layout, I am going to
4250
04:48:23,624 --> 04:48:24,624
put three text views. After that, I'm going
to move this TextView this high level two
4251
04:48:24,624 --> 04:48:25,624
below that relative layout. So I can see relative
layout. For the width and height. Let's say
4252
04:48:25,624 --> 04:48:26,624
wrap content, you can say match parent. Like
other UI elements, we can define an ID for
4253
04:48:26,624 --> 04:48:27,624
our relative layout. For example, I can say
first relative layout. So up until this point,
4254
04:48:27,624 --> 04:48:28,624
when we wanted to create our UI elements after
setting the attributes, we could have used
4255
04:48:28,624 --> 04:48:29,624
this backslash in order to finish creating
our UI elements. But in this case, because
4256
04:48:29,624 --> 04:48:30,624
we are going to put some other elements inside
this relative layout, we need to create an
4257
04:48:30,624 --> 04:48:31,624
opening tag and instead of using the backslash,
I am going to use the left anchor bracket.
4258
04:48:31,624 --> 04:48:32,624
By doing that, we also create this ending
tag. And inside this opening and ending tag,
4259
04:48:32,624 --> 04:48:33,624
now we can put our UI elements. For example,
we can pass our text views, let's say TextView.
4260
04:48:33,624 --> 04:48:34,624
Wrap content wrap content, I'm going to fast
forward everything for these three text views.
4261
04:48:34,624 --> 04:48:35,624
Like before, we can place our two text views
inside this relative layout correctly. For
4262
04:48:35,624 --> 04:48:36,624
example, we can move them to right of each
other. So for this second text view, I can
4263
04:48:36,624 --> 04:48:37,624
say to right off, let's say txt name, let's
also add a margin left of 10 dP. Similarly
4264
04:48:37,624 --> 04:48:38,624
for this third TextView, let's say to write
off our txt last name, and margin left of
4265
04:48:38,624 --> 04:48:39,624
maybe 10 dP once again, once again, this whole
relative layout is placed at the left corner
4266
04:48:39,624 --> 04:48:40,624
of our screen. If we want, we can use the
center vertical or center horizontal attributes
4267
04:48:40,624 --> 04:48:41,624
for this whole relative layout as well. So
inside the opening tag of this relative layout,
4268
04:48:41,624 --> 04:48:42,624
I can say center for example. And let's fast
through let's also add some margin top, let's
4269
04:48:42,624 --> 04:48:43,624
say margin top. And that's past 100 dP. You
can see that when I use these attributes on
4270
04:48:43,624 --> 04:48:44,624
a relative layout level, all of the UI elements
inside that relative layout respond to those
4271
04:48:44,624 --> 04:48:45,624
attributes. For example, in here we have centered
horizontally and also we have added the margin.
4272
04:48:45,624 --> 04:48:46,624
Now that we have this relative layout, we
can minimize that so that we can see everything
4273
04:48:46,624 --> 04:48:47,624
better. And after that, we can move this Hello
text to below this relative layout. But before
4274
04:48:47,624 --> 04:48:48,624
that, I need to delete the center parent attribute.
Once again, you can see that it moves to the
4275
04:48:48,624 --> 04:48:49,624
corner of our screen and because we have set
the layout below for this button, it moves
4276
04:48:49,624 --> 04:48:50,624
as well. So in here I can say layout below
and I can pass the ID of my relative left
4277
04:48:50,624 --> 04:48:51,624
it was first relative layer. Let's also center
it horizontally and let's pass some margin.
4278
04:48:51,624 --> 04:48:52,624
Center horizontal through margin top, let's
say 10 db. So my point being here was that
4279
04:48:52,624 --> 04:48:53,624
inside a relatively at or for that matter
inside any other kind of layout, you can have
4280
04:48:53,624 --> 04:48:54,624
other relative layouts as well. Some of these
attributes, for example, this layout below
4281
04:48:54,624 --> 04:48:55,624
are specific to relative layout. And if you're
inside some other kind of layout, for example,
4282
04:48:55,624 --> 04:48:56,624
a linear layout, and also constraint layout,
you cannot use this layout below. During the
4283
04:48:56,624 --> 04:48:57,624
course, we will see that each of these attributes
are specific to relative layout, before we
4284
04:48:57,624 --> 04:48:58,624
move to the next kind of layouts, let's also
switch to this design view. At the left pane
4285
04:48:58,624 --> 04:48:59,624
in here, you can see this component three,
as the name of this component tree applies.
4286
04:48:59,624 --> 04:49:00,624
This in here is showing us the hierarchy of
different elements in our UI. So for example,
4287
04:49:00,624 --> 04:49:01,624
in here, we have this parent relative layout.
Inside that we have this first relative layout,
4288
04:49:01,624 --> 04:49:02,624
and also txt, hello, and btn. Hello. Inside
our first relative layout, we have three text
4289
04:49:02,624 --> 04:49:03,624
views. Sometimes when you have a messy UI,
you may use this component tree in order to
4290
04:49:03,624 --> 04:49:04,624
keep track of different UI elements. Okay,
let's switch back to our TextView. And let's
4291
04:49:04,624 --> 04:49:05,624
start talking about linear layout. Before
that, I'm going to delete all of these. And
4292
04:49:05,624 --> 04:49:06,624
let's change the strategy of layout to a linear
layout. Inside a linear layout, all of the
4293
04:49:06,624 --> 04:49:07,624
UI elements will be placed line after line,
let's quickly see that I'm going to define
4294
04:49:07,624 --> 04:49:08,624
a TextView. Once again, wrap content and wrap
content. Let's pass the text. Let's say hello
4295
04:49:08,624 --> 04:49:09,624
world once again. Let's also give it an ID.
That's a txt Hello. So the first thing that
4296
04:49:09,624 --> 04:49:10,624
is different inside a linear layout is that
you do not have the center in Parent Center
4297
04:49:10,624 --> 04:49:11,624
horizontally or center vertically attributes
when you use linear layout. For example, in
4298
04:49:11,624 --> 04:49:12,624
here, I cannot say centering parent, you can
see that it's not available. If you want to
4299
04:49:12,624 --> 04:49:13,624
center your UI elements, you can have for
example, a relative layout inside this linear
4300
04:49:13,624 --> 04:49:14,624
layout. Let's move on from this part. And
let's add another UI element. Let's say button
4301
04:49:14,624 --> 04:49:15,624
wrap content wrap content for the text, let's
say Say hello.
4302
04:49:15,624 --> 04:49:16,624
And let's give it an ID, let's say btn. Hello,
you can see that even though I didn't use
4303
04:49:16,624 --> 04:49:17,624
any attribute to move this button to the right
of our TextView, it automatically placed to
4304
04:49:17,624 --> 04:49:18,624
the right of our TextView. And that's because
we are using a linear layout. Inside a linear
4305
04:49:18,624 --> 04:49:19,624
layout, all of our UI elements will be placed
one after another. If you want, you can change
4306
04:49:19,624 --> 04:49:20,624
the orientation of this linear layout so that
the UI elements will be placed one after another
4307
04:49:20,624 --> 04:49:21,624
vertically. Let's quickly do that. So in the
definition of my linear layout, I'm going
4308
04:49:21,624 --> 04:49:22,624
to pass an attribute in here, let's say orientation.
That's fast vertical, you can see that they
4309
04:49:22,624 --> 04:49:23,624
have been placed vertically. There is also
another attribute that I'm going to talk about
4310
04:49:23,624 --> 04:49:24,624
in linear layout. Let's quickly delete this
button. And let's define another TextView.
4311
04:49:24,624 --> 04:49:25,624
For the text, let's say name. And for the
ID, let's say txt. Like before, do you have
4312
04:49:25,624 --> 04:49:26,624
the margins. For example, you can say margin
top, and we can pass any number that we want.
4313
04:49:26,624 --> 04:49:27,624
But there is also another attribute in here
before I talk about that attribute, I'm going
4314
04:49:27,624 --> 04:49:28,624
to give some background color for these two
text fields so that we can see the difference
4315
04:49:28,624 --> 04:49:29,624
better. So I can say background, and I can
pass a color, for example, this color accent
4316
04:49:29,624 --> 04:49:30,624
that we have in our color resource. Let's
also set the color for the first TextView.
4317
04:49:30,624 --> 04:49:31,624
Let's say background, and let's fast color
primary this thing. So the attribute that
4318
04:49:31,624 --> 04:49:32,624
I'm going to talk about is wait. Let's quickly
see that you can see this layout waiting here.
4319
04:49:32,624 --> 04:49:33,624
And we can pass any number that we want. For
example, I can say 20. Let's also pass a weight
4320
04:49:33,624 --> 04:49:34,624
to this second TextView. And we will talk
about it. Let's say Wait, let's fast at for
4321
04:49:34,624 --> 04:49:35,624
example, you can probably guess the usage
of this weight attribute right now, it will
4322
04:49:35,624 --> 04:49:36,624
give our attribute so weight according to
the numbers that we have used. For example,
4323
04:49:36,624 --> 04:49:37,624
in this case, the entire width of our layout
is 20 plus 80, which is 100. And it will give
4324
04:49:37,624 --> 04:49:38,624
the first TextView 20% of the weight of the
whole layout. It doesn't matter what number
4325
04:49:38,624 --> 04:49:39,624
you put in here. For example, in here, if
I say two and for the second one, if I say
4326
04:49:39,624 --> 04:49:40,624
eight, it will work the same. Also, you can
see this space in here. That's because we
4327
04:49:40,624 --> 04:49:41,624
have this margin top if we remove that, you
can see that the whole layout will be divided
4328
04:49:41,624 --> 04:49:42,624
accordingly. So this rate was another attribute
that I was going to talk about. If I change
4329
04:49:42,624 --> 04:49:43,624
this orientation to horizontal, you can see
that it's working the same. There is not much
4330
04:49:43,624 --> 04:49:44,624
to this linear layout, I just wanted to show
you that there is value in your layout with
4331
04:49:44,624 --> 04:49:45,624
some specific attributes, for example, this
orientation and this weight. And in this course,
4332
04:49:45,624 --> 04:49:46,624
we are not going to use linear layout that
much it's for simple layouts, but you can
4333
04:49:46,624 --> 04:49:47,624
use it if you want. Before I remove all of
this, I'm going to say that inside a linear
4334
04:49:47,624 --> 04:49:48,624
layout, you can also have other layouts, for
example, a relative layout or a constraint
4335
04:49:48,624 --> 04:49:49,624
layout. Okay, let's remove one of these. And
let's talk about constraint layout. Before
4336
04:49:49,624 --> 04:49:50,624
that, I'm going to remove this orientation
because it was specific to linear layout.
4337
04:49:50,624 --> 04:49:51,624
Okay, let's say constraint layout. We have
two options in here, visit constraint and
4338
04:49:51,624 --> 04:49:52,624
visa dot constraint layout, this is the one
that we are going to need. For this constraint
4339
04:49:52,624 --> 04:49:53,624
layout, I'm going to switch to design view
because I think it's much better to work with
4340
04:49:53,624 --> 04:49:54,624
the design view. First of all, I'm going to
enable the blueprint so that we can see the
4341
04:49:54,624 --> 04:49:55,624
difference. If there is any, you can click
on this blue stack in here, and you can select
4342
04:49:55,624 --> 04:49:56,624
this design plus blueprint, we will talk about
the difference between this design mode and
4343
04:49:56,624 --> 04:49:57,624
this blueprint in a minute. Okay, for working
with the constraint layout, you can drag your
4344
04:49:57,624 --> 04:49:58,624
UI elements like before you can drag, for
example, a TextView. And as soon as I did
4345
04:49:58,624 --> 04:49:59,624
that, you can see that we have four circles
at the four edges of our UI element. Let's
4346
04:49:59,624 --> 04:50:00,624
zoom in a little bit, you can see that the
four circles are at the four edges of our
4347
04:50:00,624 --> 04:50:01,624
UI element. Let's zoom out a bit, I just wanted
to show you the four circles. Also, the shortcuts
4348
04:50:01,624 --> 04:50:02,624
that I'm using in here is Ctrl. Plus Plus
in order to zoom in and control plus or minus
4349
04:50:02,624 --> 04:50:03,624
in order to zoom out. Also at any time, you
can use Ctrl plus zero in order to have a
4350
04:50:03,624 --> 04:50:04,624
fit screen. It's also worth mentioning that
if you have a keyboard that has some numbers
4351
04:50:04,624 --> 04:50:05,624
and keys for the calculator, the plus minus
and zero from the calculator keys are not
4352
04:50:05,624 --> 04:50:06,624
going to be helpful in here. So the shortcuts
are going to be only useful for when you are
4353
04:50:06,624 --> 04:50:07,624
using the upper numbers. And also the upper
plus and minus. The most basic thing in a
4354
04:50:07,624 --> 04:50:08,624
constraint layout is to constrain your UI
elements right now you can see that we have
4355
04:50:08,624 --> 04:50:09,624
a red area in here. Similarly, we have the
same error in our components three, it says
4356
04:50:09,624 --> 04:50:10,624
that we need to constraint your UI element.
In order to constraint your UI elements, you
4357
04:50:10,624 --> 04:50:11,624
can drag one of these circles to for example,
the edge of your screen. So right now we have
4358
04:50:11,624 --> 04:50:12,624
added one constraint, if we switch back to
our TextView, you can see that we have this
4359
04:50:12,624 --> 04:50:13,624
constraint in here. I'm talking about these
two line of code in here. Okay, let's go back
4360
04:50:13,624 --> 04:50:14,624
to the design view, we can add another constraint.
For example, for the right constraint of our
4361
04:50:14,624 --> 04:50:15,624
UI element, we can add the constraint to the
right edge of our screen. But still this constraint
4362
04:50:15,624 --> 04:50:16,624
layout is not happy. It says that not vertically
constraint, it means that we need to at least
4363
04:50:16,624 --> 04:50:17,624
add another constraint in here. And now the
error is gone. And it has been changed to
4364
04:50:17,624 --> 04:50:18,624
some warning. If you want to center your UI
element into the center of your parent, you
4365
04:50:18,624 --> 04:50:19,624
can add another constraint for example, from
the bottom of this TextView to the bottom
4366
04:50:19,624 --> 04:50:20,624
of the screen. If you want to remove one of
these constraints, you can just click on the
4367
04:50:20,624 --> 04:50:21,624
circle and you can press the delete key, you
can see that the constraint will be deleted.
4368
04:50:21,624 --> 04:50:22,624
Let's press Ctrl Z in order to redo that.
If you take a look at this attributes painting
4369
04:50:22,624 --> 04:50:23,624
here, you can see that you have a slider for
this TextView for different constraints. Right
4370
04:50:23,624 --> 04:50:24,624
now the number says 50. But if you want you
can change that, for example, if you move
4371
04:50:24,624 --> 04:50:25,624
it a little bit, you can see that the TextView
responds accordingly. It will move according
4372
04:50:25,624 --> 04:50:26,624
to this slider. Similar to that we have this
horizontal slider in which you can guess we
4373
04:50:26,624 --> 04:50:27,624
can move our UI element. So constraining your
UI element to the edges of the screen is one
4374
04:50:27,624 --> 04:50:28,624
way you can also add constraints to other
UI elements. For example, if I have a button
4375
04:50:28,624 --> 04:50:29,624
in here, let's quickly add that and let's
delete the constraints of this text view all
4376
04:50:29,624 --> 04:50:30,624
of them. Let's move it so that we can see
everything better. This though, if we want
4377
04:50:30,624 --> 04:50:31,624
we can constrain this button to for example,
the bottom of this text view, we can see that
4378
04:50:31,624 --> 04:50:32,624
it moves accordingly. You can also see that
when I click on one of these circles, I can
4379
04:50:32,624 --> 04:50:33,624
see all of the available targets for example,
I can constrain it to the right or My TextView
4380
04:50:33,624 --> 04:50:34,624
sometimes it might not work, and you may want
to add that one more time. So this way, we
4381
04:50:34,624 --> 04:50:35,624
have constraints with our button to the bottom
of this TextView, and also to the right of
4382
04:50:35,624 --> 04:50:36,624
this TextView. If we want to make it center,
we can add another constraint to the left
4383
04:50:36,624 --> 04:50:37,624
of this TextView. And both of these UI elements
are now somehow centered. Let's also add a
4384
04:50:37,624 --> 04:50:38,624
margin so that we can see everything better.
So by clicking on one of your UI elements,
4385
04:50:38,624 --> 04:50:39,624
you can see that in the attributes pane, we
can add a margin. For example, from top, let's
4386
04:50:39,624 --> 04:50:40,624
add 32. If you take a look at this componentry
in here, you can see that we have no error
4387
04:50:40,624 --> 04:50:41,624
about this button. But we have an error about
this TextView. It means that we have all of
4388
04:50:41,624 --> 04:50:42,624
the necessary constraints about this button.
But we need to add some constraints about
4389
04:50:42,624 --> 04:50:43,624
this TextView. For example, we need to constraint
it vertically or maybe horizontally as well.
4390
04:50:43,624 --> 04:50:44,624
Like before, we can add the constraints to
the both edges of our screen. You can also
4391
04:50:44,624 --> 04:50:45,624
see that when I add the constraint, this button
moves as well, it means that no matter where
4392
04:50:45,624 --> 04:50:46,624
this TextView is, we want our button to be
concentrated to this TextView. Let's add another
4393
04:50:46,624 --> 04:50:47,624
one. Let's also add a margin top for this
TextView as well.
4394
04:50:47,624 --> 04:50:48,624
I think 32 is not enough. So let's say 100.
Let's also add another TextView. And let's
4395
04:50:48,624 --> 04:50:49,624
see other kinds of constraints. So constraining
your text fields, for example, from top of
4396
04:50:49,624 --> 04:50:50,624
this TextView to the top of this TextView.
And from bottom of this TextView to the bottom
4397
04:50:50,624 --> 04:50:51,624
of this TextView is one way of having two
text views at the same line horizontally.
4398
04:50:51,624 --> 04:50:52,624
There is also another way, I'm going to press
Ctrl Z two times so that we can do the constraints,
4399
04:50:52,624 --> 04:50:53,624
we can right click on one of our UI elements,
and we can select this show baseline. Now
4400
04:50:53,624 --> 04:50:54,624
you can see this baseline, instead of adding
the two constraints from top and bottom, we
4401
04:50:54,624 --> 04:50:55,624
can select this baseline, and we can constraint
it to the baseline of this TextView. This
4402
04:50:55,624 --> 04:50:56,624
time, once again, they are at the same line
horizontally. Let's zoom in a little bit.
4403
04:50:56,624 --> 04:50:57,624
In order to navigate through all of your layout,
you can press the space key, and you can drag
4404
04:50:57,624 --> 04:50:58,624
your mouse like if you work with Photoshop,
you have the same ability, you can see that
4405
04:50:58,624 --> 04:50:59,624
both of these text views are at the same line
horizontally by adding some constraint to
4406
04:50:59,624 --> 04:51:00,624
the baseline. Once again, the baseline is
not available by default, you can right click,
4407
04:51:00,624 --> 04:51:01,624
and you can select this show baseline. Let's
zoom out. So we have seen that we can constrain
4408
04:51:01,624 --> 04:51:02,624
our UI element to know both edges of our screen.
But what if we want to, for example, have
4409
04:51:02,624 --> 04:51:03,624
another line for example, if we want to move
these TextView or constrain this TextView
4410
04:51:03,624 --> 04:51:04,624
to some other space beside these two edges.
For that, we can right click on our layout.
4411
04:51:04,624 --> 04:51:05,624
down in here inside these helpers. We have
these guidelines add vertical guideline and
4412
04:51:05,624 --> 04:51:06,624
add horizontal guideline, you can guess each
one of these will create a line. For example,
4413
04:51:06,624 --> 04:51:07,624
let's add a vertical guideline. Let's zoom
in a little bit so that we can see better,
4414
04:51:07,624 --> 04:51:08,624
you can see that this line has been added
to our layout, we can move it for example,
4415
04:51:08,624 --> 04:51:09,624
we can move it to the right of our screen.
As soon as I did that, you can see that we
4416
04:51:09,624 --> 04:51:10,624
have some margin from the left side of our
screen, you can see this triangle in here.
4417
04:51:10,624 --> 04:51:11,624
If I click on that, it will change the margin
from left to the right of our screen. Sometimes
4418
04:51:11,624 --> 04:51:12,624
this might be useful as well. Right now the
constraint of this TextView is the edge of
4419
04:51:12,624 --> 04:51:13,624
our screen, let's quickly delete the left
constraint of this TextView. And if we want,
4420
04:51:13,624 --> 04:51:14,624
we can add this constraint to this guideline,
for example, we can add it in here. Sometimes
4421
04:51:14,624 --> 04:51:15,624
once again, it might not work. You can see
the difference the TextView now constraints
4422
04:51:15,624 --> 04:51:16,624
to this guideline instead of the edge of our
screen. If you want you can constrain this
4423
04:51:16,624 --> 04:51:17,624
TextView to this guideline as well. But before
that, we need to delete this baseline constraint.
4424
04:51:17,624 --> 04:51:18,624
Let's quickly delete that, sorry, I deleted
the whole TextView and let's constraint it
4425
04:51:18,624 --> 04:51:19,624
to this guideline and also to the edge of
our screen. If you want you can also have
4426
04:51:19,624 --> 04:51:20,624
multiple guidelines, we will add another one
in a second. So for example, I can once again
4427
04:51:20,624 --> 04:51:21,624
right click on the layouts. by going to this
helpers. I can add another vertical guideline.
4428
04:51:21,624 --> 04:51:22,624
We can see that this guideline has been added
and I can use it like I've used this first
4429
04:51:22,624 --> 04:51:23,624
guideline. We also have a vertical guideline.
In our helpers, we can add this horizontal
4430
04:51:23,624 --> 04:51:24,624
guideline. We can Guess it will add a horizontal
guideline. Once again, we have this triangle
4431
04:51:24,624 --> 04:51:25,624
in here, if we click on that it will change
the margins from top to bottom. Let's also
4432
04:51:25,624 --> 04:51:26,624
constrain this TextView to our guideline.
Sometimes when you're working with different
4433
04:51:26,624 --> 04:51:27,624
UI elements in a constraint layout, adding
some foam strength might be difficult. For
4434
04:51:27,624 --> 04:51:28,624
example, here, we don't have much space. For
that, we can select both of the items that
4435
04:51:28,624 --> 04:51:29,624
we want to add constraints, for example, this
TextView. And this guideline, you can select
4436
04:51:29,624 --> 04:51:30,624
multiple UI elements by pressing down the
control key. After you have selected both
4437
04:51:30,624 --> 04:51:31,624
of them, you can right click, you can go to
this constraint in here, we want to constrain
4438
04:51:31,624 --> 04:51:32,624
our TextView to which is the ID of this TextView
to our guideline, so I'm going to say top
4439
04:51:32,624 --> 04:51:33,624
two, let's say bottom of our guideline tree.
This way, we can add the constraint sometimes
4440
04:51:33,624 --> 04:51:34,624
where there is not much space to work with,
you can use the right click menu by selecting
4441
04:51:34,624 --> 04:51:35,624
this constraint, you can add the required
constraint path for that, once again, you
4442
04:51:35,624 --> 04:51:36,624
need to select the path of items that you
want. For example, this takes you on this
4443
04:51:36,624 --> 04:51:37,624
guideline. Or if you want to add the constraint
from this TextView to this button, for example,
4444
04:51:37,624 --> 04:51:38,624
you can select this TextView and this button.
Right now, if you have set the left constraint
4445
04:51:38,624 --> 04:51:39,624
of this TextView to this guideline, we can
also move this guideline for example, we can
4446
04:51:39,624 --> 04:51:40,624
move it accordingly to right and left, you
can see that once I removed this guideline,
4447
04:51:40,624 --> 04:51:41,624
the UI elements move as well. But depending
on your system, this operation in here might
4448
04:51:41,624 --> 04:51:42,624
be a slow because as you can see, where we
are tracking the live response of our UI elements.
4449
04:51:42,624 --> 04:51:43,624
If you want, you can disable this live rendering,
you can go to this icon here. And you can
4450
04:51:43,624 --> 04:51:44,624
uncheck this live rendering. Let's zoom in
a little bit. Now if I move this guideline,
4451
04:51:44,624 --> 04:51:45,624
you can see that the UI elements themselves
do not move. But we can see the outline of
4452
04:51:45,624 --> 04:51:46,624
every UI element. So in some cases, when you
do not have all of the required resources,
4453
04:51:46,624 --> 04:51:47,624
for example, some large amount of memory RAM,
you can disable this live rendering. But in
4454
04:51:47,624 --> 04:51:48,624
here, you can see the difference between this
blueprint and also this design view, you can
4455
04:51:48,624 --> 04:51:49,624
see that when I moved this guideline, the
UI elements inside the blueprint view are
4456
04:51:49,624 --> 04:51:50,624
moving accordingly. And that's because the
blueprints view do not render our UI elements
4457
04:51:50,624 --> 04:51:51,624
at all. It's just some blueprint of all of
our UI elements. Later on, when we add some
4458
04:51:51,624 --> 04:51:52,624
image view to our layout, we will see the
difference much better. Okay, let's move on
4459
04:51:52,624 --> 04:51:53,624
from this part. Sometimes when you're creating
your layout files, you may want to have some
4460
04:51:53,624 --> 04:51:54,624
sample data, or as we might call it in programming,
you may want to have some placeholders. For
4461
04:51:54,624 --> 04:51:55,624
example, right now, the placeholder for this
TextView is this text view. If we want, we
4462
04:51:55,624 --> 04:51:56,624
can change that, we can right click on our
element, we can go to this set sample data.
4463
04:51:56,624 --> 04:51:57,624
And we can select some text, we can see that
we have this cities for example, it will change
4464
04:51:57,624 --> 04:51:58,624
the text of our TextView to Shanghai, for
example. Let's see others. For example. Another
4465
04:51:58,624 --> 04:51:59,624
popular choice is this date in here, you can
see different formats, we can select that
4466
04:51:59,624 --> 04:52:00,624
and we can have some sample data about the
date. The other one is lorem ipsum. If you
4467
04:52:00,624 --> 04:52:01,624
search on the internet for sample text, you
probably get this lorem ipsum text. Let's
4468
04:52:01,624 --> 04:52:02,624
select that. And we can have some sample text.
And according to that, we can have this text
4469
04:52:02,624 --> 04:52:03,624
on our layout, we also have the Lorem text.
Let's quickly see that.
4470
04:52:03,624 --> 04:52:04,624
So instead of Lorem random, we can select
this Lorem, which is basically this Lorem
4471
04:52:04,624 --> 04:52:05,624
text. Before we finish off this video, let's
also see the sample data for this image view
4472
04:52:05,624 --> 04:52:06,624
as well. I know that we didn't talk about
image view so far in the course, but we will
4473
04:52:06,624 --> 04:52:07,624
talk about that in the next video, we can
drag some image view to our layout. First
4474
04:52:07,624 --> 04:52:08,624
thing we need to set some sample data for
our image view. For example, we can select
4475
04:52:08,624 --> 04:52:09,624
from the avatars. Right now this is the avatar
that by default we have selected for our imagery.
4476
04:52:09,624 --> 04:52:10,624
If you want to you can change that by right
clicking and set some sample data, we can
4477
04:52:10,624 --> 04:52:11,624
select another avatar or we can select one
of these for example this background. Let's
4478
04:52:11,624 --> 04:52:12,624
select another avatar. For example this one
like before you can constrain this image view
4479
04:52:12,624 --> 04:52:13,624
for example to the both edges of your TextView.
Let's quickly do that. If you take a look
4480
04:52:13,624 --> 04:52:14,624
at the attributes painting here, you can see
that for the layout width, we have set the
4481
04:52:14,624 --> 04:52:15,624
width to content, we can change that, for
example, we can say match constraint. When
4482
04:52:15,624 --> 04:52:16,624
we do that, the width of our image view will
change according to its parent constraint,
4483
04:52:16,624 --> 04:52:17,624
which is this TextView in this case. But there
is also another difference when we change
4484
04:52:17,624 --> 04:52:18,624
this layout width to match constraint. When
we do that, if you take a closer look in here,
4485
04:52:18,624 --> 04:52:19,624
inside this constraint widget, you can see
that we have this line in here. Let's click
4486
04:52:19,624 --> 04:52:20,624
on that. And once I do that, you can see that
this ratio in here has been added. It's useful
4487
04:52:20,624 --> 04:52:21,624
for when you want to show some images from
the internet, when you don't know the size
4488
04:52:21,624 --> 04:52:22,624
or the ratio of your image. Right now, the
ratio is one to one, if you want, you can
4489
04:52:22,624 --> 04:52:23,624
change that, for example, you can say one
to two, you can see that now the ratio has
4490
04:52:23,624 --> 04:52:24,624
been changed, or let's say two to one, the
ratio of our image view responds accordingly.
4491
04:52:24,624 --> 04:52:25,624
Also, you have the same option from above
in here layout constraint ID, which you can
4492
04:52:25,624 --> 04:52:26,624
change this to 212. Once again, one to one.
Also know that you have used this image view,
4493
04:52:26,624 --> 04:52:27,624
you can see the difference between this blueprint
and this design view, you can see that in
4494
04:52:27,624 --> 04:52:28,624
the blueprint view, we are not rendering the
image view. And in the blueprint view, we
4495
04:52:28,624 --> 04:52:29,624
are not rendering our layout. It's very useful
for when you're working with constraint layout,
4496
04:52:29,624 --> 04:52:30,624
especially if you do not have the required
resources. For example, if you do not have
4497
04:52:30,624 --> 04:52:31,624
large amount of RAM, you can always go off
to this blue stack icon. And you can select
4498
04:52:31,624 --> 04:52:32,624
this blueprint so that you have only the blueprint.
Okay, I think that's enough for this video
4499
04:52:32,624 --> 04:52:33,624
in the next video. First of all, we are going
to work with this image view element. See
4500
04:52:33,624 --> 04:52:34,624
you in the next video.
4501
04:52:34,624 --> 04:52:35,624
In this video, we are going to talk about
images in Android, and I believe it will be
4502
04:52:35,624 --> 04:52:36,624
a short video. As always, let's switch back
to our layout file. And let's change this
4503
04:52:36,624 --> 04:52:37,624
constraint layout to a relative layout. You
can keep working with constraint layout, I'm
4504
04:52:37,624 --> 04:52:38,624
just more comfortable with this relative layout.
First of all, I'm going to delete this TextView.
4505
04:52:38,624 --> 04:52:39,624
And let's create our image view. This time
instead of wrap content or mass filing for
4506
04:52:39,624 --> 04:52:40,624
the width and height, I'm going to put some
number for example, I'm going to say 150 db.
4507
04:52:40,624 --> 04:52:41,624
Right now, you cannot see any image in your
layout file in this preview because we do
4508
04:52:41,624 --> 04:52:42,624
not have any placeholder. But you can see
the outline of your image. If you want to
4509
04:52:42,624 --> 04:52:43,624
have some placeholder you can say something
like this, you can say source this src. And
4510
04:52:43,624 --> 04:52:44,624
you can add a sub image from your project.
By default, you have some images in your project.
4511
04:52:44,624 --> 04:52:45,624
For example, in your driver folder, you have
this IC launcher background. Let's select
4512
04:52:45,624 --> 04:52:46,624
that, for example. And as soon as I select
that, you can see that we can see this green
4513
04:52:46,624 --> 04:52:47,624
image in our preview. Later on, we will talk
about this driver folder and other folders
4514
04:52:47,624 --> 04:52:48,624
that are related to images in Android projects.
So this is going to be a placeholder. And
4515
04:52:48,624 --> 04:52:49,624
if you want we can later on change this image
in our Java file. Let's also give an ID to
4516
04:52:49,624 --> 04:52:50,624
our image view. Let's say ID, let's name it
might image. Let's also move it to the center
4517
04:52:50,624 --> 04:52:51,624
of our path. Let's say center your parent
and through. So when we have address this
4518
04:52:51,624 --> 04:52:52,624
image, we have used this app driver, which
means that this image this IC launcher background
4519
04:52:52,624 --> 04:52:53,624
lives in our driver folder. If you take a
look at your project pane, in the Resources
4520
04:52:53,624 --> 04:52:54,624
folder, we have seen the layout folder in
which we have this activity main dot XML file.
4521
04:52:54,624 --> 04:52:55,624
But beside that we have this driver folder.
Inside this driver folder we have some XML
4522
04:52:55,624 --> 04:52:56,624
files as well. So if you take a look at this
IC launcher background, you can see that we
4523
04:52:56,624 --> 04:52:57,624
have this XML file which at the end it result
is this image, or to be specific is this drawable.
4524
04:52:57,624 --> 04:52:58,624
Inside this XML file, we have some elements
like vector path and everything. Don't worry
4525
04:52:58,624 --> 04:52:59,624
about them yet. We will talk about them later
on in the course but at the end, the result
4526
04:52:59,624 --> 04:53:00,624
of this XML file is this driver in here. If
you want you can add other drivers in your
4527
04:53:00,624 --> 04:53:01,624
driver folder as well. For example, if you
right click on your driver folder by selecting
4528
04:53:01,624 --> 04:53:02,624
new vector asset or image asset depending
on your need, you can add new drivers. Let's
4529
04:53:02,624 --> 04:53:03,624
select this new image asset. In here first
of all, we can select the type of our image
4530
04:53:03,624 --> 04:53:04,624
right now. It's launcher icons if you want
you can change that. For example, you can
4531
04:53:04,624 --> 04:53:05,624
see action bar and tap icons. So basically
this You can add some icons to your project.
4532
04:53:05,624 --> 04:53:06,624
First of all, you need to name your icon.
This is going to be the name that later on,
4533
04:53:06,624 --> 04:53:07,624
you will pass to your images, for example,
your image view, you can see that we have
4534
04:53:07,624 --> 04:53:08,624
passed this IC launcher background. That name
is coming from here. IC stands for icon. It's
4535
04:53:08,624 --> 04:53:09,624
the convention to name your icons with this
IC. So let's say I see alarm. For example,
4536
04:53:09,624 --> 04:53:10,624
in Android Studio, you have some icons. For
example, if you click on this icon here, this
4537
04:53:10,624 --> 04:53:11,624
Android icon in here, you can see the clip
art, you can see that there are all sorts
4538
04:53:11,624 --> 04:53:12,624
of icons, and you can use whichever you want.
Also notice that these icons are available
4539
04:53:12,624 --> 04:53:13,624
under this Apache License, so make sure to
check that before you use them in your project.
4540
04:53:13,624 --> 04:53:14,624
Because I've named this icon Ico alarm, I'm
going to select one of these alarms. For example,
4541
04:53:14,624 --> 04:53:15,624
this one, let's select okay. And in here,
you probably can see the icon, it's somehow
4542
04:53:15,624 --> 04:53:16,624
void. If you want, you can change the color
by opening this drop down and select a custom
4543
04:53:16,624 --> 04:53:17,624
color. For example, let's pass in black.
4544
04:53:17,624 --> 04:53:18,624
Now you can see the alarm better. Also notice
that there are multiple sizes of this icon.
4545
04:53:18,624 --> 04:53:19,624
And that's because in different screen sizes,
the Android system is going to select one
4546
04:53:19,624 --> 04:53:20,624
of these sizes. For example, if it's a larger
screen size with a large pizza rate, it will
4547
04:53:20,624 --> 04:53:21,624
select one of these larger images similar
to that if your application is going to run
4548
04:53:21,624 --> 04:53:22,624
on a smaller screen size device, the Android
system will select one of these smaller icons.
4549
04:53:22,624 --> 04:53:23,624
Okay, let's select Next from here. Once again,
you can see that in your Resources folder
4550
04:53:23,624 --> 04:53:24,624
inside the driver folder, multiple images,
multiple driver files with multiple qualities
4551
04:53:24,624 --> 04:53:25,624
will be added. And you can also see the output
file this is going to be the XML file when
4552
04:53:25,624 --> 04:53:26,624
you create your driver file. Basically this
at the end is going to create that alarm icon.
4553
04:53:26,624 --> 04:53:27,624
Let's click finish in here. And if you take
a look at your driver folder, now you can
4554
04:53:27,624 --> 04:53:28,624
see that we have this icy alarm, which once
again is presented with five different qualities.
4555
04:53:28,624 --> 04:53:29,624
Now in our activity main dot XML file, instead
of passing this IC launcher background, we
4556
04:53:29,624 --> 04:53:30,624
can pass IC Allah. So this way, by adding
some deriva into your driver folder, you can
4557
04:53:30,624 --> 04:53:31,624
add some images to your project. Similarly,
you can add some images into your mipmaps
4558
04:53:31,624 --> 04:53:32,624
folder as well. Right now we have some images
in here, for example, we have this IC launcher.
4559
04:53:32,624 --> 04:53:33,624
And also we have this IC launcher around which
at the end we'll create around it icon. If
4560
04:53:33,624 --> 04:53:34,624
you want to add some extra images into your
project from for example, let's say your computer,
4561
04:53:34,624 --> 04:53:35,624
you can add them into this magma folder. For
example, I have prepared this PNG file, I
4562
04:53:35,624 --> 04:53:36,624
can copy that that's a copy. And in my micpa
folder, I can paste that. Let's select the
4563
04:53:36,624 --> 04:53:37,624
directory. And also Let's name our file. It's
also very important to note that when you
4564
04:53:37,624 --> 04:53:38,624
are importing some images into our project,
the name of your map file, the name of your
4565
04:53:38,624 --> 04:53:39,624
PNG or JPG file shouldn't have any spaces.
And also for that matter, they shouldn't have
4566
04:53:39,624 --> 04:53:40,624
any awkward cases. So the naming here I think
is fine. Let's press OK. And as you can see,
4567
04:53:40,624 --> 04:53:41,624
this maker dot png has been added to my lead
map folder. Now that I have this image into
4568
04:53:41,624 --> 04:53:42,624
my project, I can address that from inside
the activity main dot XML file. For example,
4569
04:53:42,624 --> 04:53:43,624
in here instead of saying iclm, I can say
me. But because we have added that file into
4570
04:53:43,624 --> 04:53:44,624
our map folder, we are getting a red warning
because we are saying as driver. For that
4571
04:53:44,624 --> 04:53:45,624
I'm going to delete all of this. And I'm going
to say add map. And I can add this makeup,
4572
04:53:45,624 --> 04:53:46,624
we can see that the logo has been added into
our project successfully. So far, we have
4573
04:53:46,624 --> 04:53:47,624
added these images and drivers in our project
in this Android view. Let's also check the
4574
04:53:47,624 --> 04:53:48,624
project view. And let's see if we can locate
different images in the project view as well.
4575
04:53:48,624 --> 04:53:49,624
So you can guess that it's in our app folder
inside the sources folder. Inside the main
4576
04:53:49,624 --> 04:53:50,624
folder inside Resources folder, we can see
all these drivers and mipmap folders. Once
4577
04:53:50,624 --> 04:53:51,624
again, each one of these is going to respond
to the quality of the screen of the user's
4578
04:53:51,624 --> 04:53:52,624
device. So for example, if we want to locate
this macro dot png because it was a high quality
4579
04:53:52,624 --> 04:53:53,624
image, I believe it will be inside this week
map x x hdpi. As you can see it's in here.
4580
04:53:53,624 --> 04:53:54,624
But if you want to have an application that
response to each one of the screen sizes and
4581
04:53:54,624 --> 04:53:55,624
the screen qualities correctly, you need to
create images for each one of these qualities
4582
04:53:55,624 --> 04:53:56,624
and add them into your project. Right now,
if you take a look at, for example, this mdpi,
4583
04:53:56,624 --> 04:53:57,624
you can see that inside this folder, we do
not have any macro dot png, it's not going
4584
04:53:57,624 --> 04:53:58,624
to cause any problem in order to show the
images in different screen sizes. As long
4585
04:53:58,624 --> 04:53:59,624
as there is some image with the name maker
dot png, it's going to show something to the
4586
04:53:59,624 --> 04:54:00,624
user, but it may cause some bad user experience.
Okay, let's switch back to our Android view.
4587
04:54:00,624 --> 04:54:01,624
And let's talk about how you can change the
icon of your application. But before that,
4588
04:54:01,624 --> 04:54:02,624
let's quickly run the application. And let's
see what is the current icon for our application.
4589
04:54:02,624 --> 04:54:03,624
Right now you can see that the name of this
application is images. And if you take a look
4590
04:54:03,624 --> 04:54:04,624
at the list of your applications on your device,
you can see that the logo of these images
4591
04:54:04,624 --> 04:54:05,624
is this rounded Android icon. This is the
icon that we have seen inside our MC map folder.
4592
04:54:05,624 --> 04:54:06,624
So if you take a look at your map folder,
4593
04:54:06,624 --> 04:54:07,624
then in here we have this IC launcher round,
which is this icon, which at the end will
4594
04:54:07,624 --> 04:54:08,624
be somehow rounded. And if you want to change
that you can go to your manifest folder, you
4595
04:54:08,624 --> 04:54:09,624
can see that we have this Android manifest
that XML file. If you click on that, you can
4596
04:54:09,624 --> 04:54:10,624
see some general information about your application.
later on. We will talk about this manifest
4597
04:54:10,624 --> 04:54:11,624
file and also other XML files in your project.
But for now, you can see that inside this
4598
04:54:11,624 --> 04:54:12,624
application tags, we have two attributes this
icon and also this round icon. Both of them
4599
04:54:12,624 --> 04:54:13,624
are addressing the IC launcher icon for now,
if you want you can change that, for example,
4600
04:54:13,624 --> 04:54:14,624
in the devices that you are not going to show
around the icon, you can change this, let's
4601
04:54:14,624 --> 04:54:15,624
quickly change this one and let's address
our makeup. Similarly for the round icon,
4602
04:54:15,624 --> 04:54:16,624
I'm going to pass me but let's run the application.
And let's see if we have changed the icon
4603
04:54:16,624 --> 04:54:17,624
successfully. If you take a look at the list
of your applications, once again, you can
4604
04:54:17,624 --> 04:54:18,624
see that the icon for this images application
has been changed. Okay, I think that's enough
4605
04:54:18,624 --> 04:54:19,624
for this video. In the next video, we are
going to start working on the list view and
4606
04:54:19,624 --> 04:54:20,624
also a spinner. So in the next video, we are
going to show how can we have a list of different
4607
04:54:20,624 --> 04:54:21,624
items. See you in the next video.
4608
04:54:21,624 --> 04:54:22,624
In this video, we are going to talk about
how we can show a list of different items
4609
04:54:22,624 --> 04:54:23,624
in our application. For example, at least
have different cities or at least have different
4610
04:54:23,624 --> 04:54:24,624
students. For that, first of all, I'm going
to start talking about ListView. ListView
4611
04:54:24,624 --> 04:54:25,624
is one of the options that we can use in order
to show a list of different options. For that,
4612
04:54:25,624 --> 04:54:26,624
first of all, I'm going to close this java
file for now inside my XML file, first of
4613
04:54:26,624 --> 04:54:27,624
all, I'm going to delete this TextView. And
after that, I'm going to change this constraint
4614
04:54:27,624 --> 04:54:28,624
layout to a relative layout. If you want,
you can work with constraint layout, but I'm
4615
04:54:28,624 --> 04:54:29,624
more comfortable with relative layout. After
that down in here, I'm going to say list view.
4616
04:54:29,624 --> 04:54:30,624
For the bid standpoint. This time I'm going
to say match parent. And as soon as I do that,
4617
04:54:30,624 --> 04:54:31,624
you can see that in here in our preview, we
can see a list of different items on our screen.
4618
04:54:31,624 --> 04:54:32,624
These are just some sample data indicating
that we are going to use a ListView. I'm also
4619
04:54:32,624 --> 04:54:33,624
going to add a margin top, let's say margin
top. And let's say 100 dP, let's also give
4620
04:54:33,624 --> 04:54:34,624
an ID. But before that if you take a closer
look in here, I'm not sure that if you can
4621
04:54:34,624 --> 04:54:35,624
see this scroll bar at the left pane in here,
but it indicates that we can scroll our ListView
4622
04:54:35,624 --> 04:54:36,624
in case if all of our items do not fit on
the screen. So if we have a large amount of
4623
04:54:36,624 --> 04:54:37,624
items, we can scroll over our list view. Okay,
let's give an ID to our list view. Let's say
4624
04:54:37,624 --> 04:54:38,624
ID. I'm going to name it cities ListView or
cities list. And I think that's enough for
4625
04:54:38,624 --> 04:54:39,624
now. Let's quickly switch back to our Java
file and let's initialize this list view.
4626
04:54:39,624 --> 04:54:40,624
Like before above in here I'm going to say
private list view. Let's name it ListView
4627
04:54:40,624 --> 04:54:41,624
or Let's name it cities list. down in here
inside the onCreate method I'm going to say
4628
04:54:41,624 --> 04:54:42,624
city's list is equal to find view by ID. Also
if you have noticed I'm using a shortcut for
4629
04:54:42,624 --> 04:54:43,624
this find view by ID method. Instead of typing
all of the method I'm using FB shortcut. If
4630
04:54:43,624 --> 04:54:44,624
you press Enter or tab you will get the whole
method we will talk about these available
4631
04:54:44,624 --> 04:54:45,624
shortcuts in IntelliJ and Android Studio later
on in the course. Okay, let's pass the address
4632
04:54:45,624 --> 04:54:46,624
of our ListView, let's say our.id dot cities
list. After that, we need to create the data
4633
04:54:46,624 --> 04:54:47,624
that we want to show in our list view. For
example, in here, I'm going to create an ArrayList
4634
04:54:47,624 --> 04:54:48,624
of different strings. And later on, I'm going
to pass it to this city's ListView. For that,
4635
04:54:48,624 --> 04:54:49,624
I'm going to say ArrayList. This one in here,
of different strings, let's name it cities,
4636
04:54:49,624 --> 04:54:50,624
is equal to new ArrayList. And after that,
I'm going to add some cities to this array
4637
04:54:50,624 --> 04:54:51,624
list, let's say cities dot add. And let's
add some items.
4638
04:54:51,624 --> 04:54:52,624
In here, I'm adding all of these cities to
my city's array list manually. But in most
4639
04:54:52,624 --> 04:54:53,624
cases, the list of your objects will be coming
from the web server or maybe your database.
4640
04:54:53,624 --> 04:54:54,624
Because we didn't talk about this stuff yet.
We are going to hard coding them and we are
4641
04:54:54,624 --> 04:54:55,624
going to pass them manually to our ArrayList.
Okay, after providing your data, you need
4642
04:54:55,624 --> 04:54:56,624
to pass this data to your ListView. But before
that, you need to create an adapter. The purpose
4643
04:54:56,624 --> 04:54:57,624
of the adapter is to fetch the data to your
ListView. Let's quickly see how we can create
4644
04:54:57,624 --> 04:54:58,624
our adapter. There are multiple kinds of adapters,
but the one that I'm going to use in here
4645
04:54:58,624 --> 04:54:59,624
is this array adapter. This is the simplest
one, and we are going to work with that in
4646
04:54:59,624 --> 04:55:00,624
here. Let's say array adapter of type string.
Let's name it cities adapter. That's equal
4647
04:55:00,624 --> 04:55:01,624
to new array adapter. Inside the parentheses
of this array adapter, we need three things.
4648
04:55:01,624 --> 04:55:02,624
First of all, we need a context, which I'm
going to pass this later on in the course,
4649
04:55:02,624 --> 04:55:03,624
we will talk about context. But in here, because
we are inside an activity, I'm talking about
4650
04:55:03,624 --> 04:55:04,624
this main activity and activities are context.
So this one in here is going to work fine.
4651
04:55:04,624 --> 04:55:05,624
So by passing this, we are passing our main
activity as the context. Don't worry about
4652
04:55:05,624 --> 04:55:06,624
that yet. We will talk about context later
on in the course. After that, we need to pass
4653
04:55:06,624 --> 04:55:07,624
a layout file for every item in our list view.
So for example, if you are going to show the
4654
04:55:07,624 --> 04:55:08,624
name of a city, we need a layout for that
city in our list view, we can create our customized
4655
04:55:08,624 --> 04:55:09,624
layout, but in here, I'm going to use one
of the built in layouts. For that I'm going
4656
04:55:09,624 --> 04:55:10,624
to say Android dot r dot layout dot, let's
say simple list item. So basically, this is
4657
04:55:10,624 --> 04:55:11,624
a built in layout file in which we can use
in order to show different cities in our list
4658
04:55:11,624 --> 04:55:12,624
view. After passing the context, and also
the layout file for every item, we also need
4659
04:55:12,624 --> 04:55:13,624
to pass the data. In this case, we need to
pass the city's array list. So let's say cities
4660
04:55:13,624 --> 04:55:14,624
in here. And now we have our adapter for our
ListView. Once again, the purpose of this
4661
04:55:14,624 --> 04:55:15,624
adapter is to fetch the data, in this case,
this city's ArrayList to our ListView. After
4662
04:55:15,624 --> 04:55:16,624
creating your adapter, you can say city's
list dot set adapter, and you can pass your
4663
04:55:16,624 --> 04:55:17,624
adapter, let's say city's adapter. before
running the application, let's quickly review
4664
04:55:17,624 --> 04:55:18,624
what we are doing in here. First of all, we
have created our ListView inside our layout
4665
04:55:18,624 --> 04:55:19,624
file. After that we have initialized it inside
this onCreate method in here. After that we
4666
04:55:19,624 --> 04:55:20,624
have prepared our data in this case, we have
created this simple ArrayList of different
4667
04:55:20,624 --> 04:55:21,624
strings called cities. After that we have
created an adapter in order to fetch these
4668
04:55:21,624 --> 04:55:22,624
cities array list to our list view. And after
creating the adapter, we have passed it to
4669
04:55:22,624 --> 04:55:23,624
our list view. Let's quickly run our application.
And let's see if we can see these items in
4670
04:55:23,624 --> 04:55:24,624
our list view. As you can see, we have created
this list view and also populated the data
4671
04:55:24,624 --> 04:55:25,624
that we have passed in our ArrayList. Also,
if we had more data that wouldn't fit on the
4672
04:55:25,624 --> 04:55:26,624
screen, the ListView would be a scrollable
display of in here for every item is this
4673
04:55:26,624 --> 04:55:27,624
simple this item that we have passed in here.
If you want to take a look at that you can
4674
04:55:27,624 --> 04:55:28,624
press down the CTRL key in Mac it should be
command key. And by clicking on the symbol
4675
04:55:28,624 --> 04:55:29,624
list item one you can check the layout file.
You can see that it's a simple text view with
4676
04:55:29,624 --> 04:55:30,624
some attributes. Nothing special in here.
You can also make your items clickable right
4677
04:55:30,624 --> 04:55:31,624
now if you click on one item, nothing happens.
But you can set a listener for each one of
4678
04:55:31,624 --> 04:55:32,624
these items. Let's quickly see that. So after
passing the adapter in here, I can say city
4679
04:55:32,624 --> 04:55:33,624
List
4680
04:55:33,624 --> 04:55:34,624
dot set on item click listener this one. And
I can pass my interface, I can say new on
4681
04:55:34,624 --> 04:55:35,624
item click listener. Notice that it's adapter
view.on item click listener, it's different
4682
04:55:35,624 --> 04:55:36,624
from on click listener. When we pass this
interface, we have this on item click method
4683
04:55:36,624 --> 04:55:37,624
in which has some inputs. The one that we
are interested in is this position in here.
4684
04:55:37,624 --> 04:55:38,624
Basically, this is going to be the position
of our item in our ListView. Let's quickly
4685
04:55:38,624 --> 04:55:39,624
see how we can use that. For example in here
I'm going to show a toast message after clicking
4686
04:55:39,624 --> 04:55:40,624
on each one of the items. Once again, when
we create our toast message, first of all,
4687
04:55:40,624 --> 04:55:41,624
we need to pass a context. Because we are
inside this on item click method which exists
4688
04:55:41,624 --> 04:55:42,624
in another interface, we cannot simply pass
this we need to pass main activity dot this
4689
04:55:42,624 --> 04:55:43,624
in order to refer to our context. After passing
the context, we can pass our text for the
4690
04:55:43,624 --> 04:55:44,624
text in here I'm going to say cities dot get.
And for the index in here, I can pass this
4691
04:55:44,624 --> 04:55:45,624
position. So this way, we are going to get
the city name that we have clicked on. We
4692
04:55:45,624 --> 04:55:46,624
are using this position as the index in order
to get the appropriate city name from our
4693
04:55:46,624 --> 04:55:47,624
cities ArrayList. Also, if you have noticed
when I use this cities inside this on item
4694
04:55:47,624 --> 04:55:48,624
click method, this final keyword has been
added to the declaration of ArrayList. As
4695
04:55:48,624 --> 04:55:49,624
a reminder from the Java session, when you
add a final keyword at the declaration of
4696
04:55:49,624 --> 04:55:50,624
a variable or an object, that variable or
the instance of that object would be constant,
4697
04:55:50,624 --> 04:55:51,624
it means that you cannot change that instance.
But although we cannot change the instance
4698
04:55:51,624 --> 04:55:52,624
of this cities, after declaring it as final,
we can use all of the methods inside that
4699
04:55:52,624 --> 04:55:53,624
object. For example, we can use this dot add
method, because we are using this city's ArrayList
4700
04:55:53,624 --> 04:55:54,624
inside this on item click method which exists
in another interface, the city's ArrayList
4701
04:55:54,624 --> 04:55:55,624
needs to be constant. And this final keyword
has been added automatically. And if I remove
4702
04:55:55,624 --> 04:55:56,624
it right now from here, you can see that we
are getting this error down in here. And it
4703
04:55:56,624 --> 04:55:57,624
says that your variable should be constant.
So let's add the final keyword once again.
4704
04:55:57,624 --> 04:55:58,624
Also, let's add another text in here, let's
say plus selected. Okay, let's run the application
4705
04:55:58,624 --> 04:55:59,624
once again. And let's see if we have set on
item click listener successfully. So now if
4706
04:55:59,624 --> 04:56:00,624
we click on one of the items inside our list
view, for example, this New York, you can
4707
04:56:00,624 --> 04:56:01,624
see the toast message in here, New York selected
Berlin Berlin selected. Okay, it seems like
4708
04:56:01,624 --> 04:56:02,624
our on item click listener is working fine.
Once again, this on item click listener is
4709
04:56:02,624 --> 04:56:03,624
different from the onClick. listener. If you
set the onclicklistener on your ListView,
4710
04:56:03,624 --> 04:56:04,624
it's not going to work according to every
item in your ListView. Okay, I think that's
4711
04:56:04,624 --> 04:56:05,624
enough talking about ListView. You can work
around ListView, if you want. But I do not
4712
04:56:05,624 --> 04:56:06,624
suggest that because in Android, we have modern
solutions for populating a list of different
4713
04:56:06,624 --> 04:56:07,624
options. For example, we have RecyclerView,
which has a lot of flexibilities. If you take
4714
04:56:07,624 --> 04:56:08,624
a look at your layout file inside the design
view, you can find a ListView somewhere inside
4715
04:56:08,624 --> 04:56:09,624
this legacy. You can see that in here. And
it's inside this legacy because these days,
4716
04:56:09,624 --> 04:56:10,624
almost no one uses ListView. I just wanted
to show how can you create a ListView. The
4717
04:56:10,624 --> 04:56:11,624
reason that you may want to avoid using ListView
is that ListView does not have much flexibility.
4718
04:56:11,624 --> 04:56:12,624
For example, you cannot customize the layout
of every item in your list view. You can create
4719
04:56:12,624 --> 04:56:13,624
a customized layout for every item. But in
recycler view, you have much more flexibilities.
4720
04:56:13,624 --> 04:56:14,624
And also, the other issue is about performance.
If you have a large amount of data that you
4721
04:56:14,624 --> 04:56:15,624
want to show in a list for example, if you
have 1000 items. In those cases, you will
4722
04:56:15,624 --> 04:56:16,624
see the performance issue when you're working
with ListView. recycler view has done a much
4723
04:56:16,624 --> 04:56:17,624
better job when it comes to performance. We
will talk about recycler view in one or two
4724
04:56:17,624 --> 04:56:18,624
videos from now. But let's move on from this
part. The other layout for that I'm going
4725
04:56:18,624 --> 04:56:19,624
to talk about is called a spinner. Let's quickly
see that I'm going to define my spinner above
4726
04:56:19,624 --> 04:56:20,624
this ListView. So in here, let's say a spinner.
For the width, same height. This time, let's
4727
04:56:20,624 --> 04:56:21,624
say wrap content. Let's center it horizontally
and also let's add some margin top. Center
4728
04:56:21,624 --> 04:56:22,624
horizontal through margin top, let's say 50
dP. Right now you cannot see your spinner
4729
04:56:22,624 --> 04:56:23,624
inside this preview very well. And that's
because you do not have any data inside your
4730
04:56:23,624 --> 04:56:24,624
spinner yet. Basically, this is
4731
04:56:24,624 --> 04:56:25,624
Winner is going to create a drop down menu.
For example, if you click on this, you will
4732
04:56:25,624 --> 04:56:26,624
see a list of different options. We will see
that in action in a minute. But for now let's
4733
04:56:26,624 --> 04:56:27,624
just pass an ID for our spinner. Let's say
students list or students spinner, it's a
4734
04:56:27,624 --> 04:56:28,624
better name. Also, let's move our ListView
to below this has been a layout below. And
4735
04:56:28,624 --> 04:56:29,624
let's pass the ID of our spinner. Let's initialize
our spinner in our Java file. Like before,
4736
04:56:29,624 --> 04:56:30,624
I'm going to come above in here and say, private
spinner. Let's name it a student spinner.
4737
04:56:30,624 --> 04:56:31,624
And then in here inside the onCreate method,
I can't initialize that. Let's see student
4738
04:56:31,624 --> 04:56:32,624
spinner is equal to find view by ID dot, let's
say a student spinner. Once again, like the
4739
04:56:32,624 --> 04:56:33,624
ListView, first of all, we need to create
the list of items, the data that we want to
4740
04:56:33,624 --> 04:56:34,624
show in our spinner. For that I'm going to
create another ArrayList in here. So let's
4741
04:56:34,624 --> 04:56:35,624
say array list of different strings. Let's
call it the students is equal to new ArrayList.
4742
04:56:35,624 --> 04:56:36,624
After that, let's pass on data to this students
ArrayList.
4743
04:56:36,624 --> 04:56:37,624
Similar to when we are working with listviews.
After providing our data, we also need to
4744
04:56:37,624 --> 04:56:38,624
create an adapter. Once again, the purpose
of this adapter is going to be fetching the
4745
04:56:38,624 --> 04:56:39,624
data into our spinner, we can create our adapter
like we have did for the ListView. So in here
4746
04:56:39,624 --> 04:56:40,624
I can see array adapter of different strings.
Let's name it the students adapter is equal
4747
04:56:40,624 --> 04:56:41,624
to new array adapter. Like before, inside
this parentheses, we need three items. First
4748
04:56:41,624 --> 04:56:42,624
of all, we need the context. After that, we
need to lay out five for every item in our
4749
04:56:42,624 --> 04:56:43,624
screener. Once again, you can create your
customized layout file if you want. But in
4750
04:56:43,624 --> 04:56:44,624
here for the sake of simplicity, I'm going
to use a built in one. So let's say Android
4751
04:56:44,624 --> 04:56:45,624
dot r dot layout dot the layout font that
I'm going to use this time is this simple
4752
04:56:45,624 --> 04:56:46,624
spinner drop down item. When we created the
adapter for ListView. We have used this simple
4753
04:56:46,624 --> 04:56:47,624
list item Vaughn. But in here we are going
to use this simple spinner drop down item.
4754
04:56:47,624 --> 04:56:48,624
After that, once again, we need our data which
I'm going to pass my students I released.
4755
04:56:48,624 --> 04:56:49,624
After creating your adapter you can see the
students the spinner dot set adapter, and
4756
04:56:49,624 --> 04:56:50,624
you can pass your students adapter. Let's
run our application. And let's see what a
4757
04:56:50,624 --> 04:56:51,624
spinner looks like. You can see this winner
above in here, it's going to generate a drop
4758
04:56:51,624 --> 04:56:52,624
down menu. If I click on this triangle in
here, you can see the name of the students
4759
04:56:52,624 --> 04:56:53,624
that I have passed into my ArrayList. Once
again, these items can be also clickable.
4760
04:56:53,624 --> 04:56:54,624
For example, if I click on one of these, we
can take some action. Let's quickly see that
4761
04:56:54,624 --> 04:56:55,624
how can we create an onclicklistener for each
one of these items. So after setting the adapter
4762
04:56:55,624 --> 04:56:56,624
down in here, I'm going to say students spinner
dot set on item selected listener, this is
4763
04:56:56,624 --> 04:56:57,624
different from the listener that we have used
for the ListView. For the ListView we have
4764
04:56:57,624 --> 04:56:58,624
used set on item click listener but it's not
going to work in here. Instead we are going
4765
04:56:58,624 --> 04:56:59,624
to use this set on item selected listener.
Let's pass our interface let's say new on
4766
04:56:59,624 --> 04:57:00,624
item select listener adapter view.on item
selected listener and this interface is going
4767
04:57:00,624 --> 04:57:01,624
to generate two methods on item selected and
on nothing selected. The names are descriptive
4768
04:57:01,624 --> 04:57:02,624
on nothing selected means that if you do not
select anything inside your spinner, you are
4769
04:57:02,624 --> 04:57:03,624
not going to work with this method and we
are not going to write any logic in here.
4770
04:57:03,624 --> 04:57:04,624
Instead inside this on item selected we are
going to show a toast message. Once again,
4771
04:57:04,624 --> 04:57:05,624
you can see that we have this position in
here in which we can use it in order to for
4772
04:57:05,624 --> 04:57:06,624
example get the name of that student. First
of all like before we need the context because
4773
04:57:06,624 --> 04:57:07,624
we are inside another interface we are going
to pass main activity that this after that
4774
04:57:07,624 --> 04:57:08,624
for the text I'm going to say students that
get as the index I'm going to pass position.
4775
04:57:08,624 --> 04:57:09,624
That's a plus selected.
4776
04:57:09,624 --> 04:57:10,624
Let's run the application and let's see if
everything is going to work. So in here you
4777
04:57:10,624 --> 04:57:11,624
can see that even though I never clicked on
any item I see the toast message. And that's
4778
04:57:11,624 --> 04:57:12,624
because we have used this set on item selected
listener. Once again, this is different from
4779
04:57:12,624 --> 04:57:13,624
the on item click listener. I don't know why.
But we cannot use an item click listener for
4780
04:57:13,624 --> 04:57:14,624
the spinner, if you try to use that you will
get an exception. Instead, the other option
4781
04:57:14,624 --> 04:57:15,624
that you have is this set on item selected
listener. Let's see if we can show the toast
4782
04:57:15,624 --> 04:57:16,624
message. If we select another item, for example,
Sarah, you can see that those messages down
4783
04:57:16,624 --> 04:57:17,624
in here. This is one way of showing data inside
your spinner. It's useful for when you do
4784
04:57:17,624 --> 04:57:18,624
not know what data you're going to show. For
example, if you're going to retrieve the data
4785
04:57:18,624 --> 04:57:19,624
from a web server or a database, to be precise,
in case your data is dynamic. But in case
4786
04:57:19,624 --> 04:57:20,624
you know that data previously in case your
data is static, you can use another way, let's
4787
04:57:20,624 --> 04:57:21,624
quickly see that. So before that, I'm going
to open this project pane. Inside my app folder
4788
04:57:21,624 --> 04:57:22,624
inside this Resources folder, we have this
values folder in here, in which inside that
4789
04:57:22,624 --> 04:57:23,624
we have this strings dot XML file, we can
create a string array inside this XML file,
4790
04:57:23,624 --> 04:57:24,624
and later on, we can pass it into our espinar.
Basically, this strings file is going to be
4791
04:57:24,624 --> 04:57:25,624
the place that we put all of the static strings.
For example, right now you can see that we
4792
04:57:25,624 --> 04:57:26,624
have this app name string, in which we have
used it inside our manifest file. Let's quickly
4793
04:57:26,624 --> 04:57:27,624
see that inside this Android manifest dot
XML file, we have this label attribute. If
4794
04:57:27,624 --> 04:57:28,624
we click on the label, you can see that it's
addressing the string file. And this is the
4795
04:57:28,624 --> 04:57:29,624
name of our string from strings that XML file,
we know for sure that the name of our application
4796
04:57:29,624 --> 04:57:30,624
is not going to change, for example, it's
not dependent on some string from our database
4797
04:57:30,624 --> 04:57:31,624
or the web server. So we have passed it in
here. In the next video, we will talk about
4798
04:57:31,624 --> 04:57:32,624
the usages of these strings that XML file
and also for that matter, other XML files
4799
04:57:32,624 --> 04:57:33,624
in our application. But for now, let's create
a string array statically in here, and let's
4800
04:57:33,624 --> 04:57:34,624
pass it to our spinner. So inside this strings
dot XML file, you can create two different
4801
04:57:34,624 --> 04:57:35,624
kinds of items, you can create a string, or
alternatively, you can create a string array.
4802
04:57:35,624 --> 04:57:36,624
The way to do that is like this, you can say
a string array, you need to name your array.
4803
04:57:36,624 --> 04:57:37,624
For example, in here, I'm going to say to
students after that, inside this opening and
4804
04:57:37,624 --> 04:57:38,624
closing tag, I can create different items,
let's say item. And inside these item tags,
4805
04:57:38,624 --> 04:57:39,624
I can provide the value for example, I can
say Mesa, you can copy and paste this item
4806
04:57:39,624 --> 04:57:40,624
as many times that you want.
4807
04:57:40,624 --> 04:57:41,624
Once again, don't worry, if you don't understand
these strings, that XML file, we will talk
4808
04:57:41,624 --> 04:57:42,624
about it in the next video. Now that we have
created this string array, we can pass it
4809
04:57:42,624 --> 04:57:43,624
to our spinner item inside our activity main
dot XML file. When we have created our spinner,
4810
04:57:43,624 --> 04:57:44,624
you have an attribute called entries. This
attribute in here, you can see that we have
4811
04:57:44,624 --> 04:57:45,624
these add airy students this is the address
of our string array in our strings dot XML
4812
04:57:45,624 --> 04:57:46,624
file that's passed that and as soon as I pass
that, you can see that we have some sample
4813
04:57:46,624 --> 04:57:47,624
data in our spinner. So now that I have passed
the data to our espinar, aesthetically, I
4814
04:57:47,624 --> 04:57:48,624
don't need to pass them in my jar file dynamically.
For that I can comment all of these lines
4815
04:57:48,624 --> 04:57:49,624
of code, for example, from here to top in
here. But right now we are getting an error
4816
04:57:49,624 --> 04:57:50,624
inside this on item selected method. And that's
because we no longer have this students array.
4817
04:57:50,624 --> 04:57:51,624
If you want to show the name of the student
when you select some item, instead of using
4818
04:57:51,624 --> 04:57:52,624
the students array list, you can use another
way. Let's delete this line of code and let's
4819
04:57:52,624 --> 04:57:53,624
create a new toast message. For the text.
This time I'm going to say students spinner
4820
04:57:53,624 --> 04:57:54,624
dot let's say get selected item. You can see
that this method is going to return an object,
4821
04:57:54,624 --> 04:57:55,624
let's say get selected item after that we
can see.to string. This to a string is just
4822
04:57:55,624 --> 04:57:56,624
another level of conversion in order to convert
the object that has been returned to a string.
4823
04:57:56,624 --> 04:57:57,624
After that, let's say plus selected once again
before running the application. Let's review
4824
04:57:57,624 --> 04:57:58,624
what we have done. First of all we have created
this string array after that we have passed
4825
04:57:58,624 --> 04:57:59,624
it to our spinner statically and after that
inside our Java five, we have created this
4826
04:57:59,624 --> 04:58:00,624
set on item selected listener. Inside the
on item selected method, we have created this
4827
04:58:00,624 --> 04:58:01,624
toast message. And we are getting the value
of every item by using a student's spinner
4828
04:58:01,624 --> 04:58:02,624
dot get selected item method. Let's run the
application. And let's see if you have the
4829
04:58:02,624 --> 04:58:03,624
same behavior. You can see that we have some
data in our spinner. If I select breath for
4830
04:58:03,624 --> 04:58:04,624
example, you can see the toast message Brad
selected. It seems like our spinner is working
4831
04:58:04,624 --> 04:58:05,624
fine. So far, we have talked about different
layout files, for example, this activity main
4832
04:58:05,624 --> 04:58:06,624
dot XML, which is a layout file. In the next
video, we are going to talk about other kinds
4833
04:58:06,624 --> 04:58:07,624
of XML files, for example, this strings that
XML or this Android manifest dot XML. Also,
4834
04:58:07,624 --> 04:58:08,624
we have other kinds of XML files. In the next
video, we are going to talk about them. Okay,
4835
04:58:08,624 --> 04:58:09,624
see you in the next video.
4836
04:58:09,624 --> 04:58:10,624
So far, we have talked about different layout
funds in our application placed inside this
4837
04:58:10,624 --> 04:58:11,624
Resources folder inside the layout folder.
For example, we have talked about this activity
4838
04:58:11,624 --> 04:58:12,624
main dot XML file, in which we have defined
the looks of our layout file for our main
4839
04:58:12,624 --> 04:58:13,624
activity. But in Android, there are a lot
of other XML files. For example, at the end
4840
04:58:13,624 --> 04:58:14,624
of the previous video, we have take a look
at the strings dot XML file. Let's close these
4841
04:58:14,624 --> 04:58:15,624
days. Let's talk a bit about that strings
dot XML file. As I said, it's placed inside
4842
04:58:15,624 --> 04:58:16,624
this Resources folder inside the values folder,
you can see these strings that XML file. If
4843
04:58:16,624 --> 04:58:17,624
you remember, I said that this is the place
that you will put all of the static strings
4844
04:58:17,624 --> 04:58:18,624
of your application. Let's quickly see what
I mean by that. So for example, inside my
4845
04:58:18,624 --> 04:58:19,624
layout file, if I define a TextView, we have
a TextView. In here, let's give it an ID.
4846
04:58:19,624 --> 04:58:20,624
Let's say txt Hello, let's go to our Java
file. And let's initialize this TextView.
4847
04:58:20,624 --> 04:58:21,624
Let's say private TextView. Let's name it.
txt Hello. And then in here inside the onCreate,
4848
04:58:21,624 --> 04:58:22,624
let's curricular initialize this TextView.
Let's see what would happen if we set the
4849
04:58:22,624 --> 04:58:23,624
text of this text a fellow manually. For example,
in here, if we say txt Hello dot set text.
4850
04:58:23,624 --> 04:58:24,624
And if we pass Hello, you can see that we
are getting some highlights in here. And the
4851
04:58:24,624 --> 04:58:25,624
highlight says that this string cannot be
translated use Android resources instead.
4852
04:58:25,624 --> 04:58:26,624
If you want, you can have some more information
about this warning, as the suggestion says,
4853
04:58:26,624 --> 04:58:27,624
you can press Ctrl plus f1. And you can see
a whole dialogue about this morning. Basically,
4854
04:58:27,624 --> 04:58:28,624
this in here means that this text is going
to be the same in all of the languages. For
4855
04:58:28,624 --> 04:58:29,624
example, if you have an application that has
some users around the world, this text is
4856
04:58:29,624 --> 04:58:30,624
going to be the same in all of the devices
regardless of the user's language. As the
4857
04:58:30,624 --> 04:58:31,624
warning says you can define this text inside
your strings dot XML file. Let's go to see
4858
04:58:31,624 --> 04:58:32,624
how we can use this strings dot XML file in
order to localize our application. So in here,
4859
04:58:32,624 --> 04:58:33,624
I'm going to define another string, the way
to do that is to open a tag and say a string.
4860
04:58:33,624 --> 04:58:34,624
After that, we need to name our string. For
example, in here, I'm going to simply say
4861
04:58:34,624 --> 04:58:35,624
hello. And for the value of this string, I'm
going to say hello. Now I can pass this string
4862
04:58:35,624 --> 04:58:36,624
to my text view inside my java file. Instead
of hard coding this text, I can say get a
4863
04:58:36,624 --> 04:58:37,624
string. This get a string method like this
find view by ID is an inner method inside
4864
04:58:37,624 --> 04:58:38,624
every activity, we can use that because we
are extending this app compat activity. So
4865
04:58:38,624 --> 04:58:39,624
inside these practices, we can address our
string file. For example, I can say R dot
4866
04:58:39,624 --> 04:58:40,624
string, dot let's say hello, right now the
result is going to be exactly as before. For
4867
04:58:40,624 --> 04:58:41,624
example, we are just going to show hello to
the user. But the option that we have in here
4868
04:58:41,624 --> 04:58:42,624
is to create another strings dot XML file
inside our values folder. For example, in
4869
04:58:42,624 --> 04:58:43,624
here, I can right click on my values folder.
I can say new value resource file. I need
4870
04:58:43,624 --> 04:58:44,624
to name this new XML file exactly strings,
let's say strings. The name is important in
4871
04:58:44,624 --> 04:58:45,624
here it should be exactly the same. And in
here you have a few options. We will talk
4872
04:58:45,624 --> 04:58:46,624
about few of them in here. But the one that
we are We're interested in here is this locale.
4873
04:58:46,624 --> 04:58:47,624
Let's select that. And let's add it to this
chosen qualifiers. So basically this locale
4874
04:58:47,624 --> 04:58:48,624
is going to be useful for when you want to
expose your application to different languages.
4875
04:58:48,624 --> 04:58:49,624
For example, if you know that you have some
users from Germany, you can translate your
4876
04:58:49,624 --> 04:58:50,624
application this way for those users as well.
Let's quickly see that if we can see Germany
4877
04:58:50,624 --> 04:58:51,624
here,
4878
04:58:51,624 --> 04:58:52,624
you can see that we have this the German D
stands for Deutsche in the right hand side,
4879
04:58:52,624 --> 04:58:53,624
we can select a specific region, we know that
there are multiple countries that spoke German,
4880
04:58:53,624 --> 04:58:54,624
let's select Germany. And let's press OK.
If you take a look at your values folder,
4881
04:58:54,624 --> 04:58:55,624
you can see that now you have these strings
folder in which inside that you have two strings
4882
04:58:55,624 --> 04:58:56,624
dot XML file, the names are exactly the same,
but the second one is for the German language
4883
04:58:56,624 --> 04:58:57,624
users. Right now, this strings dot XML file
for German users is empty. If you want, you
4884
04:58:57,624 --> 04:58:58,624
can define some strings in here. For example,
I'm going to define my Hello text in here
4885
04:58:58,624 --> 04:58:59,624
as well. Let's see a string. Let's name it
Hello. It's very important that this name
4886
04:58:59,624 --> 04:59:00,624
would be exactly the same name that you have
passed inside the other strings dot XML file.
4887
04:59:00,624 --> 04:59:01,624
If you remember in here, we have this string
called Hello, we are going to name it in here
4888
04:59:01,624 --> 04:59:02,624
hello as well. Now let's pass on value for
this string. For example, the German translation
4889
04:59:02,624 --> 04:59:03,624
of Hello is Hello, I believe. So inside this
strings dot XML file for the German users,
4890
04:59:03,624 --> 04:59:04,624
we have a string called Hello. But the value
in here is different from the one that we
4891
04:59:04,624 --> 04:59:05,624
had inside the default strings dot XML. Now
that we have created two strings, that XML
4892
04:59:05,624 --> 04:59:06,624
file, because inside our main activity, we
are using this get string method and we are
4893
04:59:06,624 --> 04:59:07,624
addressing our Hello. If some German language
user uses our application, the value of this
4894
04:59:07,624 --> 04:59:08,624
TextView will be different in different languages.
Let's quickly run our application. And let's
4895
04:59:08,624 --> 04:59:09,624
see the difference. Right now the language
of this device is English. And because of
4896
04:59:09,624 --> 04:59:10,624
that, we are seeing this Hello text. But if
we change the language of this device to let's
4897
04:59:10,624 --> 04:59:11,624
say German, we should see how though, let's
close this application, and let's quickly
4898
04:59:11,624 --> 04:59:12,624
change the language of this device. And let's
see if we can see the difference. So we can
4899
04:59:12,624 --> 04:59:13,624
go to the settings. down in here inside the
systems, I believe, or system, language and
4900
04:59:13,624 --> 04:59:14,624
input. Let's go to languages. And let's add
another language. Let's search for German,
4901
04:59:14,624 --> 04:59:15,624
or its equivalent storage, we need to select
a region let's select Deutschland. And also,
4902
04:59:15,624 --> 04:59:16,624
we need to make the storage as the default
language of this phone. So let's move this
4903
04:59:16,624 --> 04:59:17,624
to above this English. And now you can see
that the language of this phone has changed
4904
04:59:17,624 --> 04:59:18,624
to Deutsch. If we run our application, once
again, we should see the difference. This
4905
04:59:18,624 --> 04:59:19,624
time, you can see that we are seeing hollow
instead of Hello. Without changing a line
4906
04:59:19,624 --> 04:59:20,624
of code, we now have an application that looks
different in different languages. So these
4907
04:59:20,624 --> 04:59:21,624
are the strings dot XML file is extremely
useful for localization purposes. If you want,
4908
04:59:21,624 --> 04:59:22,624
you can add another string file. For example,
once again, on our values folder, we can right
4909
04:59:22,624 --> 04:59:23,624
click, we can say new value resource point.
Once again, we can name it strings. And once
4910
04:59:23,624 --> 04:59:24,624
again, we can select this locale. And if you
want, you can select another language and
4911
04:59:24,624 --> 04:59:25,624
you can do the same process. After creating
your application, if you want to translate
4912
04:59:25,624 --> 04:59:26,624
your application for another language, you
can pass this strings dot XML file to maybe
4913
04:59:26,624 --> 04:59:27,624
a translator and ask him or her to change
some of the values for your string files.
4914
04:59:27,624 --> 04:59:28,624
As simple as that you can localize your application
for different languages, you can see that
4915
04:59:28,624 --> 04:59:29,624
we also have an error in here this URL is
not important. Basically, it says that you
4916
04:59:29,624 --> 04:59:30,624
do not have an equivalent string for this
app name. You can also change the app name
4917
04:59:30,624 --> 04:59:31,624
in different languages as well. But we are
not going to do that in this application.
4918
04:59:31,624 --> 04:59:32,624
Okay, let's close all of these. And let's
talk about another kind of XML file in Android
4919
04:59:32,624 --> 04:59:33,624
applications. The other XML file that I'm
going to talk about is this colors dot XML
4920
04:59:33,624 --> 04:59:34,624
file. Once again, it's placed inside this
values folder. Let's take a look at that.
4921
04:59:34,624 --> 04:59:35,624
You See, that's right now we have three colors
in here. Like strings, this is going to be
4922
04:59:35,624 --> 04:59:36,624
the place that we pass all of our static colors.
If we want, we can define another color in
4923
04:59:36,624 --> 04:59:37,624
here for that we can come between these resources
tag. And we can say color. We can name our
4924
04:59:37,624 --> 04:59:38,624
color. For example, let's say blue. And for
the value, we need to pass a hexadecimal value,
4925
04:59:38,624 --> 04:59:39,624
we can get that value by searching for HTML
color picker. Let's quickly do that.
4926
04:59:39,624 --> 04:59:40,624
For example, in here, if we want a blue color,
a lighter blue, maybe we can copy the hexadecimal
4927
04:59:40,624 --> 04:59:41,624
value from here and we can paste it in our
project. We can see that at the left painting
4928
04:59:41,624 --> 04:59:42,624
here we have this blue color. Now that we
have this blue color, we can use it in different
4929
04:59:42,624 --> 04:59:43,624
places of our application. For example, inside
our activity main dot XML file. When we have
4930
04:59:43,624 --> 04:59:44,624
defined this TextView, we can change its color.
We can say color or to Riverside's text color,
4931
04:59:44,624 --> 04:59:45,624
and we can address our color. For example,
I can say add color slash blue. This add color
4932
04:59:45,624 --> 04:59:46,624
is referencing our colors that XML file this
morning, here, you can see that the color
4933
04:59:46,624 --> 04:59:47,624
of our text has been changed to this light
blue. Instead of searching for different colors
4934
04:59:47,624 --> 04:59:48,624
on the internet, you can also define them
another way. For example, you can copy this
4935
04:59:48,624 --> 04:59:49,624
color accent color. Let's copy that. Let's
paste it in here. And let's define avoid color.
4936
04:59:49,624 --> 04:59:50,624
But for the value of this white color, I can
click on this a square. And I can select a
4937
04:59:50,624 --> 04:59:51,624
void color. For example, let's select this
white. And as you can see, now we have a white
4938
04:59:51,624 --> 04:59:52,624
color without searching the internet. We can
get the hexadecimal values for different colors.
4939
04:59:52,624 --> 04:59:53,624
Okay, there is not much more to these colors
that XML file, I just wanted to show you how
4940
04:59:53,624 --> 04:59:54,624
you can define new colors. Let's talk about
the other XML file in Android applications,
4941
04:59:54,624 --> 04:59:55,624
which is this styles dot XML. But before that,
let's close these two. And let's open this
4942
04:59:55,624 --> 04:59:56,624
styles dot XML. Once again, it lives inside
our values folder. So basically, this styles
4943
04:59:56,624 --> 04:59:57,624
dot XML file is the place that we define the
theme of our application. Right now we have
4944
04:59:57,624 --> 04:59:58,624
one style in here. Later on, we will see that
where we have used this, the name of this
4945
04:59:58,624 --> 04:59:59,624
style is app theme. But if we want we can
override this for example, we can change the
4946
04:59:59,624 --> 05:00:00,624
value of some of these colors if we want.
Or we can change other attributes. You can
4947
05:00:00,624 --> 05:00:01,624
also define multiple styles. For example,
let's copy this whole style. And let's paste
4948
05:00:01,624 --> 05:00:02,624
it down in here. We need to name our style
differently. For example, let's say customized
4949
05:00:02,624 --> 05:00:03,624
app team.
4950
05:00:03,624 --> 05:00:04,624
And let's override the previous style. For
example, we have this parent attribute in
4951
05:00:04,624 --> 05:00:05,624
here, which you can guess it's implementing
all of the features of this exact theme in
4952
05:00:05,624 --> 05:00:06,624
here. If you want, first of all, you can change
this parent. For example, let's take a look
4953
05:00:06,624 --> 05:00:07,624
at the other ones. Let's say light.we have
these no action bar in which we are going
4954
05:00:07,624 --> 05:00:08,624
to use in future videos, we have these dialogues,
I'm not going to change the parent in here
4955
05:00:08,624 --> 05:00:09,624
instead, if you want, you can change some
of the values of this color. So in different
4956
05:00:09,624 --> 05:00:10,624
parts of our application, we may use this
color primary for different texts. The value
4957
05:00:10,624 --> 05:00:11,624
of this color primary is right now, this value
in here add color slash color primary. If
4958
05:00:11,624 --> 05:00:12,624
you want, you can change that, for example,
you can say blue the color that we just created.
4959
05:00:12,624 --> 05:00:13,624
You can see that now the color primary in
these two styles is different. For the first
4960
05:00:13,624 --> 05:00:14,624
one is this dark green for the second one
is this light blue. Now that you have defined
4961
05:00:14,624 --> 05:00:15,624
this customized app theme slide you can pass
it to let's say different activities in your
4962
05:00:15,624 --> 05:00:16,624
application. It means that different activities
in your application different pages in your
4963
05:00:16,624 --> 05:00:17,624
application can have different styles. For
example, one of them may have some toolbar,
4964
05:00:17,624 --> 05:00:18,624
some of them may not have that toolbar if
you use another parent. Later on we will see
4965
05:00:18,624 --> 05:00:19,624
that how we can do that. Also we will talk
about activities later on in the course Don't
4966
05:00:19,624 --> 05:00:20,624
worry about activities if you don't understand
them yet. So this styles dot XML file is the
4967
05:00:20,624 --> 05:00:21,624
place that you define different styles for
your application. We will be coming back to
4968
05:00:21,624 --> 05:00:22,624
this styles dot XML file later on. In the
course, specifically when we talk about material
4969
05:00:22,624 --> 05:00:23,624
design, but for now let's move on from this
part, I believe you grasp the general idea.
4970
05:00:23,624 --> 05:00:24,624
The other XML file that I'm going to talk
about is this manifest file, this Android
4971
05:00:24,624 --> 05:00:25,624
manifest dot XML file. I believe this is the
most important XML file in every Android application.
4972
05:00:25,624 --> 05:00:26,624
Basically, this is where you define the general
attributes the general features of your application,
4973
05:00:26,624 --> 05:00:27,624
you can see that we have this manifest tag.
Inside that we have this application tag,
4974
05:00:27,624 --> 05:00:28,624
we have seen this icon and round icon previously.
Basically, these are helpful for when you
4975
05:00:28,624 --> 05:00:29,624
want to define different icons for your application.
We also have this label, which is basically
4976
05:00:29,624 --> 05:00:30,624
the name of your application that is going
to be shown to the user. You also have a general
4977
05:00:30,624 --> 05:00:31,624
theme for your application, which as you can
see is addressing the style, the app theme
4978
05:00:31,624 --> 05:00:32,624
style, if you want, you can pass your customized
app theme. It will override all of the activities
4979
05:00:32,624 --> 05:00:33,624
in your application. But in here, I'm not
going to do that.
4980
05:00:33,624 --> 05:00:34,624
Right now, inside the application tag, you
have one activity, which is basically our
4981
05:00:34,624 --> 05:00:35,624
main activity. So basically, every activity
needs to be declared inside the manifest file
4982
05:00:35,624 --> 05:00:36,624
inside this application tag as well. This
activity in here has been added automatically,
4983
05:00:36,624 --> 05:00:37,624
because when we have created our application,
we have selected the empty activity template.
4984
05:00:37,624 --> 05:00:38,624
But if we don't select that template, one
of the things that we need to do is to come
4985
05:00:38,624 --> 05:00:39,624
inside this manifest file and declare our
activity. Inside this activity tag, you have
4986
05:00:39,624 --> 05:00:40,624
this intent filter tag, don't worry about
that yet. We will talk about that later on
4987
05:00:40,624 --> 05:00:41,624
in the course, I believe in the broadcast
receiver section of the course. But probably
4988
05:00:41,624 --> 05:00:42,624
you can guess the user of this intent filter
from this launcher and main, it means that
4989
05:00:42,624 --> 05:00:43,624
this main activity is the launcher activity
when we run our application. Don't worry about
4990
05:00:43,624 --> 05:00:44,624
all of these yet. These are just some basic
properties, some basic features of our application.
4991
05:00:44,624 --> 05:00:45,624
So beside activities, you need to define three
other elements in the manifest file as well.
4992
05:00:45,624 --> 05:00:46,624
So in every Android application, we have four
main components, activities, content providers,
4993
05:00:46,624 --> 05:00:47,624
broadcast receivers, and services. We will
talk about all of them later on in the course.
4994
05:00:47,624 --> 05:00:48,624
But these are the four main components of
every Android application, all of them needs
4995
05:00:48,624 --> 05:00:49,624
to be declared inside the manifest file. So
as you can see, this manifest file is very
4996
05:00:49,624 --> 05:00:50,624
important. It contains all of the important
features of your application, we will be coming
4997
05:00:50,624 --> 05:00:51,624
back to this manifest file a lot during the
course. So don't worry if you don't understand
4998
05:00:51,624 --> 05:00:52,624
all of these new stuff that we are seeing
in here. Okay, the next XML file that I'm
4999
05:00:52,624 --> 05:00:53,624
going to talk about is menus, let's quickly
see them as well. So in your project pane
5000
05:00:53,624 --> 05:00:54,624
inside the Resources folder, you can create
another directory for different menus. By
5001
05:00:54,624 --> 05:00:55,624
default, you do not have that directory like
we did have four different values and layout
5002
05:00:55,624 --> 05:00:56,624
files, we need to create that directory, we
can right click on our resource folder, we
5003
05:00:56,624 --> 05:00:57,624
can say new directory, it's very important
to name this directory menu. And after that,
5004
05:00:57,624 --> 05:00:58,624
you can see that we have this menu folder
in here. You can right click on that, and
5005
05:00:58,624 --> 05:00:59,624
you can say new menu resource file. This way,
you can create a menu dot XML file. For example,
5006
05:00:59,624 --> 05:01:00,624
let's name this menu file main menu. Let's
create this file. And let's see what does
5007
05:01:00,624 --> 05:01:01,624
it look like. Let's switch to text view. In
here you can see that we have this menu attack.
5008
05:01:01,624 --> 05:01:02,624
Basically menus are useful for when you want
to create some sort of menu in your application.
5009
05:01:02,624 --> 05:01:03,624
And the way to do that is to come inside this
menu tag and create different items I can
5010
05:01:03,624 --> 05:01:04,624
say item you can define multiple attributes
for your item but the one that is mandatory
5011
05:01:04,624 --> 05:01:05,624
and you have to create that is this title.
You can say Android column title, and you
5012
05:01:05,624 --> 05:01:06,624
can pass a title for your menu. For example,
we can say settings in here. You can see that
5013
05:01:06,624 --> 05:01:07,624
some menu has been created in here. later
on. We will pass this menu to for example,
5014
05:01:07,624 --> 05:01:08,624
our main activity to have some sort of menu
in our main activity. The other attributes
5015
05:01:08,624 --> 05:01:09,624
that I have in here is ID for example, I can
say Settings menu. We also can have an icon
5016
05:01:09,624 --> 05:01:10,624
for our menu item. But I believe we do not
have any icon in our project yet before by
5017
05:01:10,624 --> 05:01:11,624
An icon let's quickly create some icons in
our driver folder, we have seen how we can
5018
05:01:11,624 --> 05:01:12,624
create different icons, we can right click
on our driver folder by saying new image asset,
5019
05:01:12,624 --> 05:01:13,624
we can create icons. In the icon type, I'm
going to say action bar and tap icons. For
5020
05:01:13,624 --> 05:01:14,624
example, inside this clip art, let's search
for settings icon. You can see that we have
5021
05:01:14,624 --> 05:01:15,624
one icon in here, let's add that. Let's change
the name to IC settings. And let's change
5022
05:01:15,624 --> 05:01:16,624
the color to black. Let's add another icon
in our driver folder. Mirror Image asset like
5023
05:01:16,624 --> 05:01:17,624
before. Let's name this one icy alarm.
5024
05:01:17,624 --> 05:01:18,624
Let's search for alar. Color is fine, let's
just create this icon. Now that we have these
5025
05:01:18,624 --> 05:01:19,624
two icons, we can pass them in our menu file.
For example, in this item, I can say icon.
5026
05:01:19,624 --> 05:01:20,624
Let's pass our settings icon icy settings.
Right now we cannot see the icon of the setting
5027
05:01:20,624 --> 05:01:21,624
in here. But if you want to see that you can
define another attribute that's called show
5028
05:01:21,624 --> 05:01:22,624
as action. So we have some values for this
show as action attributes. If you say always,
5029
05:01:22,624 --> 05:01:23,624
it means that we always want our menu items
in the toolbar, you can see that we have other
5030
05:01:23,624 --> 05:01:24,624
options. For example, we have this if row,
it means that place them in the toolbar if
5031
05:01:24,624 --> 05:01:25,624
there is room for this menu item. We have
also this never it means that always show
5032
05:01:25,624 --> 05:01:26,624
the menu items like this and never show them
in the toolbar. Let's change the value for
5033
05:01:26,624 --> 05:01:27,624
now to always. And let's create another item
in here. So I can say item like before the
5034
05:01:27,624 --> 05:01:28,624
title is mandatory. And also we are getting
a warning for the previous items title. Once
5035
05:01:28,624 --> 05:01:29,624
again, it says that you need to define the
title in your strings dot XML file. Like before,
5036
05:01:29,624 --> 05:01:30,624
this is for the localization purposes. And
the best practice is to create this settings
5037
05:01:30,624 --> 05:01:31,624
title in your strings dot XML file. In your
real world applications, make sure to do that.
5038
05:01:31,624 --> 05:01:32,624
But in here for the sake of simplicity, I'm
not going to do that I'm going to cope with
5039
05:01:32,624 --> 05:01:33,624
this warning. So let's pass a title in here.
I'm going to say alarm. Let's give you time
5040
05:01:33,624 --> 05:01:34,624
Id let's say alarm menu. After that, let's
give you an icon. Let's say icy alarm. Let's
5041
05:01:34,624 --> 05:01:35,624
set the show as action attribute for this
one to e4. You can create as many items as
5042
05:01:35,624 --> 05:01:36,624
you want. But I think for this simple menu
item, these two would be fine. Okay, now that
5043
05:01:36,624 --> 05:01:37,624
we have created this menu file, let's see
how we can pass these two for example, our
5044
05:01:37,624 --> 05:01:38,624
main activity. Let's close this file. Once
again, it was placed inside our Resources
5045
05:01:38,624 --> 05:01:39,624
folder inside the menu directory that we just
created. In order to show this menu in your
5046
05:01:39,624 --> 05:01:40,624
main activity, you need to open the main activity
dot java file. And you need to override another
5047
05:01:40,624 --> 05:01:41,624
method in here. So because we are extending
the app compat activity inside this main activity,
5048
05:01:41,624 --> 05:01:42,624
we have some methods that we can override
one of these methods is this onCreate method.
5049
05:01:42,624 --> 05:01:43,624
If you come outside of this onCreate method
and press Ctrl plus or you can see the list
5050
05:01:43,624 --> 05:01:44,624
of all of the available methods you can see
that there are a lot of them. The one that
5051
05:01:44,624 --> 05:01:45,624
we need in here is called on create Options
menu. If you type on create Options menu,
5052
05:01:45,624 --> 05:01:46,624
you can see this method in here let's select
that. And in here we have this method. First
5053
05:01:46,624 --> 05:01:47,624
of all I'm going to delete this statement
from here this return super.on create Options
5054
05:01:47,624 --> 05:01:48,624
menu and I'm going to create or inflate my
menu for that I can say menu inflator this
5055
05:01:48,624 --> 05:01:49,624
class in here. Let's name it inflator is equal
to get menu inflator this gets menu inflator
5056
05:01:49,624 --> 05:01:50,624
is like this, find view by ID method. It's
an inner class in every activity. So basically
5057
05:01:50,624 --> 05:01:51,624
with the help of this inflator, we are going
to inflate or create our menu after accessing
5058
05:01:51,624 --> 05:01:52,624
to our inflator we can say inflator dot inflate.
For this inflate method. First of all we need
5059
05:01:52,624 --> 05:01:53,624
to address the menu font that we just created.
For that we can see our that menu that main
5060
05:01:53,624 --> 05:01:54,624
menu, the menu that we just created. The second
argument is the menu that He's being passed
5061
05:01:54,624 --> 05:01:55,624
to this method. That's fast that you can see
that the return type of this method is a Boolean.
5062
05:01:55,624 --> 05:01:56,624
This Boolean is indicating that if we have
successfully inflated our menu, by this point,
5063
05:01:56,624 --> 05:01:57,624
we have inflated our menu and after that we
can return through. This is all of the logic
5064
05:01:57,624 --> 05:01:58,624
that we need in order to inflate or create
our menu in our main activity. Let's run the
5065
05:01:58,624 --> 05:01:59,624
application. And let's see if we can see our
menu in our application.
5066
05:01:59,624 --> 05:02:00,624
You can see that in our activity, we have
these two items, this settings icon and this
5067
05:02:00,624 --> 05:02:01,624
alarm icon. We can also set an onclicklistener
for each one of these menu items. Let's quickly
5068
05:02:01,624 --> 05:02:02,624
see how we can do that. So we need to override
another method outside of this onCreate Options
5069
05:02:02,624 --> 05:02:03,624
menu method. If we press Ctrl plus Oh, and
if we search for on options item selected,
5070
05:02:03,624 --> 05:02:04,624
this method in here if we select that, we
can override that this time, I'm going to
5071
05:02:04,624 --> 05:02:05,624
keep this super statement we will talk about
I'm going to keep that. As you can see as
5072
05:02:05,624 --> 05:02:06,624
the input of this method, we are getting a
menu item. This is the item that user is going
5073
05:02:06,624 --> 05:02:07,624
to click on. And we can use it. For example,
to create a switch statement. Let's create
5074
05:02:07,624 --> 05:02:08,624
our switch statement. So our switch statement
is going to work on the item dot get item
5075
05:02:08,624 --> 05:02:09,624
ID. If you remember in our menu file for different
menu items, we have set different IDs. Let's
5076
05:02:09,624 --> 05:02:10,624
quickly see that in our main menu dot XML
file, we have set ID for each one of these
5077
05:02:10,624 --> 05:02:11,624
items. And in here, we are going to act upon
those IDs. Let's create our switch statement.
5078
05:02:11,624 --> 05:02:12,624
Let's say in case it's our.id. That settings
menu, we are going to show a toast message,
5079
05:02:12,624 --> 05:02:13,624
let's say toast. For the text let's see settings
clicked or settings selected. After showing
5080
05:02:13,624 --> 05:02:14,624
the toast message in here, instead of adding
a break for distribution statement for this
5081
05:02:14,624 --> 05:02:15,624
specific case, we can return something. Once
again you can see that the return type of
5082
05:02:15,624 --> 05:02:16,624
this method is a Boolean as well. This Boolean
indicates that if we have acted according
5083
05:02:16,624 --> 05:02:17,624
to this item successfully or not. In here,
after showing the toast message we are going
5084
05:02:17,624 --> 05:02:18,624
to return true. So let's say return true.
And let's create other cases. For example,
5085
05:02:18,624 --> 05:02:19,624
in case it's our that Id does alarm menu.
Once again, we are going to show another toast
5086
05:02:19,624 --> 05:02:20,624
message. Let's say alarm selected. In Here
I'm showing a toast message. But in a real
5087
05:02:20,624 --> 05:02:21,624
world application, you may want to navigate
the user to some other part of your application.
5088
05:02:21,624 --> 05:02:22,624
But more on that later on when we know about
different activities and fragment. Okay, after
5089
05:02:22,624 --> 05:02:23,624
showing the toast message, let's return through
once again. And also let's create the default
5090
05:02:23,624 --> 05:02:24,624
case. In the default case, I am going to return
this super statement. Because I'm not interested
5091
05:02:24,624 --> 05:02:25,624
in other items besides these two. So do whatever
you're doing inside the parent class. So basically,
5092
05:02:25,624 --> 05:02:26,624
I can call this super segment from here, I
can return it in the default case. Before
5093
05:02:26,624 --> 05:02:27,624
we run our application. Let's review what
we are doing in here. As the input of this
5094
05:02:27,624 --> 05:02:28,624
on options item selected method we are getting
a menu item, we can use this item ID in order
5095
05:02:28,624 --> 05:02:29,624
to create a switch statement. In case the
ID is the ID of our settings menu item, we
5096
05:02:29,624 --> 05:02:30,624
are showing this source message setting selected.
In case it's alarm menu item ID we are showing
5097
05:02:30,624 --> 05:02:31,624
another toast message. But in the default
case in case if it's another item beside these
5098
05:02:31,624 --> 05:02:32,624
two, we are passing the job to the parent
class. Let's run the application and let's
5099
05:02:32,624 --> 05:02:33,624
see if we have set the listener successfully.
So for example, if we click on this settings
5100
05:02:33,624 --> 05:02:34,624
icon, you can see that settings selected.
If we click on this alarm, we can see alarm
5101
05:02:34,624 --> 05:02:35,624
selected. It seems like you have done a good
job inside this on options item selected method.
5102
05:02:35,624 --> 05:02:36,624
Okay, I think that's enough for this video.
In the next video we are going to continue
5103
05:02:36,624 --> 05:02:37,624
our talk about other kinds of XML files available
in Android. See you in the next video.
5104
05:02:37,624 --> 05:02:38,624
Let's continue our talk about different XML
files in Android. In previous videos we take
5105
05:02:38,624 --> 05:02:39,624
a look at different XML files for example
drawable for its layout files, some files
5106
05:02:39,624 --> 05:02:40,624
inside our mipmap folder. We have seen a strange
and colors and styles. Beside these, we take
5107
05:02:40,624 --> 05:02:41,624
a look at menus and also our manifest file.
Let's see what else do we have in here. Right
5108
05:02:41,624 --> 05:02:42,624
now our main activity has only one layout
file this activity main dot XML. But if you
5109
05:02:42,624 --> 05:02:43,624
want, you can create more layout files for
this activity. For example, right now, this
5110
05:02:43,624 --> 05:02:44,624
layout is for the portrait mode, if you want,
you can create another layout file for the
5111
05:02:44,624 --> 05:02:45,624
let's say landscape mode. If you don't know
what the portrait and landscape modes are,
5112
05:02:45,624 --> 05:02:46,624
let me quickly show that. So right now, this
is called the portrait mode. If we change
5113
05:02:46,624 --> 05:02:47,624
the rotation of our device, and this is going
to be the landscape, I think I need to activate
5114
05:02:47,624 --> 05:02:48,624
the rotation of this device. Let's go to the
certification center. And from here, I believe
5115
05:02:48,624 --> 05:02:49,624
this is the option, this auto rotate, we need
to activate that. And now if we rotate our
5116
05:02:49,624 --> 05:02:50,624
device, you can see that now we have a different
look for our application. Sometimes this might
5117
05:02:50,624 --> 05:02:51,624
work. But if you have a complex lab file that
you want to show it differently in different
5118
05:02:51,624 --> 05:02:52,624
rotations, you need to create two separate
files for each one of these rotations, one
5119
05:02:52,624 --> 05:02:53,624
for the portrait mode and one for the landscape
mode. Let's quickly see that how we can create
5120
05:02:53,624 --> 05:02:54,624
another layout file for this main activity.
So inside my layout folder, on the layout
5121
05:02:54,624 --> 05:02:55,624
folder, I can right click by selecting new
layout resource file, I can create a layout
5122
05:02:55,624 --> 05:02:56,624
file, or alternatively, inside this activity
main dot XML file. From here, if you click
5123
05:02:56,624 --> 05:02:57,624
on this icon, you can see that we have this
portrait selected right now, if you want,
5124
05:02:57,624 --> 05:02:58,624
you can create a landscape variation for the
layout of your main activity. But before I
5125
05:02:58,624 --> 05:02:59,624
do that, I'm going to add another text. And
also I'm going to add some constraints to
5126
05:02:59,624 --> 05:03:00,624
this layout file. So I'm going to move this
new TextView to below my level text. Let's
5127
05:03:00,624 --> 05:03:01,624
quickly add some constraints. As I mentioned
before, if your items are too close to each
5128
05:03:01,624 --> 05:03:02,624
other, you can always select both elements.
And you can use the right click menu by going
5129
05:03:02,624 --> 05:03:03,624
to constraint and add constraint from here,
too. But I'm not going to do that. Instead,
5130
05:03:03,624 --> 05:03:04,624
I'm going to add some margins of let's say
500. So for the simple application, this is
5131
05:03:04,624 --> 05:03:05,624
going to be the look of my application in
the portrait view, go to text views are placed
5132
05:03:05,624 --> 05:03:06,624
after each other vertically. But in the landscape
mode, I'm going to put the two text beside
5133
05:03:06,624 --> 05:03:07,624
each other, let's quickly create a variation
for our landscape mode. By selecting this
5134
05:03:07,624 --> 05:03:08,624
create landscape variation, you can see that
inside our layout file, now we have to find
5135
05:03:08,624 --> 05:03:09,624
inside the activity main folder, we have this
activity main dot XML, which is for the portrait
5136
05:03:09,624 --> 05:03:10,624
mode, and the other one, which is for the
landscape mode. And now if we want we can
5137
05:03:10,624 --> 05:03:11,624
change this landscape for example, I'm going
to change the constraint of this TextView.
5138
05:03:11,624 --> 05:03:12,624
Let's delete all of the previous constraints.
I'm also going to delete the constraint of
5139
05:03:12,624 --> 05:03:13,624
this TextView the left constraint. Let's place
this fun in here, and let's add some constraint.
5140
05:03:13,624 --> 05:03:14,624
Also, another point in here that you may find
useful is that if you want to center your
5141
05:03:14,624 --> 05:03:15,624
elements, for example, horizontally, you can
select the two items, for example, these two
5142
05:03:15,624 --> 05:03:16,624
by right clicking on the item, and by going
to this center, and selecting horizontally,
5143
05:03:16,624 --> 05:03:17,624
you can center your items horizontally. So
once again, this is the view for the landscape
5144
05:03:17,624 --> 05:03:18,624
mode, the layout for the landscape mode, and
this is going to be the layer for the portrait
5145
05:03:18,624 --> 05:03:19,624
mode. Now if I run my application, I should
see the difference. You can see that in the
5146
05:03:19,624 --> 05:03:20,624
landscape mode, the two texts are beside each
other. But if you change the rotation to portrait
5147
05:03:20,624 --> 05:03:21,624
mode, the two texts are vertically after each
other. So this way, by coming to this icon
5148
05:03:21,624 --> 05:03:22,624
in here and selecting create landscape variation,
you can create another layout file for your
5149
05:03:22,624 --> 05:03:23,624
main activity. But this is not the only option.
You can also create a nice version in case
5150
05:03:23,624 --> 05:03:24,624
if you want to activate a nice theme for your
application. I'm not going to do that. Let's
5151
05:03:24,624 --> 05:03:25,624
quickly see how we can create that. So in
here, I can go to this create other and from
5152
05:03:25,624 --> 05:03:26,624
here I have all sorts of options. For example,
if we want to create a night mode for our
5153
05:03:26,624 --> 05:03:27,624
activity, we can select this night mode, we
can pass it to this qualifiers. And for the
5154
05:03:27,624 --> 05:03:28,624
night mode. That's a night. Let's create this
fun. And let's take a look at our project
5155
05:03:28,624 --> 05:03:29,624
thing. Now inside this activity main folder
you can see that we have three activity main
5156
05:03:29,624 --> 05:03:30,624
dot XML files, the middle one is for the night
view. If you want, you can change that, for
5157
05:03:30,624 --> 05:03:31,624
example, you can change some colors, but I'm
not going to do that in here. I'm just going
5158
05:03:31,624 --> 05:03:32,624
to ignore this layout file for the night view.
Let's see what other layouts we can create
5159
05:03:32,624 --> 05:03:33,624
for our activity. So once again, I can go
to this icon in here by saying create other
5160
05:03:33,624 --> 05:03:34,624
you can see that we have all sorts of options.
For example, In here, we have this version,
5161
05:03:34,624 --> 05:03:35,624
in which we can create different layers for
IDs for different versions of the Android
5162
05:03:35,624 --> 05:03:36,624
on the user's device. Let's add this to this
chosen qualifiers. And in here, we can put
5163
05:03:36,624 --> 05:03:37,624
a number. For example, if we want our application
to look different in API level 21, we can
5164
05:03:37,624 --> 05:03:38,624
put 21 in here, and we can create the layout
file. Let's take a look at our layout folder
5165
05:03:38,624 --> 05:03:39,624
inside activity main folder. Now we have this
activity main dot XML, which is a specific
5166
05:03:39,624 --> 05:03:40,624
for the version 21 of the API. This way, you
can also create other layout files, and you
5167
05:03:40,624 --> 05:03:41,624
can change them if you want. Before I finish
off this video, I'm going to talk about another
5168
05:03:41,624 --> 05:03:42,624
point of XML files in Android. let's right
click on our layout file, and let's select
5169
05:03:42,624 --> 05:03:43,624
new layout resource file. And let's see what
an include is. So for the file name of this
5170
05:03:43,624 --> 05:03:44,624
XML file, I'm going to say trademark. And
also I'm going to change the root element
5171
05:03:44,624 --> 05:03:45,624
to let's say, relative layout. Let's select
Okay, and let's switch to TextView. Inside
5172
05:03:45,624 --> 05:03:46,624
this TextView, you can see that we have this
relative layout. So basically you use include
5173
05:03:46,624 --> 05:03:47,624
whenever you want to reuse some layout file,
for example, now I have created this trademark
5174
05:03:47,624 --> 05:03:48,624
dot XML file in which I'm going to put an
image view and a text view inside that after
5175
05:03:48,624 --> 05:03:49,624
creating this layout file, I can reuse this
trademark dot XML in order to show this trademark
5176
05:03:49,624 --> 05:03:50,624
in multiple activities of my application.
So the use of include tag is to reuse your
5177
05:03:50,624 --> 05:03:51,624
layout files. First of all, let's design this
trademark dot XML. In here, I'm going to say
5178
05:03:51,624 --> 05:03:52,624
TextView. Or before that let's define an image
view,
5179
05:03:52,624 --> 05:03:53,624
let's say 15. dp for the width and height.
Let's give it an ID.
5180
05:03:53,624 --> 05:03:54,624
And for the placeholder, I'm not going to
import any image into my project. Instead,
5181
05:03:54,624 --> 05:03:55,624
I'm going to use this IC launcher background
which exists in our driver folder. Let's also
5182
05:03:55,624 --> 05:03:56,624
define a TextView. In here, wrap content and
wrap content for the text. I'm going to say
5183
05:03:56,624 --> 05:03:57,624
developed by me Sam. After that, let's move
this TextView to the right of our image view,
5184
05:03:57,624 --> 05:03:58,624
let's say to the right of logo. Let's add
some margin left, maybe 10 dP, let's style
5185
05:03:58,624 --> 05:03:59,624
this text a little bit. Let's say italic,
and forked. Let's also change the size of
5186
05:03:59,624 --> 05:04:00,624
this TextView. Right now the width and height
of this relative layout, this whole parent
5187
05:04:00,624 --> 05:04:01,624
is match parent in here, I'm going to change
that I'm going to change both of these to
5188
05:04:01,624 --> 05:04:02,624
wrap content, you will see why when we use
this layout fine. And also I'm going to add
5189
05:04:02,624 --> 05:04:03,624
another attribute for both of these elements.
Let's center them vertically, center vertical
5190
05:04:03,624 --> 05:04:04,624
for both of them, let's say through between
place them at the center vertically. So this
5191
05:04:04,624 --> 05:04:05,624
is our simple layout file. In here we are
showing a TextView and also an image view.
5192
05:04:05,624 --> 05:04:06,624
If we want to show this layout file in multiple
places in our application, we can use the
5193
05:04:06,624 --> 05:04:07,624
include tag include tag is very useful for
when you have a complex layout file, and you
5194
05:04:07,624 --> 05:04:08,624
don't want to write the exact same file twice.
Let's switch to our activity main dot XML
5195
05:04:08,624 --> 05:04:09,624
file. And let's see how we can use that trademark.
So down here after the TextView, I'm going
5196
05:04:09,624 --> 05:04:10,624
to say include, and I can add a layout attribute.
And I need to pass the address of my layout
5197
05:04:10,624 --> 05:04:11,624
file. In this case, it's at layer slash trade.
As simple as that we can include another XML
5198
05:04:11,624 --> 05:04:12,624
file inside this layout file. If you want,
we can also constraint this layout file, you
5199
05:04:12,624 --> 05:04:13,624
can see that I can select this whole layout
file. But right now if I try to constraint
5200
05:04:13,624 --> 05:04:14,624
my layout file, you can see that it won't
work, we can see that it will be placed once
5201
05:04:14,624 --> 05:04:15,624
again at the top of my screen. And that's
because we need to override the width and
5202
05:04:15,624 --> 05:04:16,624
height of this layout, right. The way to do
that is to compensate this include tag. And
5203
05:04:16,624 --> 05:04:17,624
in here for my whole layout file, I need to
define my layout width and height once again.
5204
05:04:17,624 --> 05:04:18,624
So for example for the layout, which I'm going
to say wrap content for the height as well,
5205
05:04:18,624 --> 05:04:19,624
I'm going to say wrap content This way, you
can see that now we can constraint our layout
5206
05:04:19,624 --> 05:04:20,624
file, we just need to override the width and
height of our layout file. Let's also add
5207
05:04:20,624 --> 05:04:21,624
two more constraints. I can use this trademark
dot XML file as many times as I want in different
5208
05:04:21,624 --> 05:04:22,624
parts of my application that can be useful
sometimes also where you're using include
5209
05:04:22,624 --> 05:04:23,624
tag, you may want another tag. Let's quickly
see that. So instead of this relative layout,
5210
05:04:23,624 --> 05:04:24,624
sometimes you may see merge this tagging here.
So basically what this merge is doing is that
5211
05:04:24,624 --> 05:04:25,624
it's avoiding redundancy. Let's talk about
what I mean by that. So right now these two
5212
05:04:25,624 --> 05:04:26,624
elements have no idea about about their parents,
they do not know where they are going to be
5213
05:04:26,624 --> 05:04:27,624
used inside relative layout, a linear layout
or a constraint layout. For that reason, because
5214
05:04:27,624 --> 05:04:28,624
they don't know about the parent, you can
see that our layout does not look very beautiful
5215
05:04:28,624 --> 05:04:29,624
in here. But if you know for sure that where
you're going to use this layout file, for
5216
05:04:29,624 --> 05:04:30,624
example, if you are going to use this layout
file inside a linear layout, you do not need
5217
05:04:30,624 --> 05:04:31,624
to write the linear layout. Once again, you
can use this merge tag in order to avoid redundancy.
5218
05:04:31,624 --> 05:04:32,624
At the time of rendering your layout file,
this merge tag will be ignored. And these
5219
05:04:32,624 --> 05:04:33,624
two elements will be placed inside the parent
that they are going to be used. Sometimes
5220
05:04:33,624 --> 05:04:34,624
when you know for sure, that's where you're
going to use your layout file using this merge
5221
05:04:34,624 --> 05:04:35,624
tag can improve the performance when rendering
your layout. But in here, I'm just going to
5222
05:04:35,624 --> 05:04:36,624
change this to a relative layout. Okay, I
think that's enough talking about different
5223
05:04:36,624 --> 05:04:37,624
XML files in Android, we have others as well,
we will take a look at them later on in the
5224
05:04:37,624 --> 05:04:38,624
course. But for now, I think it's enough.
In the next video, we are going to talk about
5225
05:04:38,624 --> 05:04:39,624
the material design library, see you in the
next video.
5226
05:04:39,624 --> 05:04:40,624
In creating Android applications, designing
is an important aspect. And creating a beautiful
5227
05:04:40,624 --> 05:04:41,624
layout will impact the number of downloads
for your application on the Play Store. And
5228
05:04:41,624 --> 05:04:42,624
also it will impact the user's feedback. But
sometimes designing can be challenging, especially
5229
05:04:42,624 --> 05:04:43,624
if you're like me if you are more interested
in the code side of the application. But luckily,
5230
05:04:43,624 --> 05:04:44,624
we have material design as a guideline for
our designing. Despite being a guideline Material
5231
05:04:44,624 --> 05:04:45,624
Design also introduces some new components
that we can use in our application. Let's
5232
05:04:45,624 --> 05:04:46,624
check the material design website. And let's
see how we can use that in our project. The
5233
05:04:46,624 --> 05:04:47,624
website for material design is material that
5234
05:04:47,624 --> 05:04:48,624
if you go to this Developer tab in here, you
can see that besides Android applications,
5235
05:04:48,624 --> 05:04:49,624
you can also use material design for iOS app
development, you can use it for web development,
5236
05:04:49,624 --> 05:04:50,624
and also you can use it with hybrid development
with flutter. Let's go to the Android page.
5237
05:04:50,624 --> 05:04:51,624
As I said, Material Design consists of two
things. First of all, it's a guideline for
5238
05:04:51,624 --> 05:04:52,624
designing your applications. Also, besides
being a guideline, you have some components
5239
05:04:52,624 --> 05:04:53,624
that you can use in your application, we will
take a look at those components in a minute.
5240
05:04:53,624 --> 05:04:54,624
But before that, if you take a look at this
theming in here, for example, if you want
5241
05:04:54,624 --> 05:04:55,624
to check the guidelines for typography theming,
you can click on this link. And after that,
5242
05:04:55,624 --> 05:04:56,624
if you take a look at this material design
guideline for typography, you will see some
5243
05:04:56,624 --> 05:04:57,624
guidelines. Basically these guidelines are
helpful for when you want to design and create
5244
05:04:57,624 --> 05:04:58,624
a beautiful application. Basically, these
are some to do's and not to do's in order
5245
05:04:58,624 --> 05:04:59,624
to follow when you are designing different
applications. So make sure to check this webpage
5246
05:04:59,624 --> 05:05:00,624
and the guidelines. In here we are not going
to talk about guidelines. Instead we are going
5247
05:05:00,624 --> 05:05:01,624
to take a look at the components. If you go
to this components link, you will see that
5248
05:05:01,624 --> 05:05:02,624
we have a lot of components. We are familiar
with some of these components. For example,
5249
05:05:02,624 --> 05:05:03,624
we have seen different buttons and checkboxes.
But some of them are new. Don't worry about
5250
05:05:03,624 --> 05:05:04,624
that later on. We will see a lot of these
in the course. But let's take a look at a
5251
05:05:04,624 --> 05:05:05,624
few components that we already know. For example,
let's take these buttons. Then in here in
5252
05:05:05,624 --> 05:05:06,624
this image, you can see that we have four
different kinds of material buttons. These
5253
05:05:06,624 --> 05:05:07,624
are somehow different from the buttons that
we have used already in our applications.
5254
05:05:07,624 --> 05:05:08,624
Basically, the main difference is in the styling
of different buttons. Similar for button we
5255
05:05:08,624 --> 05:05:09,624
have all sorts of other components. For example,
we have checkboxes for some of these components.
5256
05:05:09,624 --> 05:05:10,624
We have also some documentation and guidelines
for designing them. For example, if you want
5257
05:05:10,624 --> 05:05:11,624
to check the guidelines for designing a checkbox,
you can click on that link. And in here, you
5258
05:05:11,624 --> 05:05:12,624
can see some guidelines. So make sure to check
that if you need but let's see how we can
5259
05:05:12,624 --> 05:05:13,624
use these components in our applications.
Before that, I'm just going to say that in
5260
05:05:13,624 --> 05:05:14,624
this video, we are not going to vote with
all of these components. During the course
5261
05:05:14,624 --> 05:05:15,624
we will be coming back to this web page and
a lot of these components. But for now first
5262
05:05:15,624 --> 05:05:16,624
of all, we are going to start with material
buttons. We will also take a look at the floating
5263
05:05:16,624 --> 05:05:17,624
action button. This one in here. We will also
take a look at the material cart and things
5264
05:05:17,624 --> 05:05:18,624
like a snack bars. Later on in the course
we will take a look at the top app bars, bottom
5265
05:05:18,624 --> 05:05:19,624
up bars, and also things like tab layouts
and a lot more. Okay, instead of talking let's
5266
05:05:19,624 --> 05:05:20,624
implement these maffeo buttons in our application.
In order to use material design components
5267
05:05:20,624 --> 05:05:21,624
in your application, you need to add a dependency
in your application. We haven't used any extra
5268
05:05:21,624 --> 05:05:22,624
dependencies in our applications yet, but
Let's see how we can do that. If you remember
5269
05:05:22,624 --> 05:05:23,624
I said that Gradle will combine all of the
layout files, all of the Java files all of
5270
05:05:23,624 --> 05:05:24,624
the static resources in our application. And
besides all of those Gradle will also use
5271
05:05:24,624 --> 05:05:25,624
the third party libraries that we use in our
application. One of those libraries that we
5272
05:05:25,624 --> 05:05:26,624
are going to use in here and later on Gradle
is going to handle combining it in our application
5273
05:05:26,624 --> 05:05:27,624
is this material design. So we need to add
this material design library in our project
5274
05:05:27,624 --> 05:05:28,624
with the help of Gradle. In order to add the
material design library, you can go to this
5275
05:05:28,624 --> 05:05:29,624
Developer tab. After that, you can select
this Android. And inside this documentation,
5276
05:05:29,624 --> 05:05:30,624
you can go to this getting started. And from
there, you can find the dependency that you
5277
05:05:30,624 --> 05:05:31,624
need. In this page, you can see a step by
step guide for adding the library, the material
5278
05:05:31,624 --> 05:05:32,624
design library into your project, make sure
to read this page if you need. But basically
5279
05:05:32,624 --> 05:05:33,624
what they are saying is that, first of all,
make sure that you are using Google repositories
5280
05:05:33,624 --> 05:05:34,624
in your project. Let's see where that is in
Android Studio, in Android Studio inside this
5281
05:05:34,624 --> 05:05:35,624
Gradle script inside this build that Gradle.
This project module, you need to add the Google
5282
05:05:35,624 --> 05:05:36,624
repository, Google repositories has been added
by default into our project inside this all
5283
05:05:36,624 --> 05:05:37,624
projects. But if you don't see this Google
in here, make sure to add that the next step
5284
05:05:37,624 --> 05:05:38,624
after adding the Google repository is to add
Material Design dependency, you can copy this
5285
05:05:38,624 --> 05:05:39,624
dependency from here, and you can add it to
your project. Let's copy that this time, we
5286
05:05:39,624 --> 05:05:40,624
need to go to this build that Gradle module
app this one in here. But before that, let's
5287
05:05:40,624 --> 05:05:41,624
close this one. And let's also minimize all
these extra pains, we need to add that dependency
5288
05:05:41,624 --> 05:05:42,624
inside these dependencies in here. For example,
you can add that dependency before all the
5289
05:05:42,624 --> 05:05:43,624
other dependencies. Let's face that in here.
But we also need to change this line because
5290
05:05:43,624 --> 05:05:44,624
right now we have this version tag in here.
And this is not the version, it's better always
5291
05:05:44,624 --> 05:05:45,624
check the internet for the latest version
of material design, you can also use the help
5292
05:05:45,624 --> 05:05:46,624
of Andrew the studio. Right now I'm getting
a highlight. If I hover over this highlight,
5293
05:05:46,624 --> 05:05:47,624
you can see that the latest version available
is 1.1 point zero. So instead of this version,
5294
05:05:47,624 --> 05:05:48,624
I'm going to say 1.1 point zero. After adding
this dependency, you need to click on this
5295
05:05:48,624 --> 05:05:49,624
sync now. And after that Gradle will download
some files to this specific material design
5296
05:05:49,624 --> 05:05:50,624
library files from the internet. And it will
add the library to your project. So let's
5297
05:05:50,624 --> 05:05:51,624
click on the sync. Now before that, make sure
that you have an internet connection, it was
5298
05:05:51,624 --> 05:05:52,624
somehow fast. And now we can use material
design components in our project. Also inside
5299
05:05:52,624 --> 05:05:53,624
these dependencies, you can see that by default,
we have other dependencies as well. For example,
5300
05:05:53,624 --> 05:05:54,624
we have this constraint layout, we have these
dependencies for testing. And also we have
5301
05:05:54,624 --> 05:05:55,624
these app compat dependencies which help backward
compatibility, but more on them later on.
5302
05:05:55,624 --> 05:05:56,624
Okay, now that we have added this material
dependency, let's see how we can use them.
5303
05:05:56,624 --> 05:05:57,624
But before using them, I'm going to add a
button in my layout file. Let's quickly see
5304
05:05:57,624 --> 05:05:58,624
that. Let's also enable this preview. And
before everything, let's delete this TextView.
5305
05:05:58,624 --> 05:05:59,624
So in here, I'm going to save button, let's
say wrap content and wrap content. And let's
5306
05:05:59,624 --> 05:06:00,624
define a text,
5307
05:06:00,624 --> 05:06:01,624
let's say Say hello. Let's also change this
constraint layout to a relative layout. And
5308
05:06:01,624 --> 05:06:02,624
let's center our button. You can see that
we are seeing the previous slide for our button.
5309
05:06:02,624 --> 05:06:03,624
Now let's implement material theme. And let's
see how that changes the size of our button.
5310
05:06:03,624 --> 05:06:04,624
So for that, I'm going to open my project
pane. If you remember inside the Resources
5311
05:06:04,624 --> 05:06:05,624
folder inside the values folder, we have these
slides that XML. Let's open that right now
5312
05:06:05,624 --> 05:06:06,624
we have this app theme style in which its
parent is this theme that we are seeing in
5313
05:06:06,624 --> 05:06:07,624
here. If we want to implement Matthew, your
theme, we can change this parent. Let's quickly
5314
05:06:07,624 --> 05:06:08,624
do that. And the theme in here is going to
be themed material components. We also have
5315
05:06:08,624 --> 05:06:09,624
another theme called theme dot material components
that light this one in here video, we'll take
5316
05:06:09,624 --> 05:06:10,624
a look at that in a minute. But in here, I'm
not going to customize this theme that material
5317
05:06:10,624 --> 05:06:11,624
components. So for that, I'm going to delete
all these three items. I'm going to use all
5318
05:06:11,624 --> 05:06:12,624
of the values from inside these material components
theme, so let's delete them. We just change
5319
05:06:12,624 --> 05:06:13,624
the past theme of this afternoon. But right
now if we switch to our activity main dot
5320
05:06:13,624 --> 05:06:14,624
XML file, you can see that we are getting
this dark theme somehow we also change the
5321
05:06:14,624 --> 05:06:15,624
style of our button. This is good for a night
theme. But in here I'm going to change the
5322
05:06:15,624 --> 05:06:16,624
parents theme to a light theme so that we
can see better. So let's select this lighting
5323
05:06:16,624 --> 05:06:17,624
here. And if we switch back to our activity
main dot XML file, you can see that we are
5324
05:06:17,624 --> 05:06:18,624
seeing this beautiful light theme out button
is now installed, we have some round corners,
5325
05:06:18,624 --> 05:06:19,624
the font is somehow different. And also the
spacing between the letters is different.
5326
05:06:19,624 --> 05:06:20,624
We didn't change anything for our button elements.
But because we are using the material theme
5327
05:06:20,624 --> 05:06:21,624
as the parent theme of our application, all
of these styles have been applied automatically.
5328
05:06:21,624 --> 05:06:22,624
We can also customize this button for example,
we can change the background color of this
5329
05:06:22,624 --> 05:06:23,624
button. But before that, let's quickly add
a color to our color resources. Inside colors
5330
05:06:23,624 --> 05:06:24,624
that XML I'm going to add a new color, let's
copy this font. Let's face it, and let's change
5331
05:06:24,624 --> 05:06:25,624
the name to let's say orange. For the value,
I'm going to select this orange color. Now
5332
05:06:25,624 --> 05:06:26,624
in our activity main dot XML file, if we want
to change the background color of this button
5333
05:06:26,624 --> 05:06:27,624
to the orange, we can say background tint
this morning here, and we can address our
5334
05:06:27,624 --> 05:06:28,624
orange color. We can also change the text
color of this button. But I don't think that's
5335
05:06:28,624 --> 05:06:29,624
necessary, I just wanted to show you that
you can customize your buttons. Let's delete
5336
05:06:29,624 --> 05:06:30,624
this color. As you saw on the material design
website, we have some styles for our button.
5337
05:06:30,624 --> 05:06:31,624
Let's see other slides as well. For example,
in here I can see a style. Let's search for
5338
05:06:31,624 --> 05:06:32,624
material button, you can see that we have
this on elevated button style. Let's quickly
5339
05:06:32,624 --> 05:06:33,624
see that this is basically this style that
we are currently using. It's the default case.
5340
05:06:33,624 --> 05:06:34,624
Let's remove that. And let's see others, we
have this button style, we have this outline
5341
05:06:34,624 --> 05:06:35,624
button. If you take a look at that, you can
see that the style of your button is changed
5342
05:06:35,624 --> 05:06:36,624
to some outline button. Sometimes this might
be useful. We also have a text button, let's
5343
05:06:36,624 --> 05:06:37,624
quickly see that. This is the one material
components Start button the text button, this
5344
05:06:37,624 --> 05:06:38,624
will change our button to a text button. Sometimes
you may want to use this, I'm going to stay
5345
05:06:38,624 --> 05:06:39,624
with the default case. So I'm going to delete
this whole state attribute. So you can see
5346
05:06:39,624 --> 05:06:40,624
that using material design is very easy. Basically,
we just added the material dependency into
5347
05:06:40,624 --> 05:06:41,624
our project inside this, like build that Gradle
module app. After that inside the slides,
5348
05:06:41,624 --> 05:06:42,624
we change the parent to this team, that material
components team. And after that we can use
5349
05:06:42,624 --> 05:06:43,624
all of the elements from inside the material
design library. If you add another element
5350
05:06:43,624 --> 05:06:44,624
into your layout file, for example, a checkbox
By default, the checkbox from the material
5351
05:06:44,624 --> 05:06:45,624
design library will be used. Okay, let's move
on from this part. And let's talk about some
5352
05:06:45,624 --> 05:06:46,624
of the components that we don't have in our
applications. If we don't use the material
5353
05:06:46,624 --> 05:06:47,624
design library, the first of those components
that I'm going to talk about in here is called
5354
05:06:47,624 --> 05:06:48,624
floating action button. Let's delete this
button. And let's see that floating action
5355
05:06:48,624 --> 05:06:49,624
button. So in here I can say floating action
button, we can see that we have two option,
5356
05:06:49,624 --> 05:06:50,624
we have this floating action button and we
have this extended floating action button.
5357
05:06:50,624 --> 05:06:51,624
Let's use the first one for the width and
height I'm going to say wrap content. And
5358
05:06:51,624 --> 05:06:52,624
as soon as I do that, you can see that we
have this beautiful button in here, this floating
5359
05:06:52,624 --> 05:06:53,624
action button is going to be floating on our
layout file. Even if for example, we scroll
5360
05:06:53,624 --> 05:06:54,624
the page, if you want, you can change the
location of this button for example, you can
5361
05:06:54,624 --> 05:06:55,624
move it to down below in here. For that I
can use two attributes that we haven't seen
5362
05:06:55,624 --> 05:06:56,624
so far. For example, I can see a line parent
button and we can set it to true, we can see
5363
05:06:56,624 --> 05:06:57,624
that as soon as I do that its location will
be changed to the bottom of our screen, we
5364
05:06:57,624 --> 05:06:58,624
can also use another attribute called align
parent. And in order to move it to the right
5365
05:06:58,624 --> 05:06:59,624
of our screen, let's say through and now you
can see the difference. Let's also add some
5366
05:06:59,624 --> 05:07:00,624
margins. Let's say margin. In here, we have
two options, we can add this margin right.
5367
05:07:00,624 --> 05:07:01,624
Or alternatively, we can use this version.
And if I use this module right in here, I
5368
05:07:01,624 --> 05:07:02,624
will get a warning. Let's quickly use that.
And let's see the warning. For example. In
5369
05:07:02,624 --> 05:07:03,624
here, I'm going to say 20 dP, you can see
the highlighting here. If you take a look
5370
05:07:03,624 --> 05:07:04,624
at this, it says that it's better to use margin.
And the reason for this is because some of
5371
05:07:04,624 --> 05:07:05,624
the languages it starts from right. So for
example, if the language of your user is Arabic,
5372
05:07:05,624 --> 05:07:06,624
in that case, this floating action button
will be placed at the left of the screen.
5373
05:07:06,624 --> 05:07:07,624
And if you add a margin, right, it's going
to put the floating action button to outside
5374
05:07:07,624 --> 05:07:08,624
of the screen. For that reason. It's better
to use margin and so let's say margin and,
5375
05:07:08,624 --> 05:07:09,624
and let's fast 20 dP, you can see that we
are getting the same result in here. We also
5376
05:07:09,624 --> 05:07:10,624
need to add a margin button. That's a margin
button. And that's first 20 dP. You can also
5377
05:07:10,624 --> 05:07:11,624
add an icon for your floating action button
as well. Let's quickly add an icon into our
5378
05:07:11,624 --> 05:07:12,624
driver folder and let's add it for our floating
action button. Let's say new image assets
5379
05:07:12,624 --> 05:07:13,624
on our driver folder. Let's select action
bar and tab icons. And let's select a plus
5380
05:07:13,624 --> 05:07:14,624
sign in here. Let's search for add. And let's
add this one. For the name, let's say ICF.
5381
05:07:14,624 --> 05:07:15,624
And also, let's change the color. I'm not
sure that if this is a white color for that,
5382
05:07:15,624 --> 05:07:16,624
I'm going to use this custom color. And I'm
going to use white. Right now the selector
5383
05:07:16,624 --> 05:07:17,624
is on white. So let's say choose, and let's
create our icon. And now you can add the icon
5384
05:07:17,624 --> 05:07:18,624
for your floating action button. But for some
reason, I cannot use the source attribute,
5385
05:07:18,624 --> 05:07:19,624
which is the attribute to pass an icon for
your floating action button. I think because
5386
05:07:19,624 --> 05:07:20,624
I added this dependency in my project, I need
to restart my Android Studio, or the other
5387
05:07:20,624 --> 05:07:21,624
option is to rebuild my project. Or the better
option is to go to this file, and invalidate
5388
05:07:21,624 --> 05:07:22,624
your cache and restart Android Studio.
5389
05:07:22,624 --> 05:07:23,624
Let's select that, let's see, invalidate and
restart. Now that I have invalidated my cache,
5390
05:07:23,624 --> 05:07:24,624
I can use the source attribute. So inside
this floating action button tag, I can say
5391
05:07:24,624 --> 05:07:25,624
source, this 100 source, and I can pass my
ice icon, you can see that as soon as I do
5392
05:07:25,624 --> 05:07:26,624
that, this icon will be added to my floating
action button. If you want, you can also change
5393
05:07:26,624 --> 05:07:27,624
the color of this floating action button.
For example, let's change it to the orange
5394
05:07:27,624 --> 05:07:28,624
color that we have defined a few minutes ago.
For that I can say background tint this one
5395
05:07:28,624 --> 05:07:29,624
here, and I can pass color orange, you can
see that the color will be changed to orange.
5396
05:07:29,624 --> 05:07:30,624
Also, you can see that we have some sort of
border for this floating action button. If
5397
05:07:30,624 --> 05:07:31,624
you want, you can also change that. The way
to do that is like this, you can say app column,
5398
05:07:31,624 --> 05:07:32,624
background tint. And this way, you can pass
your new collar for example, if you want to
5399
05:07:32,624 --> 05:07:33,624
pass avoid color. First of all, you can create
that color inside your collar resources. And
5400
05:07:33,624 --> 05:07:34,624
after that pass that or alternatively, you
can pass the hexadecimal value of that color
5401
05:07:34,624 --> 05:07:35,624
directly in here. For example, I know that
hashtag FFF is for the void color. So if I
5402
05:07:35,624 --> 05:07:36,624
pass that, you can see that we have a void
color in here. And also you can see that the
5403
05:07:36,624 --> 05:07:37,624
border of this floating action button is now
white. So this Android column background tint
5404
05:07:37,624 --> 05:07:38,624
is for the background color of your floating
action button. But this app column background
5405
05:07:38,624 --> 05:07:39,624
tint is for the border of your floating action
button. You can also define another color
5406
05:07:39,624 --> 05:07:40,624
anything. Let's quickly see that you can see
rebuild color this one in here, app column
5407
05:07:40,624 --> 05:07:41,624
repo color, and pass a color. For example,
once again, I'm going to pass void. This repo
5408
05:07:41,624 --> 05:07:42,624
color is the color of the floating action
button when you click on it, let's run this
5409
05:07:42,624 --> 05:07:43,624
application. And let's see that you can see
that we have this beautiful floating action
5410
05:07:43,624 --> 05:07:44,624
button at the bottom of our screen. And if
we click on that, you can see some color.
5411
05:07:44,624 --> 05:07:45,624
That white color is this rainbow color. Beside
that you may also have noticed that the color
5412
05:07:45,624 --> 05:07:46,624
of this top bar has changed as well. This
is because we are using material components
5413
05:07:46,624 --> 05:07:47,624
theme in our application. Later on, we will
see that we have a beautiful toolbar when
5414
05:07:47,624 --> 05:07:48,624
we work with material design. We will implement
that toolbar in our applications later on.
5415
05:07:48,624 --> 05:07:49,624
But for now let's talk more about this floating
action button. You can also set an onclicklistener
5416
05:07:49,624 --> 05:07:50,624
for this button if you want as well. But for
that we need to have access to this floating
5417
05:07:50,624 --> 05:07:51,624
action button inside our main activity dot
java file. So for example, like before, I
5418
05:07:51,624 --> 05:07:52,624
can say private floating action button, this
one that comes from material, that floating
5419
05:07:52,624 --> 05:07:53,624
action button package. Let's name it FA B.
After that we need to instantiate it inside
5420
05:07:53,624 --> 05:07:54,624
our on create method. Let's say FA v is equal
to find view by ID. But before that, we need
5421
05:07:54,624 --> 05:07:55,624
to give an ID to our floating action button.
I don't think we did that in here. Let's say
5422
05:07:55,624 --> 05:07:56,624
ID, let's just name it FA B, let's say our.id
dot FAA. After that you can say fab dot set
5423
05:07:56,624 --> 05:07:57,624
onclicklistener is fine in here. And we can
pass our listener. For example, let's try
5424
05:07:57,624 --> 05:07:58,624
a toast message. Let's say fab clicked. Let's
run the application. And let's see if we see
5425
05:07:58,624 --> 05:07:59,624
the toast message. You can see that you're
seeing that toast message it seems to be perfect.
5426
05:07:59,624 --> 05:08:00,624
There are also other ways of using this floating
action buttons for example, one way is to
5427
05:08:00,624 --> 05:08:01,624
use a combination of floating action button
and app bar navigation. In that case, we will
5428
05:08:01,624 --> 05:08:02,624
see that we can implement a beautiful style
and a beautiful design for our application.
5429
05:08:02,624 --> 05:08:03,624
But more on that later on when we talk about
bottom apart navigations Okay, I think that's
5430
05:08:03,624 --> 05:08:04,624
enough for this video. In the next video we
will talk about two more components in material
5431
05:08:04,624 --> 05:08:05,624
design library. We will talk about snack bars
and also card views. See you in the next video.
5432
05:08:05,624 --> 05:08:06,624
In this video, we are going to talk about
two more material components. The first of
5433
05:08:06,624 --> 05:08:07,624
those components is snack bar. Before we start
implementing the snack bar, let's see what
5434
05:08:07,624 --> 05:08:08,624
does it look like. So if you go to material
that i o in these components, in this listening
5435
05:08:08,624 --> 05:08:09,624
here, you can see a snack bar. Let's see that.
This is what a snack bar looks like. It's
5436
05:08:09,624 --> 05:08:10,624
a lot like a toast message. But the style
is a little bit different. And also inside
5437
05:08:10,624 --> 05:08:11,624
the snack bar, you can have a button that
will do some action. Beside that you can make
5438
05:08:11,624 --> 05:08:12,624
your snack bars to be indefinite, it means
that they won't be dismissed until the user
5439
05:08:12,624 --> 05:08:13,624
clicks on this action. Also, you can style
your snack bar as well. For example, you can
5440
05:08:13,624 --> 05:08:14,624
change the color of this text and also the
color of this action button. As we will see
5441
05:08:14,624 --> 05:08:15,624
later on in the course snack bars are extremely
useful when you're developing Android applications.
5442
05:08:15,624 --> 05:08:16,624
In order to use the snack bars. First of all,
you need to add the material design library
5443
05:08:16,624 --> 05:08:17,624
into your project. So let's go to this Developer
tab. In this Android section, let's go to
5444
05:08:17,624 --> 05:08:18,624
the documentation getting started. Let's copy
the dependency. In our project inside the
5445
05:08:18,624 --> 05:08:19,624
Gradle scripts build that Gradle. Let's quickly
add that dependency. For the version, if you
5446
05:08:19,624 --> 05:08:20,624
hover over this, you will see the latest version,
which is 1.1 point or after adding the material
5447
05:08:20,624 --> 05:08:21,624
dependency. First of all in my layout file,
I'm going to do some changes, I'm going to
5448
05:08:21,624 --> 05:08:22,624
add a button. And also beside that I'm going
to give an ID to my parent layout, both First
5449
05:08:22,624 --> 05:08:23,624
of all, you may have noticed some changes
in the looks of Android Studio compared to
5450
05:08:23,624 --> 05:08:24,624
the previous video. That's because I have
updated my Android Studio. And we have slight
5451
05:08:24,624 --> 05:08:25,624
differences. Don't worry about that we will
talk about these differences in the next video.
5452
05:08:25,624 --> 05:08:26,624
For this video, let's just implement our snack
bar. The first difference inside this new
5453
05:08:26,624 --> 05:08:27,624
Android Studio is that you no longer can see
the text and design mode in here. And if you
5454
05:08:27,624 --> 05:08:28,624
want to go to your text, you can go to this
split view in here. Once again, we will talk
5455
05:08:28,624 --> 05:08:29,624
about these differences. Let's give an ID
to our constraint layout. Let's say ID, I'm
5456
05:08:29,624 --> 05:08:30,624
going to name it parent. Later on in this
video, we will see that why I'm giving an
5457
05:08:30,624 --> 05:08:31,624
ID to this constraint layout. Let's also go
back to our design view. And let's add a button.
5458
05:08:31,624 --> 05:08:32,624
Before that let's delete this text here. Let's
quickly constraint it. And also Let's change
5459
05:08:32,624 --> 05:08:33,624
its text. I'm going to change it to show snack
bar. Let's also quickly changed the theme
5460
05:08:33,624 --> 05:08:34,624
of my application so that we can see the material
button. In our project like we saw in the
5461
05:08:34,624 --> 05:08:35,624
previous video in our values folder inside
the styles folder. I'm just going to change
5462
05:08:35,624 --> 05:08:36,624
the panel theme here. I'm going to change
it to this team that material components stuff
5463
05:08:36,624 --> 05:08:37,624
light. If you switch back to your layout file,
now you can see that we are using the material
5464
05:08:37,624 --> 05:08:38,624
buttons in my java file. First of all, I'm
going to initialize this parent this constraint
5465
05:08:38,624 --> 05:08:39,624
layout and also this button. Let's quickly
do that, let's say private constraint layout.
5466
05:08:39,624 --> 05:08:40,624
Let's name it parent. And also private button.
Let's name it btn. Show sniper. Down here
5467
05:08:40,624 --> 05:08:41,624
inside the onCreate method. Let's initialize
these two parent is equal to find view by
5468
05:08:41,624 --> 05:08:42,624
ID r.id. That parent potential snack bar is
equal to find view by ID r.id. That I believe
5469
05:08:42,624 --> 05:08:43,624
its ID was button after that set onclicklistener
for this button. Let's say btn show snack
5470
05:08:43,624 --> 05:08:44,624
bar dot set onclicklistener new onclicklistener
I'm going to hand the job to another method.
5471
05:08:44,624 --> 05:08:45,624
So let's say show snack bar. Let's quickly
create this method down here. Private void
5472
05:08:45,624 --> 05:08:46,624
show snack bar. We don't need any input.
5473
05:08:46,624 --> 05:08:47,624
And here is how you can show snack what you
can say snack bar dots make without instantiating
5474
05:08:47,624 --> 05:08:48,624
a new snack bar you can use this static make
method in order to create your snack bar.
5475
05:08:48,624 --> 05:08:49,624
This make method needs three inputs. First
of all we need to pass the parent layout the
5476
05:08:49,624 --> 05:08:50,624
layout that we are going to show our snack
bar inside that in this case our parents layout
5477
05:08:50,624 --> 05:08:51,624
is a constraint layout. And as you saw I have
initialized that constraint layout Here, that's
5478
05:08:51,624 --> 05:08:52,624
the reason I gave an ID to my constraint layout.
Now in here, I can say parent. After that
5479
05:08:52,624 --> 05:08:53,624
I need to text that I want to show in my snack
bar. For example, I'm going to say this is
5480
05:08:53,624 --> 05:08:54,624
a snack bar. The third parameter in here is
a constant that will indicate the time that
5481
05:08:54,624 --> 05:08:55,624
you want to show your a snack bar. And you
can pass that constant like this, you can
5482
05:08:55,624 --> 05:08:56,624
say, a snack bar, that you have three constants
in here, this length long, net short, and
5483
05:08:56,624 --> 05:08:57,624
also left indefinite. The long and short are
descriptive, but if you personally have indefinite,
5484
05:08:57,624 --> 05:08:58,624
you will show your snack bar indefinitely,
and it won't be dismissed until the user clicks
5485
05:08:58,624 --> 05:08:59,624
on that action button. Let's select this one
for now. indefinite after this make method,
5486
05:08:59,624 --> 05:09:00,624
you can call dot show method in order to show
your snack bar. This is going to be a really
5487
05:09:00,624 --> 05:09:01,624
simple snack bar. And we don't have any action
yet. Let's see if it's going to work. If we
5488
05:09:01,624 --> 05:09:02,624
click on this button, we should see our snack
bar. And here it is. It's a really basic snack
5489
05:09:02,624 --> 05:09:03,624
bar. And we don't have any action beside that
we haven't changed a slide of our text. Also,
5490
05:09:03,624 --> 05:09:04,624
because we have past this length, indefinite
there is no way to dismiss this snackbar.
5491
05:09:04,624 --> 05:09:05,624
Let's quickly change this constant to length
short, and let's see the difference. Now if
5492
05:09:05,624 --> 05:09:06,624
we show our snack bar, you can see that it
will be dismissed after a few moments. Okay,
5493
05:09:06,624 --> 05:09:07,624
now let's see how we can add an action to
our snack bar. But first of all, I'm going
5494
05:09:07,624 --> 05:09:08,624
to change this constant to left indefinite
once again. And before the show method, I'm
5495
05:09:08,624 --> 05:09:09,624
going to call another method. That method
is called dot set action. First of all, we
5496
05:09:09,624 --> 05:09:10,624
need a text in here, this is going to be the
text of our action. For example, if you want
5497
05:09:10,624 --> 05:09:11,624
you can say retry in here. After that you
need an onClick listener that's a new onClick
5498
05:09:11,624 --> 05:09:12,624
listener. This is going to be an interface.
And inside the onClick method, we can do whatever
5499
05:09:12,624 --> 05:09:13,624
we want when the user clicks on this retry
text. For example, in this case, I'm just
5500
05:09:13,624 --> 05:09:14,624
going to show a toast message. Let's say retry
clicked. Let's see if we can see the action.
5501
05:09:14,624 --> 05:09:15,624
As you can see in here, now we have this retry
option. Remember that the length is still
5502
05:09:15,624 --> 05:09:16,624
indefinite. But if we click on this retry,
this snack bar will be dismissed. And also
5503
05:09:16,624 --> 05:09:17,624
we will see a toast message. Let's also see
that how we can change the color of these
5504
05:09:17,624 --> 05:09:18,624
two texts this retry and also the color of
the message of our snack bar. For that once
5505
05:09:18,624 --> 05:09:19,624
again before the show method in here, I'm
going to call another method. Let's say that
5506
05:09:19,624 --> 05:09:20,624
set action text color in order to change the
color of our action text. In here, you can
5507
05:09:20,624 --> 05:09:21,624
pass your colors in two different ways. First
of all, you can address the color from your
5508
05:09:21,624 --> 05:09:22,624
color resources, if you remember in your project
inside the Resources folder, inside the values
5509
05:09:22,624 --> 05:09:23,624
folder, you had these colors, that XML file
in which you have few colors inside that you
5510
05:09:23,624 --> 05:09:24,624
can address one of these corners inside your
set action text color like this, you can say
5511
05:09:24,624 --> 05:09:25,624
get color. And after that our dot color dot
for example, color primary. Beside that if
5512
05:09:25,624 --> 05:09:26,624
you have noticed, we have some colors that
are coming from the material design libraries.
5513
05:09:26,624 --> 05:09:27,624
You can see that we have these colors as well.
But for example right now, if I use this color
5514
05:09:27,624 --> 05:09:28,624
primary in here, you can see that we are getting
this warning. And this warning says that you
5515
05:09:28,624 --> 05:09:29,624
can use this method in API level 23. and higher.
There is also another way to get our color
5516
05:09:29,624 --> 05:09:30,624
without seeing this error. Let's we can see
that.
5517
05:09:30,624 --> 05:09:31,624
Let's say get resources. This phone dot let's
say get column. Inside this method, you can
5518
05:09:31,624 --> 05:09:32,624
address your color, you can say our color,
that for example, color accent. Let's also
5519
05:09:32,624 --> 05:09:33,624
see the other way. Instead of using the resources
in my project, I can use a Java class. So
5520
05:09:33,624 --> 05:09:34,624
for example, instead of this whole method
in here, I can say color with a capital C,
5521
05:09:34,624 --> 05:09:35,624
which is coming from Android dot graphics
dot let's say read. This way we can get red
5522
05:09:35,624 --> 05:09:36,624
color with the help of this color class. So
if you don't have this red color in your resources,
5523
05:09:36,624 --> 05:09:37,624
you can also use this class. Okay, now let's
see how we can change the color of the message
5524
05:09:37,624 --> 05:09:38,624
of our snack. For that I can say that set
text color this method. Once again, in order
5525
05:09:38,624 --> 05:09:39,624
to pass a color you have two options. In this
case, I'm going to say color dot for example
5526
05:09:39,624 --> 05:09:40,624
yellow. Let's run the application and let's
see if we have successfully changed the color
5527
05:09:40,624 --> 05:09:41,624
or not. Let's show our snack bar. You can
see that the color of the message is yellow.
5528
05:09:41,624 --> 05:09:42,624
And also the color of our action button is
red. Okay, I think that's enough talking about
5529
05:09:42,624 --> 05:09:43,624
the snack bar. Later on, we will use snack
bars a lot in the course. The next material
5530
05:09:43,624 --> 05:09:44,624
component that I'm going to talk about is
material court view. Let's quickly see what
5531
05:09:44,624 --> 05:09:45,624
does it look like in material that I have.
Once again, inside this components tab inside
5532
05:09:45,624 --> 05:09:46,624
this cart, you can see your cart views. Let's
scroll down and filled with this is what cart
5533
05:09:46,624 --> 05:09:47,624
view looks like. You can use it to, for example,
show different items in your application,
5534
05:09:47,624 --> 05:09:48,624
you can mix UI elements in form of a cart.
Beside that you can also have some actions
5535
05:09:48,624 --> 05:09:49,624
for your cart view. Also, you can have some
coronary radius, and also you can have some
5536
05:09:49,624 --> 05:09:50,624
shadow or elevation to be precise beside your
current view. In this video, we are not going
5537
05:09:50,624 --> 05:09:51,624
to implement the actions we will do that later
on in the course. But for now, let's just
5538
05:09:51,624 --> 05:09:52,624
implement a simple cartoon in our application,
we have added the material design dependencies
5539
05:09:52,624 --> 05:09:53,624
in our project, so we can add the cart view
without any problem. But before that, let's
5540
05:09:53,624 --> 05:09:54,624
close this style and colors. In my layout
file in here, I'm going to add my cart view
5541
05:09:54,624 --> 05:09:55,624
below this button. Let's switch to the split
view. And let's add that down in here, I'm
5542
05:09:55,624 --> 05:09:56,624
going to define my cart view, let's say material
cart view. This one, beside the material cart
5543
05:09:56,624 --> 05:09:57,624
view, you also have a native cards view. But
style is a little bit different. If you don't
5544
05:09:57,624 --> 05:09:58,624
want to use the material card view, you can
always go to developer.android.com and get
5545
05:09:58,624 --> 05:09:59,624
the dependency from the Android x libraries
for cart view. But in here, let's implement
5546
05:09:59,624 --> 05:10:00,624
this material cart view. I'm going to enter
my width and height manually. For the bits,
5547
05:10:00,624 --> 05:10:01,624
I'm going to say 100 dP, I think that's fine.
And for the height, let's say 200 DPI, it
5548
05:10:01,624 --> 05:10:02,624
has an odd shape, I think it's better to change
the width to 150 dpi. Let's give it an ID.
5549
05:10:02,624 --> 05:10:03,624
I'm going to say card view. After that inside
this card view, let's enter some elements.
5550
05:10:03,624 --> 05:10:04,624
But before that, let's quickly concentrate
our call to you. Inside this cart view, first
5551
05:10:04,624 --> 05:10:05,624
of all, I'm going to define a relative layout.
For the width and height, let's say match
5552
05:10:05,624 --> 05:10:06,624
parent, the parent is this card view. So we
are going to fit the whole cart view. And
5553
05:10:06,624 --> 05:10:07,624
inside this relative layout, I'm going to
put my elements for example, I'm going to
5554
05:10:07,624 --> 05:10:08,624
put a TextView wrap content and wrap content.
Let's also center it horizontally. For its
5555
05:10:08,624 --> 05:10:09,624
text, let's just say hello. Let's also change
the style of this code, let's say text style.
5556
05:10:09,624 --> 05:10:10,624
And let's pass both beside that let's increase
the size. Let's say 18. So after that below
5557
05:10:10,624 --> 05:10:11,624
this TextView, I'm going to define an image
view, you can see that in a cart view, you
5558
05:10:11,624 --> 05:10:12,624
can have multiple UI elements for the width
of this image, I'm going to save 140 dP. And
5559
05:10:12,624 --> 05:10:13,624
for the height, let's say match parent, I'm
going to move this image view to below this
5560
05:10:13,624 --> 05:10:14,624
TextView. So let's say layout below. But before
that I need to give an ID to this TextView.
5561
05:10:14,624 --> 05:10:15,624
Let's say ID. txt Hello. Let's also add a
margin top and let's center this image view
5562
05:10:15,624 --> 05:10:16,624
margin top, let's say 10 dP centered horizontal
through. Let's also use a placeholder, let's
5563
05:10:16,624 --> 05:10:17,624
say source. I'm going to pass this IC luncheon.
Okay, I think that's enough for understanding
5564
05:10:17,624 --> 05:10:18,624
the cardview. Beside that in their current
view elements, you have other attributes as
5565
05:10:18,624 --> 05:10:19,624
well. For example, if you want to make the
coordinates of your card view radius, you
5566
05:10:19,624 --> 05:10:20,624
have this option corner radius, called corner
radius. You can say five dB, for example,
5567
05:10:20,624 --> 05:10:21,624
right now you may not see the difference.
But if we run our application, we will see
5568
05:10:21,624 --> 05:10:22,624
the corner radius You can also change the
corner radius if you think that five DB is
5569
05:10:22,624 --> 05:10:23,624
not enough. The other option that we have
in here is this card elevation in which will
5570
05:10:23,624 --> 05:10:24,624
give some 3d shape to our card, we have some
shadow behind our cards. Once again, I'm going
5571
05:10:24,624 --> 05:10:25,624
to say five up, you can also define this card
view like any other UI elements in your Java
5572
05:10:25,624 --> 05:10:26,624
file. Let's quickly do that private cart view.
In here you can see that we have two options.
5573
05:10:26,624 --> 05:10:27,624
We have this card view which is coming from
Android x card view. And also we have this
5574
05:10:27,624 --> 05:10:28,624
material card view which is coming from the
material design libraries. In here we are
5575
05:10:28,624 --> 05:10:29,624
going to use this material called view but
once again, the difference is only in the
5576
05:10:29,624 --> 05:10:30,624
style. Let's name it card view. And down in
here inside the onCreate method. Let's
5577
05:10:30,624 --> 05:10:31,624
initialize it Let's quickly set an onclicklistener
for this cart view, cart view dot set onclicklistener.
5578
05:10:31,624 --> 05:10:32,624
Beside that, you can see that we have a lot
of options in here. We will talk about them
5579
05:10:32,624 --> 05:10:33,624
later on in the course, set onclicklistener.
Let's just show a toast message. Let's run
5580
05:10:33,624 --> 05:10:34,624
the application. And let's see what does our
cart view look like? Our cart view looks like
5581
05:10:34,624 --> 05:10:35,624
something like this, which is not that good.
But we can do much better with the card views.
5582
05:10:35,624 --> 05:10:36,624
We will do that later on in the course. Specifically
when we talk about recycler view, the combination
5583
05:10:36,624 --> 05:10:37,624
of recycler view and card view for its items
will create a really beautiful layout file.
5584
05:10:37,624 --> 05:10:38,624
We will see that I believe in the next video.
But in here, you can see that we have this
5585
05:10:38,624 --> 05:10:39,624
corner radius and also we have this elevation
which is this 3d shape for our cart view.
5586
05:10:39,624 --> 05:10:40,624
Beside that later on in the course, we will
see how we can create an expandable card view.
5587
05:10:40,624 --> 05:10:41,624
That means that we will have some button in
here for example, an arrow in which by clicking
5588
05:10:41,624 --> 05:10:42,624
on that view, expand our cart view and we
will see some more information and maybe actions
5589
05:10:42,624 --> 05:10:43,624
for our item in our cart view. Okay, I think
that's enough for this video. In this video,
5590
05:10:43,624 --> 05:10:44,624
I just wanted to show you how you can create
a snack bar and also implement this cart view.
5591
05:10:44,624 --> 05:10:45,624
In the next video, we are going to start talking
about recycler view. In previous videos we
5592
05:10:45,624 --> 05:10:46,624
have seen list view for showing different
items. But recycler view is a much more better
5593
05:10:46,624 --> 05:10:47,624
option to show lists of different items. In
the next two or three videos we are going
5594
05:10:47,624 --> 05:10:48,624
to see how we can use recycler views. See
you in the next video.
5595
05:10:48,624 --> 05:10:49,624
In this video, we are going to talk about
recycler view, which as I said before is another
5596
05:10:49,624 --> 05:10:50,624
option for showing a list of different items
in our application. But before that, as you
5597
05:10:50,624 --> 05:10:51,624
can see, I've updated my Android Studio and
in here we have some changes. Before talking
5598
05:10:51,624 --> 05:10:52,624
about recycler view, I'm going to show some
of these changes in the newest version of
5599
05:10:52,624 --> 05:10:53,624
Android Studio, I strongly suggest that you
always keep your Android Studio updated. You
5600
05:10:53,624 --> 05:10:54,624
can do that by going to this Help menu. down
in here, you can check for different updates.
5601
05:10:54,624 --> 05:10:55,624
And if there is any updates available, you
can see a list of different options. This
5602
05:10:55,624 --> 05:10:56,624
in here is Firebase services, we are not going
to use that I'm going to cancel this update,
5603
05:10:56,624 --> 05:10:57,624
you can see that I've also have some updates
for my emulator and hexam installer. I will
5604
05:10:57,624 --> 05:10:58,624
update them after this video. But for now,
let's just say remind me later. Also, if you
5605
05:10:58,624 --> 05:10:59,624
remember from the installation videos, I've
said that always check the updates from a
5606
05:10:59,624 --> 05:11:00,624
stable channel. If you're not sure about your
channel, you can always go to settings by
5607
05:11:00,624 --> 05:11:01,624
going to this file tab in here by going to
settings. In here, if you check for updates,
5608
05:11:01,624 --> 05:11:02,624
you can see that my updates are being checked
from inside the stable channel. We have others
5609
05:11:02,624 --> 05:11:03,624
as well. These other channels are for preview
purposes. And it's basically for professional
5610
05:11:03,624 --> 05:11:04,624
developers to check new features in Android
and report some bugs if there is any to the
5611
05:11:04,624 --> 05:11:05,624
Android team. But for you for the starter,
it's probably best to check your updates from
5612
05:11:05,624 --> 05:11:06,624
a stable channel. The first thing that is
different in this newest version of Android
5613
05:11:06,624 --> 05:11:07,624
Studio is the way you go to full screen, you
can go to full screen by going to this view
5614
05:11:07,624 --> 05:11:08,624
menu. And by going to appearance. From here,
you can go to full screen. In previous versions,
5615
05:11:08,624 --> 05:11:09,624
this option was somewhere down in here. Let's
go to full screen mode. The other change that
5616
05:11:09,624 --> 05:11:10,624
you can probably see in here is this component
tree. This type in here is a little bit different.
5617
05:11:10,624 --> 05:11:11,624
There are not much changes in here just this
time. But there is a very important change
5618
05:11:11,624 --> 05:11:12,624
above in here. You can see that down in here
we no longer have that design and text view
5619
05:11:12,624 --> 05:11:13,624
mode. Instead of in here we have three options.
Right now we are inside the design view. We
5620
05:11:13,624 --> 05:11:14,624
can also select this one to go to the text
view. And in between these two, we have this
5621
05:11:14,624 --> 05:11:15,624
splitter mode. As you can see, it acts like
if you have enabled preview inside your text
5622
05:11:15,624 --> 05:11:16,624
view, but there is a difference. Before that
let's quickly minimize this project pane.
5623
05:11:16,624 --> 05:11:17,624
From here from inside this splitter view,
you can also enable attributes. For example,
5624
05:11:17,624 --> 05:11:18,624
if I select this button, I can see the text
view of this button I can see the design view
5625
05:11:18,624 --> 05:11:19,624
of this button and also I can see all of the
attributes in here as well. This is going
5626
05:11:19,624 --> 05:11:20,624
to be helpful, especially if you're using
multiple monitors. For now I'm just going
5627
05:11:20,624 --> 05:11:21,624
to minimize this attribute pane. You You can
also change the size of this preview. Besides
5628
05:11:21,624 --> 05:11:22,624
attributes, you can also enable this palette
from here in which you can see all of different
5629
05:11:22,624 --> 05:11:23,624
layout elements. And you can drag them directly
into your design view.
5630
05:11:23,624 --> 05:11:24,624
Once again, this is going to be helpful if
you are using multiple monitors. The other
5631
05:11:24,624 --> 05:11:25,624
thing that you can see in here is this resource
manager, we had this resource manager in Android
5632
05:11:25,624 --> 05:11:26,624
Studio 3.5. But there has been some improvements
in this newest version. Basically, inside
5633
05:11:26,624 --> 05:11:27,624
this resource manager, you can see all of
the static resources inside your project.
5634
05:11:27,624 --> 05:11:28,624
Previously, if you wanted to see different
rival items inside your project, you could
5635
05:11:28,624 --> 05:11:29,624
have go to this project training here. And
inside the resource folder, you could have
5636
05:11:29,624 --> 05:11:30,624
checked your drivers. Now with this resource
manager, you can have access to those drivers
5637
05:11:30,624 --> 05:11:31,624
more easily. Also, beside that you can see
different colors, you can see different layouts
5638
05:11:31,624 --> 05:11:32,624
mipmaps, the strings, and also if you click
on these three dots in here, you can see all
5639
05:11:32,624 --> 05:11:33,624
sorts of resources in your project. Also,
if you want, you can use these resources directly
5640
05:11:33,624 --> 05:11:34,624
from here. For example, right now, in our
layout file, we have this image view, which
5641
05:11:34,624 --> 05:11:35,624
its source is this IC launcher background
which exists in our driver folder. If we want
5642
05:11:35,624 --> 05:11:36,624
to change this to this IC launcher mipmap
file, we can simply drag this IC launcher,
5643
05:11:36,624 --> 05:11:37,624
and we can paste it in here, you can see that
the source of our image view has now changed.
5644
05:11:37,624 --> 05:11:38,624
Similarly, we can do the same thing for the
strings. For example, right now in our string
5645
05:11:38,624 --> 05:11:39,624
resources, we have this app name string, we
can drag that for example, instead of this
5646
05:11:39,624 --> 05:11:40,624
Hello, we can see that the text has been changed.
Let's undo the changes. So from time to time,
5647
05:11:40,624 --> 05:11:41,624
this resource manager can be useful. The other
thing that has changed in this newest version
5648
05:11:41,624 --> 05:11:42,624
of Android Studio is the font of my code.
I actually don't like this font, and I'm going
5649
05:11:42,624 --> 05:11:43,624
to change it to the previous font that we
had in our Android Studio. For that I can
5650
05:11:43,624 --> 05:11:44,624
go to this file inside the settings. You can
search for fonts. And in here, you can change
5651
05:11:44,624 --> 05:11:45,624
the font. For example, I think I'm going to
select this one. Let's press OK. And yes,
5652
05:11:45,624 --> 05:11:46,624
it is the previous font that we had when we
worked with different layouts, right. Also,
5653
05:11:46,624 --> 05:11:47,624
besides Android Studio, I've updated my Gradle
tool as well. If you want to check that you
5654
05:11:47,624 --> 05:11:48,624
can go to your project menu, you can go to
this Gradle script, we have changed two things
5655
05:11:48,624 --> 05:11:49,624
about the Gradle. First of all, if you take
a look at this Gradle dot rapper properties,
5656
05:11:49,624 --> 05:11:50,624
you can see that we have the latest version
of Gradle in here. And also besides that,
5657
05:11:50,624 --> 05:11:51,624
if you go to this build dot Gradle project
module, you can see that we are using the
5658
05:11:51,624 --> 05:11:52,624
latest Gradle tools version. So beside Android
Studio, I've updated my Gradle as well. Probably
5659
05:11:52,624 --> 05:11:53,624
you wouldn't see any difference when you update
your Gradle. In most cases, it's going to
5660
05:11:53,624 --> 05:11:54,624
fix some bugs, and also it may improve some
performance. Once again, my suggestion is
5661
05:11:54,624 --> 05:11:55,624
to keep everything updated when you're working
with Android Studio. Okay, let's move on from
5662
05:11:55,624 --> 05:11:56,624
this part. And let's talk about recycler view
in Android Studio. For that, I'm going to
5663
05:11:56,624 --> 05:11:57,624
close this project. And I'm going to create
a new project. Let's say start a new Android
5664
05:11:57,624 --> 05:11:58,624
Studio project. Now that we have in here under
the studio has a new update in these templates
5665
05:11:58,624 --> 05:11:59,624
as well. For example inside this TV, you can
see that we have other options for different
5666
05:11:59,624 --> 05:12:00,624
operating systems, or for different devices,
some new templates has been added. But we
5667
05:12:00,624 --> 05:12:01,624
are not going to work with them. Like before
we are going to use this empty activity. Let's
5668
05:12:01,624 --> 05:12:02,624
name this project recycler view. Or let's
say RecyclerView example. It's interesting
5669
05:12:02,624 --> 05:12:03,624
that in here, we have this new option in here.
It says use legacy Android dot support that
5670
05:12:03,624 --> 05:12:04,624
libraries. So basically, before Android x,
we had to use support libraries for having
5671
05:12:04,624 --> 05:12:05,624
backward compatibility is in older devices.
As you saw in the previous videos, the use
5672
05:12:05,624 --> 05:12:06,624
of Android X was mandatory. But now in here
we have this new option. I don't think we
5673
05:12:06,624 --> 05:12:07,624
are going to use this legacy support libraries
in our course but it seems like we now have
5674
05:12:07,624 --> 05:12:08,624
this option. Okay, let's create our project.
5675
05:12:08,624 --> 05:12:09,624
And let's see how we can use recycler view.
for that. We have two options. First of all,
5676
05:12:09,624 --> 05:12:10,624
we can go to our activity main dot XML file.
As you can see, we have this recycler view
5677
05:12:10,624 --> 05:12:11,624
option in here but before using it, we need
to download its dependency into our project.
5678
05:12:11,624 --> 05:12:12,624
You can click on this download option in here
and it will be automatically added to our
5679
05:12:12,624 --> 05:12:13,624
gradle file. Or if you don't want to add it
from here you can always search for recycler
5680
05:12:13,624 --> 05:12:14,624
view dependencies and add Manually into a
gradle file. Before I add this recycler view,
5681
05:12:14,624 --> 05:12:15,624
let's quickly open our Gradle scripts build
that Gradle module app. down in here in these
5682
05:12:15,624 --> 05:12:16,624
dependencies, you can see that we don't have
any recycler view. Let's download this recycler
5683
05:12:16,624 --> 05:12:17,624
view by clicking on this. You can see a warning
in here. Would you like to add this now, let's
5684
05:12:17,624 --> 05:12:18,624
say okay, it seems like we have added recycler
view successfully. Let's check out we've got
5685
05:12:18,624 --> 05:12:19,624
gradle file. And then in here, you can see
the recycler view. But as you can see the
5686
05:12:19,624 --> 05:12:20,624
highlighting here, this is not the latest
version. For that reason, it's always better
5687
05:12:20,624 --> 05:12:21,624
to check the internet for the latest RecyclerView
dependencies. The highlighting here says that
5688
05:12:21,624 --> 05:12:22,624
you have version 1.1 point oh available. So
let's change this one to 1.1 point oh, and
5689
05:12:22,624 --> 05:12:23,624
let's sync our project. Okay, now that we
have added this RecyclerView dependency, we
5690
05:12:23,624 --> 05:12:24,624
can add it to our layout file. I'm going to
switch to text view from above in here. And
5691
05:12:24,624 --> 05:12:25,624
I'm going to minimize this resource manager.
First of all, I'm going to change this constraint
5692
05:12:25,624 --> 05:12:26,624
layout to a relative layout. After that's
like before, I'm going to delete this TextView.
5693
05:12:26,624 --> 05:12:27,624
And let's add a recycler view. So you can
see recycler view this option down in here.
5694
05:12:27,624 --> 05:12:28,624
For the width and height. Let's say match
parent, I'm going to match the whole relative
5695
05:12:28,624 --> 05:12:29,624
layout. Let's also give you an idea. Let's
say contacts recycler view. As you can guess
5696
05:12:29,624 --> 05:12:30,624
I'm going to show a list of different contacts
in my recycler view. After that, we can finish
5697
05:12:30,624 --> 05:12:31,624
creating our recycler view. And if you enable
your preview from inside this split view,
5698
05:12:31,624 --> 05:12:32,624
you can see that we have these options inside
this recycler view. I'm also going to add
5699
05:12:32,624 --> 05:12:33,624
some padding to this parent relative layout.
So in here inside the starting tag, let's
5700
05:12:33,624 --> 05:12:34,624
say padding, and let's say 10 dP. That seems
better. After that, we need to initialize
5701
05:12:34,624 --> 05:12:35,624
our recycler view in our Java file. So inside
this main activity dot java file. First of
5702
05:12:35,624 --> 05:12:36,624
all, I'm going to say private recycler view.
This one that comes from Android x dot recycler
5703
05:12:36,624 --> 05:12:37,624
view v jet. Let's name it contacts recycler
view. And down in here inside the onCreate
5704
05:12:37,624 --> 05:12:38,624
method, let's initialize it. Contacts, recycler
view is equal to find view by ID dot contact
5705
05:12:38,624 --> 05:12:39,624
recycler view. When we have worked with ListView,
right after this point, right after initializing
5706
05:12:39,624 --> 05:12:40,624
our ListView, we could have create an array
adapter and after that, we could have passed
5707
05:12:40,624 --> 05:12:41,624
our array adapter to our list view. But when
we are using recycler view, we need to create
5708
05:12:41,624 --> 05:12:42,624
our own adapter. Creating your own recycler
view adapter is probably the most complex
5709
05:12:42,624 --> 05:12:43,624
code that you have written so far. But we
are going to go through each step together.
5710
05:12:43,624 --> 05:12:44,624
Before creating our adapter. We also need
a layout file. If you remember when we have
5711
05:12:44,624 --> 05:12:45,624
used ListView. For every item in our list
view, we have past a built in layout. In recycler
5712
05:12:45,624 --> 05:12:46,624
view, we need to create that layout as well.
So inside my project inside the Resources
5713
05:12:46,624 --> 05:12:47,624
folder inside the layout folder, I'm going
to create a new layout resource file. I'm
5714
05:12:47,624 --> 05:12:48,624
going to name this phone contacts this item.
I'm also going to change this route element
5715
05:12:48,624 --> 05:12:49,624
to a relative layout. Okay, let's create our
layout file. And let's switch to our TextView.
5716
05:12:49,624 --> 05:12:50,624
If you remember from previous videos, I said
that when you are working with RecyclerView,
5717
05:12:50,624 --> 05:12:51,624
you have all sorts of flexibilities. In designing
your items layout files, we will take a look
5718
05:12:51,624 --> 05:12:52,624
at more complex layout files. But in here,
I'm just going to add a simple text view.
5719
05:12:52,624 --> 05:12:53,624
Let's switch to the split view so that we
can see the preview. Okay, let's say TextView.
5720
05:12:53,624 --> 05:12:54,624
Wrap content and wrap content. For the text,
let's say contact Nate. Later on in our recycler
5721
05:12:54,624 --> 05:12:55,624
view, we are going to change this text view.
Beside that let's also give an ID to this
5722
05:12:55,624 --> 05:12:56,624
TextView
5723
05:12:56,624 --> 05:12:57,624
let's say txt name. I'm also going to change
the height of this relative layout to wrap
5724
05:12:57,624 --> 05:12:58,624
content because we don't want to occupy the
whole screen for just one item in our recycler
5725
05:12:58,624 --> 05:12:59,624
view. For that, let's change this font to
wrap content. Let's also add some interest
5726
05:12:59,624 --> 05:13:00,624
in here so that we can see it better. And
also let's add some padding. Let's say padding.
5727
05:13:00,624 --> 05:13:01,624
And that's past 10 Up. Sorry for that. Let's
also give an ID to this relative layout, we
5728
05:13:01,624 --> 05:13:02,624
will see how it can be useful. Let's just
name it parent. So this in here is going to
5729
05:13:02,624 --> 05:13:03,624
be the layout file for every item in our recycler
view. Now that we have created this context,
5730
05:13:03,624 --> 05:13:04,624
this item dot XML file, we can create our
recycler view adapter. For that we need to
5731
05:13:04,624 --> 05:13:05,624
create a Java class in our project. So inside
our package, I'm going to say new Java class.
5732
05:13:05,624 --> 05:13:06,624
Let's name this class contacts, recycler view
adapter. Let's create our class and let's
5733
05:13:06,624 --> 05:13:07,624
see what do we have in here. So first of all,
before everything, I'm going to create an
5734
05:13:07,624 --> 05:13:08,624
inner class inside this class, we will see
what this inner class is. this inner class
5735
05:13:08,624 --> 05:13:09,624
needs to be public, let's say public class.
I'm going to name it view holder because it's
5736
05:13:09,624 --> 05:13:10,624
the convention. This view holder class needs
to extend recycler view dot view holder this
5737
05:13:10,624 --> 05:13:11,624
class in here, but as you can see, we are
getting an error in here. And that's because
5738
05:13:11,624 --> 05:13:12,624
we need to create our constructor. Let's quickly
do that by pressing ALT plus insert. This
5739
05:13:12,624 --> 05:13:13,624
inner view holder class is going to hold the
view for every item in our recycler view.
5740
05:13:13,624 --> 05:13:14,624
For example, in this case, it's going to generate
a new contact list item, in which later on
5741
05:13:14,624 --> 05:13:15,624
we will see how we can use this layout file
and also all of the elements inside that.
5742
05:13:15,624 --> 05:13:16,624
So basically, this inner class is responsible
for generating our view objects. If you want
5743
05:13:16,624 --> 05:13:17,624
to have access to elements inside your view
object, you can add them as the fields of
5744
05:13:17,624 --> 05:13:18,624
this inner class. For example, in here, I
can say private TextView. If you remember,
5745
05:13:18,624 --> 05:13:19,624
we had a TextView in our list item, the ID
was txt name. Let's name it. txt name. And
5746
05:13:19,624 --> 05:13:20,624
down in here inside the constructor, let's
say txt name is equal to find view by ID.
5747
05:13:20,624 --> 05:13:21,624
But because we are not inside an activity,
we cannot use this find view by ID method
5748
05:13:21,624 --> 05:13:22,624
like this. Instead, what we can do is that
we can use this view object that has been
5749
05:13:22,624 --> 05:13:23,624
passed to this constructor. For example, I
can say item view dot find view by ID. You
5750
05:13:23,624 --> 05:13:24,624
can see that we have this method in here.
So the only difference is that inside an activity,
5751
05:13:24,624 --> 05:13:25,624
you can simply say find view by ID. But in
here because we are not inside an activity,
5752
05:13:25,624 --> 05:13:26,624
we can use this view or we can say item view
dot find view by ID. And after that we can
5753
05:13:26,624 --> 05:13:27,624
address our TextView I can see our.id dot
let's say fix the name. If you have other
5754
05:13:27,624 --> 05:13:28,624
elements inside your layer file, you can add
them like this one by one. We will see that
5755
05:13:28,624 --> 05:13:29,624
later on in few minutes. But for now we are
done with this interview holder class. Let's
5756
05:13:29,624 --> 05:13:30,624
minimize this. And now we can extend the RecyclerView
adapter at the declaration of our adapter
5757
05:13:30,624 --> 05:13:31,624
class. The way to do that is like this, we
can say extends recycler view dot adapter,
5758
05:13:31,624 --> 05:13:32,624
we can see that we have this option. Also
take a look at this VH in here It stands for
5759
05:13:32,624 --> 05:13:33,624
view holder. If you import this adapter inside
the diamonds, you need to pass view holder.
5760
05:13:33,624 --> 05:13:34,624
There are also multiple view holders Make
sure to import the one that is inside this
5761
05:13:34,624 --> 05:13:35,624
class this orb that may code that your project
name. You also have one in Android x that
5762
05:13:35,624 --> 05:13:36,624
recycler view, so make sure to don't confuse
them. If you remember when we have used array
5763
05:13:36,624 --> 05:13:37,624
adapter for list views as the data type of
our adapter, we have passed the string. This
5764
05:13:37,624 --> 05:13:38,624
view holder class is exactly like that. And
we are saying that the data type of our adapter
5765
05:13:38,624 --> 05:13:39,624
at the end is this view holder class, the
class that we created inside our adapter.
5766
05:13:39,624 --> 05:13:40,624
Okay, now that we have extended recycler view
adapter, we need to create some methods or
5767
05:13:40,624 --> 05:13:41,624
to be specific we need to implement some methods.
For that I can press Ctrl plus i and as you
5768
05:13:41,624 --> 05:13:42,624
can see, we have three mandatory methods on
create view holder on bind view holder and
5769
05:13:42,624 --> 05:13:43,624
this get item count method. let's implement
all three of them. You're also going to need
5770
05:13:43,624 --> 05:13:44,624
a constructor for this adapter class. Let's
quickly create that by pressing ALT plus insert
5771
05:13:44,624 --> 05:13:45,624
by going to construct it we can create our
constructor. Before explaining these three
5772
05:13:45,624 --> 05:13:46,624
methods in here, I'm going to create a model
in my project. As I said previously, in this
5773
05:13:46,624 --> 05:13:47,624
video, I'm going to show some contacts inside
my recycler view. And I'm going to create
5774
05:13:47,624 --> 05:13:48,624
a model for those contacts. So inside my project,
I'm going to create a new Java class. Let's
5775
05:13:48,624 --> 05:13:49,624
name it contact. For this contact, I'm going
to define some features. First of all, let's
5776
05:13:49,624 --> 05:13:50,624
say private string name. After that, Let's
say private string email. After that, I'm
5777
05:13:50,624 --> 05:13:51,624
going to define a new string, let's say private
string, image URL bvoc, how this one is going
5778
05:13:51,624 --> 05:13:52,624
to be useful. Basically, we are going to show
some images from the internet by using the
5779
05:13:52,624 --> 05:13:53,624
URLs. And after that, we need to create our
constructor. Once again, by pressing ALT plus
5780
05:13:53,624 --> 05:13:54,624
insert, we can do that. Let's also create
some getters and setters. I'm not sure that
5781
05:13:54,624 --> 05:13:55,624
I'm going to use all of them. later on. If
you did not use them, I will delete them.
5782
05:13:55,624 --> 05:13:56,624
Let's also create a two string method this
might be useful as well. Okay, now we have
5783
05:13:56,624 --> 05:13:57,624
our contact model. Let's close this class.
So once again, inside my recycler view, I'm
5784
05:13:57,624 --> 05:13:58,624
going to show a list of different contacts.
Because of that, I need to pass the list of
5785
05:13:58,624 --> 05:13:59,624
different contacts to this adapter class.
For that before everything above in here,
5786
05:13:59,624 --> 05:14:00,624
I'm going to create an ArrayList or to a specific
private ArrayList of different contacts. Let's
5787
05:14:00,624 --> 05:14:01,624
say private ArrayList of contacts, let's name
it contacts is equal to new ArrayList. It's
5788
05:14:01,624 --> 05:14:02,624
very important to initialize your ArrayList
if you don't initialize it, you will definitely
5789
05:14:02,624 --> 05:14:03,624
get a nullpointerexception. Also, because
we have set this context ArrayList as private,
5790
05:14:03,624 --> 05:14:04,624
we also need to create a setter method so
that later on for example, from inside our
5791
05:14:04,624 --> 05:14:05,624
main activity, we can set the initial data
for our contacts for that down in here. After
5792
05:14:05,624 --> 05:14:06,624
the get item count method, I'm going to press
ALT plus insert. And by going to this setup
5793
05:14:06,624 --> 05:14:07,624
menu, we can create a setter for our contacts
are released. Later on from inside our main
5794
05:14:07,624 --> 05:14:08,624
activity, we will pass our data we're using
this set contacts method. But there is a very
5795
05:14:08,624 --> 05:14:09,624
important point in here. Imagine that you
are getting your data for example, the list
5796
05:14:09,624 --> 05:14:10,624
of your contacts from a web server from internet,
the list of different contacts that are coming
5797
05:14:10,624 --> 05:14:11,624
from the internet may change during the time,
so the list of different contacts inside your
5798
05:14:11,624 --> 05:14:12,624
recycler view should be changed as well. If
you want at any time, you can reuse this set
5799
05:14:12,624 --> 05:14:13,624
contacts method in order to change the data
inside your adapter. But in order to refresh
5800
05:14:13,624 --> 05:14:14,624
the data inside your recycler view, you need
to use another method inside the set contacts
5801
05:14:14,624 --> 05:14:15,624
method. That method is called notify data
set changed this one in here. So basically,
5802
05:14:15,624 --> 05:14:16,624
the data set for our adapter in this case
is our contacts ArrayList. After changing
5803
05:14:16,624 --> 05:14:17,624
the list of different contacts at any time,
we are going to notify the adapter that the
5804
05:14:17,624 --> 05:14:18,624
data set has been changed. This way we can
refresh our recycler view with the new data
5805
05:14:18,624 --> 05:14:19,624
that we have received or passed to this adapter
class. Okay, let's move on from this part.
5806
05:14:19,624 --> 05:14:20,624
And let's start working on these three different
methods. The simplest of these methods is
5807
05:14:20,624 --> 05:14:21,624
this get item count, you can guess its purpose,
it's just going to return the count of different
5808
05:14:21,624 --> 05:14:22,624
items in your adapter. In this case, we know
that the data in our adapter is a list of
5809
05:14:22,624 --> 05:14:23,624
contacts. so we can simply return the list
of our contacts, we can say contacts that
5810
05:14:23,624 --> 05:14:24,624
size.
5811
05:14:24,624 --> 05:14:25,624
That's all we need to do inside this get item
count. For some reason RecyclerView needs
5812
05:14:25,624 --> 05:14:26,624
the number of items inside your adapter. And
in here, we can simply return that. Okay,
5813
05:14:26,624 --> 05:14:27,624
I think that's enough for this video. In the
next video at the beginning we will start
5814
05:14:27,624 --> 05:14:28,624
working on these two methods. After that,
we will see that how we can have more complex
5815
05:14:28,624 --> 05:14:29,624
layouts in recycler view. And after that we
will see how we can use that image URL in
5816
05:14:29,624 --> 05:14:30,624
order to show different contact images in
our application. Okay, see you in the next
5817
05:14:30,624 --> 05:14:31,624
video.
5818
05:14:31,624 --> 05:14:32,624
Okay, let's continue our talk about creating
a recycler view adapter. But before that,
5819
05:14:32,624 --> 05:14:33,624
let's quickly have a review of what we have
done so far. So first of all, we have added
5820
05:14:33,624 --> 05:14:34,624
the RecyclerView dependency in our Gradle
script. If you take a look at build dot Gradle
5821
05:14:34,624 --> 05:14:35,624
module app inside these dependencies, you
can see that we have added recycler view.
5822
05:14:35,624 --> 05:14:36,624
After that we have added this recycler view
in our activity main dot XML file. Inside
5823
05:14:36,624 --> 05:14:37,624
this relative layout, we have this recycler
view. After that inside our main activity
5824
05:14:37,624 --> 05:14:38,624
dot java file. We have initialized our recycler
view. After that it was the time to create
5825
05:14:38,624 --> 05:14:39,624
the layout file for every item in our recycler
view. We have done that by creating a new
5826
05:14:39,624 --> 05:14:40,624
layout file called contacts list item in our
layers folder. If we want to take another
5827
05:14:40,624 --> 05:14:41,624
look It's a simple relative layout, which
contains only one TextView. Later on, we will
5828
05:14:41,624 --> 05:14:42,624
change this. After creating the layout file
for every item, it was time to create our
5829
05:14:42,624 --> 05:14:43,624
adapter class. We have done that by adding
a new Java class. In our package, the name
5830
05:14:43,624 --> 05:14:44,624
of that class was contacts recycler view adapter
inside this class. First of all, we have created
5831
05:14:44,624 --> 05:14:45,624
this inner class, we have named it view holder
because it's the convention. After that, we
5832
05:14:45,624 --> 05:14:46,624
have extended the recycler view dot view holder.
We needed a constructor so we have created
5833
05:14:46,624 --> 05:14:47,624
this constructor in here. As I said in the
previous video, this view holder class is
5834
05:14:47,624 --> 05:14:48,624
responsible for holding the view items for
every item in our recycler view. We can also
5835
05:14:48,624 --> 05:14:49,624
instantiate all of our UI elements from inside
our contacts list item dot XML file inside
5836
05:14:49,624 --> 05:14:50,624
this view holder class if you want. After
creating this view holder class this inner
5837
05:14:50,624 --> 05:14:51,624
class in here, we have extended the recycler
view dot adapter class in which as its data
5838
05:14:51,624 --> 05:14:52,624
type, we have passed our inner view holder
class. When we extended this recycler view
5839
05:14:52,624 --> 05:14:53,624
adapter, we needed to implement three mandatory
methods. This oncreateview holder on bind
5840
05:14:53,624 --> 05:14:54,624
view holder and get item count methods. We
are going to talk about these methods in this
5841
05:14:54,624 --> 05:14:55,624
video. But before that, we also created a
model for our contacts. We have added that
5842
05:14:55,624 --> 05:14:56,624
in our package. This contact has a name, an
email, and also an image URL. later on. In
5843
05:14:56,624 --> 05:14:57,624
this video, we will see that how we are going
to show an image based on its image URL. After
5844
05:14:57,624 --> 05:14:58,624
creating our model inside our adapter class,
we have created an array list of different
5845
05:14:58,624 --> 05:14:59,624
contexts in which we have named that contacts.
It was also important to initialize this array
5846
05:14:59,624 --> 05:15:00,624
list I said that we are going to get a nullpointerexception
if we don't initialize our ArrayList. Also
5847
05:15:00,624 --> 05:15:01,624
later on when we create an instance of this
adapter class, we are going to need a constructor.
5848
05:15:01,624 --> 05:15:02,624
So for that we have created this empty constructor.
I believe later on we will change this. Also
5849
05:15:02,624 --> 05:15:03,624
in order to pass the data into this adapter
request. In this case an ArrayList of contacts
5850
05:15:03,624 --> 05:15:04,624
we have used this set contacts method in which
we are going to call it from inside our main
5851
05:15:04,624 --> 05:15:05,624
activity. After passing the data to this adapter,
we have notified the adapter that the data
5852
05:15:05,624 --> 05:15:06,624
set has changed so that we can refresh the
list of items in our recycler view. Also in
5853
05:15:06,624 --> 05:15:07,624
the previous video, we have talked about this
get item count method. Once again, it's going
5854
05:15:07,624 --> 05:15:08,624
to return the number of items in our adapter.
In this case, we have returned our contacts
5855
05:15:08,624 --> 05:15:09,624
array list dot size. Okay, now it's time to
work on these two methods. But before everything,
5856
05:15:09,624 --> 05:15:10,624
let's close all of the extra classes and files.
5857
05:15:10,624 --> 05:15:11,624
And let's start working on this onCreate view
holder method. As you can see, the return
5858
05:15:11,624 --> 05:15:12,624
type of this method is a view holder. It means
that this method is the place to generate
5859
05:15:12,624 --> 05:15:13,624
our view holder to place to instantiate our
view holder class that we have created down
5860
05:15:13,624 --> 05:15:14,624
here. In order to create our view holder class.
First of all, we need to create a view object.
5861
05:15:14,624 --> 05:15:15,624
We can do that by saying view this one in
here. Let's name it view is equal to if we
5862
05:15:15,624 --> 05:15:16,624
were inside an activity, we could have said
get layout inflator. But because we are inside
5863
05:15:16,624 --> 05:15:17,624
a class beside our activity, we can't use
that instead we can say something like this
5864
05:15:17,624 --> 05:15:18,624
we can say layout inflator with capital L
dot from this method in here, you can see
5865
05:15:18,624 --> 05:15:19,624
that this from method is waiting for our context.
You can also guess by the style of this from
5866
05:15:19,624 --> 05:15:20,624
method that this is a static method. In here
we don't have any context. But we can use
5867
05:15:20,624 --> 05:15:21,624
this view group in order to get the context.
Let's get the context from this view group
5868
05:15:21,624 --> 05:15:22,624
and I will talk about it. You can see that
the name of this view group is parent so I
5869
05:15:22,624 --> 05:15:23,624
can say parent dot get context. As you can
see in here we are creating a view object
5870
05:15:23,624 --> 05:15:24,624
and we are using this layout inflator to generate
that view object. The job of this layout inflator
5871
05:15:24,624 --> 05:15:25,624
is that to inflate a layout for some reason
this layout inflator needs a context so we
5872
05:15:25,624 --> 05:15:26,624
can say dot from and inside that method we
can pass our context. The way to inflate our
5873
05:15:26,624 --> 05:15:27,624
layout with the help of this layout inflator
is that to use dot inflate method. So in here
5874
05:15:27,624 --> 05:15:28,624
after this Fromme method, we can say that
inflate and after passing some arguments,
5875
05:15:28,624 --> 05:15:29,624
we can generate our view object. In here,
we need to pass three arguments. First of
5876
05:15:29,624 --> 05:15:30,624
all, we need to pass the address of our layout
file for every item in our recycler view.
5877
05:15:30,624 --> 05:15:31,624
I mean that contacts list item, that XML file
that we just created. So for that I can say
5878
05:15:31,624 --> 05:15:32,624
our dot layout dot, let's say contact list
item. After that, we need to pass our view
5879
05:15:32,624 --> 05:15:33,624
group, in this case, its name is parent, once
again, I will talk about it. And also after
5880
05:15:33,624 --> 05:15:34,624
that, we need to pass a Boolean. I will talk
about that after we talked about to be over.
5881
05:15:34,624 --> 05:15:35,624
Let's fast forward. So in this case, and now
we have our view object. Okay, let's talk
5882
05:15:35,624 --> 05:15:36,624
about these two arguments in here, this view
group and also this Boolean. Basically, the
5883
05:15:36,624 --> 05:15:37,624
view group is the parent of all of the layout
files. For example, it's the parent of relative
5884
05:15:37,624 --> 05:15:38,624
layout, linear layout, and also constraint
layout. This view group can be used to group
5885
05:15:38,624 --> 05:15:39,624
different views inside it. If you want to
make sure of that, for example, you can go
5886
05:15:39,624 --> 05:15:40,624
to your main activity just for testing purposes.
If you save private relative layout, let's
5887
05:15:40,624 --> 05:15:41,624
name with relative layout. from the previous
videos, we know that if we want to check some
5888
05:15:41,624 --> 05:15:42,624
documentation in Android, we can press down
the control key. And by selecting the class
5889
05:15:42,624 --> 05:15:43,624
name, we can have access to the relative layout
documentation and also declaration. In here
5890
05:15:43,624 --> 05:15:44,624
you can see that this relative layout is extending
view group. So once again, view group is the
5891
05:15:44,624 --> 05:15:45,624
parent of all of our layout files. But why
we are passing this view group as one of the
5892
05:15:45,624 --> 05:15:46,624
arguments inside our adapter class. Before
talking about that, let's quickly delete this
5893
05:15:46,624 --> 05:15:47,624
line of code. We have created this just to
read the documentation and declaration. So
5894
05:15:47,624 --> 05:15:48,624
in here inside this inflate method, we are
passing this view group. Basically, this tells
5895
05:15:48,624 --> 05:15:49,624
that Where do you want to attach your view
object, for example, when we create an instance
5896
05:15:49,624 --> 05:15:50,624
of our adapter object from inside our main
activity, because in our main activity layout
5897
05:15:50,624 --> 05:15:51,624
files, we have defined a relative layout,
this view group is going to be a relative
5898
05:15:51,624 --> 05:15:52,624
layout. And this view object in here, our
layout file for every item is going to be
5899
05:15:52,624 --> 05:15:53,624
attached to that relative layout inside our
main activity. That's also the reason why
5900
05:15:53,624 --> 05:15:54,624
this view group has the name of parent in
here. But what is this Boolean in here you
5901
05:15:54,624 --> 05:15:55,624
can see that the name of this Boolean is attached
to root. And also I'm passing false. In here,
5902
05:15:55,624 --> 05:15:56,624
I just said that I want to attach my view
object to its parent. I'm passing false in
5903
05:15:56,624 --> 05:15:57,624
here because if we pass through, it will be
a redundant job, we already passed our view
5904
05:15:57,624 --> 05:15:58,624
group as the parent of our view object. And
if you pass through, it's going to be redundant.
5905
05:15:58,624 --> 05:15:59,624
We have this Boolean because we can also pass
null in here. For example, if you're not sure
5906
05:15:59,624 --> 05:16:00,624
about our parent, and about where our view
is going to be attached, instead of our parents
5907
05:16:00,624 --> 05:16:01,624
view group, we can simply pass now, you can
see that the name of this argument has changed
5908
05:16:01,624 --> 05:16:02,624
to route. So if you passed now, you're not
going to be sure about the place that this
5909
05:16:02,624 --> 05:16:03,624
view is going to be attached to. But of course,
this is an option that you have. Once again,
5910
05:16:03,624 --> 05:16:04,624
I'm going to pass parent. And for the Boolean
I'm going to pass false. So at the end of
5911
05:16:04,624 --> 05:16:05,624
this line of code with the help of this layout,
inflator dot inflate method, we have our view
5912
05:16:05,624 --> 05:16:06,624
object. Now it's the time to create our view
holder object. And after that, return it,
5913
05:16:06,624 --> 05:16:07,624
if you take a look at the constructor of this
class that you have created, you can see that
5914
05:16:07,624 --> 05:16:08,624
this constructor in here is accepting a view.
Now that we have our view object, we can create
5915
05:16:08,624 --> 05:16:09,624
an instance of this view holder class. So
above in here before the return statement,
5916
05:16:09,624 --> 05:16:10,624
I can say, view holder,
5917
05:16:10,624 --> 05:16:11,624
the one that exists in our adapter class,
not the one inside the Android x that recycler
5918
05:16:11,624 --> 05:16:12,624
view, let's say view holder, let's name it
holder, is equal to new view holder. And let's
5919
05:16:12,624 --> 05:16:13,624
first review. After that, let's return our
holder. So once again, the purpose of this
5920
05:16:13,624 --> 05:16:14,624
method in here this oncreateview holder is
to create an instance of our view holder class
5921
05:16:14,624 --> 05:16:15,624
for every item in our recycler view. Later
on this holder will be passed to this unbind
5922
05:16:15,624 --> 05:16:16,624
view holder method as you can see in here,
and we can use it in order to change the properties
5923
05:16:16,624 --> 05:16:17,624
of our UI elements. Let's see how we can do
that. So inside this unbind view holder method,
5924
05:16:17,624 --> 05:16:18,624
which I believe is the most important method
inside every recycler view adapter. We are
5925
05:16:18,624 --> 05:16:19,624
going to use the properties of our view holder
class. For example, right now inside our layout
5926
05:16:19,624 --> 05:16:20,624
file, we have only one TextView. So if I want
to change the value of my TextView I can say
5927
05:16:20,624 --> 05:16:21,624
something like this, I can say holder dot
txt name, dot set text and we can pass our
5928
05:16:21,624 --> 05:16:22,624
text. You can see that in here we are able
to use holder dot txt name if you remember
5929
05:16:22,624 --> 05:16:23,624
Inside our view holder class, we had such
a field. But also this field was private.
5930
05:16:23,624 --> 05:16:24,624
If you remember from the Java session, I said
that whenever you use inner classes from inside
5931
05:16:24,624 --> 05:16:25,624
the parent class, you can have access to all
of the private fields and methods of your
5932
05:16:25,624 --> 05:16:26,624
inner class. In this case, inside this adapter
class, we can use this folder dot txt name,
5933
05:16:26,624 --> 05:16:27,624
even though it's private. Okay, but what do
we pass inside this set text method. As you
5934
05:16:27,624 --> 05:16:28,624
can see, beside this folder, we also are getting
some integer called position. It's exactly
5935
05:16:28,624 --> 05:16:29,624
like the position that we have inside ListView
adapters. This is going to be the position
5936
05:16:29,624 --> 05:16:30,624
of the item in our recycler view. Once again,
we can use this position in order to get the
5937
05:16:30,624 --> 05:16:31,624
data for example from our contacts ArrayList.
For example, inside the set text method, I
5938
05:16:31,624 --> 05:16:32,624
can say contacts, dot, let's say get. And
in here as the index i can pass position.
5939
05:16:32,624 --> 05:16:33,624
Also, during the time, I've seen that the
name of this integer was different. Sometimes
5940
05:16:33,624 --> 05:16:34,624
it was named it sometimes it was position
and also I think I've seen others. But basically,
5941
05:16:34,624 --> 05:16:35,624
this integer is just the position of every
item in your recycler view data set. Okay,
5942
05:16:35,624 --> 05:16:36,624
after getting the corresponding contacts,
we can use dot get name, for example, in order
5943
05:16:36,624 --> 05:16:37,624
to set the name of our contact to this TextView.
This is going to be the simplest recycler
5944
05:16:37,624 --> 05:16:38,624
view that you can create. You can have all
sorts of UI elements inside your list item
5945
05:16:38,624 --> 05:16:39,624
layout file, and after that inside this unbind
view holder method. You can also set different
5946
05:16:39,624 --> 05:16:40,624
onClick listeners for each one of you I elements.
But more on that in few minutes. So by this
5947
05:16:40,624 --> 05:16:41,624
time, our adapter class is ready, we have
done a great job and we have created our recycler
5948
05:16:41,624 --> 05:16:42,624
view adapter class. Now that we have created
our recycler view adapter, it's time to switch
5949
05:16:42,624 --> 05:16:43,624
back to our main activity and pass some data
to this adapter class. So inside this onCreate
5950
05:16:43,624 --> 05:16:44,624
method, we have instantiated a recycler view.
After that, we need to create an ArrayList
5951
05:16:44,624 --> 05:16:45,624
of different contacts. For example, let's
say array list of different contacts. Let's
5952
05:16:45,624 --> 05:16:46,624
name it contacts is equal to new ArrayList.
Once again, I'm passing the data manually
5953
05:16:46,624 --> 05:16:47,624
in here because we don't know anything about
databases and web servers yet. In a real world
5954
05:16:47,624 --> 05:16:48,624
application, these data are probably coming
from a database or a web server. Let's quickly
5955
05:16:48,624 --> 05:16:49,624
add some data to our contacts ArrayList.
5956
05:16:49,624 --> 05:16:50,624
As you can see, I've added some contacts into
my area list. For every contact, I've selected
5957
05:16:50,624 --> 05:16:51,624
a name also an email. And beside that I've
get these image URLs from the internet. There
5958
05:16:51,624 --> 05:16:52,624
is just one point in here. And that's regarding
these image URLs. Sometimes when you're copying
5959
05:16:52,624 --> 05:16:53,624
the address of some image from the internet,
that address may not have an extension, for
5960
05:16:53,624 --> 05:16:54,624
example, like this dot jpg, or dot png. Those
images are not going to be useful in here.
5961
05:16:54,624 --> 05:16:55,624
So make sure to select the images that have
the extension of that JPG or dot png. Just
5962
05:16:55,624 --> 05:16:56,624
because some students asked me before how
to get the address of these images. Let me
5963
05:16:56,624 --> 05:16:57,624
quickly show you one example. For example,
in Google Images, you can search for different
5964
05:16:57,624 --> 05:16:58,624
images. And by right clicking on the image,
you can copy the image address. For example,
5965
05:16:58,624 --> 05:16:59,624
in another tab, you can paste that address
if it's ending with dot JPG or dot png, you're
5966
05:16:59,624 --> 05:17:00,624
good to go. Okay, after creating your contacts,
it's time to create your recycler view adapter.
5967
05:17:00,624 --> 05:17:01,624
For that, I'm going to say contact recycler
view adapter. Let's name it adapter is equal
5968
05:17:01,624 --> 05:17:02,624
to new contacts recycler view adapter. We
didn't need anything for the constructor,
5969
05:17:02,624 --> 05:17:03,624
so you're good to go. After that, we can use
the setter method inside our adapter we can
5970
05:17:03,624 --> 05:17:04,624
say adapter dot set contacts, and we can pass
our ArrayList. After creating our adapter
5971
05:17:04,624 --> 05:17:05,624
instance, and setting the data, we can set
this adapter to our recycler view. So down
5972
05:17:05,624 --> 05:17:06,624
in here, I can say contacts recycler view
dot set adapter. And I can pass my adapter
5973
05:17:06,624 --> 05:17:07,624
in a ListView. That would be the end of everything.
And we could have test our application. But
5974
05:17:07,624 --> 05:17:08,624
in recycler view, there is also one more step.
We also need to set a layout manager for our
5975
05:17:08,624 --> 05:17:09,624
recycler view as well. Let's quickly set that
and we will talk about let's say contacts
5976
05:17:09,624 --> 05:17:10,624
RecyclerView dot set layout manager. In here
we have few options. First of all, we can
5977
05:17:10,624 --> 05:17:11,624
pass a linear layout manager and we can pass
Like this we can say new linear layout manager,
5978
05:17:11,624 --> 05:17:12,624
in which for its constructor, we need the
context. This time because we are inside an
5979
05:17:12,624 --> 05:17:13,624
activity, we can safely pass this as the context.
By setting the layout manager of our recycler
5980
05:17:13,624 --> 05:17:14,624
view to a linear layout manager. We are saying
that display the items in our recycler view
5981
05:17:14,624 --> 05:17:15,624
in a linear fashion. The other option that
we have is grid layout manager, we will take
5982
05:17:15,624 --> 05:17:16,624
a look at that. But in here, this linear layout
manager is by default vertical. If you want
5983
05:17:16,624 --> 05:17:17,624
to change that, for example, if you want to
have a horizontal view, you can do that. We
5984
05:17:17,624 --> 05:17:18,624
will do that in a minute after running our
application. So by this point, our application
5985
05:17:18,624 --> 05:17:19,624
is ready to test. We have created our recycler
view we have instantiated down in here, we
5986
05:17:19,624 --> 05:17:20,624
also have created this dynamic list of contacts.
After that we have instantiated our adapter
5987
05:17:20,624 --> 05:17:21,624
class. We have set the data inside this adapter
class. After that we have set the adapter
5988
05:17:21,624 --> 05:17:22,624
to a recycler view. We also have set a layout
manager for our recycler view. Let's test
5989
05:17:22,624 --> 05:17:23,624
the application. And let's see what does a
recycler view look like. As you can see the
5990
05:17:23,624 --> 05:17:24,624
list of different contacts name are being
shown inside a recycler view in my activity.
5991
05:17:24,624 --> 05:17:25,624
If I had more contacts, I could have scrolled
my recycler view and I could have access to
5992
05:17:25,624 --> 05:17:26,624
all of them. Right now they all can fit on
the screen. So this is not a scroll view.
5993
05:17:26,624 --> 05:17:27,624
Let's quickly change this linear layer to
a horizontal linear layout and see the difference.
5994
05:17:27,624 --> 05:17:28,624
So in order to have a horizontal linear layout,
after this context, you can say linear layout
5995
05:17:28,624 --> 05:17:29,624
manager dot horizontal, this one in here.
This is a constant in which you can use in
5996
05:17:29,624 --> 05:17:30,624
order to have a linear layout manager. Beside
that you also have a Boolean in here. Let's
5997
05:17:30,624 --> 05:17:31,624
pass that I'm going to pass false. As you
can see, the name of this Boolean is reverse
5998
05:17:31,624 --> 05:17:32,624
layout. If you want to reverse the order of
items inside your recycler view you can pass
5999
05:17:32,624 --> 05:17:33,624
through in here. But I don't see any reason
for doing that. So I'm going to pass false.
6000
05:17:33,624 --> 05:17:34,624
Let's run the application once again. And
let's see what a horizontal linear layout
6001
05:17:34,624 --> 05:17:35,624
looks like. You can see that this time right
now we are seeing only one contact on the
6002
05:17:35,624 --> 05:17:36,624
screen. And that's because when we created
the layout for every item in our recycler
6003
05:17:36,624 --> 05:17:37,624
view, we have past match parent as the width
of the item. But if you want to see other
6004
05:17:37,624 --> 05:17:38,624
items, you can scroll the window horizontally,
you can see that we are seeing other contacts.
6005
05:17:38,624 --> 05:17:39,624
So this is what this horizontal linear layout
looks like.
6006
05:17:39,624 --> 05:17:40,624
Okay, let's
6007
05:17:40,624 --> 05:17:41,624
quickly see the other layout manager that
I talked about. And let's move on from this
6008
05:17:41,624 --> 05:17:42,624
one. The name of the other layout manager
was grid layout manager. So I'm going to delete
6009
05:17:42,624 --> 05:17:43,624
all of these. And inside this set layout manager
method, I'm going to say new grid layout manager.
6010
05:17:43,624 --> 05:17:44,624
For the inputs of this constructor, we need
two arguments. First of all, we need the context.
6011
05:17:44,624 --> 05:17:45,624
After that we need the number of columns.
For example, I can pass two in here, these
6012
05:17:45,624 --> 05:17:46,624
two in here is going to divide our screen
horizontally into different parts, or to be
6013
05:17:46,624 --> 05:17:47,624
specific to different columns. And it's going
to show different contacts in those two columns.
6014
05:17:47,624 --> 05:17:48,624
But before I run my application, I'm going
to change one thing in the application in
6015
05:17:48,624 --> 05:17:49,624
the contacts list item that XML file, I'm
going to change the match parent attribute
6016
05:17:49,624 --> 05:17:50,624
of my relative layout to let's say wrap content.
You can see that for this video, we have set
6017
05:17:50,624 --> 05:17:51,624
it to match parent. I'm going to change that
to wrap content. Okay, now let's run the application.
6018
05:17:51,624 --> 05:17:52,624
You can see that the screen has been divided
into two different columns horizontally, and
6019
05:17:52,624 --> 05:17:53,624
we can see the contacts name in two different
columns. So sometimes you may want to use
6020
05:17:53,624 --> 05:17:54,624
this grid layout manager. Okay, now let's
see how we can set an onclicklistener for
6021
05:17:54,624 --> 05:17:55,624
each one of these items in our recycler view.
Right now, if we click on each one of these
6022
05:17:55,624 --> 05:17:56,624
items, they are not going to do anything.
But if we want to set an onclicklistener we
6023
05:17:56,624 --> 05:17:57,624
need to do that inside our contacts recycler
view adapter. Let's quickly open that class.
6024
05:17:57,624 --> 05:17:58,624
It was in our Java folder inside our project
folder. Contacts recycler view adapter. If
6025
05:17:58,624 --> 05:17:59,624
you remember, I said that this unbind view
holder method is the most important method
6026
05:17:59,624 --> 05:18:00,624
in your adapter class. And inside this method,
you can set an onclicklistener for every UI
6027
05:18:00,624 --> 05:18:01,624
element that you want. Also, if you remember
inside this contacts list item, I've defined
6028
05:18:01,624 --> 05:18:02,624
an ID for this whole relative layout. If you
want you can set the onclicklistener on this
6029
05:18:02,624 --> 05:18:03,624
TextView but I think it's better To define
the onclicklistener on the whole relative
6030
05:18:03,624 --> 05:18:04,624
layout, because as you can see, we have this
padding of 10 dP and some places in our relative
6031
05:18:04,624 --> 05:18:05,624
layout are not going to be clickable if we
set the onclicklistener on the TextView. But
6032
05:18:05,624 --> 05:18:06,624
because I'm going to define the onClick listener
on this relative layout, inside my adapter
6033
05:18:06,624 --> 05:18:07,624
class, when I've created this view holder
class, after creating the TextView, I need
6034
05:18:07,624 --> 05:18:08,624
to reference the relative layout as well.
So I can say private relative layout. Let's
6035
05:18:08,624 --> 05:18:09,624
name it parent. We also need to instantiate
it inside the constructor down in here, I
6036
05:18:09,624 --> 05:18:10,624
can say parent is equal to item view dot find
view by ID, once again, let's say r.id dot
6037
05:18:10,624 --> 05:18:11,624
parent. After defining this relative layout,
now you can have access to that inside this
6038
05:18:11,624 --> 05:18:12,624
unbind view holder method. So for example,
in here, I can say holder dot parent, which
6039
05:18:12,624 --> 05:18:13,624
is the name of this relative layout, dot,
let's say set onclicklistener. You can see
6040
05:18:13,624 --> 05:18:14,624
that we have this option. Let's pass our interface
new onclicklistener. And in here, I'm just
6041
05:18:14,624 --> 05:18:15,624
going to show some toast message. Let's say
toast. If you remember when we have used this
6042
05:18:15,624 --> 05:18:16,624
make text method on our toast, we needed a
context. But in here, we do not have access
6043
05:18:16,624 --> 05:18:17,624
to any kind of context. Once again, we will
talk about context later on when we talk about
6044
05:18:17,624 --> 05:18:18,624
activities in Android, both inside this adapter
class. Right now, we don't have any context.
6045
05:18:18,624 --> 05:18:19,624
So we have to pass it inside the constructor.
For example, I'm going to define the context
6046
05:18:19,624 --> 05:18:20,624
as the member variable above in here, let's
say private context. Let's name it context.
6047
05:18:20,624 --> 05:18:21,624
And after that, I'm going to receive that
where this constructor, let's say context.
6048
05:18:21,624 --> 05:18:22,624
Let's name it context. Inside the constructor,
I can say this context is equal to context.
6049
05:18:22,624 --> 05:18:23,624
Whatever now that I have changed this constructor,
if I switch back to main activity, I should
6050
05:18:23,624 --> 05:18:24,624
see some red warning. When we have instantiated
our adapter class, we need to pass our context
6051
05:18:24,624 --> 05:18:25,624
once again, because this time we are inside
this activity, I can simply pass this. So
6052
05:18:25,624 --> 05:18:26,624
this way inside our adapter, now we have a
context. And when we create our toast message,
6053
05:18:26,624 --> 05:18:27,624
we can pass that as the context. For the text,
I'm going to show the name of the contact.
6054
05:18:27,624 --> 05:18:28,624
So I can say contacts the ArrayList that we
had, let's say that get once again, I'm going
6055
05:18:28,624 --> 05:18:29,624
to use this position as the index.
6056
05:18:29,624 --> 05:18:30,624
Let's say that get me. Also, if you have noticed
that as soon as I've used this position inside
6057
05:18:30,624 --> 05:18:31,624
this onClick method, because we are inside
an interface, this final keyword has been
6058
05:18:31,624 --> 05:18:32,624
added to the declaration of this integer.
Let's also add another text in here, let's
6059
05:18:32,624 --> 05:18:33,624
say selected. Let's quickly run the application.
And let's see if we have correctly set the
6060
05:18:33,624 --> 05:18:34,624
onclicklistener on our relative layout. Let's
click on the first one margravine selected
6061
05:18:34,624 --> 05:18:35,624
social arona social romance. It seems like
our onclicklistener is working fine. Okay,
6062
05:18:35,624 --> 05:18:36,624
I think that's a good point to stop the video
in here. I did want to talk about that image
6063
05:18:36,624 --> 05:18:37,624
URL and how to show that in our recycler view.
But it seems like this video is getting a
6064
05:18:37,624 --> 05:18:38,624
little bit long. So I'm going to talk about
that in the next video. Besides that we will
6065
05:18:38,624 --> 05:18:39,624
also talk about using a combination of recycler
view and card view. The combination of these
6066
05:18:39,624 --> 05:18:40,624
two will generate a really beautiful look
for our recycler view. Okay, see you in the
6067
05:18:40,624 --> 05:18:41,624
next video. In the previous video, we have
created a functional recycler view. By functional
6068
05:18:41,624 --> 05:18:42,624
I mean, we have created a skeleton of our
recycler view. But right now, it's very simple.
6069
05:18:42,624 --> 05:18:43,624
If you take a look at your contact list item
that XML file, you can see that every item
6070
05:18:43,624 --> 05:18:44,624
in your recycler view is just a simple text
view. In this video, we are going to work
6071
05:18:44,624 --> 05:18:45,624
around this and we are going to see how much
flexibility we have when we are working with
6072
05:18:45,624 --> 05:18:46,624
recycler views. The first thing that I'm going
to do in here is that to use a cart view instead
6073
05:18:46,624 --> 05:18:47,624
of this simple TextView and its parent which
is relative layout, we are going to show our
6074
05:18:47,624 --> 05:18:48,624
contacts in a card view. For that first of
all, we need to add the card view dependencies
6075
05:18:48,624 --> 05:18:49,624
into our project. In previous videos when
we wanted to use card view in our project,
6076
05:18:49,624 --> 05:18:50,624
we have added the material design library.
And from there we have used the material cart
6077
05:18:50,624 --> 05:18:51,624
view instead of using the material design
library. This time I'm going to use the native
6078
05:18:51,624 --> 05:18:52,624
Android cards Let's see where we can find
that card view the finances. So if you search
6079
05:18:52,624 --> 05:18:53,624
for card view dependencies, you will end up
with a link like this, make sure to put Android
6080
05:18:53,624 --> 05:18:54,624
x in your search because if you search for
Android, you may get some link like this.
6081
05:18:54,624 --> 05:18:55,624
Let's quickly take a look at that. In here
inside this dependencies, you can see that
6082
05:18:55,624 --> 05:18:56,624
you are using comm dot Android dot Support
Library. We are not using support libraries
6083
05:18:56,624 --> 05:18:57,624
in our project right now, if you remember,
when we have created our project, we have
6084
05:18:57,624 --> 05:18:58,624
added the Android x artifacts. If you want
to make sure of that, you can always check
6085
05:18:58,624 --> 05:18:59,624
your gradle file. Let's quickly do that. So
in my project pane inside this Gradle script,
6086
05:18:59,624 --> 05:19:00,624
in this build that Gradle module app, in these
dependencies, you can see that we are always
6087
05:19:00,624 --> 05:19:01,624
using Android x. If for some reason you're
using the support libraries, you can implement
6088
05:19:01,624 --> 05:19:02,624
this support libraries version. But because
we are using Android x, this dependency is
6089
05:19:02,624 --> 05:19:03,624
not going to be helpful for us. So make sure
to include this Android x in your search.
6090
05:19:03,624 --> 05:19:04,624
And the first link from the official Android
developers website is probably our needed
6091
05:19:04,624 --> 05:19:05,624
link. Then in here, you can see the dependencies,
you can see that this time, we are going to
6092
05:19:05,624 --> 05:19:06,624
add the dependency from Android x packages.
Let's copy this line of code. And let's add
6093
05:19:06,624 --> 05:19:07,624
it into our project. So once again, inside
this built up Gradle module app file. Inside
6094
05:19:07,624 --> 05:19:08,624
these dependencies, we are going to add our
dependency. Let's quickly do that in here.
6095
05:19:08,624 --> 05:19:09,624
And let's sync our project. Now that I've
added cardview dependencies into my project,
6096
05:19:09,624 --> 05:19:10,624
I can use it in my layout files, for example,
inside this contacts list item that XML file,
6097
05:19:10,624 --> 05:19:11,624
instead of this relative layout, I'm going
to use card view, you can see that it's coming
6098
05:19:11,624 --> 05:19:12,624
from Android x packages. If you remember in
card views, we had two attributes that can
6099
05:19:12,624 --> 05:19:13,624
be helpful in here. The first one is card
elevation, this one in here, which will give
6100
05:19:13,624 --> 05:19:14,624
some 3d shapes to our card. For example, if
I say five dp, we will get an elevation of
6101
05:19:14,624 --> 05:19:15,624
FTP. The next one is called corner radius.
This one in here, which once again, I'm going
6102
05:19:15,624 --> 05:19:16,624
to save five dB, I will change these numbers
if it was necessary. And also inside this
6103
05:19:16,624 --> 05:19:17,624
card view, instead of this TextView, I'm going
to use a relative layout. The reason for the
6104
05:19:17,624 --> 05:19:18,624
relative layout is that because I'm going
to include multiple UI elements, and I'm going
6105
05:19:18,624 --> 05:19:19,624
to use some of the attributes like layout
below center, horizontal and attributes like
6106
05:19:19,624 --> 05:19:20,624
that. And those attributes are available inside
a relative layout. So for that, first of all,
6107
05:19:20,624 --> 05:19:21,624
in here, I'm going to create a relative layout.
For the width and height, let's say match
6108
05:19:21,624 --> 05:19:22,624
parent. And I think that's enough for our
relative layout. Inside that, first of all,
6109
05:19:22,624 --> 05:19:23,624
I'm going to move this TextView. First of
all, let's center this TextView horizontally,
6110
05:19:23,624 --> 05:19:24,624
let's say center horizontal and let's pass
through. Beside that let's change the style
6111
05:19:24,624 --> 05:19:25,624
of this text. That's a text style. And let's
change it to book. After that, I'm going to
6112
05:19:25,624 --> 05:19:26,624
create another text view and I'm going to
show the contacts email. Let's add that after
6113
05:19:26,624 --> 05:19:27,624
this TextView. Let's change the contact name
to contact email. And let's change the ID
6114
05:19:27,624 --> 05:19:28,624
to txt email. Let's also move this to below
our contact name. Layout below, let's say
6115
05:19:28,624 --> 05:19:29,624
txt name. Let's also change the styling here.
Let's say it's added I think it's better to
6116
05:19:29,624 --> 05:19:30,624
add some margin top margin top, let's say
seven dp. Also I'm going to delete the center
6117
05:19:30,624 --> 05:19:31,624
horizontal attributes. Okay after these two
texts, I'm going to show the contacts image
6118
05:19:31,624 --> 05:19:32,624
for that I'm going to create an image view.
Let's say image view for the width and height
6119
05:19:32,624 --> 05:19:33,624
of this image view because I want to have
a consistent image view I'm going to put the
6120
05:19:33,624 --> 05:19:34,624
numbers manually because my images are coming
from the internet. And I don't know anything
6121
05:19:34,624 --> 05:19:35,624
about their size. For example in here I'm
going to say 550 dP, I think that would be
6122
05:19:35,624 --> 05:19:36,624
fine. For the height as well. Once again 150
dP that's also give an ID to this image view.
6123
05:19:36,624 --> 05:19:37,624
Let's just say image. I'm going to move these
two below my contacts email. Let's say layout
6124
05:19:37,624 --> 05:19:38,624
below. And let's pass the email. Let's add
a margin top, I think 10 dP would be fine.
6125
05:19:38,624 --> 05:19:39,624
I'm also going to center this image via horizontally.
So let's say center horizontal. And let's
6126
05:19:39,624 --> 05:19:40,624
pass through. Let's also add a placeholder,
I'm going to say source. And let's address
6127
05:19:40,624 --> 05:19:41,624
one of our images in our package. I think
this IC launcher background would be fine.
6128
05:19:41,624 --> 05:19:42,624
Okay, I think that's enough for our current
view, I don't think we need anything else,
6129
05:19:42,624 --> 05:19:43,624
let's just have a quick look of what our current
view looks like. I think I need to increase
6130
05:19:43,624 --> 05:19:44,624
this coronary radius. For example, let's say
10. In here, that should be better. Besides
6131
05:19:44,624 --> 05:19:45,624
that, let's increase this card elevation.
You can play around with these numbers until
6132
05:19:45,624 --> 05:19:46,624
you're satisfied. I'm almost done with this
contact list item file, I'm just going to
6133
05:19:46,624 --> 05:19:47,624
reformat my code so that when I publish this
code, you and I have the same base code. For
6134
05:19:47,624 --> 05:19:48,624
that, I'm going to go to this code tab in
here.
6135
05:19:48,624 --> 05:19:49,624
And down in here, I'm going to select this
reformat code, you can see that the code has
6136
05:19:49,624 --> 05:19:50,624
been rearranged. Okay, now that I have changed
this layout file, we need to also apply the
6137
05:19:50,624 --> 05:19:51,624
changes inside our recycler view adapter file
as well. So down in here, when we have created
6138
05:19:51,624 --> 05:19:52,624
our view holder class, we need to address
all of the new elements in our contacts list
6139
05:19:52,624 --> 05:19:53,624
item dot XML file. First of all, we have deleted
this relative layout, and we have replaced
6140
05:19:53,624 --> 05:19:54,624
that with the current view. So in here, instead
of using a relative layout, I'm going to say
6141
05:19:54,624 --> 05:19:55,624
current view. And let's also add our email
text, I'm going to add it in the same line
6142
05:19:55,624 --> 05:19:56,624
with this txt name. Let's say txt email. And
let's initialize it down in here inside this
6143
05:19:56,624 --> 05:19:57,624
constructor. So let's say txt email is equal
to item view dot find view by ID r.id dot
6144
05:19:57,624 --> 05:19:58,624
txt email. We also need to add some quotes
inside this unbind view holder method. For
6145
05:19:58,624 --> 05:19:59,624
example, we are going to show the contacts
email, we need to do that in here. Let's say
6146
05:19:59,624 --> 05:20:00,624
holder dot txt email, dot set text. Let's
pass our contacts dot get which for the index,
6147
05:20:00,624 --> 05:20:01,624
I'm going to pass position, let's say dot
get email. before showing the image view,
6148
05:20:01,624 --> 05:20:02,624
I'm going to run the application. And I'm
going to see that if everything is working
6149
05:20:02,624 --> 05:20:03,624
fine. So let's run the application. You can
instantly see that with this view changes
6150
05:20:03,624 --> 05:20:04,624
how much we have improved our layout. But
right now this layout needs some modification.
6151
05:20:04,624 --> 05:20:05,624
For example, we need to add some margin between
these card views. And also we need to add
6152
05:20:05,624 --> 05:20:06,624
a margin top for this txt name. Let's quickly
apply them. So if I switch back to my contact
6153
05:20:06,624 --> 05:20:07,624
list item dot XML file, for this parent card
view, I'm going to add a margin that's a margin
6154
05:20:07,624 --> 05:20:08,624
top or instead of margin top, I'm just going
to say margin. I think 10 dP would be fine.
6155
05:20:08,624 --> 05:20:09,624
Beside that, let's also add a margin for this
TextView. Let's say margin top and that's
6156
05:20:09,624 --> 05:20:10,624
a seven dp. Let's also add a margin was thought
for this email. So in here for this txt email,
6157
05:20:10,624 --> 05:20:11,624
I'm going to say margin start this one in
here and I'm going to say five dp. Let's run
6158
05:20:11,624 --> 05:20:12,624
the application once again. You can see that
it's much better. Now, you can also see the
6159
05:20:12,624 --> 05:20:13,624
two attributes that we have added for our
current views much better in here. First of
6160
05:20:13,624 --> 05:20:14,624
all, we have added this corner radius, you
can see that in here and also this 3d shape
6161
05:20:14,624 --> 05:20:15,624
with a shadow behind our card view is for
that called elevation attribute. Beside that
6162
05:20:15,624 --> 05:20:16,624
the reason that we are seeing two columns
in here is because of that grid layout manager
6163
05:20:16,624 --> 05:20:17,624
that we have passed to our recycler view.
If you want to have only one column, you can
6164
05:20:17,624 --> 05:20:18,624
change that, for example inside our main activity.
down in here, instead of a grid layout manager,
6165
05:20:18,624 --> 05:20:19,624
we can pass a linear layout manager. So in
here, let's say new linear layout manager.
6166
05:20:19,624 --> 05:20:20,624
And let's pass our context. Before I run this
application, I'm going to make sure that I've
6167
05:20:20,624 --> 05:20:21,624
centered my recycler view. Let's switch to
our main activity layout file. And let's center
6168
05:20:21,624 --> 05:20:22,624
our recycler view. Let's say center horizontal.
And let's fast through. Also I'm going to
6169
05:20:22,624 --> 05:20:23,624
change the width attribute in here to wrap
content. Let's run the application once again.
6170
05:20:23,624 --> 05:20:24,624
Right now you can see that we have only three
contacts in our screen but we can scroll our
6171
05:20:24,624 --> 05:20:25,624
recycler view that's we could do that. You
can see that if our item do not fit on the
6172
05:20:25,624 --> 05:20:26,624
screen. Our recycler view is a scroller but
besides that, let's also check the onclicklistener
6173
05:20:26,624 --> 05:20:27,624
Let's click on this link. cart view. And you
can see that social romance selected, it seems
6174
05:20:27,624 --> 05:20:28,624
to be perfect. Okay, now let's work on this
image view, let's see how we can show our
6175
05:20:28,624 --> 05:20:29,624
images. For that we have multiple options,
the easiest one is to use an external library
6176
05:20:29,624 --> 05:20:30,624
called glide. Let's add that into our project.
And we will talk about it. So if you search
6177
05:20:30,624 --> 05:20:31,624
for glide dependency, the first link from
this GitHub page is the link to that external
6178
05:20:31,624 --> 05:20:32,624
library. Let's see what does it look like.
down in here, you should see the dependencies.
6179
05:20:32,624 --> 05:20:33,624
First of all, we need to add these two into
our repositories. Let's see if we have them
6180
05:20:33,624 --> 05:20:34,624
in our gradle file. Let's open our project
pane in the Gradle scripts inside this build
6181
05:20:34,624 --> 05:20:35,624
that Gradle project spawn in these repositories,
we need to add the Maven repositories. Let's
6182
05:20:35,624 --> 05:20:36,624
quickly add them. I'm going to copy this one
in here. And I'm going to add it inside these
6183
05:20:36,624 --> 05:20:37,624
repositories for all projects.
6184
05:20:37,624 --> 05:20:38,624
Beside that, we need to add these two dependencies
into our project. Inside this build dot Gradle
6185
05:20:38,624 --> 05:20:39,624
design module app, we need to add the dependencies.
Let's sync our project. And while it's sinking,
6186
05:20:39,624 --> 05:20:40,624
let's also talk about the SQLite. So glide
is an image loader library in which has developed
6187
05:20:40,624 --> 05:20:41,624
by Google so you can safely use it in your
projects. glide has a lot of functionalities
6188
05:20:41,624 --> 05:20:42,624
in which one of them is to load images from
the internet. Later on in the course, we will
6189
05:20:42,624 --> 05:20:43,624
see other functionalities as well. But if
you want to know more about why they have
6190
05:20:43,624 --> 05:20:44,624
documentation in here, let's click on this
vehicle lights documentation. You can read
6191
05:20:44,624 --> 05:20:45,624
this page if you want. But we will talk about
glide more later on in the course. Okay, let's
6192
05:20:45,624 --> 05:20:46,624
switch back to our project. And it seems like
we have added the glide library successfully.
6193
05:20:46,624 --> 05:20:47,624
Let's close these two Gradle files. Also,
let's close this activity main dot XML file.
6194
05:20:47,624 --> 05:20:48,624
And let's go to our contacts recycler view
adapter. That's java file. So inside this
6195
05:20:48,624 --> 05:20:49,624
unbind view holder method, I'm going to show
the images of different contacts. For that
6196
05:20:49,624 --> 05:20:50,624
I'm going to use the glide inside this method.
Let's see how we can use glide. In order to
6197
05:20:50,624 --> 05:20:51,624
show images with glide library, you can say
something like this, you can say glide with
6198
05:20:51,624 --> 05:20:52,624
capital G. And after that, you need to pass
your context, you can do that by saying that
6199
05:20:52,624 --> 05:20:53,624
with for the context in here, I'm going to
pass my context. As a reminder, we have get
6200
05:20:53,624 --> 05:20:54,624
the context via the constructor of this class.
In the previous video, I'm talking about this
6201
05:20:54,624 --> 05:20:55,624
one in here. Now that we have our context,
we can use it with this width method. After
6202
05:20:55,624 --> 05:20:56,624
that, I'm going to say that this image is
going to be a bitmap. So let's say that as
6203
05:20:56,624 --> 05:20:57,624
bitmap. After that, we need to pass the source
of our image. In this case, we are going to
6204
05:20:57,624 --> 05:20:58,624
pass the image URL for our contact image,
let's say dot load. And let's pass our context
6205
05:20:58,624 --> 05:20:59,624
image URL for that I'm going to say contacts
that get once again, I'm going to use the
6206
05:20:59,624 --> 05:21:00,624
position as the index. That's a dot get image
URL. After that, we need to pass the image
6207
05:21:00,624 --> 05:21:01,624
view that we want to show our image inside
that I'm going to save that into. And I'm
6208
05:21:01,624 --> 05:21:02,624
going to address my image view, which I can
say holder dot image. But for that, first
6209
05:21:02,624 --> 05:21:03,624
of all, we need to add this image inside this
view holder class. We never did that. So inside
6210
05:21:03,624 --> 05:21:04,624
this view holder class, first of all, I'm
going to say private image view. Let's call
6211
05:21:04,624 --> 05:21:05,624
it image. And let's also instantiate it inside
the constructor. Let's say image is equal
6212
05:21:05,624 --> 05:21:06,624
to item view dot find view by ID r.id dot
image. This is the simplest way of using glide.
6213
05:21:06,624 --> 05:21:07,624
First of all, we need to pass our context.
After that, we need to say that it's going
6214
05:21:07,624 --> 05:21:08,624
to be a bitmap file. After that, we need to
pass the source of our image with this start
6215
05:21:08,624 --> 05:21:09,624
load method. And after that, we need to specify
the image view that we are going to show our
6216
05:21:09,624 --> 05:21:10,624
image inside that this is going to work fine.
But before I run my application, I need to
6217
05:21:10,624 --> 05:21:11,624
do one more thing. We are going to load different
images from the internet. For that we need
6218
05:21:11,624 --> 05:21:12,624
to add a permission into our manifest file
for accessing the internet. So far, we haven't
6219
05:21:12,624 --> 05:21:13,624
talked about requesting permission. Let's
quickly see how we can do that. So in our
6220
05:21:13,624 --> 05:21:14,624
project main inside the app folder, inside
this manifest folder, if we open this Android
6221
05:21:14,624 --> 05:21:15,624
manifest dot XML file, we can add our permission
in here above the application tag. In here,
6222
05:21:15,624 --> 05:21:16,624
I can open a tag and the tag is going to be
users permission, this funding here and we
6223
05:21:16,624 --> 05:21:17,624
need to assess To find the kind of permission,
let's search for internet permission, you
6224
05:21:17,624 --> 05:21:18,624
can see this Android permission that internet.
Let's close our tag. There are much more concepts
6225
05:21:18,624 --> 05:21:19,624
regarding these permissions, we will talk
about them later on in the course, for some
6226
05:21:19,624 --> 05:21:20,624
permissions, you need to create a UI logic
in order to ask the user to grant that permission
6227
05:21:20,624 --> 05:21:21,624
to your application. But this internet permission
is a safe permission. And if you add it to
6228
05:21:21,624 --> 05:21:22,624
your manifest file, the Android system is
going to grant it automatically. That's all
6229
05:21:22,624 --> 05:21:23,624
we need to do in order to request for an internet
permission. Later on, we will talk more about
6230
05:21:23,624 --> 05:21:24,624
permissions. Let's just run our application.
And let's see if we can successfully show
6231
05:21:24,624 --> 05:21:25,624
different images.
6232
05:21:25,624 --> 05:21:26,624
You can see that we are seeing different images
in here. But there was a problem when I ran
6233
05:21:26,624 --> 05:21:27,624
this application after adding the permission
for the first time. The first time I wasn't
6234
05:21:27,624 --> 05:21:28,624
able to see the images. And that's because
we have changed our manifest file. Whenever
6235
05:21:28,624 --> 05:21:29,624
you change your manifest file, you need to
uninstall the application and install it once
6236
05:21:29,624 --> 05:21:30,624
again. So if you're not seeing different images,
make sure to uninstall the application and
6237
05:21:30,624 --> 05:21:31,624
install it once again. After that, you should
see different images. Let's also check the
6238
05:21:31,624 --> 05:21:32,624
others you can see that the images are loading
from the internet after a few seconds. You
6239
05:21:32,624 --> 05:21:33,624
can see that our recycler view our cart views
and also the glide library is working perfect.
6240
05:21:33,624 --> 05:21:34,624
The combination of these three we'll create
a really beautiful layout for when we want
6241
05:21:34,624 --> 05:21:35,624
to show a list of different items. And probably
by now you can see the flexibility and all
6242
05:21:35,624 --> 05:21:36,624
of the functionalities of recycler view, you
can see that we can modify our layout files,
6243
05:21:36,624 --> 05:21:37,624
our list item layout files however we want.
Okay, I think that's enough for this video
6244
05:21:37,624 --> 05:21:38,624
just to see our recycler view better. I'm
going to change the linear layout manager
6245
05:21:38,624 --> 05:21:39,624
once again to a grid layout manager. And after
that, we will finish our video. So let's change
6246
05:21:39,624 --> 05:21:40,624
it back to grid layout manager and let's run
our application once again. You can see that
6247
05:21:40,624 --> 05:21:41,624
our RecyclerView seems perfect. Okay, I think
that's enough for this video. In the next
6248
05:21:41,624 --> 05:21:42,624
video, we are going to see how we can use
different fonts for our text views, for example,
6249
05:21:42,624 --> 05:21:43,624
and external font that we don't have in Android
Studio. Okay, see you in the next video.
6250
05:21:43,624 --> 05:21:44,624
In this video, we are going to talk about
different fonts, how we can apply different
6251
05:21:44,624 --> 05:21:45,624
fonts to our TextView. Whether that font exists
in our project, or whether we are going to
6252
05:21:45,624 --> 05:21:46,624
add it externally. I believe this will be
a short video. First of all, let's see how
6253
05:21:46,624 --> 05:21:47,624
we can add an external font into our project.
Before that, I'm going to go to this layout
6254
05:21:47,624 --> 05:21:48,624
file and give an ID to this TextView. Let's
just name it. txt Hello. In order to add an
6255
05:21:48,624 --> 05:21:49,624
external font in your Resources folder, you
need to create a new directory, you can right
6256
05:21:49,624 --> 05:21:50,624
click on your resource folder. By selecting
new and Resource Directory you can create
6257
05:21:50,624 --> 05:21:51,624
that directory. We have done this when we
have created a menu resource file. In here,
6258
05:21:51,624 --> 05:21:52,624
you just need to define the type of your resource,
you can search for font in here. And that's
6259
05:21:52,624 --> 05:21:53,624
all you need to do. Now you can see that you
have this font folder, and you can copy your
6260
05:21:53,624 --> 05:21:54,624
external fonts into this folder. For example,
I have prepared this font. But before adding
6261
05:21:54,624 --> 05:21:55,624
this font into my project, I need to do some
modifications. For example, I need to change
6262
05:21:55,624 --> 05:21:56,624
the name of this one. In order to add this
font into your project, you need to follow
6263
05:21:56,624 --> 05:21:57,624
some rules. For example, you need to change
all of the upper cases into lower cases. And
6264
05:21:57,624 --> 05:21:58,624
also you shouldn't use hyphens, instead of
that you can use on their lines. So in here,
6265
05:21:58,624 --> 05:21:59,624
I'm going to change the name a little bit.
Let's change this opportunity to learn. After
6266
05:21:59,624 --> 05:22:00,624
you have changed the name, you can simply
copy your font and inside your font folder,
6267
05:22:00,624 --> 05:22:01,624
you can paste it. The name is fine. Let's
just press OK. Now in your font folder, you
6268
05:22:01,624 --> 05:22:02,624
have this Amsterdam, the TTF. If your font
has some preview, you can see that by double
6269
05:22:02,624 --> 05:22:03,624
clicking on your font. Okay, now that we have
added this font, let's see how we can use
6270
05:22:03,624 --> 05:22:04,624
it on our text view. Let's switch to our TextView
mode. And on this TextView we have one attribute
6271
05:22:04,624 --> 05:22:05,624
called font family. This one in here. And
now we can address our font, which is add
6272
05:22:05,624 --> 05:22:06,624
font slash amster. No. Let's use to split
here and let's see if we can see our font
6273
05:22:06,624 --> 05:22:07,624
in the preview. You can see that right now
the font hasn't been rendered, but I believe
6274
05:22:07,624 --> 05:22:08,624
this will be changed when we run our application.
Let's run the application and let's see if
6275
05:22:08,624 --> 05:22:09,624
we can successfully change the font up artix
view, you can see that this beautiful font
6276
05:22:09,624 --> 05:22:10,624
has been applied on our text the beside using
your own fonts in your project, you can also
6277
05:22:10,624 --> 05:22:11,624
use some fonts called Google fonts. Let's
quickly see how we can use them. So for that,
6278
05:22:11,624 --> 05:22:12,624
I'm going to switch to design view and search
for font family attributes in here. But before
6279
05:22:12,624 --> 05:22:13,624
that, we need to click on this text view.
Let's search for font family. You can see
6280
05:22:13,624 --> 05:22:14,624
that we have this attribute, and beside our
font, which is right now at font Amsterdam,
6281
05:22:14,624 --> 05:22:15,624
you have this drop down icon. If you click
on that, you can see that in Android, we have
6282
05:22:15,624 --> 05:22:16,624
some fonts that we can use. But besides all
of these, we have these more fonts option.
6283
05:22:16,624 --> 05:22:17,624
And when we click on that, you can see that
we have at least have a lot of more fonts.
6284
05:22:17,624 --> 05:22:18,624
Also, you can see the source in here it says
Google fonts, these Google fonts are free,
6285
05:22:18,624 --> 05:22:19,624
and you can safely use them in your applications.
You just need to add a license in your application
6286
05:22:19,624 --> 05:22:20,624
somewhere. Okay, let's see what do we have
in this Google fonts. For example, we have
6287
05:22:20,624 --> 05:22:21,624
this calligraphy T or something like that,
that we can use. In here you can see that
6288
05:22:21,624 --> 05:22:22,624
we have two options, create downloadable font
and add font to your project. The first option
6289
05:22:22,624 --> 05:22:23,624
means that you don't want to add the font
to your project. And you want to use the online
6290
05:22:23,624 --> 05:22:24,624
font every time that a user uses your application.
If you are creating an offline application,
6291
05:22:24,624 --> 05:22:25,624
this option in here might not be that useful.
Instead of that, I'm going to use the second
6292
05:22:25,624 --> 05:22:26,624
option which will add the font to my project.
But it will increase the size of the final
6293
05:22:26,624 --> 05:22:27,624
apk file. Also, then in here, you can see
this preview for some fonts, you have a more
6294
05:22:27,624 --> 05:22:28,624
than one preview. And you can select between
them both for this font, we have only this
6295
05:22:28,624 --> 05:22:29,624
regular. Also down in here, you can see the
license that you need to show somewhere in
6296
05:22:29,624 --> 05:22:30,624
your application. For the simple application.
I'm not going to do that because this is not
6297
05:22:30,624 --> 05:22:31,624
going to be a commercial application. Okay,
let's add this font into our project by pressing
6298
05:22:31,624 --> 05:22:32,624
OK, you can see that this calligraphy font
has been added into my font folder. I believe
6299
05:22:32,624 --> 05:22:33,624
this will have a preview. So if I click on
that, you can see a preview of your font.
6300
05:22:33,624 --> 05:22:34,624
Now if I run my application, I should see
this font instead of this Amsterdam font.
6301
05:22:34,624 --> 05:22:35,624
You can see that the font has been applied
successfully. For some fonts, you may have
6302
05:22:35,624 --> 05:22:36,624
different styles for example, regular or normal,
Italia, bold and bold italic. Let's quickly
6303
05:22:36,624 --> 05:22:37,624
see a font that has those styles. And let's
see how we can apply those styles on our text
6304
05:22:37,624 --> 05:22:38,624
views. So once again, I'm going to click on
this drop down menu. And once again, I'm going
6305
05:22:38,624 --> 05:22:39,624
to click on this more fonts. The font that
I'm going to use in here is called lobster
6306
05:22:39,624 --> 05:22:40,624
two. If you click on that, you can see that
in this preview pane, you have four options,
6307
05:22:40,624 --> 05:22:41,624
I'm going to add them one by one into my project.
For the first one, I'm going to name it lobster
6308
05:22:41,624 --> 05:22:42,624
to underline regular. And also I'm going to
add it to my project, I'm not going to make
6309
05:22:42,624 --> 05:22:43,624
a downloadable font. Let's press OK. And let's
do the same for the other three slides.
6310
05:22:43,624 --> 05:22:44,624
You can see that now we have four different
lobster two fonts. If we want to apply them
6311
05:22:44,624 --> 05:22:45,624
on our text views, we can do the same as we
did for the other fonts, which we had only
6312
05:22:45,624 --> 05:22:46,624
one font. For example, inside our text view.
For this font family in here, we can pass
6313
05:22:46,624 --> 05:22:47,624
the fonts manually by specifying the name
of our font, or we can pass this styling job
6314
05:22:47,624 --> 05:22:48,624
to the Android. For example, we can add a
text style in here. And we can specify the
6315
05:22:48,624 --> 05:22:49,624
size which can be it's fairly bold or normal.
Right now if I select text style as bold and
6316
05:22:49,624 --> 05:22:50,624
if I change this lobster to Bold Italic to
just lobster to regular. Let's quickly do
6317
05:22:50,624 --> 05:22:51,624
that. If I run my application, I'm going to
get a bolder style for this specific font.
6318
05:22:51,624 --> 05:22:52,624
Let's quickly run that. You can see that we
have this bold font but this in here is not
6319
05:22:52,624 --> 05:22:53,624
accurate because we have added the style manually.
This actually in here is using our lobster
6320
05:22:53,624 --> 05:22:54,624
to regular font. And if we want to use a specifically
this lobster to bold font, we can create a
6321
05:22:54,624 --> 05:22:55,624
font family for that. Let's quickly see how
we can create a font family. So in my font
6322
05:22:55,624 --> 05:22:56,624
resource folder, I can Right click, and by
pressing new fonts resource file, we can create
6323
05:22:56,624 --> 05:22:57,624
a new font family. For the file name, I'm
going to put the font name, which in this
6324
05:22:57,624 --> 05:22:58,624
case is lobster two. Let's create that. Inside
this font family. I'm going to define four
6325
05:22:58,624 --> 05:22:59,624
fonts, we need three attributes in here. First
of all, we are going to need the font itself.
6326
05:22:59,624 --> 05:23:00,624
For the first one, I'm going to pass the lobster
to regular font. After that, I'm going to
6327
05:23:00,624 --> 05:23:01,624
pass a style for this font, let's say font
style. You can see that in here we have two
6328
05:23:01,624 --> 05:23:02,624
styles Italia, and normal. In here, I'm going
to pass normal. But if you want to pass a
6329
05:23:02,624 --> 05:23:03,624
bolder style, you need to pass that as a font
weight. Let's quickly see that you can see
6330
05:23:03,624 --> 05:23:04,624
that we have this font fate attribute in which
we can pass a number. Basically, for normal
6331
05:23:04,624 --> 05:23:05,624
fonts, these weights should be 400, I will
show a link for where you can find the exact
6332
05:23:05,624 --> 05:23:06,624
font weight that you need. But for now, let's
just pass 400. And let's finish creating our
6333
05:23:06,624 --> 05:23:07,624
font. After that, I'm going to create three
more font elements. And I'm going to pass
6334
05:23:07,624 --> 05:23:08,624
the other three fonts, let's say font. Once
again, we need to address our font. This time,
6335
05:23:08,624 --> 05:23:09,624
let's say lobster to retallick. For this style,
I'm going to say Italian this time. And once
6336
05:23:09,624 --> 05:23:10,624
again, because this is not a bold font, we
are going to pass 400 as the font weight.
6337
05:23:10,624 --> 05:23:11,624
Let's quickly add two more fonts for the bold
styles. You can see that for the normal bold
6338
05:23:11,624 --> 05:23:12,624
font I first normal as the font style. But
for the Italian font I first Italian, the
6339
05:23:12,624 --> 05:23:13,624
only difference between a bold and a regular
size font is in this wait for the regular
6340
05:23:13,624 --> 05:23:14,624
view half past 400 both for the bold ones
we have past 700. Before we go further, let's
6341
05:23:14,624 --> 05:23:15,624
quickly see where we can find the proper font
weight. So if you search for font weight Android
6342
05:23:15,624 --> 05:23:16,624
developer, you can see this font weight from
the developer.android.com. Let's quickly see
6343
05:23:16,624 --> 05:23:17,624
that.
6344
05:23:17,624 --> 05:23:18,624
And in here inside this companion properties,
we can see different weights for different
6345
05:23:18,624 --> 05:23:19,624
fonts. For example, we have past 400 for the
regular fonts, which is the correct number.
6346
05:23:19,624 --> 05:23:20,624
For the bold ones, we also passed 700 I believe
in Android, you won't see much difference.
6347
05:23:20,624 --> 05:23:21,624
If you pass for example 500. When the fonts
are being rendered, the only difference is
6348
05:23:21,624 --> 05:23:22,624
between these 407 100. So if you want you
can check this webpage. Okay, let's switch
6349
05:23:22,624 --> 05:23:23,624
back to Android Studio. Now that we have created
this font family, we can pass it in our activity
6350
05:23:23,624 --> 05:23:24,624
main dot XML file. But before that, you can
see that we have a lot of warnings in here.
6351
05:23:24,624 --> 05:23:25,624
If you hover over one of them, it says that
these attributes are only usable in API level
6352
05:23:25,624 --> 05:23:26,624
26 and higher. So if your application is running
on API level lower than that, this font family
6353
05:23:26,624 --> 05:23:27,624
won't be applied. I've seen that some people
use App namespace instead of this Android.
6354
05:23:27,624 --> 05:23:28,624
For example, you can see up in here, of course,
you need to add the namespace. By pressing
6355
05:23:28,624 --> 05:23:29,624
ALT plus enter, you can see that this new
namespace has been added. I'm not going to
6356
05:23:29,624 --> 05:23:30,624
talk about name spacing here. But basically
it's like importing in XML file. In other
6357
05:23:30,624 --> 05:23:31,624
programming languages, you also have namespaces.
Basically, it will define that where this
6358
05:23:31,624 --> 05:23:32,624
attribute is coming from. So some people are
using this app namespace. They say that if
6359
05:23:32,624 --> 05:23:33,624
you use this app namespace instead of Android,
the font family will be applied in lower APA
6360
05:23:33,624 --> 05:23:34,624
levels as well. But honestly, I didn't see
that difference. So I'm going to change this
6361
05:23:34,624 --> 05:23:35,624
one back to Android. And in my activity, main
dot XML file, I'm going to pass the font family.
6362
05:23:35,624 --> 05:23:36,624
So in here instead of passing lobster to regular
I'm going to pass lobster to this XML file
6363
05:23:36,624 --> 05:23:37,624
that we just created. And now this way we
are passing the styling job to the Android
6364
05:23:37,624 --> 05:23:38,624
system. So even though we are using this lobster
to because we have set a style to bold in
6365
05:23:38,624 --> 05:23:39,624
here, at the end, our font is going to be
rendered to this lobster to bold. Let's run
6366
05:23:39,624 --> 05:23:40,624
our application and let's see if we can see
a bold font. You can see that we are seeing
6367
05:23:40,624 --> 05:23:41,624
the bold font. Once again the difference in
here is that this time we are using exactly
6368
05:23:41,624 --> 05:23:42,624
this lobster to bold. But when we have passed
lobster to regular and we have set the style
6369
05:23:42,624 --> 05:23:43,624
to bold, only the Android Studio increase
the size. Sometimes there might be some differences
6370
05:23:43,624 --> 05:23:44,624
between different font styles. When the designer
created the font, so this way of creating
6371
05:23:44,624 --> 05:23:45,624
a font family is much more accurate. Okay,
just before finishing this video, I'm going
6372
05:23:45,624 --> 05:23:46,624
to show you how you can apply different fonts
in Java. For example, inside this layout file
6373
05:23:46,624 --> 05:23:47,624
in the design view, I'm going to add a button.
And by clicking on this button, I am going
6374
05:23:47,624 --> 05:23:48,624
to change the font of this TextView. Let's
change the ID of this button. First of all,
6375
05:23:48,624 --> 05:23:49,624
to let's say btn, change font. And also let's
add some constraints. And let's change the
6376
05:23:49,624 --> 05:23:50,624
text. I'm just going to say change in our
main activity. First of all, let's initialize
6377
05:23:50,624 --> 05:23:51,624
these two items.
6378
05:23:51,624 --> 05:23:52,624
After that, let's set an onclicklistener.
For our button. Let's say that set onclicklistener
6379
05:23:52,624 --> 05:23:53,624
new onclicklistener. Before this onClick listener,
I'm going to create a typeface. And after
6380
05:23:53,624 --> 05:23:54,624
that, I'm going to change the type face of
this TextView inside this onClick method.
6381
05:23:54,624 --> 05:23:55,624
So above in here, let's say typeface, you
can see that it's coming from Android dot
6382
05:23:55,624 --> 05:23:56,624
graphics package. Let's name it typeface is
equal to and here we are going to get a reference
6383
05:23:56,624 --> 05:23:57,624
to our font in our Resources folder. If I
tried get resources in here, as we can see,
6384
05:23:57,624 --> 05:23:58,624
that gets resources that get font. And if
I pass my font address, for example, if I
6385
05:23:58,624 --> 05:23:59,624
say R dot font dot, let's say Amsterdam, you
can see that we are getting a red warning
6386
05:23:59,624 --> 05:24:00,624
in here. And the warning says that this method
is only available in API level 26. and higher.
6387
05:24:00,624 --> 05:24:01,624
You can overcome this problem by using resource
compat. Instead of this get resources method.
6388
05:24:01,624 --> 05:24:02,624
Let's quickly see that. So in here, I can
say resource compat dot get font. First of
6389
05:24:02,624 --> 05:24:03,624
all, we need the context which I'm going to
pass this. After that we need to address our
6390
05:24:03,624 --> 05:24:04,624
font, let's say our dot font dot Amsterdam.
So instead of get resources, you can use this
6391
05:24:04,624 --> 05:24:05,624
resource compat, which is a good option for
backward compatibility. After you have defined
6392
05:24:05,624 --> 05:24:06,624
your typeface inside the onClick method, you
can say txt. Hello, that's that typeface.
6393
05:24:06,624 --> 05:24:07,624
And you can pass your typeface. As simple
as that you can change the font of your text
6394
05:24:07,624 --> 05:24:08,624
to let's run the application. And let's see
if everything is working fine. Right now the
6395
05:24:08,624 --> 05:24:09,624
font is lobster too. If we click on this button,
we can see that the font is changing to Amsterdam,
6396
05:24:09,624 --> 05:24:10,624
it seems to be perfect. Okay, I think that's
enough for this video. And also for that matter
6397
05:24:10,624 --> 05:24:11,624
for this section of the course. There are
a lot more concepts and tools that you have
6398
05:24:11,624 --> 05:24:12,624
when you're designing your layout files. For
example, you have navigation drawers, you
6399
05:24:12,624 --> 05:24:13,624
have bottom navigation views, you have animations
that you can apply in your application. But
6400
05:24:13,624 --> 05:24:14,624
I'm not going to talk about them in this section
of the course because there are a lot more
6401
05:24:14,624 --> 05:24:15,624
concepts that we don't know anything about
them yet. Later on in the course we will see
6402
05:24:15,624 --> 05:24:16,624
a lot more tools for designing our layout
files. Okay, in the next video, we are going
6403
05:24:16,624 --> 05:24:17,624
to have a quick challenge for this section
of the course so that we make sure we have
6404
05:24:17,624 --> 05:24:18,624
learned everything that we have talked so
far. See you in the next video. As I said
6405
05:24:18,624 --> 05:24:19,624
at the end of the previous video, In this
video, we are going to have a quick challenge.
6406
05:24:19,624 --> 05:24:20,624
The purpose of this challenge is to make sure
that we have learned everything that we have
6407
05:24:20,624 --> 05:24:21,624
talked so far. And here is our challenge,
I want you to create this simple layout file.
6408
05:24:21,624 --> 05:24:22,624
You can think of this layout file as a simple
registration for in which we are receiving
6409
05:24:22,624 --> 05:24:23,624
the user's name, email, password, gender,
and also country. Beside that we have this
6410
05:24:23,624 --> 05:24:24,624
image view in here plus this button which
normally in a normal application, this potential
6411
05:24:24,624 --> 05:24:25,624
navigates to the user's gallery, or maybe
the camera in order to take a photo. But because
6412
05:24:25,624 --> 05:24:26,624
we don't know anything about that yet, this
button and this image view are just for showing
6413
05:24:26,624 --> 05:24:27,624
purposes, these are not going to do anything.
Just if you want, you can show a toast message
6414
05:24:27,624 --> 05:24:28,624
after clicking on this image button. Beside
that we have this License Agreement text which
6415
05:24:28,624 --> 05:24:29,624
down below that you can see this checkbox
if everything works fine. When we click on
6416
05:24:29,624 --> 05:24:30,624
the register, we are going to show a snack
bar indicating that the user has registered
6417
05:24:30,624 --> 05:24:31,624
successfully. But if for example, the user
leaves one of these edit texts empty you're
6418
05:24:31,624 --> 05:24:32,624
going to show a warning. Beside that we are
going to make sure that the user agrees to
6419
05:24:32,624 --> 05:24:33,624
our license agreement. Okay, this is our challenge.
As you can see, this is a simple layout file.
6420
05:24:33,624 --> 05:24:34,624
I'm sure that you can do it by yourself. But
as you can see, we are going to practice a
6421
05:24:34,624 --> 05:24:35,624
lot of things that we have learned in In this
section of the course, we are going to work
6422
05:24:35,624 --> 05:24:36,624
with a lot of UI elements beside that as you
can see, we are implementing material theming.
6423
05:24:36,624 --> 05:24:37,624
Also, we are going to show a snack bars. And
beside that we are going to see how we can
6424
05:24:37,624 --> 05:24:38,624
initialize and work with our UI elements in
the Java file. Feel free to use any kind of
6425
05:24:38,624 --> 05:24:39,624
layout for that you want in order to design
the simple layout file in here I have used
6426
05:24:39,624 --> 05:24:40,624
in constraint layout. But if you want, you
can use linear layout or relative layout.
6427
05:24:40,624 --> 05:24:41,624
Okay, pause the video in here and go solve
the challenge. Whenever you're done, come
6428
05:24:41,624 --> 05:24:42,624
back to see my solution as well. Okay,
6429
05:24:42,624 --> 05:24:43,624
I hope you solve the challenge, let's quickly
see my solution as well. I'm going to start
6430
05:24:43,624 --> 05:24:44,624
by creating a new project. For the name of
this project, I'm going to say UI challenge.
6431
05:24:44,624 --> 05:24:45,624
I'm going to save it API level 19. In this
simple application, it shouldn't matter. Okay,
6432
05:24:45,624 --> 05:24:46,624
let's start working on our layout file. But
before that, I'm going to close all of my
6433
05:24:46,624 --> 05:24:47,624
extra files and panes. As I said, I'm going
to work with constraint layout. But if you
6434
05:24:47,624 --> 05:24:48,624
want, you can work with linear layout or relative
layout. Before everything, let's remove this
6435
05:24:48,624 --> 05:24:49,624
TextView. And let's drag all of our items.
First of all, we need an image view for the
6436
05:24:49,624 --> 05:24:50,624
image profile. For the sample data, I'm going
to select one of the avatars, we also need
6437
05:24:50,624 --> 05:24:51,624
a button. Beside that we are going to need
material design components. We will add that
6438
05:24:51,624 --> 05:24:52,624
in a minute. But before that, let's quickly
drag all of our UI elements, we need four
6439
05:24:52,624 --> 05:24:53,624
edit texts in here, the first two are going
to be in plain text, and the next ones are
6440
05:24:53,624 --> 05:24:54,624
going to be password. The first two are for
the name and email. Let's quickly add two
6441
05:24:54,624 --> 05:24:55,624
passwords in here. The difference between
this password and this plain text edit text
6442
05:24:55,624 --> 05:24:56,624
is that when you type something inside the
password edit text, it won't show the text
6443
05:24:56,624 --> 05:24:57,624
itself, but you will see some dots. Also beside
that we need a TextView for our gender text.
6444
05:24:57,624 --> 05:24:58,624
After that we need a radio group. Let's quickly
add that inside this radio group we need four
6445
05:24:58,624 --> 05:24:59,624
radio buttons. Also, I'm not sure that if
you have seen this way of dragging radio buttons
6446
05:24:59,624 --> 05:25:00,624
in your radio group in the design view. Previously,
we have seen how to add radio buttons inside
6447
05:25:00,624 --> 05:25:01,624
a radio group in the TextView. But here is
how you can do that in the design view. Basically,
6448
05:25:01,624 --> 05:25:02,624
you can use these components three, for example,
I can drag my radio buttons to inside this
6449
05:25:02,624 --> 05:25:03,624
radio group. Let's add two more. Let's also
quickly change the orientation of this radio
6450
05:25:03,624 --> 05:25:04,624
group. Let's search for the orientation in
the attributes. This one in here, I'm going
6451
05:25:04,624 --> 05:25:05,624
to change it to horizontal. Beside that we
need a TextView for the countries. And also
6452
05:25:05,624 --> 05:25:06,624
we need a spinner in here for selecting one
of the countries that search for a spinner.
6453
05:25:06,624 --> 05:25:07,624
Let's quickly change the viets of this spinner
to wrap content instead of match parent, this
6454
05:25:07,624 --> 05:25:08,624
is not match parent in here, it's just some
ntps we are going to change that to wrap wanted.
6455
05:25:08,624 --> 05:25:09,624
Also I think it's better to put it above in
here beside our country's text. And after
6456
05:25:09,624 --> 05:25:10,624
everything. If you remember we had the button.
Let's quickly add that this is going to be
6457
05:25:10,624 --> 05:25:11,624
our button register. Beside that we need a
TextView in here for our license agreement.
6458
05:25:11,624 --> 05:25:12,624
And also we needed a checkbox. Let's search
for checkbox. And let's change the checked
6459
05:25:12,624 --> 05:25:13,624
value of this checkbox to true let's search
for checked. Or you can see that down in here.
6460
05:25:13,624 --> 05:25:14,624
Also, I'm going to change the checked value
of this radio button to checked as well. Also,
6461
05:25:14,624 --> 05:25:15,624
let's quickly change the idea of our UI elements
so that we can use them in our Java file.
6462
05:25:15,624 --> 05:25:16,624
I'm going to fast forward the process of giving
them an ID basically, you can select your
6463
05:25:16,624 --> 05:25:17,624
UI element and you can change the attributes
from this top right pane in here.
6464
05:25:17,624 --> 05:25:18,624
Just a quick review, let's quickly see the
ID of each one of them because later on you're
6465
05:25:18,624 --> 05:25:19,624
going to use them in our Java file. This one
is image profile. This one is btn peak image,
6466
05:25:19,624 --> 05:25:20,624
edit text name edit text email, edit text
password edit text pass repeat. txt gender
6467
05:25:20,624 --> 05:25:21,624
this morning here is our agenda for our radio
group. The three radio buttons are named or
6468
05:25:21,624 --> 05:25:22,624
be male or be female and or the other. This
TextView is called txt country. The screener
6469
05:25:22,624 --> 05:25:23,624
is called a screener country and this button
is btn register. This TextView in here is
6470
05:25:23,624 --> 05:25:24,624
called a txt agreement. And this checkbox
is called agreement check. We are also going
6471
05:25:24,624 --> 05:25:25,624
to need four more text views. If you remember
I said that when we click on this register
6472
05:25:25,624 --> 05:25:26,624
button, we are going to make sure that we
have received some input from the user. And
6473
05:25:26,624 --> 05:25:27,624
if for some reason the user don't enter anything
on our edit text, we are going to show some
6474
05:25:27,624 --> 05:25:28,624
warnings and we are going to use text views
for those warnings. So let's quickly add four
6475
05:25:28,624 --> 05:25:29,624
more text views. I'm going to add Warning
for each one of edit text above the editors.
6476
05:25:29,624 --> 05:25:30,624
Let's quickly give some ID to these. For example,
for the first one, I'm going to say txt var
6477
05:25:30,624 --> 05:25:31,624
name. You can guess
6478
05:25:31,624 --> 05:25:32,624
the rest of them. This one is txt one email.
This one txt one pass. And the last one is
6479
05:25:32,624 --> 05:25:33,624
txt var is repeat. Okay, now that we have
the ID for all of these UI elements, let's
6480
05:25:33,624 --> 05:25:34,624
quickly change the initial values. For example,
for this TextView, I'm not going to change
6481
05:25:34,624 --> 05:25:35,624
anything, but for this edit text, I'm going
to delete the text and I'm going to add a
6482
05:25:35,624 --> 05:25:36,624
hint. Let's search for the hint. That's just
a name. Sometimes like this, you can see some
6483
05:25:36,624 --> 05:25:37,624
options, which can be annoying by pressing
the Escape key on your keyboard, you can get
6484
05:25:37,624 --> 05:25:38,624
rid of that suggestion, let's do the same
thing for these three remaining editors. For
6485
05:25:38,624 --> 05:25:39,624
this TextView, I'm going to change its value
to let's say gender. For the radio buttons,
6486
05:25:39,624 --> 05:25:40,624
let's change the first one to make the next
one female. And the other one, I'm going to
6487
05:25:40,624 --> 05:25:41,624
name either this TextView in here, let's change
it to countries or country. Let's change these
6488
05:25:41,624 --> 05:25:42,624
buttons text to peak image. And this font
to register. Also this checkbox to I agree.
6489
05:25:42,624 --> 05:25:43,624
Beside that, we need to change the value of
this text view. But we are going to do that
6490
05:25:43,624 --> 05:25:44,624
later on when we created our constraints.
I'm not going to do anything about this yet.
6491
05:25:44,624 --> 05:25:45,624
Also, I'm going to change the color of these
four warning text views. Let's quickly do
6492
05:25:45,624 --> 05:25:46,624
that. Let's search for color. And let's change
it to some red color, I think this one would
6493
05:25:46,624 --> 05:25:47,624
be fine. Now let's start constraining our
UI elements. For example, for this image view,
6494
05:25:47,624 --> 05:25:48,624
I'm going to constrain it to the left of my
screen and also to the top of my screen. But
6495
05:25:48,624 --> 05:25:49,624
of course, I need some merging. I will add
that in a minute beside that the right of
6496
05:25:49,624 --> 05:25:50,624
this image view to the left of my button,
and also the right of this button to the right
6497
05:25:50,624 --> 05:25:51,624
of my screen. Beside that I'm going to constrain
this button to the bottom of my image view.
6498
05:25:51,624 --> 05:25:52,624
Similar thing for the top of my image view.
Also, let's add some margin for this image
6499
05:25:52,624 --> 05:25:53,624
view. For example, margin top, let's say 32,
I believe is fine. And let's move it to left.
6500
05:25:53,624 --> 05:25:54,624
But as you can see, when I move this image
view to the left and this button do not move,
6501
05:25:54,624 --> 05:25:55,624
I'm going to delete this right constraint.
And I'm going to drag it from the left of
6502
05:25:55,624 --> 05:25:56,624
this button to the right of my image view.
Now let's constrain this image view to the
6503
05:25:56,624 --> 05:25:57,624
right of our screen. And now we can move it.
It seems better now for these text views.
6504
05:25:57,624 --> 05:25:58,624
First of all, I'm going to add a horizontal
guideline. So let's right click on our layout,
6505
05:25:58,624 --> 05:25:59,624
and let's go to this helpers. And let's add
horizontal vide line. Let's move this guideline
6506
05:25:59,624 --> 05:26:00,624
to somewhere about here. I think it's fine.
Beside that we need a vertical guideline.
6507
05:26:00,624 --> 05:26:01,624
Let's quickly add that helpers add vertical
guideline. And let's move it a little bit.
6508
05:26:01,624 --> 05:26:02,624
Now let's constraint our edit texts. For example,
this one to our guideline, I'm going to constrain
6509
05:26:02,624 --> 05:26:03,624
this TextView to this guideline as well. Also,
I'm going to add a constraint from the top
6510
05:26:03,624 --> 05:26:04,624
of this edit text to the bottom of this TextView.
But as you can see these are too close to
6511
05:26:04,624 --> 05:26:05,624
each other. What you can do is that by pressing
down the CTRL key, you can select both of
6512
05:26:05,624 --> 05:26:06,624
them. And by right clicking, you can use this
right click menu, you can go to this constraint,
6513
05:26:06,624 --> 05:26:07,624
let's say Edit Text name, let's say top to
the bottom of our txt warning name, we can
6514
05:26:07,624 --> 05:26:08,624
see that now we have a constraint. Beside
that I'm going to add a constraint from the
6515
05:26:08,624 --> 05:26:09,624
top of this TextView to our horizontal guideline.
So once again, let's select both of them.
6516
05:26:09,624 --> 05:26:10,624
And let's right click constraint txt warning
name top to bottom of our guideline, I think
6517
05:26:10,624 --> 05:26:11,624
I'm going to remove the margin here you can
see that we have an 80 margin. Let's decrease
6518
05:26:11,624 --> 05:26:12,624
that to zero, I believe is fine. Let's do
the same thing for the rest of edit text and
6519
05:26:12,624 --> 05:26:13,624
text views. For these three remaining text
views, I'm going to add a margin top to the
6520
05:26:13,624 --> 05:26:14,624
top edit text but I'm also going to add a
margin. let's right click constraint txt warning
6521
05:26:14,624 --> 05:26:15,624
email top to the bottom of our edit text name.
Let's see what is the margin here 15 I think
6522
05:26:15,624 --> 05:26:16,624
I'm going to save it 16 Okay, let's do the
same thing for the remaining two edit text
6523
05:26:16,624 --> 05:26:17,624
and text views.
6524
05:26:17,624 --> 05:26:18,624
We can see that everything looks better. Now.
Also, if for any reason your computer is slow,
6525
05:26:18,624 --> 05:26:19,624
you can always go to this icon in here and
you can disable This live rendering, or alternatively,
6526
05:26:19,624 --> 05:26:20,624
you can work with blueprint view, which you
can show from this blueprint option in here.
6527
05:26:20,624 --> 05:26:21,624
Let's quickly see that you can work with this
one as well. But like before, I'm going to
6528
05:26:21,624 --> 05:26:22,624
save it to design view. Okay, let's add a
constraint for this gender text. First of
6529
05:26:22,624 --> 05:26:23,624
all, I'm going to construct it to the top
of this edit text, constraint. txt gender,
6530
05:26:23,624 --> 05:26:24,624
top to, let's say, bottom of our edit text,
let's also add a margin, I think a margin
6531
05:26:24,624 --> 05:26:25,624
24 would be fine, it seems too much, let's
say 60. Let's also add a margin to the left
6532
05:26:25,624 --> 05:26:26,624
of our screen, let's add a margin in here
as for the radio group, I'm going to select
6533
05:26:26,624 --> 05:26:27,624
the radio group from this component tree.
For the left constraint, I'm going to add
6534
05:26:27,624 --> 05:26:28,624
it to this guideline. Also, I'm going to add
a constraint from the top of this radio group
6535
05:26:28,624 --> 05:26:29,624
to the bottom of this gender TextView. Once
again, I'm going to use the right click menu,
6536
05:26:29,624 --> 05:26:30,624
let's say constraint. It seems like I didn't
select the radio group, you can always use
6537
05:26:30,624 --> 05:26:31,624
this component three if you want. Let's select
both of them. let's right click constraint,
6538
05:26:31,624 --> 05:26:32,624
let's say radio group gender, top to bottom
of RT x d gender, that seems to be better,
6539
05:26:32,624 --> 05:26:33,624
let's also add a margin, I think 16 would
be fine. For the country text, I'm going to
6540
05:26:33,624 --> 05:26:34,624
do the same thing. For the left margin, let's
say 16. Let's constraint it to our radio group.
6541
05:26:34,624 --> 05:26:35,624
constraint. txt country talk to the bottom
of our radio for the margin, let's say 16
6542
05:26:35,624 --> 05:26:36,624
months again, for the spinner, I'm going to
constraint it to the right of my country TextView.
6543
05:26:36,624 --> 05:26:37,624
And also its bottom and top to the bottom
on top of this country TextView as well. Of
6544
05:26:37,624 --> 05:26:38,624
course, we need a margin left. Let's quickly
add that. For this register button, I'm going
6545
05:26:38,624 --> 05:26:39,624
to constraint it to the three edges of my
screen. But I need a margin button. Let's
6546
05:26:39,624 --> 05:26:40,624
add 32. For example, I think that's fine.
For this TextView. I'm going to add another
6547
05:26:40,624 --> 05:26:41,624
guideline in here. Let's quickly add that
first add vertical guideline, I'm going to
6548
05:26:41,624 --> 05:26:42,624
move this guideline, let's move it to somewhere
about here. And let's concentrate our TextView
6549
05:26:42,624 --> 05:26:43,624
to this one in here. For the top, I'm going
to constrain it to the vertical guideline.
6550
05:26:43,624 --> 05:26:44,624
Let's also constraint this checkbox. I'm also
going to constraint the top of this checkbox
6551
05:26:44,624 --> 05:26:45,624
to the bottom of our TextView. Let's add another
guideline in here. This one is going to be
6552
05:26:45,624 --> 05:26:46,624
a horizontal guideline. And let's move it
down below this edit text. Now I'm going to
6553
05:26:46,624 --> 05:26:47,624
add a constraint from the bottom of this checkbox
to the to this horizontal guideline, it seems
6554
05:26:47,624 --> 05:26:48,624
like we don't have a proper constraint in
here. For that I'm going to delete this top
6555
05:26:48,624 --> 05:26:49,624
constraint, we can delete the constraint by
selecting the constraint and pressing down
6556
05:26:49,624 --> 05:26:50,624
to delete key. Let's also add a constraint
from the bottom of this TextView to the bottom
6557
05:26:50,624 --> 05:26:51,624
horizontal guideline. Also, I'm going to move
it a little bit above. That seems better.
6558
05:26:51,624 --> 05:26:52,624
Now, let's also change the text of this TextView.
And also let's change the visibility of these
6559
05:26:52,624 --> 05:26:53,624
four warning text to gone. First of all, let's
add the text for this TextView. But you can
6560
05:26:53,624 --> 05:26:54,624
see that as soon as I typed the TextView in
here, the text of our TextView somehow occupies
6561
05:26:54,624 --> 05:26:55,624
other layout files. And also it doesn't fit
on the screen. If you want to fix that you
6562
05:26:55,624 --> 05:26:56,624
can click on this to arrow in here. And you
can change the constraint to a match constraint
6563
05:26:56,624 --> 05:26:57,624
what we also need a margin. Let's quickly
add eight in here. Also for the right. Okay,
6564
05:26:57,624 --> 05:26:58,624
that seems with it. Also, I think it's better
to move this checkbox to above a little bit.
6565
05:26:58,624 --> 05:26:59,624
Let's click on that. And let's move it above.
Okay, that seems better. Let's quickly change
6566
05:26:59,624 --> 05:27:00,624
the visibility of this for one new text views
as well. Let's search for visibility. And
6567
05:27:00,624 --> 05:27:01,624
let's change it to gone. Similarly for the
next three text views. But now that I've done
6568
05:27:01,624 --> 05:27:02,624
that, you can see that we have two small margins,
you can increase that. For example, let's
6569
05:27:02,624 --> 05:27:03,624
say eight in here. Similarly for the next
two editors, sorry for the repetition. Okay,
6570
05:27:03,624 --> 05:27:04,624
that's about the size of our layout file.
We also need to add some entry for our explainer.
6571
05:27:04,624 --> 05:27:05,624
Let's quickly add them in our strings values
in our resources in values folder inside the
6572
05:27:05,624 --> 05:27:06,624
strings. I'm going to add an array string,
6573
05:27:06,624 --> 05:27:07,624
let's say array. Let's name it countries.
And let's add our items. Now in our activity
6574
05:27:07,624 --> 05:27:08,624
main dot XML file, we can pass this as the
entries of our espinar. Let's say For entries,
6575
05:27:08,624 --> 05:27:09,624
and let's address our country's array. Okay,
that seems better. Besides that, for this
6576
05:27:09,624 --> 05:27:10,624
image view, we also need a source. Let's search
for source in here. For some reason we can't
6577
05:27:10,624 --> 05:27:11,624
see source in here, let's switch to text view
or this split view. And let's add it in here.
6578
05:27:11,624 --> 05:27:12,624
Let's say source. And let's pass this mid
map file that we have in our project. Before
6579
05:27:12,624 --> 05:27:13,624
testing the application, let's quickly add
the material design team. For that, I'm going
6580
05:27:13,624 --> 05:27:14,624
to go to material.io. Let's go to this Developer
tab. We have seen all of this before in this
6581
05:27:14,624 --> 05:27:15,624
Android in the documentation getting started.
down in here you can see the dependency. Let's
6582
05:27:15,624 --> 05:27:16,624
add that in our project Gradle script build
dot Gradle module app. down in here inside
6583
05:27:16,624 --> 05:27:17,624
the dependencies. Let's add that we also need
to add a version in here which as you can
6584
05:27:17,624 --> 05:27:18,624
see in the warning, the latest version is
1.1 point oh, let's sync our project. And
6585
05:27:18,624 --> 05:27:19,624
let's go to our styles and change the theme
of our application. I'm going to delete this
6586
05:27:19,624 --> 05:27:20,624
theme in here. And I'm going to use team dot
material components that lights this one.
6587
05:27:20,624 --> 05:27:21,624
We have used these previously in previous
videos, let's switch back to our activity
6588
05:27:21,624 --> 05:27:22,624
main file, you can see that the state of our
buttons has changed. Let's quickly test everything
6589
05:27:22,624 --> 05:27:23,624
I'm going to run the application on pixel
three API 29. It seems like we have done a
6590
05:27:23,624 --> 05:27:24,624
relatively good job, we just need to increase
the size of this image. For that I'm going
6591
05:27:24,624 --> 05:27:25,624
to change the width and height of this image
view. I'm going to add them manually, let's
6592
05:27:25,624 --> 05:27:26,624
say 130 dP, I think that would be fine. Let's
run the application. Once again, if you want,
6593
05:27:26,624 --> 05:27:27,624
you can also decrease the size of this TextView
as well. Yes, that seems to be better. Your
6594
05:27:27,624 --> 05:27:28,624
design might be different than mine. It's
okay. It's probably better than me. I'm not
6595
05:27:28,624 --> 05:27:29,624
a designer, but we just wanted to practice
everything that we have learned. Okay, I think
6596
05:27:29,624 --> 05:27:30,624
that's enough for this video. In the next
video, we are going to switch to our Java
6597
05:27:30,624 --> 05:27:31,624
file and we are going to work on the logic
of our application. See you in the next video.
6598
05:27:31,624 --> 05:27:32,624
In the previous video, we have created this
layout file, which isn't that bad. In this
6599
05:27:32,624 --> 05:27:33,624
video, we are going to work on the Java file
and the logic for this application. So for
6600
05:27:33,624 --> 05:27:34,624
that, let's switch to our main activity dot
java file. And let's close our layout file.
6601
05:27:34,624 --> 05:27:35,624
First of all in here, I'm going to initialize
all of my UI elements. Before that, let's
6602
05:27:35,624 --> 05:27:36,624
quickly add a lock, I believe it will be useful,
let's say private edit text we had for edit
6603
05:27:36,624 --> 05:27:37,624
text, I'm going to fast forward the process
of defining and initializing these UI elements,
6604
05:27:37,624 --> 05:27:38,624
you can see that it's very boring. I'm also
going to define a constraint layout element
6605
05:27:38,624 --> 05:27:39,624
in here, let's say private constraint layout.
I'm going to name it parent, I believe later
6606
05:27:39,624 --> 05:27:40,624
on, we will use this constraint layout to
show a snack bar, I also need to give an ID
6607
05:27:40,624 --> 05:27:41,624
to my constraint layout. So for that, let's
switch to our activity main dot XML file in
6608
05:27:41,624 --> 05:27:42,624
our split view, in the parent constraint layout,
I'm going to give it an ID. Let's say ID,
6609
05:27:42,624 --> 05:27:43,624
I'm going to say parent. Okay, now let's initialize
all of these UI elements. I'm going to do
6610
05:27:43,624 --> 05:27:44,624
that inside another method. Let's name that
method it needs to be used. First of all,
6611
05:27:44,624 --> 05:27:45,624
let's create that method down in here, private
void in interviews. First of all, let's add
6612
05:27:45,624 --> 05:27:46,624
a look. Let's get started. Let's minimize
this project thing. And let's initialize once
6613
05:27:46,624 --> 05:27:47,624
again, I'm going to fast forward the process
of initializing these UI elements. I think
6614
05:27:47,624 --> 05:27:48,624
I have a typo in here. But I have set an ID
for this pretend register. Let's quickly fix
6615
05:27:48,624 --> 05:27:49,624
that in our layout file. If I click on this
register button in here, you can see my type
6616
05:27:49,624 --> 05:27:50,624
of sorry for that, I also need to change it
in here. Okay, now that we have initialized
6617
05:27:50,624 --> 05:27:51,624
all of our views, I'm going to set an onclicklistener.
First of all for my btn peak image, I'm talking
6618
05:27:51,624 --> 05:27:52,624
about this one in here. So after this method,
I'm going to say btn peak image dot set onclicklistener
6619
05:27:52,624 --> 05:27:53,624
new onclicklistener.
6620
05:27:53,624 --> 05:27:54,624
Let's just show a toast message. We haven't
seen how to pick an image for example from
6621
05:27:54,624 --> 05:27:55,624
a gallery, or for that matter to take a picture
using the device's camera for that. We are
6622
05:27:55,624 --> 05:27:56,624
just going to show this first message. After
that let's define an onclicklistener for our
6623
05:27:56,624 --> 05:27:57,624
btn finish or btn register. I believe I named
it set onclicklistener new onclicklistener.
6624
05:27:57,624 --> 05:27:58,624
I'm going to do the job inside another method,
let's say in its register. Let's create that
6625
05:27:58,624 --> 05:27:59,624
method down in here. Let's say private void
we need to register Once again, let's add
6626
05:27:59,624 --> 05:28:00,624
a look. Let's say start in here. First of
all, I'm going to check that if the user has
6627
05:28:00,624 --> 05:28:01,624
entered all of the data for that, I'm going
to say if I'm going to create another method,
6628
05:28:01,624 --> 05:28:02,624
I will create that in a minute. But let's
say if validates data. If that's the case,
6629
05:28:02,624 --> 05:28:03,624
we are going to continue. Let's quickly create
this method down in here. And let's see what
6630
05:28:03,624 --> 05:28:04,624
we are going to do inside this method. Private
the return type is going to be a Boolean,
6631
05:28:04,624 --> 05:28:05,624
let's say private, Boolean, validate data.
Once again, let's add a lot. Let's say if
6632
05:28:05,624 --> 05:28:06,624
edit txt name dot get text.to string if it's
equal to an empty string. First of all, we
6633
05:28:06,624 --> 05:28:07,624
are going to show the warning text. So let's
say txt warning, txt warning name, dot set
6634
05:28:07,624 --> 05:28:08,624
visibility. Let's fast be Zeebo. I believe
you have seen this previously in the course
6635
05:28:08,624 --> 05:28:09,624
after that we are going to return false it
means that we didn't validate the data so
6636
05:28:09,624 --> 05:28:10,624
we shouldn't continue our code. In this method
inside this if statement, let's create three
6637
05:28:10,624 --> 05:28:11,624
more if statement for the other three edit
texts. If edit text, email dot get text.to
6638
05:28:11,624 --> 05:28:12,624
string is equal to an empty string. txt warning
email, dot set visibility, once again, is
6639
05:28:12,624 --> 05:28:13,624
also going to return false in here as well.
Also, in each one of these cases, before returning
6640
05:28:13,624 --> 05:28:14,624
false, we are going to change the text of
our txt warning. Because if you remember,
6641
05:28:14,624 --> 05:28:15,624
in the layout file, we didn't change the text
of that burning text view. So I can say txt
6642
05:28:15,624 --> 05:28:16,624
warning name, for example, dot txt, let's
just say enter your name. Similarly for the
6643
05:28:16,624 --> 05:28:17,624
other three cases. And after all of these
if statements after all of these cases, if
6644
05:28:17,624 --> 05:28:18,624
we reach to this point we are going to return
true, it means that the user has entered all
6645
05:28:18,624 --> 05:28:19,624
of the data. So inside this validate data
methods, we are validating that the user filled
6646
05:28:19,624 --> 05:28:20,624
all of the blanks after that inside this init
register method inside this if statement,
6647
05:28:20,624 --> 05:28:21,624
we are going to check that if the user agrees
to our license agreement. So let's say if
6648
05:28:21,624 --> 05:28:22,624
agreement check that is checked this option
in here. If that's the case, we are going
6649
05:28:22,624 --> 05:28:23,624
to continue. But in the else case, we are
just going to show a toast message. Let's
6650
05:28:23,624 --> 05:28:24,624
say you need to agree to the license agreement.
But if the user reaches to this point where
6651
05:28:24,624 --> 05:28:25,624
the user enters all of the data and also agree
to the license agreement, we are going to
6652
05:28:25,624 --> 05:28:26,624
show a snack bar. Let's do that in another
method, let's say show a snack bar. Let's
6653
05:28:26,624 --> 05:28:27,624
create that method down here. Private void
show snack bar. Once again, add a log. And
6654
05:28:27,624 --> 05:28:28,624
then in here inside this method, first of
all, we need to change the visibility of all
6655
05:28:28,624 --> 05:28:29,624
of our warning texts, because those warnings
are no longer valid. So let's say txt warning
6656
05:28:29,624 --> 05:28:30,624
name that says visibility, let's say gone
This time, let's do the same thing for the
6657
05:28:30,624 --> 05:28:31,624
other three remaining burning texts. After
that, we need to show our snack bar. If you
6658
05:28:31,624 --> 05:28:32,624
remember, we can do something like this, we
can say snack bar that make First of all,
6659
05:28:32,624 --> 05:28:33,624
we need to pass our constraint layout, which
is our parent in this case, after that we
6660
05:28:33,624 --> 05:28:34,624
need to text in here right now I'm going to
show a dummy text. For example, I'm going
6661
05:28:34,624 --> 05:28:35,624
to say user registered. But later on, I will
change this text for example, I will show
6662
05:28:35,624 --> 05:28:36,624
the details of the newly registered user.
For now let's say user registered. After that
6663
05:28:36,624 --> 05:28:37,624
we need the constant for the length of our
snack bar, I'm going to say snack bar dot
6664
05:28:37,624 --> 05:28:38,624
length indefinite. Let's also define an action
button for our snack bar. I'm going to say
6665
05:28:38,624 --> 05:28:39,624
that's an action. For the text, I'm just going
to say dismissed.
6666
05:28:39,624 --> 05:28:40,624
Let's also create an onclicklistener, let's
say new onclicklistener. For the time being
6667
05:28:40,624 --> 05:28:41,624
I'm not going to do inside this onClick method.
Later on, we will clear our edit text. Let's
6668
05:28:41,624 --> 05:28:42,624
finish creating our snack bar. But before
that, we just need to show it. Let's say dot
6669
05:28:42,624 --> 05:28:43,624
show. Let's run the application. And let's
see if we have right everything correctly.
6670
05:28:43,624 --> 05:28:44,624
First of all, let's click on the speak Image
button, we should see a toast message. You
6671
05:28:44,624 --> 05:28:45,624
can see that you have to be talked about.
Let's enter some data. And let's register
6672
05:28:45,624 --> 05:28:46,624
a new user. Let's say Mesa, let's add an email.
Let's enter a password. Let's re type our
6673
05:28:46,624 --> 05:28:47,624
passport. We also need to check that if these
two are the same. I forgot to do that. I will
6674
05:28:47,624 --> 05:28:48,624
do that after the testing of this phase of
the application before registering the user.
6675
05:28:48,624 --> 05:28:49,624
I'm going to uncheck this alone. License Agreement
checkbox, let's say register, you can see
6676
05:28:49,624 --> 05:28:50,624
that you need to agree to the license agreement.
Let's check it once again. And let's register
6677
05:28:50,624 --> 05:28:51,624
this time we should see this network user
register, it seems like our application is
6678
05:28:51,624 --> 05:28:52,624
working fine. If we press dismiss, the snack
bar will be dismissed. Also, let's delete
6679
05:28:52,624 --> 05:28:53,624
this email in here. And let's see if we get
the warning text. If we click on this register
6680
05:28:53,624 --> 05:28:54,624
button, you can see that insert your email,
you're successfully validating our data. If
6681
05:28:54,624 --> 05:28:55,624
we enter an email in here, for example, may
sound@gmail.com. And if we click on the Register
6682
05:28:55,624 --> 05:28:56,624
button, we should see that the email warning
text disappears. Okay, now I'm going to change
6683
05:28:56,624 --> 05:28:57,624
the application. First of all, I'm going to
check that these two are the same. After that,
6684
05:28:57,624 --> 05:28:58,624
I'm also going to empty all of these for any
text. When I click on this dismiss button
6685
05:28:58,624 --> 05:28:59,624
so that we can add a new user. Also, I'm going
to change the text of this snack bar to the
6686
05:28:59,624 --> 05:29:00,624
details of our newly registered user. Let's
quickly do that. First of all, inside this
6687
05:29:00,624 --> 05:29:01,624
validate data method, I'm going to add another
use case, let's say if edit text passport,
6688
05:29:01,624 --> 05:29:02,624
this font dot get text to string. If that's
equal to our edit text, password, repeat that
6689
05:29:02,624 --> 05:29:03,624
get text to string. If that's the case, or
let's change our logic, let's say if that's
6690
05:29:03,624 --> 05:29:04,624
not the case. First of all, we are going to
change the visibility of this txt warning
6691
05:29:04,624 --> 05:29:05,624
password repeat. And after that, we are going
to return false let's say p x the warning
6692
05:29:05,624 --> 05:29:06,624
password repeat dot set visibility, let's
say visible after that, I'm going to change
6693
05:29:06,624 --> 05:29:07,624
the text I'm going to say txt warning, repeat
that set text. Let's say password doesn't
6694
05:29:07,624 --> 05:29:08,624
match. Up. After that I'm going to return
false. Okay, now that we have validated this
6695
05:29:08,624 --> 05:29:09,624
data successfully, let's go to this onClick
method. And in here, let's clear our edit
6696
05:29:09,624 --> 05:29:10,624
text. I'm going to say Edit Text email or
any text name, that's a text. Let's just pass
6697
05:29:10,624 --> 05:29:11,624
an empty string. Similarly for the other three
edit text. Next thing that I'm going to do
6698
05:29:11,624 --> 05:29:12,624
is that I'm going to change the text of this
snack bar. First of all, I'm going to create
6699
05:29:12,624 --> 05:29:13,624
that text above in here, inside this short
snack bar method. Before everything I'm going
6700
05:29:13,624 --> 05:29:14,624
to get the user's data. For example, let's
say the string name is equal to edit txt name,
6701
05:29:14,624 --> 05:29:15,624
dot get text.to string. Similarly, for the
email, I'm going to say a string email is
6702
05:29:15,624 --> 05:29:16,624
equal to edit text email dot get text.to string,
I'm also going to get users gender and also
6703
05:29:16,624 --> 05:29:17,624
country, let's say a string country is equal
to countries a spinner that get selected item.to
6704
05:29:17,624 --> 05:29:18,624
string for getting the user's gender, I'm
going to create a switch statement before
6705
05:29:18,624 --> 05:29:19,624
that I'm going to create a string, let's say
string gender is equal to an empty string.
6706
05:29:19,624 --> 05:29:20,624
After that, let's create a switch statement
on our radio group, or ad group gender that
6707
05:29:20,624 --> 05:29:21,624
get checked radio button ID. Let's say in
case it's our.id dot radio button male, we
6708
05:29:21,624 --> 05:29:22,624
are going to change the value of this gender
string, let's say gender is equal to make
6709
05:29:22,624 --> 05:29:23,624
Sorry about that. Let's also add a break in
here. Another case our.id dot female. Let's
6710
05:29:23,624 --> 05:29:24,624
say gender is equal to female. This default
case should never happen. But just for security
6711
05:29:24,624 --> 05:29:25,624
purposes, if something goes wrong, I'm assigning
unknown to the gender a string. So after the
6712
05:29:25,624 --> 05:29:26,624
switch statement, we have all of the data
about our user. Now we can create the text
6713
05:29:26,624 --> 05:29:27,624
that we want to show inside the snack bar.
Let's say let's drink a snack text is equal
6714
05:29:27,624 --> 05:29:28,624
to
6715
05:29:28,624 --> 05:29:29,624
and let's create our text. First of all, I'm
going to say name, plus the name that we created,
6716
05:29:29,624 --> 05:29:30,624
or we got from the Edit Text name. After that,
I'm going to add a backslash n in order to
6717
05:29:30,624 --> 05:29:31,624
go to the next line, let's say email, plus
the email. Once again and backslash n. Let's
6718
05:29:31,624 --> 05:29:32,624
also show the gender and country, let's say
gender, plus the gender, once again, backslash
6719
05:29:32,624 --> 05:29:33,624
n and also let's show the country. Now that
we have created this text, we can show it
6720
05:29:33,624 --> 05:29:34,624
in our snack bar. So this time, instead of
this text, I'm going to save the snack text.
6721
05:29:34,624 --> 05:29:35,624
Let's run the application once again. And
let's see if we have fixed our application
6722
05:29:35,624 --> 05:29:36,624
successfully. Let's test the different cases.
First of all, let's check that if we see the
6723
05:29:36,624 --> 05:29:37,624
warning if we enter different passwords in
these to edit text. In the password field,
6724
05:29:37,624 --> 05:29:38,624
I'm going to enter 123456 but in the reenter
password edit text, I'm going to say 12345.
6725
05:29:38,624 --> 05:29:39,624
If we click on this register button, we should
see the warning text password doesn't match.
6726
05:29:39,624 --> 05:29:40,624
Okay, let's add six in here. Now they do match,
let's change the country to, for example,
6727
05:29:40,624 --> 05:29:41,624
Switzerland. And also let's change the gender
in here to female. And let's see if we can
6728
05:29:41,624 --> 05:29:42,624
reduce there any user. In here you can see
that we are seeing the snack text that we
6729
05:29:42,624 --> 05:29:43,624
have created. But we are not seeing all of
it, we are seeing only the first two lines.
6730
05:29:43,624 --> 05:29:44,624
If you want to change the maximum line attribute
of your snack bar, you can do that. First
6731
05:29:44,624 --> 05:29:45,624
of all, you need to get the instance of your
snack bar. After that, you need to get the
6732
05:29:45,624 --> 05:29:46,624
text view of your snack bar. And after that,
you can change the max line attribute on your
6733
05:29:46,624 --> 05:29:47,624
text view. This way, you can override the
maximum line attributes of the text view of
6734
05:29:47,624 --> 05:29:48,624
your snack bar. But that seems like a lot
of work. And we are not going to do that in
6735
05:29:48,624 --> 05:29:49,624
here. We will do that later on in the course
in other places when we use a snack bar. But
6736
05:29:49,624 --> 05:29:50,624
in here, if you want to make sure that you
have created your snack tics successfully,
6737
05:29:50,624 --> 05:29:51,624
you can simply log it. For example, in here,
I'm going to add a look. Let's say log D,
6738
05:29:51,624 --> 05:29:52,624
let's say a snack bar text. Let's pass our
snack text. But before I run my application
6739
05:29:52,624 --> 05:29:53,624
and test this, I'm going to click on this
dismiss button. If you remember, we write
6740
05:29:53,624 --> 05:29:54,624
the logic so that we clear all of our edit
text. Let's click that. And you can see we
6741
05:29:54,624 --> 05:29:55,624
are now ready to register a new user. Let's
run the application once again. And let's
6742
05:29:55,624 --> 05:29:56,624
test this snack text. For that I'm going to
open the logcat. Let's enter some data in
6743
05:29:56,624 --> 05:29:57,624
here. And let's click on this register. You
can see the snack text in here name may Some
6744
05:29:57,624 --> 05:29:58,624
may be some@gmail.com gender male country,
Norway, it seems like we have created our
6745
05:29:58,624 --> 05:29:59,624
text successfully. Okay, I think that's enough
for this video, I hope that you have solved
6746
05:29:59,624 --> 05:30:00,624
the challenge. In the next section of the
course we are going to create the first of
6747
05:30:00,624 --> 05:30:01,624
four of our applications. Now we know about
Java. Now we know about user interface. And
6748
05:30:01,624 --> 05:30:02,624
in the next section of the course we are going
to combine these two and we are going to create
6749
05:30:02,624 --> 05:30:03,624
our first real world application. Beside practicing
everything that we have learned so far, we
6750
05:30:03,624 --> 05:30:04,624
are also going to learn a lot of new stuff
as well. So stay tuned for the next section
6751
05:30:04,624 --> 05:30:05,624
of the course. And as always see you in the
next video. What's up everyone, I hope you're
6752
05:30:05,624 --> 05:30:06,624
having fun. Before we start creating our sample
application, I would like to remind you that
6753
05:30:06,624 --> 05:30:07,624
there is an extended version of this course
that you can watch. by enrolling in the extended
6754
05:30:07,624 --> 05:30:08,624
course you will have lifetime access to more
than 60 hours of videos, you can ask your
6755
05:30:08,624 --> 05:30:09,624
questions directly from me and I will come
back to you within hours, you will have access
6756
05:30:09,624 --> 05:30:10,624
to all of the source codes that I wrote in
the videos. And also you can have access to
6757
05:30:10,624 --> 05:30:11,624
all of the new videos that I upload, just
check out my code.org for more details about
6758
05:30:11,624 --> 05:30:12,624
the course. And don't forget to use Free Code
Camp as the coupon code to get 20% discount.
6759
05:30:12,624 --> 05:30:13,624
If you want to get serious in Android app
development. Taking the extended course is
6760
05:30:13,624 --> 05:30:14,624
almost a necessity. Okay, everyone, I just
wanted to remind you that there is an extended
6761
05:30:14,624 --> 05:30:15,624
version of this course that you can take without
further ado, let's start creating our sample
6762
05:30:15,624 --> 05:30:16,624
application. Have fun learning.
6763
05:30:16,624 --> 05:30:17,624
Hello, everyone, and welcome back. In this
section of the course we are going to create
6764
05:30:17,624 --> 05:30:18,624
the first application of the for real world
application that I promised. It's important
6765
05:30:18,624 --> 05:30:19,624
to say that in creating this application beside
practicing everything that we have talked
6766
05:30:19,624 --> 05:30:20,624
so far, we are also going to learn a lot of
new stuff. You will see them in action when
6767
05:30:20,624 --> 05:30:21,624
we create the application. Okay, now let's
talk about application itself, we are going
6768
05:30:21,624 --> 05:30:22,624
to create an application that will help the
user to manage his or her library. In this
6769
05:30:22,624 --> 05:30:23,624
application, we are going to show a list of
different books to the user. And the user
6770
05:30:23,624 --> 05:30:24,624
can see the details of that book. later on.
If the user wants he or she can add that book
6771
05:30:24,624 --> 05:30:25,624
to different lists, for example, at least
that indicates that the user has already read
6772
05:30:25,624 --> 05:30:26,624
this book, or maybe a wish list or also a
favorites list. We will see all of them later
6773
05:30:26,624 --> 05:30:27,624
on in the course. In the process of creating
this application, we are going to practice
6774
05:30:27,624 --> 05:30:28,624
all of our knowledge about user interface
and Java. And beside that we are going to
6775
05:30:28,624 --> 05:30:29,624
learn about a lot of new concepts. After watching
this section of the course you can change
6776
05:30:29,624 --> 05:30:30,624
the application that we create together and
you can even publish it in the Play Store.
6777
05:30:30,624 --> 05:30:31,624
So I highly suggest that you watch this section
of the course and don't skip it. Okay, without
6778
05:30:31,624 --> 05:30:32,624
more talking, let's create our project and
let's start writing our application. I'm going
6779
05:30:32,624 --> 05:30:33,624
to start with empty activity as before. Let's
change the name of this application to my
6780
05:30:33,624 --> 05:30:34,624
library. For the other fields in here, I'm
not going to change anything For the package
6781
05:30:34,624 --> 05:30:35,624
name, once again, I'm going to put my website's
name in backward, or dot maker dot project
6782
05:30:35,624 --> 05:30:36,624
name, which is my library to save location
is fine languages Java, and we are going to
6783
05:30:36,624 --> 05:30:37,624
save with API level 19. For the minimum SDK,
we are also not using the support library,
6784
05:30:37,624 --> 05:30:38,624
so I'm not going to check this option in here.
Let's create our project. And let's start
6785
05:30:38,624 --> 05:30:39,624
working on our layout file. Before everything,
I'm going to add material design library dependencies
6786
05:30:39,624 --> 05:30:40,624
into my project, because we are going to need
them for that, let's go to material.io. Let's
6787
05:30:40,624 --> 05:30:41,624
go to this Developer tab. Let's select Android
from here, documentation getting started.
6788
05:30:41,624 --> 05:30:42,624
Let's copy this line of code. We have done
this previously. So I'm not going to export
6789
05:30:42,624 --> 05:30:43,624
anything here in our Gradle scripts, build
that Gradle module app into dependencies.
6790
05:30:43,624 --> 05:30:44,624
Let's add that. And also the latest version
is 1.1. Point. Oh, so let's change this one.
6791
05:30:44,624 --> 05:30:45,624
And let's sync our project. It seems like
we have added the material components successfully.
6792
05:30:45,624 --> 05:30:46,624
First of all, let's change the style of our
application in values as those that XML instead
6793
05:30:46,624 --> 05:30:47,624
of this app compat theme, I'm going to use
theme that material components that light.
6794
05:30:47,624 --> 05:30:48,624
Let's close this. And let's switch to our
activity main dot XML file. Our application
6795
05:30:48,624 --> 05:30:49,624
is going to have multiple pages later on,
we will see how we can create multiple pages
6796
05:30:49,624 --> 05:30:50,624
or to be specific multiple activities. But
for now, this is going to be the first page
6797
05:30:50,624 --> 05:30:51,624
or the first activity of our application.
And let's quickly design that. First of all,
6798
05:30:51,624 --> 05:30:52,624
I'm going to have a text view for the name
of this application. After that, I'm going
6799
05:30:52,624 --> 05:30:53,624
to add a few buttons. One for the list of
all books, I'm going to show the list of all
6800
05:30:53,624 --> 05:30:54,624
of books by clicking on this button. I'm going
to add five more buttons and I will talk about
6801
05:30:54,624 --> 05:30:55,624
them. Let's also add an image view above in
here for our logo. For now I'm going to stay
6802
05:30:55,624 --> 05:30:56,624
with one of our avatars. And also I'm going
to add a text view in here for licensing.
6803
05:30:56,624 --> 05:30:57,624
Let's give some ideas to our elements. For
this one, I'm going to name it IMG logo, because
6804
05:30:57,624 --> 05:30:58,624
it's going to be our logo. For the TextView
I'm going to say txt name, but I'm not sure
6805
05:30:58,624 --> 05:30:59,624
that we need an ID for this one. This button
in here is going to show a list of all of
6806
05:30:59,624 --> 05:31:00,624
our books. So let's say btn all books. Also,
let's change its text to show all books or
6807
05:31:00,624 --> 05:31:01,624
see all books. Beside that I don't like that
this text is in all caps. If you want, you
6808
05:31:01,624 --> 05:31:02,624
can change that you need to search for all
caps, this text all caps, and you can change
6809
05:31:02,624 --> 05:31:03,624
its value.
6810
05:31:03,624 --> 05:31:04,624
Let's change it to false. Now you can see
that this text is much more better. For the
6811
05:31:04,624 --> 05:31:05,624
second button, I'm going to change its ID
to btn currently reading because this button
6812
05:31:05,624 --> 05:31:06,624
is going to show the list of the books that
the user is currently reading, let's say btn
6813
05:31:06,624 --> 05:31:07,624
currently reading. And let's change the text
to currently reading books as well. Like before,
6814
05:31:07,624 --> 05:31:08,624
let's change the text all caps value to false
as well. For the third button, I'm going to
6815
05:31:08,624 --> 05:31:09,624
change its ID to btn already read because
this button is going to show the list of books
6816
05:31:09,624 --> 05:31:10,624
that the user already read. So let's say btn
already read. Let's change the text to already
6817
05:31:10,624 --> 05:31:11,624
read books. Once again, let's change the text
all caps attribute to false. For the fourth
6818
05:31:11,624 --> 05:31:12,624
button, I'm going to change its ID to btn
want to read book. Let's say btn want to read
6819
05:31:12,624 --> 05:31:13,624
because this button is going to show the list
of different books that the user wants to
6820
05:31:13,624 --> 05:31:14,624
list. Basically, this is the wish list. Let's
also change the text. Let's say your wish
6821
05:31:14,624 --> 05:31:15,624
list. Let's change the all caps attributes.
That seems better. The fifth button I'm going
6822
05:31:15,624 --> 05:31:16,624
to change its ID to btn favorite books, let's
say btn favorite. And for its text, I'm going
6823
05:31:16,624 --> 05:31:17,624
to say see your favorites. Once again, let's
change the all caps attribute. And for the
6824
05:31:17,624 --> 05:31:18,624
last button, I'm going to change its ID to
btn. About by clicking on this about button,
6825
05:31:18,624 --> 05:31:19,624
we are going to show a dialog in which we
will show some information about this application,
6826
05:31:19,624 --> 05:31:20,624
let's say within about, and let's change its
text to about. For this button, I'm not going
6827
05:31:20,624 --> 05:31:21,624
to change its all caps attributes, because
I think it's better this way. Let's also give
6828
05:31:21,624 --> 05:31:22,624
an ID to this text view as well. Let's see.
txt license, I'm going to change its text
6829
05:31:22,624 --> 05:31:23,624
to developed by mesa@miko.org. I'm also going
to change the style of this book to atelic.
6830
05:31:23,624 --> 05:31:24,624
Let's search for text style. Let's change
this font to its attic. Also, let's change
6831
05:31:24,624 --> 05:31:25,624
the text of the first text view to the name
of our application. I'm just going to put
6832
05:31:25,624 --> 05:31:26,624
my library in here. Later on, we will change
the font of this TextView as well. Okay, let's
6833
05:31:26,624 --> 05:31:27,624
add our constraints. Also, you can see that
I'm using the constraint layout in here, but
6834
05:31:27,624 --> 05:31:28,624
feel free to use the relative layout or linear
layout. I'm more comfortable with constraint
6835
05:31:28,624 --> 05:31:29,624
layout. So I'm using constraint layout, let's
add our constraints. I'm going to add a margin
6836
05:31:29,624 --> 05:31:30,624
top of maybe 70 dP for this image view. But
before that, I'm going to move my TextView
6837
05:31:30,624 --> 05:31:31,624
because right now I cannot see that. Okay,
now let's add the margin top, I think 50 dP
6838
05:31:31,624 --> 05:31:32,624
would be better. Now let's concentrate our
TextView to the bottom of our image view.
6839
05:31:32,624 --> 05:31:33,624
And also to the both side of our screen. We
also need a margin top. I think once again
6840
05:31:33,624 --> 05:31:34,624
50 dP will be fine. It seems too much, let's
say 40 dP, let's concentrate all of these
6841
05:31:34,624 --> 05:31:35,624
buttons. For this one, I'm going to constraint
it to the bottom of my TextView. And also
6842
05:31:35,624 --> 05:31:36,624
to both sides of my screen, I need the margin
top, let's say 30 dP. After that, let's constrain
6843
05:31:36,624 --> 05:31:37,624
the second one, I'm going to fast forward
the process in here. Sometimes like this,
6844
05:31:37,624 --> 05:31:38,624
if the items are too close to each other as
before, we can select both of the items. And
6845
05:31:38,624 --> 05:31:39,624
by right clicking we can use this right click
menu. Let's say we can currently reading top
6846
05:31:39,624 --> 05:31:40,624
to bottom between all books. Let's add a margin
in here by selecting our button margin top
6847
05:31:40,624 --> 05:31:41,624
I think 16 would be fine. It seems too much.
Let's say 10 db. Also for the second month.
6848
05:31:41,624 --> 05:31:42,624
For this last TextView in here, first of all
I'm going to constrain to to the bottom of
6849
05:31:42,624 --> 05:31:43,624
my screen. And also beside that I'm going
to concentrate it to the both edges of my
6850
05:31:43,624 --> 05:31:44,624
screen so that it would be centered. Also,
let's add a margin bottom of maybe 16 db,
6851
05:31:44,624 --> 05:31:45,624
that seems better. For this image view, I'm
not going to use a sample data. Instead, I'm
6852
05:31:45,624 --> 05:31:46,624
going to show the actual image. I have prepared
an image in here on my desktop. Let's use
6853
05:31:46,624 --> 05:31:47,624
that I'm going to copy this image. Let's add
it into our mid map folder. Resources map,
6854
05:31:47,624 --> 05:31:48,624
right click and paste I think the name is
fine. And let's switch to our split view.
6855
05:31:48,624 --> 05:31:49,624
For this image view, if you click on that,
first of all, let's delete this last attribute
6856
05:31:49,624 --> 05:31:50,624
this tools source compat and instead of that,
let's use source. let's address the image
6857
05:31:50,624 --> 05:31:51,624
that we just added into our project. It seems
too large, we need to pass the width and height
6858
05:31:51,624 --> 05:31:52,624
manually. Let's see 150 dP I think that's
fine. And also 150 dP for the height. That
6859
05:31:52,624 --> 05:31:53,624
seems a little bit better. For this mind library.
If you remember we had an attribute called
6860
05:31:53,624 --> 05:31:54,624
font family. Let's search for that in the
design view. font family if we click on this
6861
05:31:54,624 --> 05:31:55,624
arrow more fonts. Let's search for a good
font. Once again, these are the Google fonts,
6862
05:31:55,624 --> 05:31:56,624
and you can use them in your projects free
of charge. I think this lemon font is good,
6863
05:31:56,624 --> 05:31:57,624
let's add font to our project. And let's press
OK. You can see that the font has changed.
6864
05:31:57,624 --> 05:31:58,624
Let's also increase the size a little bit.
Let's search for text size. And instead of
6865
05:31:58,624 --> 05:31:59,624
14 sp, let's say 18. So that seems better.
The next thing that I'm going to change in
6866
05:31:59,624 --> 05:32:00,624
this layout file is the width of all of these
buttons. I want my buttons to have the same
6867
05:32:00,624 --> 05:32:01,624
width right now you can see that these have
an ugly shape. If we click on one of our buttons,
6868
05:32:01,624 --> 05:32:02,624
let's switch to a split view. Once again,
let's click on that. For the width. If we
6869
05:32:02,624 --> 05:32:03,624
say 200 DPI, I think that would be fine. Yes.
For all of our buttons for the video, I'm
6870
05:32:03,624 --> 05:32:04,624
going to say 200 DPI,
6871
05:32:04,624 --> 05:32:05,624
you can see that it's much better now. Okay,
this is going to be the layout for the first
6872
05:32:05,624 --> 05:32:06,624
page of our application. Let's quickly initialize
the necessary items in our Java file. By pressing
6873
05:32:06,624 --> 05:32:07,624
double shift, you can search for main activity
dot java file. And in here, let's initialize
6874
05:32:07,624 --> 05:32:08,624
our UI elements.
6875
05:32:08,624 --> 05:32:09,624
Let's also initialize these buttons down below
inside this onCreate method. I'm going to
6876
05:32:09,624 --> 05:32:10,624
do that inside another method, let's say you
need to be used. And let's create that method
6877
05:32:10,624 --> 05:32:11,624
by pressing ALT plus enter. Once again, I'm
going to fast forward the process of initializing
6878
05:32:11,624 --> 05:32:12,624
these buttons.
6879
05:32:12,624 --> 05:32:13,624
By clicking on each one of these buttons beside
this within about I'm going to navigate the
6880
05:32:13,624 --> 05:32:14,624
user to another activity. Up until this point,
our applications had only one activity, which
6881
05:32:14,624 --> 05:32:15,624
was named main activity. But if we want, we
can create another activity in our application
6882
05:32:15,624 --> 05:32:16,624
as well. We are going to talk about activities
and fragments in the next video. But creating
6883
05:32:16,624 --> 05:32:17,624
an activity is really simple. There are a
lot more concepts regarding activities, we
6884
05:32:17,624 --> 05:32:18,624
will talk about them later on. But in here,
if you want to create an activity, you can
6885
05:32:18,624 --> 05:32:19,624
do something like this in your project inside
Java folder. Inside your project folder, you
6886
05:32:19,624 --> 05:32:20,624
can right click on that by saying new activity.
In here you can see that you have few options.
6887
05:32:20,624 --> 05:32:21,624
The names of some of these activities may
be familiar from that gallery view from where
6888
05:32:21,624 --> 05:32:22,624
we created our project. But basically in here,
we need this empty activity. You can also
6889
05:32:22,624 --> 05:32:23,624
select this gallery, in which you will get
the same dialog when you created your application.
6890
05:32:23,624 --> 05:32:24,624
Let's select empty activity. Once again. In
here we can name our activities. The convention
6891
05:32:24,624 --> 05:32:25,624
in here is that to use the keyword activity
in naming and also, you cannot use a spaces
6892
05:32:25,624 --> 05:32:26,624
and also other strange characters. For the
name of this activity in here, I'm going to
6893
05:32:26,624 --> 05:32:27,624
name it all books activity, because this is
going to be the activity that we are going
6894
05:32:27,624 --> 05:32:28,624
to navigate the user when the user clicks
on that see all books button. So let's say
6895
05:32:28,624 --> 05:32:29,624
all books activity.
6896
05:32:29,624 --> 05:32:30,624
You can also see a checkbox in here it says
that if you want to generate a layout file
6897
05:32:30,624 --> 05:32:31,624
for your activities, it is possible to create
an activity with only the Java file and not
6898
05:32:31,624 --> 05:32:32,624
the XML file. We will talk about that in the
next section of the course. But in here, we
6899
05:32:32,624 --> 05:32:33,624
do need a layout file. So I'm going to leave
this one checked in here. You can see that
6900
05:32:33,624 --> 05:32:34,624
when I changed the name of my activity, also
the layout name changed as well. In the layout
6901
05:32:34,624 --> 05:32:35,624
name, you cannot use upper cases letter. And
as you can see all the letters change to lower
6902
05:32:35,624 --> 05:32:36,624
cases. Beside that you cannot have spaces.
You have another option in here to indicate
6903
05:32:36,624 --> 05:32:37,624
that if this activity is going to be a launcher
activity. Basically, in every application,
6904
05:32:37,624 --> 05:32:38,624
one of the activities can be launcher. Right
now the launcher activity in our application
6905
05:32:38,624 --> 05:32:39,624
is this main activity. It means that when
we launch our application, this main activity
6906
05:32:39,624 --> 05:32:40,624
is going to be shown. And for that I'm not
going to check this option in here. Also,
6907
05:32:40,624 --> 05:32:41,624
if you don't check this option in here, if
you change your mind, you can change in your
6908
05:32:41,624 --> 05:32:42,624
manifest file later, but more on that later
on in the course, the package name is fine,
6909
05:32:42,624 --> 05:32:43,624
we are going to create this activity in our
package. And for the language of this activity
6910
05:32:43,624 --> 05:32:44,624
once again, I'm going to use Java. Let's finish
creating our activity. And let's see what
6911
05:32:44,624 --> 05:32:45,624
happened in our project. So first of all,
a new Java class has been added into our package,
6912
05:32:45,624 --> 05:32:46,624
you can see that we have this all books activity
dot java file. Beside that in our Resources
6913
05:32:46,624 --> 05:32:47,624
folder, in the layout folder, we have this
activity all books dot XML. Both of these
6914
05:32:47,624 --> 05:32:48,624
has been created by the Android Studio. Beside
that if you take a look at your manifest file,
6915
05:32:48,624 --> 05:32:49,624
this Android manifest dot XML, you can see
that in here we have a new line, we have a
6916
05:32:49,624 --> 05:32:50,624
new activity tag in our application tag, it
means that now our application has two activities.
6917
05:32:50,624 --> 05:32:51,624
If you want to create your activities yourself
without the help of Android Studio, that is
6918
05:32:51,624 --> 05:32:52,624
possible. Basically, you can create a java
file. After that you can create a new layout
6919
05:32:52,624 --> 05:32:53,624
file. And beside all of these, you need to
add this tag into your application tag inside
6920
05:32:53,624 --> 05:32:54,624
the manifest file. Also, you need to link
the Java file and layout file in your onCreate
6921
05:32:54,624 --> 05:32:55,624
method. We will take a look at that in a minute.
But instead of all of these, we have used
6922
05:32:55,624 --> 05:32:56,624
the help of Android Studio. Okay, let's close
this manifest file. And let's switch to activity
6923
05:32:56,624 --> 05:32:57,624
all books dot XML file, you can see that in
here we have another layout in which we can
6924
05:32:57,624 --> 05:32:58,624
design for this activity. In this video, I'm
not going to design the layout for this activity,
6925
05:32:58,624 --> 05:32:59,624
instead, I'm just going to change its background
color. If we switch to a split view in here,
6926
05:32:59,624 --> 05:33:00,624
once again, you can see that we have a constraint
layout. And inside the opening tag of this
6927
05:33:00,624 --> 05:33:01,624
constraint layout, I'm going to say background,
and I'm going to pass a color. For example,
6928
05:33:01,624 --> 05:33:02,624
I think I'm going to pass this color accent.
I'm doing this because I'm just going to indicate
6929
05:33:02,624 --> 05:33:03,624
that we have navigated the user from the main
activity to this artbooks activity. Okay,
6930
05:33:03,624 --> 05:33:04,624
now let's see how we can navigate the user.
Let's close both of these files. These are
6931
05:33:04,624 --> 05:33:05,624
all books activity dot XML file, and also
this albox activity dot java file. I said
6932
05:33:05,624 --> 05:33:06,624
that we are going to navigate the user to
the other activity by clicking on this button
6933
05:33:06,624 --> 05:33:07,624
all books activity, so we need to create an
onclicklistener in here, let's say btn. All
6934
05:33:07,624 --> 05:33:08,624
books that set onclicklistener. Let's pass
in the onClick listener. And inside this onClick
6935
05:33:08,624 --> 05:33:09,624
method is the place to write the logic to
navigate the user. In here, I'm going to use
6936
05:33:09,624 --> 05:33:10,624
an object that I have never used so far, and
that object is called intent. You can see
6937
05:33:10,624 --> 05:33:11,624
that it's coming from Android dot contents
package. Let's import that. Let's name it
6938
05:33:11,624 --> 05:33:12,624
intent is equal to new intent. It's a simple
Java class that we have defined. It's like
6939
05:33:12,624 --> 05:33:13,624
any other Java class. The constructor of this
intent needs two things. First of all, we
6940
05:33:13,624 --> 05:33:14,624
need to pass a context in here, because we
are inside this main activity. And hopefully
6941
05:33:14,624 --> 05:33:15,624
by now we know that activities are context,
we need to pass main activity dot this. Let's
6942
05:33:15,624 --> 05:33:16,624
pass that main activity dot this. After that
we need to pass the destination activity where
6943
05:33:16,624 --> 05:33:17,624
you want to navigate the user to for example,
I can say all books activity dot class.
6944
05:33:17,624 --> 05:33:18,624
So this way, we are saying that this intent
is going to navigate us from the main activity
6945
05:33:18,624 --> 05:33:19,624
to all books activity. After that when we
created our intent, we can call an inner method
6946
05:33:19,624 --> 05:33:20,624
in here called start activity, you can see
that this start activity needs an intent.
6947
05:33:20,624 --> 05:33:21,624
And we can pass our intent safely. This start
activity is an inner method like this fine
6948
05:33:21,624 --> 05:33:22,624
view by ID which exists inside every activity.
Now if we run our application, we should see
6949
05:33:22,624 --> 05:33:23,624
that when we click on this button, the user
will be navigated to all books activity. Let's
6950
05:33:23,624 --> 05:33:24,624
quickly test that. Right now you can see that
we have few issues with our layout. First
6951
05:33:24,624 --> 05:33:25,624
of all, we cannot see this license TextView
for that we need to decrease the margins from
6952
05:33:25,624 --> 05:33:26,624
here. And also beside that this currently
reading books text does not fit in our button.
6953
05:33:26,624 --> 05:33:27,624
We will increase the width of all of our buttons
as well. But for now, if we click on this
6954
05:33:27,624 --> 05:33:28,624
see all books button, we can see that we are
navigating to the other activity. Beside that
6955
05:33:28,624 --> 05:33:29,624
if you press this back button. Once again
we will be navigated to our main activity.
6956
05:33:29,624 --> 05:33:30,624
It seems to be working perfect. Let's quickly
fix the issues with our layout and let's To
6957
05:33:30,624 --> 05:33:31,624
finish off this video in our activity main
dot XML file. First of all, let's decrease
6958
05:33:31,624 --> 05:33:32,624
the margins. For example, this margin top,
I'm going to change it to 30 dP beside that
6959
05:33:32,624 --> 05:33:33,624
margin top of this my library text. Let's
decrease that to 30 dP. That seems better
6960
05:33:33,624 --> 05:33:34,624
for all of my buttons. Instead of 200 dP,
I'm going to say 230 dP. For example, for
6961
05:33:34,624 --> 05:33:35,624
the first one, let's say 230 dP. Let's run
the application once again. And let's see
6962
05:33:35,624 --> 05:33:36,624
if we have fixed the issues successfully.
The layout seems better. Okay, I think that's
6963
05:33:36,624 --> 05:33:37,624
enough for this video. Just before I finish
off, I'm going to say that you can check the
6964
05:33:37,624 --> 05:33:38,624
source code for this application@maker.org
slash code. So feel free to check that if
6965
05:33:38,624 --> 05:33:39,624
you need I upload the source code at the end
of every video. Also, we'd be very happy to
6966
05:33:39,624 --> 05:33:40,624
see your feedback. Okay, in the next video,
first of all, we are going to create a model
6967
05:33:40,624 --> 05:33:41,624
for our books beside that we will create a
recycler view and recycler view adapter to
6968
05:33:41,624 --> 05:33:41,640
show a list of different books inside the
second activity. See you in the next video.899100
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.