Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Making a Button Do Something3:56 with Ben Deitch
In this video we'll see how to make a button trigger some code by adding an OnClickListener. Listeners like this sit patiently in the background and "listen" for something to happen. And when that something happens, they run their code.
We started with our layout where we created our button. 0:00 Then we made a variable for it in our Activity class and 0:03 we set that variable in the last code challenge. 0:06 But there's still one more step to make our button work. 0:09 I'll start by adding the code from the code challenge. 0:13 I'll type showF and then hit Enter or Tab to autocomplete. 0:16 Then I´ll set it = findViewById(R.id.showFactButton). 0:21 Hit Alt + Enter to add the Cast, remove the question mark and there we go. 0:29 Now it's time to make our button do something. 0:35 On a new line, type showFactButton and 0:37 remember we can use Enter or Tab to autocomplete. 0:41 Now, if we type a dot, we get a new menu for auto complete suggestions. 0:44 These are all the methods available to a Button object. 0:49 And as we start typing, methods that don't match will be filtered out. 0:53 Let's type setOn and 0:58 then we see the setOnClickListener method near the top of our list. 0:59 That's also the method we're looking for. 1:04 So let's select this one with the brackets. 1:07 An OnClickListener is exactly what it sounds like. 1:11 It's something that listens for the button to be clicked or tapped. 1:14 Clicked and tapped mean the same thing when talking about a touchscreen. 1:17 When the listener detects a click, 1:21 it then runs some code that we've given it ahead of time. 1:23 To make our button do something, 1:26 we just need to add some code between these brackets. 1:28 But before we get to that, it looks like we've got an error. 1:31 Smart cast to Button is impossible, because showFactButton is a mutable 1:36 property that could have changed by this time. 1:40 Since we've declared our button as nullable by using a question mark, 1:44 Colin's concerned that we might be calling setOnClicListener on a null object. 1:49 To fix this, we just need to assert that showFactButton isn't null. 1:55 Let's add two exclamation points before the dot and there we go, perfect. 2:00 Getting back to making our button do something, 2:06 let's put our cursor between the brackets and hit Enter to give us some more room. 2:08 Anytime we click on our button, 2:14 the code that we write inside these curly brackets will run. 2:15 Let's start by adding a comment to describe what we'd like to do. 2:19 The button was clicked, so 2:23 update the fact TextView with a new fact. 2:27 All right, now we need a new fact, let's add a line below our comment and 2:36 create a new immutable string variable named fact. 2:40 And let's set it = "Ostriches 2:46 can run faster than horses.". 2:52 Now we need to update our TextView to show this new fact. 2:58 Remember, when we were editing the layout, we could change the text of a TextView by 3:02 editing its text property or by changing it directly in the layouts XML file? 3:06 Android also allows us to make these kinds of changes in code. 3:13 On a new line, type factTextView.text and set it = our new fact variable. 3:17 Then we'll need to assert that factTextView isn't null. 3:28 So, factTextView!! 3:33 And now, we're ready for the moment of truth. 3:38 Let's click on the Run button or hit Ctrl+R on Mac or 3:41 Shift+F10 on Windows to run the app. 3:44 Looks good. 3:48 Let's click on the button to test it. 3:49 Did you know that ostriches can run faster than horses? 3:51
You need to sign up for Treehouse in order to download course files.Sign up