Skip to main content
Menu
CodeBabes
  • Create new account
  • Request new password

or

  • Sign in with Twitter
  • Code
  • Babes
  • Sign Up
  • Log In

Looks like you missed a quiz. Go back and pass that shit!


NO STRIP FOR YOU!

Share the Love

  • Tweet

Java Event Handling in Android

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?

Lessons

  • Android Studio Tour
  • App Layout in Android Studio
  • Java Variables, Java Data Types & Android Views
  • Java Event Handling in Android
  • Android Intents, OOP This, Variable Scope
  • Passing the Data with Android Intents & Finish
  • Complete the Android App with Launcher Icons & APK
  • Android Finale :)
Android Virgin

Downloads

SignUp & Subscribe

What the What?

Lessons: 41

Much more to come!

50/50 Entertainment and Education. Learning doesn't have to be boring :)

Our Philosophy

Contact

  • support
  • Tweets!
  • Youtube
  • Google Plus
  • Facebook

Stuff

  • Blog
  • Sign Up
  • All Babes
  • All Courses
  • Become a CodeBabe!
  • MAKE A LESSON WITH CODEBABES!

Recent Courses

Internet Virgin
SEO Virgin
HTML Virgin
CSS Virgin
Programming Virgin with Javascript

Learn to Code with Babes

Copyright © 2025 CodeBabes.com • Made with

  • Privacy Policy
  • Terms of Use
Close Menu
  • Blog

  • All Courses
  • Android Virgin
    • Android Studio Tour
    • App Layout in Android Studio
    • Java Variables, Java Data Types & Android Views
    • Java Event Handling in Android
    • Android Intents, OOP This, Variable Scope
    • Passing the Data with Android Intents & Finish
    • Complete the Android App with Launcher Icons & APK
    • Android Happy Ending
  • Internet Intro
    • Backend Development
    • Front End Web Development
    • Programming Tools you Need
    • Hosting and Domain Names
  • HTML Virgin
    • HTML Text Tags
    • HTML Links and Images
    • HTML Layout with HTML 5
    • HTML Tables
  • CSS Virgin
    • Where to Stick your CSS
    • CSS Properties, Sizes and Colors
    • CSS Selectors
    • CSS Box Model
  • Programming Virgin
    • Variables in Programming
    • Programming Intro Conditional Statements
    • Programming Intro to Arrays
    • Programming with Loops
    • Programming with Functions
  • PHP Virgin
    • PHP Variables
    • PHP Conditionals
    • PHP Arrays
    • PHP Loops
    • PHP Functions
  • SEO Virgin
    • Make Google Love You
    • Understanding Keyword SEO
    • Optimizing for SEO
    • Technical SEO