the main thread. It means that the code inside the worker thread and the code inside the main thread are happening at the same time. In this situation, we say that our application is asynchronous or it is concurrent. So once again, when you run your Java or Android application, one thread will be created by default in which we call that thread the main thread or the UI thread. After that, inside that main thread, you can create as many worker threads as you want. The simplest way to create a thread is to say thread thread is equal to new thread. And after that, we can pass a new runnable to the constructor of your thread in which we'll create this run method that will be executed in a background thread. But by this way of defining your thread, there are a lot of considerations that you need to be worried about. For example, you need to be worried about canceling your thread when the work is done. Also, there are all sorts of new concepts, for example, thread pools, deadlocks and interrupting your thread. I do not intend to talk about all of those concepts in here because in Android, we have all sorts of options available for handling background tasks. In fact, later on in the course, we have a complete section dedicated to handling background tasks in Android. We have options like async tasks, services, job scheduler and work manager, we are going to talk about all of them. But more on that in future videos. Okay, now let's talk about this interrupted exception that is happening in here. Basically, whenever an exception occurs, your application will crash and your application will be closed by the operating system. There are all sorts of exceptions that can occur. For example, the ones that we have seen previously, where arithmetic exception, null pointer exception, and the one that we can see in here is interrupted exception. So in order to avoid crashes, you need to do something about the exception. Let's comment all of these and let's see what we can do about the exceptions. Let's create an application in which we'll create an arithmetic exception. In here, I'm going to define two new integers. For example, let's say int a is equal to two, and int b is equal to zero. Let's print something in here, let's say print a divided by b. If we run our application right now, we should see an arithmetic exception. You can see that down in here. The thing that we can do about this exception is that to catch it whenever it happens. And we can do that by using a try catch block. Let's quickly see that. For example, I can say try. Let's move this line of code to inside our tri block. We can say try this in case some exception happens cash that exception. The kind of exception was arithmetic exception. So let's say arithmetic exception. Let's name this exception e. And inside the catch block, we can do something else with our code. For example, we can print something. In this case, let's say b was zero. Now if we run our application, we wouldn't get an exception. Instead, we are printing this message. Sometimes this message might not be useful. And in fact, in most cases, this message will be useful for debugging purposes, but it's much better than a crash. The kind of exception that we are catching in here is arithmetic exception. If we do not know the kind of exception, we can pass the generic term exception. If we pass this exception, it's going to accept all of the exceptions. If we run our application once again, we should see the same result. And here is the same result. The other kind of exception that we have seen so far was a null pointer exception that would occur when we do some operation on a null object reference. For example, if we had a string in here, let's say a string name is equal to null. If we do some operation on our name inside the tri block, for example, if we say name dot equals, let's say may some if we do that, we are going to get a null pointer exception because we have done this dot equals method on a null object inside the catch block this time instead of saying b was zero, let's say name was no. If we run the application, it's going to work fine because we are passing the generic term exception, but we can safely pass null pointer exception. Let's run the application, you can see that name was no. The other kind of exception that we just saw was this interrupted exception. It's going to happen whenever we interrupt the current thread. For example, imagine that inside a worker thread, you are downloading some file. At the same time, you may receive a phone call. If you don't cash the interrupted exception, you may get a crash. We have all sorts of exceptions. For example, we have all your exception, which stands for input output exception. We have class cast exception, we have all sorts of other exceptions in which we are going to talk about them whenever we face them. Also, on our exception, we have a useful method for debugging purposes. And that method is called print stack trace, we can say the name of our exception dot print stack trace, we will talk about this stack in future videos when we talk about activities and fragments in Android. But for now just know that this line of code in here is going to print something into our console that might be useful when you are debugging your application. Let's run the application in here. You can see the stack trace of your exception in here, it says that the exception occurred in line 34, which is this line. And you are also seeing the name of your exception. This is not an actual exception, you are just seeing some information about your exception. This in here might be useful for when you are debugging your application. Okay, I think that's enough talking about concurrency and exceptions in Java. In the next video, we are going to talk about singleton pattern in Java. See you in the next video. In this video, we are going to talk about singleton pattern. Basically, we use singleton pattern when we want to make sure that we have only one instance of some class in our entire application. In some cases, we may want such a thing. And here is how we can implement that pattern. For example, one case of using singleton pattern is when you are using a database in your application. Don't worry about the databases, we have a complete section dedicated to them. But in here, we are just going to create a simple Java class named database. Let's quickly create that inside our source folder inside our package. And we will see how we can implement that pattern. Let's name this class database. For this database class, I'm going to define a field, let's say private string name. Also, let's create a constructor and also getters and setters. Right now, because we have this public constructor, we can create as many instance as we want from this database class. But if you want to make sure that you have only one instance of this class in your entire application, you can create another field in here. For example, I'm going to say private static as the data type of this field, I'm going to specify the class that we are in. For example, in this case, I'm going to say database. So let's say private static database, make sure to import the one that comes from our package or that may code that singleton pattern. For the name of this field, it's convention to name it instance. And that's it. Now we can create a getter method for this field. And here is how we can create that field, we can say public, because we are going to use this instance inside this field, I'm going to say static, the return type of this getter method is going to be the class itself. So I'm going to say database, let's name this method, get instance, as you can see the suggestion in here, and let's have a body for this method. In here, instead of just simply passing this instance, I'm going to create an if statement, I'm going to say if let's say null is equal to our instance. If that's the case, I'm going to create a new instance, I'm going to say instance is equal to new database. Notice that we need a name for the constructor of this database. So I'm going to receive that name via the input of this method in here. Let's say a string name. And later on, let's pass this name in here. So if our instance is null, we are going to create a new instance. And after creating that, we are going to return it. We can say return instance. But in the other case, in case if our instance is not null, in the else case, I'm just going to simply return instance. Right now, if we use this get instance method, instead of this constructor, we are sure that we have only one instance of our database class in our entire application. For example, if we go to the main class, and if we try to instantiate our database, we can have only one instance, we can say database, let's name it database is equal to database with capital D dot get instance. Of course, we need the naming here. Let's pass a name. For example, let's say DB music. Notice that because we have declared this get instance method as a static. Now we can use this database class with this capital D. It means that we are not relying on the instance of this database class. Instead, we are using the object itself. But right now, someone can declare the database class as we did for any other class. For example, in here, someone can say database, let's name this one test database is equal to new database because in our database class, we have the constructor, we can pass a name in here. And if we do that, we have another instance of this database class. For example, let's say DB movies. In order to restrict users from using the constructor, I'm going to change the access modifier of this constructor inside my database class. I'm going to change this public to private so that this constructor would be available only from inside this class and not from inside any other class like this main class. Right now we have an error in here and the error says that the constructor is private. So by changing the access modifier from public to private, now we are restricting others from creating multiple instances of our database class. But because our constructor inside our database is private, we can use this constructor inside this get instance method as you can see in line 10 in here. Right now, because of this static keyword, and also because of this if statement that we have put in here, we are sure that we have only one instance of our database class. But there is also another point that we need to be worried about. And that's when someone tries to create multiple instances of our database from multiple threads. In some cases, if someone tries to create multiple instances of our class asynchronously, they might be successful in doing such a thing. In order to make our get instance method thread safe, we have an option in here. After this static keyword, we can add another keyword in which we haven't seen so far, we can say synchronized. This synchronized keyword will make our get instance method thread safe. It means that no more than one thread can call this get instance at the same time. So this is the whole single tone pattern. First of all, you declare a static field of the kind of your class, for example, in this case, database, after that, you create a get instance method in which is static once again, and also synchronized. Inside that get instance method, you are checking that if the instance of your database is null or not. If it is not, it means that it's the first time that you are calling this get instance method. So you need to create a new instance like we are doing in line 10. After that, we are returning the instance. But in the else case, we are not going to create a new instance, we are just going to return the existing instance. Besides this get instance method in order to restrict others from using the constructor, you need to change the access modifier of your constructor to private so that it would be usable only inside your database class. We can also make this if statement shorter. For example, in here, I can delete this whole else case. And I can delete this return instance from line 11 in here. And after the if statement, I can return that instance. So by the time we reach to this line 13, in which we are returning the instance, we are sure that our instance is not now, and we are returning either a new instance or the existing instance. Okay, I think that's enough talking about the singleton pattern. Later on in the course, you will see that this pattern might be useful from time to time. Before I finish off this video, I'm going to talk about one more methods available in every class in Java. And that method is called to string. Right now, we don't have any two string method inside our database class. But if we switch back to the main class, and if we say print, database dot to string, you can see that we have such a method. And if we run our application, we should see some information about our database class. Let's quickly run it. And let's see if we can see any result. You can see that we are getting the address of our database class. If you want, you can change this. For example, inside your database class, you can override the two string method down in here inside the scope of our class, we can press control plus all. And from here, we can select this two string method. Right now we are returning the super statement. If you want to change that we can delete this whole statement and we can prepare a string in here. For example, we can say a string text is equal to let's say database class. After that, let's add a line. And after that, let's print the name of our database. Let's say name plus this dot name. After creating this text, we can return it. For example, we can say return text. Now that we have overrided this two string method, if we run our application, we should see some other results printed into our console from time to time, this two string method can be useful as well. It's available in every Java class. And basically it will print some detail about that class. Okay, I think that's enough for this video. And for that matter, I think that's enough talking about Java, our Java section is done. And hopefully we have learned a lot. In the next video, we are going to have a quick challenge in order to make sure that we have learned everything that we have talked so far. But before I finish off this video, I'm going to say that with Java, you can do a lot of things. For example, besides writing Android applications, you can use Java in order to create desktop applications for Windows, Linux, and macOS operating systems. And also with the help of a spring framework, you can use Java in order to create enterprise web applications. There are much more to Java language. For example, you can use Java in order to create applications for quantum computers. You can also use it for data analysis purposes and literally everything else that you want. In this course, we have touched the surface of Java language. But if you want to know more, I'm currently recording a new course for Java language. And I'm sure that you will find that course helpful. Besides the Java course, I plan to record a complete course for a spring framework in order to create web applications. By the time you are watching this video, those courses may be released. So make sure to check them. Okay, I think that's enough in the next video. As I said, we are going to have a quick challenge. See you in the next video. Here is our final challenge for the Java section.  Here is how our application is going to work. First of all, we are going to grid the user with some message. After that, we are going to show three options to the user. One for managing the contacts, another one for handling the messages, and another one for quitting the application. In case if the user selects the first option, which is managing the contacts, we are going to show these options. One for showing all of the contacts, another one for adding a new contact, another one for searching for a specific contact. One more for deleting a contact. And the last one is to go back to the previous menu, which was these three options. But in case if the user selects the second option, which is handling messages, we are going to show these three options. The first one is to see a list of all of the messages. The other one is to send a new message. And one last one, in order to go back to the previous menu, in which is our initial menu, which has three options. In case if the user selects these third option, we are going to quit the application. We haven't talked about databases and permanent data storage, but as a hint, if you want to store contacts and messages, you can use a static variables. Okay, pause the video in here and go solve the challenge. After that, come back and let's see my solution for this application. Okay, let's quickly see how can we create this simple application. First of all, I have created my project. And I have created this package inside my source folder. Inside this package, I have created this main class in which contains this main method. Let's continue from here. First of all, I'm going to create a new Java class called message. Inside this message class, I'm going to have three fields. Let's say private string text. Another one, let's say private string recipient. This recipient in here is going to be the name of our contact. Also in here, I'm going to define an integer called ID, let's say private int ID. This ID is not necessary for this application in here. But because in most cases, when you are working with databases, you have a column called ID in order to identify every unique message. I want to be as close as I can to a real world application. I just wanted you to see this ID field inside a model in which is a message. Okay, now that we have these three fields, let's quickly create a constructor. And let's accept all of these three fields. After that, let's create getters and setters. I'm not sure that we are going to need all of these getters and setters later on if we didn't use them, we will delete them. Also inside this message class, let's quickly create another method called get details. Basically, we are going to print the details of every message. So let's say public void. Let's name it get details. Let's say print. First of all, let's say contact name. And let's print the recipient. After that, let's print the text itself. Let's say message plus the text. We also need to add a plus in here. Sorry about that. And I think if we add a backslash n in here, it would be much better. After the text, let's also print the ID. Let's say backslash and ID plus our ID. That's it for our message class. Let's quickly create the contact class as well. New Java class, let's name this one contact. Inside this contact class, I'm going to have four fields. Let's say private string name private string number. You may think that this number should be an integer or maybe a long but because different numbers can have different formats. For example, they can have some parentheses or maybe some hyphens. I'm saving this number as a string. But if you want, you can always use integers or long. After name and number, I'm going to save another field in here. Let's say private string email. And after all of this, I'm going to have another field in here, let's say private array list of different messages. Let's name this one messages. So basically, I'm going to save different messages for every contact inside an array list. Okay, now that we have our four fields, let's quickly create our constructor. But in here, I'm going to create two constructors. Because at the time of creating a new contact, that contact does not have any messages. For that, I'm going to create another constructor in here, which will not accept an array list of different messages. This time, let's select the first three items. But because if I don't do anything about these messages, this array list will be null. Inside this constructor, I'm going to say this dot messages is equal to new array list. So at the time of creating a new contact, I'm saving the messages array list as an empty array list. Okay, now that we have two constructors in here, let's quickly create getters and setters. And also after that, let's create a get details method. Let's say public void, get details. We can also use two string methods. But in here, I'm just more comfortable with this get details method. Let's print all of the information about each contact. Let's say name plus this dot name. After that, let's print the number and email. Okay, that's it for our contact class. Let's switch to main class. And let's write the logic for our application. First of all, in here, I'm going to create an array list of different contacts. But I'm not going to do that inside this main method, because we are going to have multiple methods, and we need to have access to our contacts array list in different methods. For that, I'm going to define my array list above in here in the fields of this main class. I can say private static, because the methods that I'm going to use later on are static methods, I need to create this array list as a static. So let's say private static array list of different contact. Let's name it contacts. But this is not a good place to initialize our contacts array list, because this is not the place that our code will be started. We can initialize our contacts array list, for example, inside this main method. But before I do that, I'm going to also define a scanner in here, because we are going to need the scanner in multiple methods. So let's say private static scanner, let's name it scanner. After that, inside the main method, let's initialize our contacts array list. Let's say contacts is equal to new array list. As you can see in here, I'm going to greet the user with some message. Let's just print something. After that, I'm going to show these three different options to the user. Because I want to have a clean code, I'm going to show these three options inside another method. Let's name that method show initial options. But as you can see right now, I'm getting a red warning. And that's because I need to create this method down in here. So let's say private static void, show initial options. Let's print the options in here. After that, we need to get the user's input. And for that, we can use the scanner. So let's initialize our scanner. Right now, we have declared our scanner above in here, but we never initialized it. It's a good place to initialize our scanner in here. We can say a scanner is equal to new scanner. Let's pass system dot in. After that, we can say int choice is equal to scanner dot next int. Let's create a switch upon this choice. Let's say switch on choice. In case the choice is one, we are going to manage the contacts. Once again, let's do that in another method. Let's say manage contacts. Don't worry about this red warning, we will create this method later on. Let's also add a break in here. But in case the choice is two, we are going to handle the messages. So let's say manage messages. In case the choice is three, or for that matter, any other number, we are going to quit the application. So in here, I can say default. In the default case, I'm just going to break out of the application. Let's minimize this project pain for now. And down in here, let's create our two methods. First of all, let's manage our contacts. Let's say private static void, manage contacts. Once again, in here, I'm going to show a list of options. So let's print those options. Once again, we need to get the user's input for that we can use the scanner. Let's say int choice is equal to scanner dot next int. Notice that we are not initializing our scanner in here. And that's because we have done that inside this show initial options method. So by this time, we have an instance of our scanner. After that, once again, we need to create a switch on our choice. In case it's one, we are going to show all of the contacts. Let's create that method. Show all contacts. And also we need a break. Let's also write the rest of the cases. In the default case, I'm going to go back to the previous menu. So I'm going to once again, run this show initial options method, so that we can see all of these options once again. So in the default case, I'm going to say show initial options. After that, in here, we need a default as well. I'm saying default in here because I'm going to go back to the previous menu, in case if the user enters any other number. Okay, let's create these methods one by one. First of all, let's show a list of all of the contacts. I'm going to do that down in here. Also, instead of writing the declaration of your method, you can use the help of your ID. For that, you can click on the red error in here. And by pressing alt plus insert, the first option is to create this method. You can see that we have created this method successfully. The void is fine for the return type. And down in here, I can create the logic for this method. In order to show a list of all of the contacts, I'm going to simply create a for each loop. Let's say for contact, let's name it see inside our contacts area list. Let's just say c dot get details, the method that we have created inside our contact class. After the for loop, once again, I'm going to show the initial options. For that, I'm going to say show initial options. We are going to quit the application when the user tells us. That's all we need to do inside this method. Let's just minimize it for now. And let's create the second method, which is add new contacts. Once again, we can use the help of our ID inside this add new contact method, we are going to receive some users input. But before that, let's print something. Let's say adding a new contact. After that, let's say please enter the contacts name. Once again, we are going to use our scanner. Let's say a string name is equal to a scanner dot next. After we get the contacts name, we are going to get the contacts number. So once again, let's print something in here. Let's say please enter a contacts number. Let's say a string number is equal to a scanner dot next. Let's also get the contacts email, a string email is equal to a scanner dot next. After that, we need to make sure that the user enters something as the name, number and email of our contact. And don't just press enter and leave us with empty strings. For that, I'm going to create an if statement in here, let's say if name dot equals an empty string, let's say or if number dot equals an empty string, or if email is equal to an empty string. If each one of these is true, we are going to show some message to the user, we are going to ask to enter all of the required information. Let's say please enter all of the information. After printing this line of code, I'm going to recall this whole method, I'm going to start from the beginning of this method. For that, I'm going to say add new contact. I'm calling this method from inside the method. This is called a recursive call, you can see the icon at the left side in here. If you hover over that, you can see the recursive call calling a method from inside that method. So by this way of calling our method, we are starting once again from the beginning of this method. But in the else case, we are going to create our contact object. I'm going to say contact. Let's name it contact is equal to new contact. And after that, I'm going to pass name, number and email. If you remember inside our contact class, we had two constructors. And in here we are using the second constructor. We didn't pass any messages early list to this constructor in here. After creating our contact object, we can say contacts, our early list dot add. Let's pass our contact as simple as that. Now we have added a new contact. After this if statement, once again, I'm going to show the initial options. So let's say show initial options. Because once again, we are not going to quit the application unless the user tells us so. Okay, we are done with our second method. Let's see what else do we have in here. Let's create this search for contact method. The void is fine for the return type. Inside this method, first of all, we are going to ask for the name of the contact. So let's print something. Let's say please enter the contact name. Let's say a string name is equal to scanner dot next. After that, we need to make sure that we have received a name. So let's say if name dot equals an empty string, we are going to print some message, we are going to ask for the contact name once again. Let's say please enter the name. And after that, once again, we are going to use a recursive call. We are going to say search for contact once again. But in the else case, we are going to search for this contact inside our contact array list. For that, once again, I'm going to create a for each loop. Let's say for contact, let's name it C inside our contacts array list. Let's say if C dot get name is equal to the name that we just received. If that's the case, let's print C dot get details. Let's also define a Boolean before this for loop. We will see why I'm defining this Boolean. Let's say Boolean, let's name it does exist. Let's initialize it to false. In this if statement where we have found the contact, first of all, I'm going to change the value of this does exist to true. Let's say it does exist is equal to true. And after the for loop, I'm going to create an if statement, I'm going to say if does exist is not equal to true. Then we are going to print something. I'm going to say there is no such contact. After all of these logics, we are going to show the list of options once again. So let's say show initial options. Okay, that's it for our search for contact method. Let's also create this delete contact method. Once again, in here, we are going to ask for the contacts name. So let's say please enter the name, like before, let's say a string name is equal to a scanner dot next. Next, we need to create our if statement if name dot equals an empty string. We are going to ask for the name once again. And we are going to start from the beginning of this method. In the else case, we are going to look into our contacts are released. So let's say for contact see inside our contacts are released. Before I write the logic, let's also define a Boolean above in here like we did for the search for contacts method. Let's say Boolean does exist. Let's initialize it to false. So in here, let's say if I don't know why I set s in here, let's quickly change this one to see. So let's say if see that gets name is equal to the name that we have received. If that's the case, first of all, we are going to change the value of this does exist. Let's say it does exist is equal to true. After that, we are going to remove this contact from our contacts are released, we can say contacts dot remove. This remove method requires an object which I'm going to pass see the object that we are currently looking in this iteration. After the for loop, I'm going to say if does exist is not equal to true. Then we are going to say there is no such contact. And after all of these logics, we are going to show the list of all of the initial options. Okay, that's it for this method, let's minimize this one. So far, we have created the logic for the first option in our initial options. We have created the whole logic for this manage contacts. But right now in the logic for this manage contacts option, we have one or two possible point for multiple bugs. We are going to fix those two. And also we are going to write the rest of the logic for this application in the next video. See you in the next video. In the previous video, we have created the logic for this manage contacts option. But if you remember, I said that we have some issues right now with this manage contacts logic. And in this video, first of all, we are going to fix those issues. The first issue is inside this add new contact method. In here, we are adding the contact into our contacts array list. But before that, we need to make sure that we do not have a contact with this name saved on our device. For that before adding this contact to my contacts array list, I'm going to check that if I have such a contact on my device. So first of all, let's create a Boolean in here. Let's say Boolean does exist. Let's initialize it to false. After that, I'm going to create a for each loop. Let's say for contact C inside my contacts array list. Let's say if C dot get name is equal to the name that we just received. Inside this if statement, I'm going to change the value of this does exist to true. So let's say it does exist is equal to true. After the for loop, I'm going to check that if this does exist is equal to true or not. Let's say if does exist is equal to true. Then we are going to print something we are going to say, we have a contact with this name. After that, we are going to start from the beginning of this method. So I'm going to call this add new contact method once again. But in the else case, I'm going to add the contact to my contacts array list. So I'm going to cut this line of code from here. And I'm going to paste them inside this else case. Also after adding the contact to our contacts array list, I'm going to show some message to the user. So let's say name added successfully. Okay, that's the fix that we needed inside our add new contact method. Let's also change the show all contacts method a little bit as well. So inside this for each loop after printing the details of a contact, I'm going to print another line. And this line is going to be just a separator that will indicate the difference between different contacts. Nothing special in here. Okay, let's move on. And let's create the method that we haven't created in here. This manage messages method by pressing alt plus insert, we can create that method. First of all, we are going to show a list of options. So let's print something. After that, we need to receive the user's input, I can say int choice is equal to a scanner dot next int. And let's create our switch statement on this choice. In case it's one, we are going to show a list of all of the messages. So let's say show all messages. We will create that method in a minute. But in here, let's add a break. And let's write the other cases. In case it's two, we are going to send a new message. So let's say send new message. And let's add a break. In the default case, I'm just going to show the initial options. After that, in here, we need a break as well. Okay, let's create this show all messages method. In here, first of all, I'm going to save all of the messages from all of the contacts into an array list. So let's say a list of different messages. Let's name it all messages is equal to new array list. After that, I'm going to create a for each loop for contact C inside my contacts array list. Let's say all messages dot add all, we haven't used this add all method. But in here, we are going to do that. I'm going to say C dot get messages. So by using this add all method, we are adding all of the messages of one contact to our all messages array list. After the for loop, I'm going to check that if the size of this all messages is zero or not. So let's say if all messages dot size is greater than zero, I'm going to show the details of every message. So for that, I'm going to create another for each loop in here, let's say for message, let's name it m inside our all messages array list. Let's print m dot get details. And after that, let's also print some separator. But in the else case, I'm going to say that you do not have any message. After everything after the if statement, I'm going to show the initial options. Okay, that's it for this show all messages method. Let's also create the other method which is send a new message. In here, first of all, I'm going to get the contact or recipients name. So let's ask the user that let's say who are you going to send the message. After that, we need to get the name of our contact. So I can say a string name is equal to scanner dot next. After that, we need to check that if this name is empty or not for that I can say if name dot equals an empty string, we are going to ask the user to enter a name. So let's say, please enter the name of the contact. After that, we are going to have a recursive call. So in here, I'm going to say send new message. But in the else case, I'm going to check that if we have such a contact in our contacts array list. For that, I'm going to define a Boolean in here Boolean does exist is equal to false. After that, I'm going to create a for each loop, let's say for contact see inside our contacts array list. If C dot get name dot equals the name that we just received, let's say it does exist is equal to true. After the for loop, I'm going to check that if does exist is equal to true or not. First of all, let's write the else case. In the else case, I'm going to say there is no such contact. But inside the if block, if we do have such a contact, we are going to ask for the message itself. So let's say what are you going to say? Let's say a string text is equal to scanner dot next. After that, we need to make sure that this text is empty or not. Let's say if text dot equals an empty string. If that's the case, let's say please enter some message. And after that, we are going to start from the beginning of this method. So let's say, send new message, which once again is a recursive call. But in the else case, we are going to create our message object. And after that, we are going to add it to the contact that exists in our contacts array list. But if you remember inside our message class, we had a field called ID. Basically, whenever you are receiving these messages from a database or maybe a web server, this ID is unique to every message. In here, I'm trying to be as close as I can to a real world application. So I'm going to make this ID unique. In order to have a unique ID for every message, I'm going to define a field above in here inside my main class. I'm talking about here. And after that, we will increment the number of this ID by one every time that we create a new message. For that, I'm going to say private static integer. Let's name this one ID, and let's also initialize it to zero. After that, down in here, when we create our message object, inside this else case down in here, I can say ID plus plus. So because this ID is a static integer, whenever we increment its value, it's going to be unique for every message. So right now we have the name of our recipient, which we have received above in here. And also we have the message body, which is this text. And also we have generated this ID, which is unique. These are the three things that we need in order to create our message object. I can say message, let's name it new message is equal to new message. We need the text. After that, we need the recipient, which is name. And after that, we need the ID. Next, I need to find the proper contact to add this message to his or her area list of different messages. For that, I'm going to create a for loop. Let's say for contact, see inside our contacts area list. Let's say if C dot get name is equal to the name that we received. If that's the case, first of all, we need to get the array list of different messages of this contact. For that I can say array list of messages. Let's call this array list new messages is equal to C dot get messages. After that, we need to add this new message to this new messages array list. So let's say new messages dot add new message. Sorry, I have used this add all method, we need to remove this all. After that, we are going to save the current contact that we are looking into. Because later on we are going to remove this contact from our contacts array list. And we are going to update his or her array list of messages. And after that, we are going to add it once again to our contacts array list. So for that, I can say contact. Let's name it current contact is equal to C. Next, we need to say current contact dot set messages. And we need to pass our new messages array list. After that, it's time to remove the C from our contacts array list. We can say contacts dot remove. And we can pass the C object. Now that we have removed the previous contact, we can add the new one by saying contacts dot add. Let's say current contact. So this way we have added this new message to our specific contact and we have updated our contacts array list. Okay, that's the entire logic that we need in order to add a new message. Just after everything, after we are done with this method, we need to show the initial options once again. Let's minimize this method. And let's see what else do we have. I think we have covered everything and we are ready to test our application. So let's run the application. In here, we are seeing the greeting message, which is fine. After that, we are seeing the initial list of options. If we select one, we can see the options for managing different contacts. Right now, we do not have any contact in our array list. So if we select one, you can see that we are not seeing anything. It's better to print some message in here in case if we do not have any contact in our phone. For example, that says you do not have any contact on your device. But we will write that later on. For now, let's just see the other options. Once again, let's select manage contacts. This time, let's try to add a new contact. You can see that the console is waiting for a name, let's say may some the console is waiting for a number. Let's add some number. After that, we need to provide some email, let's say may some at gmail.com. And as you can see, may some added successfully. If you want to make sure of that, once again, you can check the list of all of your contacts by saying show all contacts. And in here, you can see the details of may some. Let's also add another contact. Let's say Sarah this time. Let's add a random number. After that, an email. This time, let's try searching for a new contact. So in here, if I say three, you can see that the console is waiting for the name of our contact. Let's type Brad in here. You can see that we are seeing this message. There is no such contact in your phone. Once again, let's try searching for a contact this time. Let's try Sarah. You can see that we are seeing the details about Sarah. Let's also try removing one of the contacts. Let's say for in order to delete a contact, let's say may some in here, we are not seeing any message indicating if that we have deleted may some successfully or not. But if we check the list of our contacts, we should see only Sarah. And as you can see, we have only Sarah in our contacts list. Let's also try this go back as well. So let's say manage contacts once again. This time, if I put any number beside one through four, for example, if I say six, you can see that we are going back to the main menu. Also, let's try adding a new contact with empty values. Sorry about that, I need to go back and select one. Let's say two in order to add a new contact. Let's don't provide anything for the name. You can see that the scanner is waiting for some input. But even if the scanner moves to the next line, we have write the logic in order to prevent adding an empty contact. Okay, that's good. Let's add me some once again. Let's add some number and some email. Let's see what we have inside the messages option. In order to see a list of all of the messages, let's press one. You can see this message, you don't have any message. Once again, let's go to the messages option. I entered two in here and let's try sending a new message. Who are you going to send a message? Let's say Sarah. What are you going to say? Let's say hello. It seems like we are getting a crash. And the crash happened because of this exception. Can current modification exception? Okay, let's take a deeper look into our code and let's debug our application for the first time. It seems like the exception occurred when we tried to add the new message. Also, you can see the line 99 in here. If you click on that, you will be navigated to that line. Yes, we have this exception inside the add new message or send new message inside this for loop. I think the error in here is happening because first of all, we are using this C to assign a new value to this current contact. And immediately after that, we are trying to remove this contact from our contacts array list. So this caused the concurrent modification exception. If you want to get over that exception, you can simplify this if statement. For example, in here, I'm going to delete all these three lines of code. So by this point, we have this new messages array list. We have added our new message to this array list. And after that, in order to update the array list inside this contact, I can say C dot set messages. And I can pass my new messages array list. So no need for removing and adding a new contact into our array list can currently let's run the application once again. And let's see if we are seeing the exception once again. First of all, we need to add a contact. So let's say manage contact, add a new contact. Let's provide a name and number after that some email. And now we can send a new message. Let's say messages, send a new message. Who are you going to send the message? Let's say may some. What are you going to say? After this line of code, we got the exception. Let's say hello. And this time, it seems like we have added or we have sent a new message. If you want to make sure of that, you can go to the messages option. And you can show the list of all of your messages. You can see that we have one message in which we have sent it to Mesa. Okay, that seems better. Once again, let's go to this messages option. And let's try going back to the main menu. So if I type three or any other number beside this one and two, we should go back to the main menu. For example, let's say four. And as you can see, we are seeing the initial options. For the last thing, let's try quitting our application. And as you can see, we have created the application successfully. Let's just fix the few errors that we had in the show all contacts method. If you remember, I said that we are going to show some message in case if we don't have any contact in our array list. So for that, first of all, I'm going to check that if we do have any contact. For that, I'm going to say if contacts that size is greater than zero. First of all, let's write the else case. In the else case, I'm just going to print. You do not have any contact. But in the if block, we are going to do the same thing that we were doing previously. Let's run the application one more time in order to see that these changes were effective or not. So right now, we do not have any contact in our contacts are a list. Also, you may be wondering why we are losing the contacts that we are keep adding into our application. That's because we are not using any permanent data storage like a database. We are using a static array list and that array list will be cleared when we close our application. For that reason, all of the contacts are being removed whenever we stop the application. So let's say manage contacts. And let's say show all contacts, you can see the message in here, you do not have any contacts. But it's better to show the list of initial options. Once again, after we print this line of code. So down in here, after this print statement, let's say show initial options. Okay, this was the last video of this section. And this was my solution for this challenge. Your solution is probably different than me. And that's okay, because there are multiple ways of creating the same application. Hopefully, you have practiced and learned everything that we have talked about in this section. In the next section of the course, we are going to switch back to Android studio and we are going to talk about user interface and how we can design a beautiful layout for our application. See you in the next section. What's up, everyone, I hope you're having fun. Before we start our talk about user interface, I just wanted to remind you that there is an extended version of this course that you can watch by enrolling in the extended course, you will have lifetime access to more than 60 hours of videos, you can ask your questions directly from me, and I will come back to you within hours, you will have access to all of the source codes that I write in the videos and also you can have access to all of the new videos that I upload. Just check out my code dot org format details about the course and don't forget to use free code camp as the coupon code to get 20% discount. If you want to get serious in Android app development, taking the extended course is almost a necessity. Okay, everyone, I just wanted to remind you that there is an extended version of this course that you can take without further ado, let's start our talk about user interface have fun learning. Hello, everyone, and welcome back. This is the user interface section of our course. And we are going to talk about a lot of concepts and tools that we have related to user interface. We are going to talk about a lot of UI elements and their attributes. We are going to talk about different kind of event listeners, for example, click listeners on different UI elements. We will talk about how can we create applications that is compatible with different screen sizes and also different languages. Besides that, we will also talk about material design and all sorts of stuff in the section of the course. Without further ado, let's jump into our project and let's implement some UI elements. As you can see, I'm no longer using IntelliJ IDE and I've switched to Android Studio. We needed IntelliJ IDE for the Java section of the course. But in here, we can switch back to Android Studio. Just in case if you forget how to create a new project in Android Studio, I'm going to start from scratch. Let's say start a new Android Studio project. In here, we needed to select a template. If you remember, I said that these templates are some applications written to some level later on in the course specifically after we talked about activities, we will see that how can we create all of these from scratch. But for now, let's just select this empty activity. After that, in here, we needed to name our project. For example, for this application, I'm going to say UI basics. I'm going to stay with this package name. I think it's fine. We needed a save location. Once again, the default is fine. And for the language, I'm going to say Java. For the minimum API level, as I said in previous videos, in the most videos for this course, I'm going to say with API level 19. After selecting all of this, let's just create our project by clicking on this finish button down in here. It seems like our project has been created successfully. Now that we know about Java, let's have another look at this class that we have created in here. But before that, let's minimize this run pain. As you can see, this is a Java class in our package. You can see that it's a class called main activity. This class is extending the app compact activity, whatever that is. Inside this class, we have this on create method, which is overrided, you can guess it probably because of this app compact activity. And also above in here, we have some imports. Right now, we are importing this app compact activity and also this bundle. Don't worry about this, we will talk about them later on in the course platform. Now you can see the general syntax of a simple Java class in here as well. If you remember, I said that when you create an activity, a Java class and also a XML file will be created. The Java part is responsible for the behavior of your application. For example, in your layout file, you may receive the user's input. In the Java file, you may decide to what to do with the user's input. Hopefully now that we know about Java, we can understand all of these much better. This on create method is a part of something called the activity lifecycle. We will talk about that in future videos. But for now, just know that this on create method is somehow like the main method inside a simple Java application. This is the start point of every activity. And right now, because our application has only one activity, this is the start point of our entire application. So this line 13 in here would be the start point of our code. You can see this super statement in here. Once again, it means that do whatever you are doing inside the parent class. In this case, the parent class is this app compact activity. For the time being, we are not concerned about that. But as you can see, we are passing this same instance state, which is a bundle to the super statement. Later on, we will talk about that. Also, it's important to know that inside activities, you have some inner methods, we didn't have this option in Java. For example, you can see this set content view method. You don't see the declaration of this method anywhere inside this class. But you can see that by using it, you are not getting any red warning. Similar to this set content view, we have a lot of other inner methods inside an activity. We will talk about them later on in the course. Okay, let's move on from this part. And let's start working on our user interface. I just wanted to have another look at our application and specifically this Java file. Now that we know about Java. Okay, let's close this Java file. And let's start talking about this activity main dot XML file. If you remember, in the first videos of this course, I said that XML is a markup language. Once again, if you are a web developer, you know the concept of markup languages. Those are just helpful for defining some elements that are going to be shown to the user. Before everything, let's quickly locate this file in our project. Right now we are inside the Android view. And if you want to find this activity main dot XML file, you can come to this resources folder. Down in here inside the layout folder, you can see the file. And if you switch to project view, you can go to your application folder inside the app folder inside the source folder. You have this main folder. And in here you have the resources folder. Once again, inside the layout file, you have this activity main dot XML file. Okay, let's switch back to Android view. And let's minimize this project pain for now. If you remember from the previous videos of this course, I said that whenever you want to define a layout file, you have two options. First of all, you can use this design view, for example, by dragging some items into your design view. You can have different elements and later on you can define some attributes for your elements. This is one way. But the other way is to switch to text view, and manually type everything that you need. In my opinion, when you are an Android developer, most of the times you are going to work with the text view. So in here, we are going to start from this text view. First of all, if you're not seeing this preview in here, you can always enable it and disable it from this preview option in here. If you have a low memory RAM, my suggestion is to disable this preview, because it's going to render the layout file in real time. And that's going to load a lot of pressures on your devices memory RAM and also CPU. For that if you have low memory RAM, disable this preview, it will speed everything up. Right now inside this simple layout file, we have this text view, you can see that when I click on this text view, this text view inside our XML file has been highlighted. It means that these are the same. I'm going to delete this text view for now. Let's select all of it and let's press delete. The next thing that I'm going to do is that I'm going to change this constraint layout. In future videos, we will talk about what a constraint layout is. But for now for a starter, I think it's better to start with relative layout. Let's select that. Also, if you have noticed, you can see that when we have changed the opening tag in here to relative layout, the closing tag has been changed to relative layout as well. Inside these opening and closing tags, I can have different UI elements. For example, if I want to define a text view, I can open an anchor bracket. And by typing text view in capital T, we have a list of options. Let's select the first one text view. Immediately after pressing enter, we are getting some attributes. So for every UI elements, you need at least two attributes a width and also a height. The values for these two attributes can be some numbers. But alternatively, you can see that we have other options in here as well. We will talk about this smash bar and wrap content in a minute. But for now, let's just select wrap content. Basically, it's going to wrap the content of this text view. For the height, let's also select wrap content. If you want, you can finish creating your text view by adding a slash in here. And that's it, you can have a text view. But if you want to have more attributes, you have a lot of options. For example, before this slash, I'm going to say, let's say text, you can see that we are getting a suggestion Android column text, let's select that. And in here, I can pass a text. For example, let's say hello world. As soon as I type this, you can see the text in the preview as well. So text is one of the attributes of this text view. Let's talk about the other attributes found by one. For example, right now, this text is at the corner of my screen. I don't like this. I'm going to change the position of this text view to the middle of this line. For that, I have multiple options. First of all, I can add some margin, in which we'll add a space between this text view at the edge of the screen. For example, I can save margin. And I can pass 50. For example, 50 DP DP is the unit when you are working with your layout files. We can see that we have some margin, but we can be more specific. For example, we can say margin top margin left margin right and also margin button. For example, I can say margin left. Let's say 100 DP this time. Margins are extremely useful. But in here, they are not going to help us because this is not accurate. We want our text view to be exactly in the center of our line. For that, instead of using a margin, I have a better option. I can say center horizontal in order to center my text view horizontally. As you can see, I can pass through or false in here. Let's pass through. And this time, and the text view has been centered horizontally. If you want, you can also center it vertically by saying center vertical and passing a value of true. You can see that it has been centered both horizontally and vertically. Besides these two, you also have another option. For example, in this case, I'm going to delete both of these attributes. Instead, I'm going to say center in parent, this one down in here. Let's pass through as the value of this attribute. This center parent is going to center our element inside its parent element. An XML file is like a tree. We have these child elements. And above them, we have the parent elements. For example, in this case, the parent element is a relative layout. If we want, we can have another relative layout in here, or maybe some other UI elements. And inside that element, we can have this text view. We can do these kinds of inheritance as many times as we want. So in this case, the text view is our child element. And this relative layout is our parent element. And when we use this center in parent attribute, it's going to center our text view in its parent element. Okay, let's move on from this part. If you want, you can also increase the size of your text. For that you can say text size, this attribute in here, and you can pass a number. For example, you can say 20 SP. When you are working with the size of your fonts, the unit is SP. You can see that it's larger now. Also, if you want, you can have some styles on this text as well. For that, we can say text style. This attribute, you can see three styles, bold, italic and normal. For example, let's pass bold. If you want your attribute to be both bold and italic, you can add a pipeline in here. And after that, you can say italic. This time, you can see both of these attributes in the same element. If you want, you can also change the color of your text. For that, we can say text color. And we can pass a color. But that color should be defined inside another XML file called color. Right now we have three colors, this color accent color primary and color primary dark. Basically, these are the names of some colors. Let's select the first one. And as you can see, the color has been changed successfully. But where does this color dot XML file locates in our project? Well, if you want to take a look at that, you can open the project pane. Inside the resources folder inside the values folder, we have these colors dot XML. You can see that we have some colors in here with the exact name that we just saw. Don't worry about this, we are going to talk about different kind of XML files in future videos. But for now, I just wanted you to see this XML file. So instead of passing a color from inside the color dot XML file, you can also pass the hexadecimal value of that color. For that, you can search the internet for something like HTML color picker. Let's search that. For example, let's select a green color in here. This one maybe. And in here, you can see the hexadecimal value. Let's copy that. And instead of passing a color from inside our color dot XML file, I'm going to paste the value of that color, you can see that the color of the text view has been changed to the color that we just selected. If you remember from the previous videos of this course, we have seen that when we wanted to have access to all of these UI elements inside our Java file, we needed an ID. Let's add an ID attribute for this text view as well. Let's say ID. The convention is to say add plus ID slash, let's select that. And let's define some ID. Let's say TXT welcome. The ID attribute should be unique inside every XML file. It means that two different elements inside the same XML file cannot have the same ID. Later on, we will use this ID in our Java file in order to have access to this text view. There are much more attributes to text views, but I think if we continue, we are going to get bored. For that, I'm going to move on from this text view. And I'm going to define a button after this text view. For that, once again, I can open an anchor bracket. And in here, I can say button with this capital B, let's select the first option. Once again, we have these two mandatory attributes. And in here, let's talk about the difference between this wrap content and match parent. Right now, if I select match parent, let's also select match parent for this height. You can see that this button is covering all of the screen. And that's because we have set the width and height to match parent. If you want, you can change this height, for example, to wrap content. And here you can see the difference, the width is matching the parent, but the height is wrapping the button's content. Let's also change the width to wrap content. Besides wrap content and match parent, you can always pass the values manually. For example, I can say 150 DP. You can see that the width has been changed. But this is highly discouraged because our application is going to be installed on devices with different screen sizes. Sometimes if we put some numbers for this, if we pass the values manually, we may not know the exact position of every element. And also sometimes, our elements might not fit in the screen in some devices. So for that matter, I'm going to change this one to wrap content in here. Also now that I have talked about different screen sizes, I'm going to say that in here inside this preview, you have this option called devices for preview. If you click on that, you can see a drop down. And from here, you can select different screen sizes. For example, let's select this one. And you can check the looks of your application in different screen sizes. For example, let's check a tablet. You can see that it's somehow different. Don't worry about this. Later on in the course, we are going to cover how we can have a consistent look for our application in different screen sizes. I just wanted to mention this option above in here. Like for the text view, we have a text option for the button as well. Let's say text. And let's say say hello. Similarly, we have the option to add some margins, but I'm not going to do that right now. Instead, I'm going to move this button to below this text view. For that, we have an attribute called below this one in here layouts below. And we need to pass the ID of the other element. For example, in this case, we need to pass the ID of this text view. I can say add plus ID slash. And I can say txt welcome. You can see that the button has been moved to below this text view. But this is not entirely what we want. We also need to center it horizontally. For that once again, we can say center horizontal and we can pass through. Let's also add a margin top. Let's say margin top. And let's pass, for example, 15 dp. That's much better. For both of these text view and button, you have another attribute called background. Let's also see that we can say background. And for example, we can pass a color. Let's pass the first color. You can see that the background color of this button has been changed somehow. You have the same option for the text view. If you want, you can apply that. I think this is an ugly color. So I'm going to change that. Let's see the other colors. Let's select this primary dark. Nope, this is not my color. Okay, this is better. You can always add the hexadecimal value of the color that you want in here as well. For different UI elements, we also have another attribute called on click, we have seen the use of this on click when we have created our first Android application. You can guess it, we are going to set an event listener for this button. And we are going to say what happens when we click on this button. So let's say on click, and let's pass the name of some method. For example, I'm going to say on hello, we can click right now, you can see that we are getting this red warning. And that's because we need to create this method inside our Java file. Let's finish creating our button by using this slash. And let's create this method inside our main activity that Java file. So let's open that file. In order to open some files, I can always switch to this project thing. Or alternatively, I can press the shift key two times, you can see that when I do that, I have a search menu. For example, in here, I can search for main activity. And if I press enter on the first option, we can open our main activity that Java file. This is just a helpful shortcut that we can use from time to time. Okay, we need to create the on click method inside the scope of our class and outside of the scope of this on create method. If you remember the signature of that method, it was something like this, we needed to set the access modifier to public, the return type was void. The name of the method is going to be exactly the name that we have passed in our XML file. Let's say on hello between clicked for the inputs of this method, if you remember, we had a view object with capital V. This view object is not important right now, we just need to know that we need to receive it as the input of this method. Let's pass view as the name of this input. And in here, we can have the logic to handle the click listener on our button. Also, if you have noticed when I added this view, a new import has been added above in here. Let's also minimize this import for now. This is one way of creating event listeners for buttons and also for that matter for different UI elements. We have other options in which may be more useful from time to time. They will take a look at them in a minute. But in here, let's quickly write some logic. For example, in here, I'm going to change the text of my text view. For that, first of all, I need to have access to my text view. I can say text view. If you take a look at your imports, you will see that this text view from the widget package has been added to our class. If you remember, I said that equivalent to every UI element, you have a class in Java. Okay, let's name this text view txt welcome. And let's initialize it. You can initialize it like this, we can say find view by ID, like this set content view method, this find view by ID is also another inner method inside every activity. Right now, this find view by ID method is waiting for some integer. And the integer is going to be the ID of our element. And here is how we can pass our ID. For example, we can say r dot ID. Once again, if you remember, I said that this are in here stands for resources, it's a special class in Java, and it will give us the access to all of the static contents in our project. So let's say the r dot ID dot, let's pass the ID of our text view, which was txt welcome, we believe. Now that we have access to this txt welcome, we can do some operation on that. For example, I can say txt welcome dot set text, we have seen this method previously. And inside this method, I can pass a text. Let's say hello again. Let's run the application and let's see if we can change the text of this text view. I'm going to run the application on pixel three API level 29, you can select whichever device that you want. Okay, right now, we can see the layout that we have created so far, which is honestly ugly. But don't worry about that, we are going to work on on our designing power. If I click on this button, you can expect what would happen, we are going to change the text from hello, what to hello again. Okay, there was nothing special in this video, I just wanted you to have another look at what we have done in the first section of this course, which was to create your first Android application. I just wanted you to have a better understanding on what you have done in that video. Also, we have learned about some other attributes in here as well. Okay, in the next video, we are going to continue our talk on different UI elements. For example, we will talk about edit texts. And also, we will talk about different ways that we can implement and click the snare for our button. Okay, see you in the next video. In the previous video, we have seen one way of creating an on click listener for our button. There are two more ways. And in here, we are going to see them. For example, in our layout file, let's quickly create a button. But before that, I'm going to change this constraint layout to a relative layout. Let's delete this text and let's create a button. Let's say wrap content and wrap content for the bits and wait, let's give it a text. For example, let's say, say hello once again, let's say center parent. And let's pass through we have seen this in the previous video. So I'm not going to discuss them. Also, we are going to need an ID, let's say, btn hello, you can pass whatever you want. So in the previous video, when we wanted to create an on click listener, we would have add this on click attribute. In here, we needed to pass the name of the equivalent method inside our Java file. But in this case, I'm going to use another approach. And for that, I'm going to delete this attribute. Okay, let's switch back to our Java file. And let's define this button. Inside my uncreated method, I'm going to say button. Let's name it btn hello is equal to find view by ID. Let's say r dot ID dot btn hello. For setting an on click listener for your button, you can use the equivalent method, you can say btn hello dot set on click listener, you can see that we have this method. Inside the parentheses, we need to pass an interface. In this case, it can be new view dot on click listener. If you take a closer look, you can see that this is an interface. Okay, now that I have passed this new interface in here, this on click method has been overrided. And inside this on click method, I can write my code. For example, if I want to print something into my console, I can say print. Let's just say hello. Let's run the application and let's see if we can see any result. I have opened this run panel in here. And if I click on this say hello button, you can see that hello is being printed in here. So this way by using this set on click listener method on our button, we can define the on click listener. Inside the parentheses of this method, we have passed this new view dot on click listener method, but there is also another way. Let's quickly see that. So instead of passing a new interface in here, I'm going to implement this exact interface in the declaration of my class. If you remember from the Java session, we can implement interfaces like this, we can say implements, let's say on click listener. There are two options in here. The first one is coming from Android dot view dot view package and the next one is from another one. We need the first one. When we implement interfaces, we also need to implement their methods. We can do that by pressing Ctrl plus I, we need to implement this on click method. As the input of this on click method, we are getting a view object. And now we can use this view in order to create different cases. For example, we can say switch on view or V in this case, it's his name. Let's say dot get ID. We are going to create our switch depending on the ID of this view object. And after that, I can say case, in case it's our dot ID dot btn hello, the exact ID that we have passed in here. In case this is the ID, let's write some code. For example, I'm going to copy this line of code from here. Let's also add a break and also a default case. Now that I have implemented this method at the declaration of point class, and also I've implemented this on click method, I can remove this between hello dot set on click listener method. Let's write it once again, let's say between hello dot set on click listener. This time, instead of saying new view dot on click listener, I can say this. If you remember from the Java session, this refers to the current object. Right now we are inside this class, which implements this interface. So this method down in here is happy with accepting this as the input. So how's the flow of things in here? First of all, we are setting the on click listener and we are passing the class itself as the interface. Whenever the user clicks on this button, this on click method will be triggered. A view will be passed to this method. In this case, it's a button. Later on, we will talk about that why a view and a button are equivalent. We are getting the idea of this view and we are creating a switch statement. In case the idea of this view is r dot id dot ptn hello, the idea of our button, we are printing this hello in here. If we had other buttons or for that matter other views, we can create different cases for them in here as well. Let's run the application once again and let's see if we can see the same result. Once again, if I click on this say hello button, we can see that hello has been printed into our run console. Also, I don't like to calm down in here inside this run in order to test my application. Instead, I'm going to show something to the user in our application. For that, instead of using this system dot out dot print line, I'm going to use another option called toast messages. Let's search for toast messages and let's see what those are like. So basically a toast message is something like this. This is not a permanent message and it will be disappear after a period of time. Let's quickly see that how can we define this in our code. So in here, I'm going to delete this line of code. Instead, I'm going to say toast. You can see that I have two options in here. The first one is a Java class, but the second one is a template. Basically, templates are provided by the ID that we are using. For example, in here, if I select this template, you can see that this whole line of code has been created. So we are using this toast class in which has this static method. Inside this static method, we are passing a context, whatever that is, we will talk about context later on in the course spot for now. So we are using this toast class in which has this static method called make text. We have three inputs for this make text method. The first is a context. Don't worry about the context yet. We will talk about that later on in the course spot for now. Just know that every activity in Android is a context. And because right now we are inside an activity, we can pass this as our context. The next input is a text. Basically, this is going to be the text that we are going to show. For example, let's say hello button clicks. After that, we have this constant in here, which is toast dot length short. Basically, this is just a constant for determining how long do we want our toast message. For example, if you want to see the other time periods, you can say dot, we have length long and length short. Let's select length short. And after this make text method, we have this dot show method, which you can guess is going to show our toast message. So before this, we have created our toast message. And after that, with the help of this show method, we are showing it to the user. In here, we have used the template. But if you don't want to use the template, you can always create your own toast message. You can say toast. This time let's select the first one. Let's say make text for the context let's pass this. We need the text in here, let's say second text. And after that, we need a constant let's say toast dot length long dot show. Basically, these two lines of code are the same. We just can't create them with the help of the ID. Okay, this time, let's run the application and let's see what would be the result. So this time, if we click on this say hello button, you can see that some toast message is showing down in our screen. And after that, it will be disappeared. I think toast messages is much better for our testing purposes. It's much better than this run paint down in here. Okay, let's close the application in here. And let's talk about this view that we are receiving in here. In a logical way, instead of this view, we need to receive a button, because we are creating our switch statement based on the ID of our button. But there is a point in here. If you press down the control key and if you click on this button class, once again, if you press down the control key and if you click on the class that this button is extending in this case, this text view, you can see that this text view class is extending the view class. It means that after two times of inheritance, our button is somehow a view. We just extended the view class two times. So at the end, a button is a view. If you remember from the Java session when we have talked about inheritance, for example, when we extended the animal class, and we created our dog class, we said that a dog is an animal. In this case, a button is also a view. For that matter, every UI element is a view, like text views, edit text, and also other UI elements that we will talk about later on in the course. So this in here proves that we can create an unclick listener for our text view and other UI elements as well. So this unclick method is not limited to our button. It's also worth mentioning that when you create your unclick listener this way by implementing the interface in your class declaration, you just need to create one unclick method for all of your UI elements. For example, if I had another method down in here, I could have passed the idea of that button to another case. And inside that case, I could write my logic. So I can have as many methods. And for that matter, as many unclick listeners for my view elements. Also, this unclick listener is one of the available event listeners. We have others as well. If you want, you can take a look at them by saying btnhello.set, let's say set listener. And you can see a lot of these options we have on click listener, we have on drag listener, you can guess the meaning of each one of these, we have set on hover listener in case if we hover over our button, we also have another useful one on long click listener, it means that if we long press on a button, let's quickly see this one. This time instead of implementing the on long click listener in my class declaration, I'm going to use the easier way by saying new view dot on long click listener. This one, you can see that in here we have a Boolean as the return type of this on long click method. This Boolean is just a callback indicating if we have long pressed our button. If you want your code inside this method to work fine, you need to pass through. Let's write some logic in here. For example, I'm going to say toast. Once again, we can see this context in here, but the format is somehow different. Because right now we are inside an interface, we are inside this online click listener interface. If we pass this, this will refer to the current object, which in this case is an interface, but interfaces are not context, and we need to pass our activity. For that matter, we can say main activity dot this, don't worry about this main activity dot this, if you didn't get that, I will talk about that later on in the course when we have talked about activities. But for now, just let's pass this as our context and let's create a text. Let's also change the time period in here to length long. I think I need to delete this case from here as well. Okay, let's run the application. So like before, if I click on this button, and you can see that we are seeing this message Hello button clicked, which is a simple on click listener for our button. But if I press my button and hold the key, you can see that we are seeing this long press message. So this time, instead of this on click, we have executed this set on non click listener method. You can also check the other event listeners for your view items later on in the course, we will check a few more of them. But in general, you can say btn hello dot set listener. And you can use each one of these that you want. Okay, let's move on from the event listeners. And let's define another UI element in our layout file. Before this button, I'm going to define an edit text, let's say edit text. For the width, let's say match parent this time. And for the height, let's say wrap content. You can see that it's something like this. If you click on that, this is our edit text. Let's also add a text in here. Let's say name. And name has been added. That seems to be better. Let's also center our edit text, I'm going to say center in parent. And I'm going to pass through. But right now we have a conflict between this edit text and our button. For that, I'm going to move this button to below this edit text. But before that, I need to give an ID to this edit text. Let's say ID, let's just name it edit text name. Let's finish creating this edit text. And down in here, inside our button element, I can say below or to be specific layout below. And I can pass the ID of my edit text before that I need this add plus ID slash, let's say edit text name. So this way we have moved this button to below our edit text. I think it's better to have some margins. That's a margin top. And let's pass 20 dp. That seems better. Okay, let's switch back to our edit text. I don't like the text inside my edit text because if I run the application. And if I try to type something in here, first of all, I need to delete this text. For that instead of this text attribute, I'm going to use another attribute called hint. I think we have hint at the first videos of this course. Let's just pass name. This time it's much better. So whenever I click on this edit text, and if I try to type something, this name hint will be disappeared. Also, if we want to decrease the widths of our edit text, we can pass a number in here. For example, we can say 250 dp, which is better in this case. But if you remember, I said that it's never a good idea to pass your widths and height manually, because in different screen sizes, you cannot predict the size of your different elements. For that once again, I'm going to change this font to match parent, I believe it was. And after that down in here, I'm going to add two margins. Let's say margin left to 50 dp. For example, I think it's better to change it to 100. And also let's add a margin right of 100. Adding different margins is one way, but we also have another way. Let's we can see that as well. So I'm going to delete both of these margins. Instead, at the declaration of my relative layout inside the opening tag, as you can see, this is our closing tag. And this is our opening tag. Inside the opening tag, I can define an attribute called padding. Once again, like margins, we have multiple paddings, we have padding bottom, padding left, right top. Let's just use padding in here. And let's pass 100 dp. So as you can see, the size of our edit text has been changed. The padding and the margin are a lot like each other. Just there is one difference. When you are adding a margin, for example, in the previous case, for our edit text, the margin would be added from the end of our element, for example, from here to the end of our screen. But when you are adding a padding, that padding will add some space from inside your element. For example, in this case, the element is this relative layout itself. And we have added the padding from inside the relative layout. So once again, margin adds the space from the outside of the element, but the padding will add the space from the inside of the element, you can use both of them depending on your need. Let's see what other attributes do we have for our edit text. Like a text view, we can change the color for this edit text as well. For example, I can say text color. And let's pass this color that we have in our color resources. Right now, we do not have any text inside our edit text. But as soon as we type something, the color will be changed to this color that we can see in here, we can also style the text by saying text style this one. We can say italic both normal. Or if we want to use two styles at the same time, we can add a pipeline. There is another attribute that we didn't have in text views. And that's called lines. For example, there are times that you may need a bigger input from the user. You can specify the number of lines in here. For example, if I say four, for example, now our edit text is going to accept four lines of inputs. In this case, I'm not going to use these lines. So I'm just going to delete that. The default number for different lines is one. With edit text, you can also define the kind of input that you are going to receive. For that, you can say input type this one. And you can pass one of this. For example, if it's a date that I'm going to receive, I can pass a date or maybe this date time. If I'm going to receive a number password, I can pass this number password. If it's just a simple text, I can pass this text, we have other options, for example, for different emails, and all sorts of options that you can see in here. For example, if I select this text password in here, and if I add some text instead of this hint, let's say text, you can see that when I type the text, it will change it to these dots. Probably you have seen this on your phone when you are trying to log into some application or some website, this will add some level of security. Also, when you define it to email, this text email address, when the user clicks on your edit text, the keyword will be prepared to type the email address. I'm not going to define any specific type of input. So I'm going to delete this one in here as well. And let's also change this one to hint. Okay, I think that's enough about edit text, we have all sorts of different options in which you can check them from here. As you can see, there are 1000 of these attributes. Okay, below this button, let's also define a text view. I'm going to say text view, wrap content and wrap content. Let's give it an ID. Let's say txt hello. After that, maybe some placeholder text. Let's just say hello. Let's also move it to below this button. I'm going to say layout below. Also, it's worth noticing that if you are not using a relative layout, for example, if you are using a constraint layout, you do not have this layout below option. This is one of the features of this relative layout. We will talk about that in a future videos. Okay, in here, let's pass the ID of our button. Let's say btn hello. Also, let's move it to center horizontal. And let's add some margins. margin top of maybe 30dp. Let's increase the size of this text by saying text size. And by passing, I believe 20 would be fine. I'm almost done with this layout file. Let's just go top in here into this code. And let's select this reformat code in order to rearrange our code so that if I publish the code, you would have the same code that I'm writing in here. Okay, let's switch to the main activity and let's define all these three items. Let's say edit text. Let's name it edit txt. I believe it was name is equal to find view by ID r dot ID dot edit text name. Let's also define our text view. Let's name it txt name is equal to find view by ID r dot ID dot txt name or txt hello was I think it's better to change the name to txt hello. But right now there is a problem with our code. We are instantiating our UI elements inside this uncreate method, which once again is the starting point of our application. But we are going to need these two UI elements inside this on click method. Because we are going to change the value of this text view, whenever the user clicks on our button. And if I try to have access to this txt hello for example from inside this method.  You can see that the ID cannot resolve this symbol. It means that inside this method we do not have access to this text view. I'm not sure if I've mentioned this in the Java section, but the problem in here is occurring because of the scope in Java. So right now the scope of this text view is inside this uncreate method. And outside of that we cannot use that. If we want to have access to this text view inside both of these methods, we can say something like this, we can declare this text view in the declaration of our class as different fields. For example, above in here I can say private, let's say text view, this one. And let's name txt hello. But right now in here if I try to instantiate my text view, for example, if I say find view by id and I pass my ID, because this part in here is not a part of this activity life cycle. We are not going to instantiate our text view at all. We need to instantiate our element inside this uncreate method, which is once again the starting point of our application. So this way we are separating the declaration and instantiating of our UI element. I can put a semicolon down in here. And this time instead of defining the whole text view, I can use this field, I can say txt hello. This one is equal to find view by ID or dot ID dot txt hello. Now that I have declared this txt hello as a field, I can use it in both of these methods. For example, in here I can say txt hello dot set text. Inside this set text method, I'm going to need the text of this edit text. For that, I'm going to define this edit text as a field as well. So let's say private edit text. Let's name it edit txt name. And then in here, let's remove this edit text. Now I can pass this edit text, for example, to this set text method. Let's say hello, plus the edit text dot get text. If you remember from the first videos of this course, I said that we need a two string method as well. We have seen this two string method in different Java classes. It's just going to generate some text. In this case, if we delete this two string, and if we just pass this edit text name dot get text, if you press down the control key, and if you hover over this get text method, you can see that the return type of this get text method is an editable. We do not know what that is yet. But we just know that this is not a string. And in here we are going to need a string. For that, we need another level of conversion, we need to convert the editable to a string. So we can use this two string method. Okay, I think after a lot of talking, our application is ready. Let's just run the application and let's see if we can see the behavior that we wanted. Let's type something in here. Let's say Mason. And let's press this button. Hello, Mason, our application is working fine. And hopefully, we understand each piece of the code that we have wrote so far. Few minutes earlier, I said that different UI elements extend the view class. We have seen that for this button. Let's also check the case for this edit text and text view. We can first down the control key, and you can click on this edit text class. We can see that it's extending the text view. If you click on this text view once again, by pressing down the control key, you can see that it's extending the view class. The same is for the text view itself. If you press down the control key, and if we click on the text view, you can see that it's extending the view class. Now that we know both of these are extending the view class, we can create an on click listener for each one of them.  If you want, we can create another case for each one of them. For example, down in here, for the edit text, I can say edit text name dot set on click listener. You can see that we have this option in here. And because we are implementing the on click listener interface inside our class declaration, I can pass this in here. Later on inside the on click method, I can create another case. For example, let's say case or dot ID dot, let's say edit text name. If that's the case, let's toast some message. Let's say attempting to type something. Before I run my application, I'm going to delete this toast because I'm not going to have a conflict between different posts. So this time, if I click on the btn hello, the text is going to be changed to hello plus the text inside our edit text. And if we click on the edit text itself, we are going to see this toast message. By clicking on the edit text, you can see the toast message down in here, that seems to be perfect. Okay, I think that's enough for this video. In the next video, we are going to talk about some other UI elements like checkboxes, radio buttons, and radio groups, and also progress bar. Just before I finish off this video, I'm going to say that I will upload the source code at my website at maycode.org slash codes. So feel free to check that if you need. Okay, see you in the next video. The first UI element that we are going to talk about in this video is checkbox. Let's quickly see that in our layout file. Before everything, I'm going to delete this text to you and also I'm going to change the constraint layout to relative layout. Down in here, we can create our checkbox like this, we can say checkbox for the widths and height, let's say, wrap content. You can see that this little checkbox has been added in here. If you want, we can add a text to this checkbox as well. Let's say text, I'm going to enter the name of a movie. So let's say Harry Potter, we can have an ID as well. Let's say ID. Let's name it checkbox Harry. So basically a checkbox is a box that we can check or uncheck. For example, in here, we have an attribute called checked. We can pass through or false for this. For example, if we say true, you can see the checkbox will be checked. Let's quickly add two more checkboxes. Basically, I'm going to copy and paste this one two more times. But this time, first of all, I'm going to change the text to let's say the matrix. And also let's change the ID as well. Also, I'm going to add another attribute. I'm going to move this new checkbox to the right of my previous checkbox. For that, we have an attribute called to write off layout to write off. Let's pass the ID of our first checkbox, let's say checkbox Harry. Let's also add a margin left, you can say margin left. And let's pass 15dp, for example, let's copy and paste this one once again. Let's change the text to let's say Joker. Let's also change the ID, let's say checkbox Joker. And also let's move it to the right of checkbox matrix. So this way, as you can see, we can have multiple checkboxes, you can check and uncheck them by default by setting an attribute of checked. Let's change the value for the Joker to let's say false. If we want to center this checkbox horizontally, we can add a margin for the first one. For example, we can say margin or margin left. And we can pass 25 for example. But as you can see, this is not accurate instead. What we can do is that we can create another relative layout and move all of these three checkboxes into that relative layout. Let's quickly see how we can do that. So inside my first relative layout, I'm going to create another relative layout. Let's say relative layout for the bits and height, let's say wrap content. Let's also give it an ID, let's say ID. I'm going to name this one movies relative layout. Now if I want, I can center this relative layer, I can say center horizontal, let's pass through. Besides that, let's add a margin top, let's say margin top of 20 dp. In other UI elements normally in here, we could have finished creating our element by adding a slash in here. But in this case, I'm not going to do that. Instead, I'm going to finish creating my first tag, my opening tag. For that, I can add a left anchor bracket. And as you can see, this closing tag has been created automatically. Now I can put my three checkboxes inside these two tags, the opening tag and the closing tag. Let's move all of them to inside those two tags. If you noticed, you can see that these three checkboxes are now centered horizontally. And also they have a margin top of 20 dp. That's because the parent layout, which is this relative layout has these two attributes. So inside XML file, you can have relative layout nested inside another relative layout. Okay, now let's see what can we do with these checkboxes inside our Java code. For that, I'm going to open my main activity dot Java file. If you are wondering, I'm pressing double shift in order to see this search menu. First of all, in here, let's define our checkboxes. Once again, I'm going to define them as fields, let's say private checkbox. For the first one, let's say checkbox Harry. In here, I have two options. First of all, I can create my checkboxes line after line. For example, I can say private checkbox. And for the second one, we can name it checkbox matrix or alternatively because the kind of these objects are the same, I can add a common here and I can define my second checkbox. For example, I can say checkbox matrix. Let's also define the checkbox Joker. And later on inside our on create method, let's instantiate them. Let's say checkbox Harry is equal to find view by ID or dot ID dot checkbox Harry. Similarly for the next two, if you want, you can have a click listener for each one of these checkboxes. For example, I can say checkbox Harry dot set unchecked changed listener. This is the listener that I need. Let's pass new unchecked changed listener. Once again, you can see that this is an interface. The exact name is compound button dot unchecked changed listener. Inside this interface, we have this unchecked changed method in which we receive two elements, the compound button itself, which is our checkbox in this case. And also this is checked Boolean. So in here, I can create an if statement, I can say if is checked. Let's show a toast message. Let's say you have watched Harry Potter. But in the else case, let's show another toast message. This time, I'm going to say you need to watch Harry Potter. Before I write the logic for the next two checkboxes, let's run our application and let's see what would be the result. Right now, Harry Potter is checked, but we are not seeing any toast message. And that's because the on click listener in here is going to react when we click on this checkbox. For example, if I uncheck this checkbox, we can see that you need to watch Harry Potter. Let's also check it. You have watched Harry Potter. But what if we want to see the toast message when we run the application without any clicking on our checkbox? For that, we have an option in here before setting this on click listener. We can check that if our checkbox is checked or not. For example, for that, I can create an if statement, I can say if checkbox Harry dot is checked. As you can see, this method is going to return the Boolean and as its name applies, it's going to check that if our checkbox is checked or not. So if it's checked, I'm going to show the toast message, let's copy it from here. But in the else case, I'm going to show another toast message. Once again, we can copy it from down in here. Let's run the application. As you can see, when I run the application without any clicking, I saw the toast message. So from time to time, this is checked method is going to be useful. I'm not going to define an on click listener for the next two checkboxes, because I'm sure that you got my point. Also, like in the previous video, instead of passing this new compound button dot unchecked changed listener, you can implement this listener at the declaration of your class, for example, in here. And after that, you need to override this unchecked changed method. After that, you can pass this instead of this new listener in here into your set unchecked changed listener method. But once again, I'm not going to go through that. Instead, let's switch back to our activity main dot XML file. And let's talk about another UI element. But before that, I'm going to minimize this relative layout. The next UI element that I'm going to talk about is radio button. Let's say radio button, this one down in here, for the width and height, let's say wrap content, you can see that this is this circular button. Let's move it to below our relative layout. I can say below or layout below. I think it was named movies relative layout. Let's also add two margins, margin left off, let's say 25 dp and also margin top of maybe 20 dp. Like checkboxes, we can also have texts. Let's say text for the text, I'm just going to say married. This is going to determine the marital status of the user. So that's what this text is about. Let's also give it an ID. Let's say radio button married. Like checkboxes, if you want to check this married radio button, you can say checked, and you can pass through. If you want to have multiple radio buttons, you can say something like this, you can copy and paste this radio button, but there is going to be some problem with our code. Before that, let's change the text and idea of this radio button. For example, in here, let's say single. And after that, let's change the ID to RB single. Let's also move this one to right of our radio button married. Let's say to write off radio button married. So the purpose of radio buttons in programming is that among multiple radio buttons, you can only select one. This is the difference between radio button and checkbox. When you have checkboxes, you can check multiple checkboxes. But when you have radio buttons, you can only have one radio button checked. But as you can see, our program is not functioning right in here. And that's because we need to group these radio buttons. And we can do that by creating another item in here called radio group. Before that, I'm going to delete these two radio buttons. Let's say radio group, this one, once again, for the bits and height, I'm going to say wrap content. Let's move it below our movies relative layout, layout below ID movies, real layout, a margin top maybe of 20 dp. And also let's center it horizontally, center horizontal to true. Once again, because I'm going to put some radio buttons inside this radio group, I'm not going to finish creating this radio group. Instead, I'm going to finish creating this opening tag by adding a left anchor bracket. Like a relative layout, you can see that now we have an opening tag and also a closing tag. Inside this radio group, I can have my radio buttons, let's say radio button, wrap content and wrap content. Let's give it an ID, let's say radio button married, a text maybe. And also let's change its default value to checked. Let's say checked through. Now we can have multiple radio buttons. Let's copy and paste this one. So as you can see, I never moved this radio button to below our first radio button, but it's moved automatically. If we change the ID to for example, RV single, you can see that even the both of these radio buttons are checked, but only one of them is showing the checked value. So when you are using radio buttons, only one of them can have a checked value. For that, I'm going to delete this checked attribute from the second radio button. And also I'm going to change the text to single. Let's also add another one. For the ID, let's say radio button in rail. And let's change the text to in a relationship. Let's also delete this checked attribute. Right now, our radio buttons are vertical.  You can have an attribute called orientation in which you can pass horizontal or vertical. If I pass horizontal, you can see that now we have our radio buttons horizontally ordered. I think it's better to have some margins between these radio buttons. So let's quickly add some margins. Let's say margin left of maybe 15 dp, I guess. And let's add another one down in here. Okay, now let's see how we can use these radio buttons and radio group inside our Java file. For that, I do not need to define my radio groups. Instead, I just need to instantiate my radio group. Let's say radio group. Let's name it radio group marathon status. But I believe I need to give an ID to this radio group. And I think I didn't do that. Yes, in here, I can say ID. Let's say radio group marathon status. In our uncreated method, let's also minimize this if statement and also this on click listener. And after the check boxes, let's instantiate our radio group. We can say radio group marathon status find view by ID or dot ID dot radio group marathon status. We can have an on click listener for this radio group by saying radio group dot set unchecked change listener. So this method is going to be called whenever we change the radio button selected. And inside the parentheses, you can guess I can pass new unchecked change listener, radio group dot unchecked change listener, which is an interface and it will create this unchecked change method. One of the inputs of this method is this checked ID in which we can use that. For example, we can create a switch statement on this checked ID. And we can create our cases, we can say in case it's our dot ID dot, let's say radio button married. So basically this checked ID is the ID of our radio button, whichever is selected. So in case if the user selects this radio button married, I'm going to show a toast message. Let's just say married. But in case it's our dot ID dot, let's say radio button single. Let's show another toast message. Let's also add another case for RV in well. As usual, let's also create our default case. And let's add a break. Let's run our application. And let's see if we can see any result. Right now this married is selected. If we select the single, you can see that the married one has been unchecked. And also we have saw the toast message. Let's select another one. You can see the toast message, which is perfect. Right now we have an on click listener for our different radio buttons inside this radio group. And depending on that button, we are doing something. For example, in this case, we are showing a toast message. But if you want to do something without clicking on these buttons, we can do something like this in our code. So before this set unchecked change listener, I can say radio group my status dot get checked radio button ID. As you can guess, this will return the ID of the checked radio button. And I can save that inside an integer. For example, I can say int checked button. After that, I can create a similar switch statement based on the checked button. So let's say switch on the checked button. And let's create our cases. I'm going to copy and paste the cases from here. Let's run the application. And let's see if we can see any result. As you can see, we are not clicking on any of these radio buttons, but we just saw the married toast, that seems to be perfect. In some cases, we might need to check for the checked radio button before clicking on them. I'm going to talk about another UI elements in this video. And that's called a progress bar. Let's quickly see that as well. Before that, I'm going to minimize this radio group. After that down in here, let's say progress bar, wrap content and wrap content. Let's give it an ID, let's say progress bar. Let's also center it, let's say center in parent, and let's pass through. Let's run the application and let's see what our application is going to look like with this progress bar. As you can see, an infinite progress bar is constantly circulating. This might be useful. For example, when we are downloading something from the internet, in order to make our progress bar visible or unvisible, we have an option. For example, in our XML file, if we want it to be not visible, we can say visibility, and we can pass gone. Also, we have this visibility attribute for almost all of the other UI elements. If we want, we can change it to gone, but by default, it's visible. Let's change it back to gone. Later on in our Java code, we will change this visibility to visible. And whenever we are done with it, we are going to change it to gone. Let's see how can we do that in our Java code. Before everything, let's minimize all of the extra methods and codes. And let's instantiate our progress bar. Above in here, I'm going to say private progress bar. Let's name it progress bar. And down in here inside the uncreate method. Let's say progress bar is equal to find view by ID R dot ID dot progress bar. I do not have any button in here in order to change the visibility of this progress bar. Instead, I'm going to change its visibility whenever the user clicks on the radio button single for that inside the radio group my total status listener inside the switch case, in case if it's RV single instead of sharing the toast message or after the toast message, I'm going to say progress bar dot set visibility and I can say visible. Instead, whenever the user clicks on this radio button in rail, I'm going to change the visibility of my progress bar to gone. Let's copy this line of code. And after the toast message, let's paste that and let's just change this constant to gone view dot gone. Let's run our application. Right now we cannot see any progress bar because in our XML file, we have added the visibility attribute and we have set the gone value. In case if we click on this single, you can see that we are seeing the progress bar. In case if I click on this in a relationship, you can guess the visibility will be changed to gone. There is also another form of progress bar. Let's quickly see that as well. So in my activity main dot XML file, I can define a style for my progress bar. Before that, I'm going to change the visibility once again to visible in order to see the progress bar in our preview. And after that, let's say a style. If I type horizontal, we can see this styling here, widget dot app compact dot progress bar dot horizontal. Let's change the width to match parents so that we can see it better. And also in here I have another option I can say progress. And I can pass an integer. For example, I can say 30. You can see that the progress of this progress bar has been changed to 30. This type of progress bar is extremely useful when you want to show the progress of something for example, downloading a file. Sometimes you may want to show that how much of the file has been downloaded. You can also set a maximum for your progress. For example, you can say max, and you can pass 100. Let's also add some margin left and right. margin left of 20 dp, margin right of 20 dp. Okay, now let's switch back to our job of oil and let's see what we can do with this progress bar. Before everything I'm going to delete these two lines of code, because I'm not going to make my progress bar visible or unvisible. Let's also minimize the switch statement and this whole listener for our radio group. After instantiating our progress bar, I'm going to create a threat. If you remember from the Java session, this is the simplest way that we can create a threat. Let's say threat threat is equal to new threat, we needed to pass a runnable, let's say new runnable.  Let's say for int i is equal to zero, i less than 10, and i plus plus. For every time of looping, I'm going to increment the progress of this progress bar by 10. So let's say progress bar dot increment progress by, as you can guess, we need to pass an integer in here. For example, let's pass 10. After that, let's freeze this threat for half a second. If you remember from the Java session, we have used this way, we set threat dot sleep. But this way, there is a probability that we create interrupted exceptions. For example, you can see that we have an error in here and the error says that unhandled exception. There is also another way in Android, I'm going to use that in here, I can say system clock dot sleep. So this way, we don't need to create the try catch block. This wasn't available in Java, the system clock is coming from Android dot OS, we can check that by take a look at these imports. This one in here, Android dot OS dot system clock is the one that we just imported. After creating our thread, we just need to start it, we can do that after the definition of our thread, we can say thread dot start. So in here, we have created this worker thread in which inside that we are creating a for loop and for each time of looping, we are incrementing the progress of our progress bar by 10. Also, after each time of looping, we are sleeping the worker thread for half a second. Let's quickly switch back to our layout file, and let's delete this progress attribute, it was just for demonstration. So if I remove this one, you can see that the progress will change to zero. Let's run the application and let's see if we can see the progress of our progress bar. As you can see, we are changing the value of this progress bar for every half a second. If you want, you can also get the progress of your progress bar at any moment that you want. For example, in here, you can say progress bar dot get progress. As you can see, this get progress is returning an integer, which is the current progress of your progress bar. Sometimes you may need that. And here is how you can get that. Okay, I think that's enough for this video. Once again, like previous videos, I'm going to upload the source code at my website at maycode dot org slash codes. Feel free to check that if you need. And also in the next video, finally, we are going to talk about the differences between a relative layout, a constraint layout, and also a linear layout. I think it's going to help you a lot with understanding the layout files better. Okay, see you in the next video. In this video, we will talk about different layouts in Android. Specifically, we will talk about relative layout, we will talk about linear layout, and also constraint layout. Let's start by talking about the relative layout. Like before, I'm going to switch to text view in my layout file. Let's also minimize this project thing. And let's change this constraint layer to relative layout. I'm also going to enable this preview so that we can see what we are doing. First of all, let's delete this text view. And let's start from the beginning. So inside a relative layout, all of our UI elements are somehow related to each other. It means that the place of every UI element is relative to another layout, or maybe it's parent. Let's quickly create a text view and let's see the relativity in action. Like before, I'm going to say wrap content for the width and height. Let's also specify a text. Let's just say hello world. As you can see right now, the text view is placed at the left corner of my screen. If I want, I can change that. For example, you have seen in the previous videos that I can center this text view vertically or horizontally. For that, I can say center horizontal. Let's pass through. You can see that it's being centered. And this is the relativity that I was talking about. When we set this layout center horizontal attribute to true, we are defining a relativity between this text view and its parent. Let's also center it vertically. Or instead of these two attributes, I can simply say center in parent. Let's also give an ID to this text view. Let's say txt hello. Now if I define another UI element, I can place that related to this text view. Let's quickly create a button in here. Let's say wrap content and wrap content for the width and height. Let's define a text. Like before, let's say say hello. Also, let's define an ID. Now if I want to move this button to below this text view, I can simply say layout below. And let's pass the ID of our text view txt hello. I can center this button as well. For example, I can say center horizontally. And let's pass through. We can also have a margin. For example, we can say margin top layout margin top and we can pass some number for example 15 dp. Inside a relative layout, you can also have another relative layout. Let's quickly see that for example, in here, I'm going to define a relative layout. And inside that relative layout, I'm going to put three text views. After that, I'm going to move this text view this hello wall to below that relative layout. So I can say relative layout. For the width and height, let's say wrap content, you can say match parent, like other UI elements, we can define an ID for our relative layout. For example, I can say first relative layout. So up until this point when we wanted to create our UI elements after setting the attributes, because I've used this backslash in order to finish creating our UI elements. But in this case, because we are going to put some other elements inside this relative layout, we need to create an opening tag. And instead of using the backslash, I'm going to use the left anchor bracket. By doing that, we also create this ending tag. And inside this opening and ending tag, now we can put our UI elements. For example, we can pass our text views. Let's say text view, wrap content and wrap content. I'm going to fast forward everything for these three text views. Like before, we can place our three text views inside this relative layout correctly. For example, we can move them to write up each other. So for this second text view, I can say to write off, let's say txt name, let's also add a margin left of 10dp. Similarly, for the third text view, let's say to write off our txt last name. And margin left of maybe 10dp once again. Once again, this whole relative layout is placed at the left corner of our screen. If we want, we can use the center vertical or center horizontal attributes for this whole relative layout as well. So inside the opening tag of this relative layout, I can say center horizontal. And let's pass through. Let's also add some margin top, let's say margin top. And let's pass 100dp. You can see that when I use these attributes on a relative layout level, all of the UI elements inside that relative layout respond to those attributes. For example, in here, we have centered them horizontally and also we have added the margin. Now that we have this relative layout, we can minimize that so that we can see everything better. And after that, we can move this hello text to below this relative layout. But before that, I need to delete this center apparent attributes. Once again, you can see that it moves to the corner of our screen. And because we have set the layout below for this button, it moves as well. So in here, I can say layout below, and I can pass the ID of my relative layout. It was first relative layout. Let's also center it horizontally and let's pass some margin. Center horizontal true margin top, let's say 10dp. So my point in here was that inside a relative layout or for that matter inside any other kind of layout, you can have other relative layouts as well. Some of these attributes, for example, this layout below are specific to relative layout. And if you're inside some other kind of layout, for example, a linear layout and also constraint layout, you cannot use this layout below. During the course, we will see that which of these attributes are specific to relative layout. Before we move to the next kind of layouts, let's also switch to this design view. At the left pane in here, you can see this component three. As the name of this component three applies, this in here is showing us the hierarchy of different elements in our UI. So for example, in here, we have this parent relative layout. Inside that we have this first relative layout and also txt hello and btn hello. Inside our first relative layout, we have three text views. Sometimes when you have a messy UI, you may use this component three in order to keep track of different UI elements. Okay, let's switch back to our text view. And let's start talking about linear layout. Before that, I'm going to delete all of these. And let's change this relative layout to a linear layout. Inside a linear layout, all of the UI elements will be placed line after line. Let's quickly see that I'm going to define a text view once again, wrap content and wrap content. Let's pass a text. Let's say hello once again. Let's also give it an ID. That's a txt hello. So the first thing that is different inside a linear layout is that you do not have the center in parent center horizontally or center vertically attributes when you use linear layout. For example, in here, I cannot say center in parent, you can see that it's not available. If you want to center your UI elements, you can have for example, a relative layout inside this linear layout. Let's move on from this part. And let's add another UI element. Let's say button wrap content and wrap content for the text. Let's say say hello. And let's give it an ID. Let's say btn hello. You can see that even though I didn't use any attribute to move this button to the right of our text view, it automatically placed the right of our text view. And that's because we are using a linear layout. Inside a linear layout, all of our UI elements will be placed one after another. If you want, you can change the orientation of this linear layout so that the UI elements will be placed one after another vertically. Let's quickly do that. So in the definition of my linear layout, I'm going to pass an attribute in here, let's say orientation, and let's pass vertical, you can see that they have been placed vertically. There is also another attribute that I'm going to talk about in linear layout. Let's quickly delete this button and let's define another text view for the text, let's say name. And for the idea, let's say txt name. Like before you have the margins, for example, you can say margin top and we can pass any number that we want. But there is also another attribute in here, before I talk about that attribute, I'm going to give some background color for these two text views so that we can see the difference better. So I can say background, and I can pass a color for example, this color accent that we have in our color resource. Let's also set a color for the first text view. Let's say background, and let's pass color primary this time. So the attribute that I'm going to talk about is weight. Let's quickly see that. We can see this layout weight in here, and we can pass any number that we want. For example, I can say 20. Let's also pass a weight to this second text view and we will talk about it. Let's say weight and let's pass 80. For example, you can probably guess the usage of this weight attribute right now. It will give our attribute some weight according to the numbers that we have used. For example, in this case, the entire weight of our layout is 20 plus 80, which is 100. And it will give the first text view 20% of the weight of the whole layout. It doesn't matter what number you put in here. For example, in here, if I say two, and for the second one, if I say eight, it will work the same. Also, you can see this space in here. That's because we have this margin top. If we remove that, you can see that the whole layout will be divided accordingly. So this weight was another attribute that I was going to talk about. If I change this orientation to horizontal, you can see that it's working the same. There is not much to this linear layout. I just wanted to show you that there is one linear layout with some specific attributes, for example, this orientation and this weight. And in this course, we are not going to use linear layout that much. It's for simple layouts, but you can use it if you want. Before I remove all of this, I'm going to say that inside a linear layout, you can also have other layouts, for example, a relative layout or a constraint layout. Okay, let's remove all of these. And let's talk about constraint layout. Before that, I'm going to remove this orientation because it was specific to linear layout. Okay, let's say constraint layout. We have two options in here, widget dot constraint and widget dot constraint layout. This is the one that we are going to need. For this constraint layout, I'm going to switch to design view because I think it's much better to work with the design view. First of all, I'm going to enable the blueprint so that we can see the difference if there is any, you can click on this blue stack in here and you can select this design plus blueprint. We will talk about the difference between this design mode and this blueprint in a minute. Okay, for working with the constraint layout, you can drag your UI elements like before you can drag, for example, a text view. And as soon as I did that, you can see that we have four circles at the four edges of our UI element. Let's zoom in a little bit. You can see that the four circles are at the four edges of our UI element. Let's zoom out a bit. I just wanted to show you the four circles. Also, the shortcuts that I'm using in here is control plus plus in order to zoom in and control plus minus in order to zoom out. Also, at any time, you can use control plus zero in order to have a feed screen. It's also worth mentioning that if you have a keyboard that has some numbers and keys for the calculator, the plus minus and zero from the calculator keys are not going to be helpful in here. So the shortcuts are going to be only useful for when you are using the upper numbers