2.2 Listening to Events From Activities
In this lesson, you will learn some different ways to handle events from Activities. You will see an example first and then try it out in your own app for some hands-on experience.
1.Introduction1 lesson, 01:32
2.Android Activities6 lessons, 41:06
2.1Activities as App Building Blocks02:33
2.2Listening to Events From Activities07:11
2.3Navigating Between Activities04:32
2.4Sharing Data Between Activities08:26
2.6Activities and Screen Rotation06:14
3.Conclusion1 lesson, 01:01
2.2 Listening to Events From Activities
In this lesson, we will be learning, how to listen to events from activities. Now what I mean is, on click of a button or on tap of the screen, or on long press, something has to happen. That is called event handling. Coming to an example of event handling, let's say this is our calculator application. On pressing 9, this is what we get, the event of pressing the button 9 is listened and nine is displayed on the screen. So this was a very basic example of event handling. Now we have values using which we can listen to events from the activities. The first we will talk about is, setting the OnClickListener in the button itself from that activity. Let us move to Android Studio and see how we can do this. Here I created the basic project using the Android activity and I'm in the project view right now. My application name is Activities Basic. Going to the app folder, in the source directory, in the main, Java, here we have our MainActivity.java, which is this file and then in the resource folder, in the layout, we have activitymain.xml. Now this is the default activity that was created when I created the project. We need to of course, modify it. Now let us make changes to activity_main.xml as per our requirement. We don't need this text box, so we'll remove it, we will implement the on click listener on the button, so let us drag two buttons here. And we will implement very basic functionality to change the background of our activity on the click of the button. So moving to the text tab of activity main.html, we will change constant layout to relative layout because this is a simple demo application and we don’t need the ConstraintLayout, we will give an ID to our RelativeLayout. For the first button, let’s give it an ID of button_yellow, it will change the background to a yellow color. And here, let us give the text of yellow. And for the second button, let's give the ID of button_white. We'll change it back to white on click of this button. Now since we have changed this layout, let's see design. And this is how it has become. Now, don't worry if you don't see the code exactly the same like mine. Just focus on the ID of the button and the text of the button. That's all that is important. Now since my layout is ready, let us move the MainActivity.java and start writing the code here. I'll first declare the relative layout. And then on the OnCreate method, let us initialize our widgets. Now we have an error here. It says, it cannot resolve symbol Button. Let us press Alt + Enter and our button widget is imported. Same way for the button of white color. Now it's time to set the OnClick listener to these buttons, buttonYellow.setOnClickListener(new_View.- OnClickListener(). And here, we need to overwrite the on-click method. Let us write the code to change the background to yellow color on clicking this button. To do that, relativeLayout.setBackgroundColor and we will give the color, similarly for the second button, we will set the background color to white. Now let us run our application and see what have we created. So here we are with our two buttons. One is yellow, one is white. We have set the listener for this and now when clicking this, we have successfully implemented the listener in our button. On clicking white, the background changes again. So this was the first way of implementing the listener from the activities. Now the second way is, we can add the event listener in the xml layout itself. Let me show you. In the Android studio, in our activity_main.xml, moving to the text tab, let us do this with our second button for button white. Now what you need to do here is on click, we will call a function. Let say SetBackgroundWhite. Now here we have an error which says, the corresponding method not found. Pressing Alt + Enter, we will select Create set background white in main activity. Yes, we need this. So selecting this, we have our function created in our main activity. So we have called the function on click of this button from the XML file and writing the code for this, we will change the background to white. And let me remove this because we don't need this any more. The first method was setting on ClickListener, which we have used for the first method. And the second method was, calling the function from the XML file, which we have used for the second button. Now on running our application, let's see, here we are, we have yellow, yes, this works fine. And now we have white and yes, congratulations. You have implemented the second way for handling the events in Android. And yes, we have a third method too. This is implementing OnClick Interface in the Activity class. Now, I would like you to implement this yourself. So that was all for this lesson. In next lesson, we will be learning how to navigate between the activities in our own app.