‫Welcome back.
‫In this video we're going to learn about buttons in Android.
‫Buttons are user interface elements that allow users to trigger actions or events by clicking or tapping
‫them.
‫Buttons are fundamental components for user interaction and are commonly used for tasks like submitting
‫forms, navigating between screens, and performing various actions in Android applications.
‫To create a button, we start with the Xaml and I'll use the button tag.
‫The width would be wrap content, wrap content and you can see this is our button.
‫Then I need to specify the ID as usual ID my button.
‫Also, you can drag and drop it to here for example, and infer the constraints later on.
‫I'll talk about Constraintlayout and infer the constraints.
‫So these are the attributes for the constraint layout.
‫You can use many attributes for the button.
‫For example the text click me, you can see it.
‫This is the text of the button.
‫Also you can use text color for example black and many other attributes that we have in the views.
‫This is the ID my button.
‫Let's move to the main activity and initialize and declare the views and the button layout initiate
‫var my button.
‫It's of type button.
‫And we need to import the class of the button here in the imports Alt+ enter to import it.
‫Then we need to initialize the views here in the Oncreate view.
‫My button equals.
‫To find view by id the id my button.
‫If you have any error like this, just build and clean the project.
‫Sometimes Android Studio will generate unexplained errors.
‫Just build and clean the project.
‫In this case we created a reference to this button in Kotlin.
‫I can use this button to trigger actions.
‫So my button dot set on click listener.
‫You can pass a view dot onclick listener interface and inside it we have this lambda expression.
‫Or you can directly remove this and replace it with this lambda expression.
‫The set on click listener function is a method provided by the view class, which is the parent class
‫of all UI elements, including buttons.
‫In Android, it allows you to specify a function, in this case a lambda expression that will be executed
‫when the button is clicked.
‫The lambda expression enclosed in curly braces is the code that will be executed when the button is
‫clicked.
‫The set on click listener method is common way to handle button clicks.
‫In Android.
‫When a button is clicked, the code within the Lambda expression will be executed, allowing you to
‫perform various actions such as showing a message, navigating to another screen, or processing user
‫input.
‫In this example, I'll create and display a short duration toast message saying hello.
‫For example toast dot Maketext this you clicked me.
‫Toast dot length long dot show toast dot make text is a factory method that creates a new toast object.
‫This parameter is the context in which the toast will be displayed, and it's typically provided within
‫the activity or fragment, and in this case this refers to the context of the main activity.
‫Hello, you clicked me.
‫This is the message that you want to display in a toast and toast dot length long.
‫This is a constant that specifies the duration of the toast message.
‫In this case, it indicates that the toast will be displayed for a for a long time.
‫Show is a method called on the toast object, which makes the toast message visible to the user.
‫Let me run our application and here we go.
‫Let me click this button and here is the toast message.
‫Hello.
‫You clicked me.
‫Congratulations guys.
‫We learned about the buttons in Android and how to handle the click events on them.