Kotlin Android Extensions5:38 with Ben Deitch
In this video we'll learn about how we can use Kotlin Android Extensions to do away with the 'findViewById' method!
We just learned about Anko and 0:00 how we can use it to entirely replace our XML layouts. 0:02 But before we can create any layouts, we need to create a project. 0:05 So let's start a new Android Studio project and name it Solitaire. 0:09 Then let's accept all the defaults, and even 0:16 though we won't be using any XML, let's let it generate a layout file for now. 0:21 Nice, now we need to set up our project with Kotlin. 0:32 To do this we first need to have the Kotlin plugin. 0:35 So let's go up here to Android Studio, and pick Preferences, or Settings if you're on 0:39 Windows and then choose Plugins and let's search for Kotlin. 0:43 Then click Browse, pick the Kotlin plugin and hit Install. 0:52 And once that's done we'll need to restart Android Studio, so let's do that. 0:57 And once we're back with our code, let's click inside MainActivity and 1:05 then go up to the Code tab and choose Convert Java File to Kotlin File. 1:09 Then, once we sync the project, 1:16 we get a little notification letting us know that Kotlin is unconfigured. 1:20 So let's click the Configure link, pick Android with Gradle and then hit OK. 1:24 And now Android Studio has made some small changes to both of our build.gradle files 1:33 to support Kotlin. 1:38 So let's sync our project one more time, and we're ready to move on to Anko. 1:40 But before we do, there's something else we need to talk about first, and 1:46 that is Kotlin Android extensions. 1:49 Kotlin Android extensions is just another way around that pesky findViewByID method. 1:52 It's kind of like Butter Knife but better. 1:58 To see how we can use these extensions, let's quickly make a counting app 2:01 with a textView to show the count, and a button to increment the count. 2:05 Let's head over to our layout, And start with the textView. 2:10 First let's give our textView an ID of counterTextView. 2:24 Then lets set the starting text to 0, 2:35 and the textSize to 24sp. 2:41 And since we're not really concerned with the layout, let's leave it at that. 2:46 Next let's create a button element below our textView, 2:50 and set its width and height to wrap_content as well. 2:55 Then let's give it an ID of counterButton. 3:02 And then since this is a relative layout, let's add the layout below property. 3:10 And set it equal to @idcounterTextview. 3:20 All right, that does it for the lab. 3:24 Now let's head over to our app's build.gradle file and 3:27 at the top, let's apply the Kotlin Android extensions plug-in. 3:30 It's in the teacher's notes as well. 3:35 Then let's sync the project, and get back to MainActivity to make the magic happen. 3:46 Let's add a space below setContentView, and 3:53 then let's start typing our button's ID. 3:56 And once we've got it, let's hit Enter to add the import statement. 4:03 And that's it, we've already got access to our button and 4:07 we barely even did anything. 4:11 Thanks to the Kotlin Android extensions, we can just import our views and 4:14 start using them right away. 4:18 So now that we've got our button, let's add an onClick listener, 4:20 .setOnClickListener. 4:23 And another cool thing about Kotlin is that instead of the regular process for 4:27 creating an onClickListener we can take advantage of lambdas 4:31 to save us from all that typing. 4:35 Inside the onClickListener, we need to increment our counter by one, and 4:37 then update the textView. 4:41 So first, let's create a var for our counter and set it equal to 0. 4:43 Then inside the OnClickListener, let's increment our counter by one, 4:52 counter++. 4:56 And then set counterTextView.text 5:00 = counter.toString. 5:07 Now, let's run the app and see what happens. 5:12 And just like we expected, we've got a TextView and a button. 5:16 And when we click on the button, the number goes up, awesome! 5:20 Kotlin Android extensions take a lot of the headache out of 5:26 dealing with our views. 5:28 And there are great tool to have in your tool belt. 5:29 But now that we've got that covered, 5:32 let's get back on track to create an app without XML layouts using Anko. 5:33
You need to sign up for Treehouse in order to download course files.Sign up