Are you concentrating now?
Support our courses! Buy the Pickup Line App!
Let’s get down to business and make our app interactive. So we’ve got one button hooked up, but it doesn’t do anything. That’s where event handling comes in. Event handling means we’re going to do something when an event happens, in this case, a button click. We do this by adding a listener, which is something that tells this button to hang out and listen for something, once the thing happens we can trigger something else, in this case we’ll change the text of the button. If the listener is triggered it runs the callback method.
Think of your stoner friend who hangs around and listens for you to smoke weed, that triggers him to ask you for some. His mooching ability is the weed listener and when he asks you to smoke him up, that’s the callback method.
Let’s make our clean button change our dirty button text. As long as we can select another view in android we can change it’s properties, doesn’t matter what type of view it is.
We’ll add a line below where we set our cleanButton variable, except now we grab dirtyButton with it’s ID and the R class.
Now below this we’ll say, "yo cleanButton listen up, if you get clicked you gotta do something." We do that using the cleanButton we created, so type cleanButton dot set On Click Listener. That’s how we tell cleanButton to start listening, but we need a little more code to give it a callback method. We start typing new View OnClickListener, but Android studio is like the NSA, it know exactly what you’re up to and gives you an autocomplete option. If you hit tab Android Studio gets freaky and writes a whole block of code for you.
What the hell is this! Well View Onclicklistener is an interface and this code defines an anonymous inner class and creates an instance of it. If your eyes just glazed over and you started drooling we don’t blame you, we could spend an entire course dissecting this code. For now just know all this code let’s you stick a callback in this onClick method, that’s the shit that gets run when the button is clicked.
You probably don’t even remember what we were talking about since I just mind fucked you so hard, but we want to change the text of dirtyButton when clean button gets clicked. We access that dirtyButton variable, let’s type that and then setText, and we pass the string variable pickupLine to it, this will change the text of the dirtyButton to the new string. But whoa what the heck was that, a final keyword just got added to our DirtyButton and PickupLine, that’s because we’re using these variables from within this anonymous inner class. Final, just means don’t change these freaking variables, because if we do, it would fuck up their use in the inner class. Let’s emulate this shit, and we click the clean button and see our bottom button change. Amazing, but not exactly what we want. We need to find a way to open our second activity screen and pass data too it, in this case, our pickup line. And then use our retry button to bring us back! The way we do that is with intents, which is how activities talk to each other. And after you pass the quiz, i’ll have an intent to remove a piece of clothing!
Questions or Comments?