Now, we've written a lot of code,
and you must be anxious to see the list of words start to appear on the screen.
Well, here's a manual way of making that happen.
We've mentioned multiple times that anything you write in the XML of your
app, we can actually accomplish in the Java code as well.
So I could create a TextView object for each element in this list and
then add it as a child view to the current layout.
So let's try that approach.
First off, I need to start by checking out the layout.
So for the NumbersActivity,
we see that the layout is defined in the activity_numbers XML file.
So I'm going to open up that file, and
then I'm going to change this RelativeLayout to a LinearLayout.
So every child view that I add to it will just come one right after another
in a single column.
Additionally, I'm going to add the orientation attribute to be vertical.
Now I'm going to be adding child views dynamically in Java code, and
because of this, I should give this LinearLayout a view id,
so that I can reference it in the Java code later.
And I'm going to call that id rootView.
Now let's go back to the NumbersActivity.
So I'm going to be adding the lines of code to display the list of words
at the end of the onCreate method.
And I'm going to do this after I've created my ArrayList.
I can go ahead and delete these log messages
because I already know my list was created properly.
So I'm going to close the project panel on the side, so
we can see the code better.
So what I need to do in the Java code is that first, I need to
find the LinearLayout that I called rootView and store it in a variable.
And then I can start creating and adding TextViews to this LinearLayout.
So to do so, I need to start off by using findViewById method
to find the view with the id rootView.
And this will return a view to me.
But I need to cast it specifically into a LinearLayout data type, and
I do so, so that I can add child views to it.
Now I'll store this view in the rootView variable, so that I can use it later.
And remember to end it with a semicolon.
[BLANK_AUDIO]
Nice.
So remember this line helps me find the LinearLayout called rootView.
And I'm going to store it in this variable called rootView.
And remember next,
I need to create and add TextViews to this LinearLayout we just created.
So start off by creating the TextView object.
So I'm going to use this constructor,
which requires a context as its only input.
Remember to help us understand which constructor to use,
we can always check the documentation.
So here's the documentation for the TextView class.
And I'm going to scroll down to the section about public constructors.
So here we see there are a couple of possible constructors we could
have used.
And each of them has a different number of inputs.
But I'm going to go ahead and use this first one,
which takes in one input context.
Remember that every time you create a view in Java code,
you need to pass in a context, so that the view knows the theme and
other information about the app in which it will be displayed.
So it gives it context as to what's going on around it.
Now in this case, we have this passed in as the input for this constructor.
And since this class is the NumbersActivity class,
this will also be the context.
All right, so now this line of code has helped me create a TextView object
that is stored in a variable called wordView, whose data type is a TextView.
So to change the text that is displayed on screen,
I call the setText method on the wordView object.
And I pass in one input, which is the desired text.
In this case, I want the TextView to display the string
stored in the words list at the zeroth index.
And so this should help us map to the word one.
Now that we have a wordView object with the correct text inside,
I'm going to add the view as a child to the parent view called rootView.
So this is our child view, wordView.
And this is our parent view, rootView.
So I'm adding the child view, wordView,
using the .addView method to the parent view, rootView.
All right, so let's run this code in our app and see what happens.
All right, so now when we go into the NumbersActivity, nice.
So we see the word one,
which was from the zeroth element of our word array list.
So what we've done here is dynamically created a TextView in Java code
based on the first element in the array list.
And at this point,
you're probably starting to think of all the possibilities.
So what if we repeat the same code for all the other elements in the list?
Then we can show a list of words on the screen.
So let's give that a try with a couple more views.
So I'm going to copy these three lines of code
that helped us create a TextView for the word, one.
Paste it below, and then modify it to help us create a TextView for
the word, two.
So it looks like I'm getting an error here.
And it says the variable wordView is already defined in the scope.
And that means that you can only define a local variable once with the same name
within the same scope.
And the scope that it's referring to here is the method.
So to help with that, I'll put a different name and call it wordView2.
Then I'm going to update the code below to reflect that.
Great, so now we have a block of code that creates another TextView
called wordView2, and we add that TextView to the parent view rootView.
But wait a second,
I want this second TextView to display the next word at the first index.
So instead of words.get(0), I'll change it to
words.get(1) to get the string located at the index 1 in the list.
So now let's copy and paste that section, and
make some modifications to display the third word in the list.
[BLANK_AUDIO]
All right, so let's give it a test and see what happens.
When we run the app on our device and hit numbers, nice.
So in the NumbersActivity, we put out a list of one, two,
and three, which are the first three elements of our array list.
All right, so we could continue this approach of copy and pasting and
making modifications to the text for you and the indexes.
But it's going to get really tedious, and
we also need to style these TextViews to make them look better.
Right now, it's just plain text.
Well, we might have well just done all this in XML and just copied and
pasted the XML element over and over and over again.
Well, hang on for a moment.
In writing this Java code, we do start to see a pattern.
So notice that each block of code for each TextView is really similar.
And the main difference is that the index position is changing from one
block to another.
So see that we're incrementing the index from 0,
1, 2, and so on and so on.
So I've rewritten the code based on the pattern we saw.
So what if we replace the energy variable that tracks our current index
position in the list and then just update it by 1 each time?
So notice that these blocks of code are looking more and more similar, so
they all call words.get, and they pass in an index as the input.
And we just have extra code now that increases the index value by 1.
So now I'm going to introduce you to an awesome new programming concept
called loops.
And once you learn about loops, I'm going to ask you to fix this code, so
that you can display all of the words on the screen without copying and
pasting this block of code over and over again ten times.