Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we've covered a bit of the generated code, it's time to write our own! In this video we'll write Kotlin code in our FunFactsActivity class to declare our TextView and Button.
-
0:00
[MUSIC]
-
0:04
Now that we're comfortable using the design editor to make changes to our
-
0:08
layouts, it's time to write some code.
-
0:10
Let's start by taking a look at some of the code that was
-
0:13
automatically generated for our project.
-
0:16
Using the Android perspective makes our code easy to find,
-
0:20
it's right here in the java folder.
-
0:22
But if you happened to be in the project perspective.
-
0:26
Remember that our Java code can be found at app > source > main > and then Java.
-
0:32
It's right above our resources folder.
-
0:35
Let's switch back to the Android perspective and
-
0:38
take a look inside the java folder.
-
0:41
Now we can see the package name we chose when we created our project.
-
0:45
This is where all of our code will go by default.
-
0:48
Inside this package, we'll find one file, FunFactsActivity.
-
0:53
And if we double click it, it opens up over here in the code editor.
-
0:58
Now we can close the rest of these files.
-
1:00
Let's right click on FunFactsActivity up here and choose Close Others.
-
1:07
And I'll also close the project pane.
-
1:10
In Android, an activity represents a screen that users can interact with.
-
1:15
We'll learn much more about activities later but for
-
1:17
now, let's focus on this onCreate method.
-
1:21
The onCreate method is called when our activity is first created.
-
1:24
And since our app only has one activity,
-
1:27
this onCreate method will be called when our app is first started.
-
1:31
You might have noticed that this method also has one parameter,
-
1:34
a bundled variable called savedInstanceState.
-
1:38
We won't be using this variable in our app, so let's ignore it for now.
-
1:42
Inside onCreate, the most important line is the call to setContentView.
-
1:47
This method tells the activity which layout file we want to use for the screen.
-
1:51
This R.layout.activity_fun_facts parameter, is an ID
-
1:56
which points to the activity_fun_facts XML file and the layout directory.
-
2:01
This is where the layout we've been working on gets attached to our activity.
-
2:06
Okay, time to make the magic happen.
-
2:08
The first thing we need to do is declare two properties or member variables.
-
2:13
One for our FunFact text view and one for our button.
-
2:16
A property is the name for a variable that is inside the class but
-
2:21
outside all of the methods.
-
2:23
Using properties for
-
2:24
our views means that we'll be able to access them from any method in our class.
-
2:29
This is especially useful because there's often more than one method that needs
-
2:33
access to our views.
-
2:34
Let's start by adding a line at the top of the class.
-
2:39
Then let's add a comment to help explain what we're doing.
-
2:42
Remember it's two forward slashes for a single line comment.
-
2:46
Let's type slash, slash space and then type Declare our View variables.
-
2:54
Then on a new line let's declare our fun fact text view as private var
-
3:00
factTextView, Then since,
-
3:05
we're using colon, we also need to set it equal to something.
-
3:10
But we won't have access to our text view and until the onCreate method.
-
3:15
So for now, let's just set it equal to no.
-
3:18
Let's have a colon and then for the data type, let's use TextView but
-
3:23
add a question mark to the end to let colon know it might be null.
-
3:29
Then let's set it equal to null and there we go.
-
3:33
Also, if TextView is in red, that just means it hasn't been imported yet.
-
3:38
Hit Alt+Enter to import it.
-
3:40
One more thing, note that we won't be using the m prefix for our properties.
-
3:45
Sometimes, when you see Android code, they might declare their variables like this.
-
3:51
And according to the Android source style guide, all fields or
-
3:54
properties should start with a lower case m.
-
3:58
However, most code isn't written that way.
-
4:01
So unless you plan on contributing to Android source code,
-
4:04
feel free to drop the m prefix as we'll be doing here.
-
4:08
If you'd like to read more about these style guidelines,
-
4:11
check out the link in the teacher's notes below.
-
4:13
Now we need to declare our button.
-
4:15
Let's add a new line in our TextView and type private var showFactButton.
-
4:22
Then, let's add a colon and for the type, start typing Button.
-
4:28
Notice that Android Studio is trying to guess what we're typing.
-
4:32
Once the top choice is button, hit Enter or
-
4:35
Tab to let Android Studio autocomplete that for us.
-
4:39
It even adds the import statement.
-
4:41
Then, to finish it up, let's add a question mark and equals null.
-
4:47
All right, but why do these names change to gray like this?
-
4:52
If we hover over one of our properties,
-
4:54
we get a quick tip saying that our Property is never used.
-
4:57
The gray coloring will go away once we use these variables.
-
5:01
Another thing I want to point out are the import statements,
-
5:04
we can view them by hitting the little plus button up here.
-
5:08
By default, our class only has a couple of imports,
-
5:11
but every time we use a new class like TextView or button.
-
5:15
Android Studio will try to automatically add the corresponding import statement,
-
5:20
if you use a class that isn't important yet.
-
5:23
Let say I delete the button import.
-
5:27
Then you'll have something it'll look like this.
-
5:30
To fix it, just place your cursor in the missing class and
-
5:34
hit Alt+Enter to import it.
-
5:36
Let's take a short break and
-
5:38
then we'll see how to assign our views to these new variables.
You need to sign up for Treehouse in order to download course files.
Sign up