There goes the flannel, you're so close, feeling overloaded… with information? In this lesson we’re going to make shit happen, but first let’s learn a little bit about variables in Java. Variables are containers, they hold stuff in your program, from strings, which are usually words and sentences, to numbers and more complex objects. Lets do an example, here we set the variable pickupLine equal to “Hey girl, you had me at hello world”. Let’s break this down, In Java we use camel case when declaring variables, so no spaces, but the second word L is capitalized. The first word String is the data type which specifies the type of data pickupLine will hold. Here’s a list of the more common data types, int holds a number, float holds a decimal, boolean holds true or false, char holds a single character, and String. Notice String is capitalized, that’s because its a complex data type while the others are known as primitives. All this means is String is actually an object that we can call methods on. If you’ve done any other coding you probably know about functions, well methods are pretty much the same thing except they exist within an object. So we can do something like String dot length and it will return the length of the string. Length is a method in the String object. Of course if we wanted to set that value to a variable we’d have to set the type to be int because it returns a number. Strings are always enclosed in double quotes.
Now that we know about variables, let’s jump back into Android studio and open the main activity java class. In Java the file name is the same as the class it contains, so MainActivity.java contains the MainActivity class. This is where the magic happens. Let’s do a few things to help us get started, first turn on line numbers by right or control clicking in the gutter, the little area to the left of the open file. Second lets turn off automatic code folding, if you click that little plus icon next to import at the top of the page it will unfold that bit of code, it’s good to see everything that’s going on when you’re a newb, and let’s make sure automatic importing is turned on, which means Android Studio will automatically add a line to import stuff when you use it in your code. Go into preferences, or settings for windows, editor, general, code folding and turn all of that off. Next, go to auto Import and make sure everything is checked. We’ll show what this does in a second. Click apply then ok.
Now back to the code! FINALLY! We’re in our MainActivity class and all the magic is going to happen in this onCreate method. This is what gets fired when our app opens up, and where it grabs our activity main layout XML. We’re not going to be using the Menu so we can delete the rest of that code below.
So hop into the curly braces in our onCreate method and create a little space below setContenView.
Now if we want to do something with the button we have to select it in code somehow. So let’s make a variable called cleanButton, first we specify the data type it’s going to contain, which is button, once we type button and hit space we’ll see that Android Studio automatically imports that Button package. It's like when mario gets the fire flower, he can shoot fireballs, and when we import Button widget we can push buttons, am I pushing your buttons?
Then we’ll name the variable cleanButton then equals and now we start typing find View By Id and we’ll see the magic of autocomplete, it shows us the method we’re trying to use, and the parameters or argument it takes, so find view by Id takes an integer. Hit enter or tab and it completes the typing.
Now inside here we type R dot Id, then the name of the ID we’re looking for, so cleanButton, then semicolon at the end. The R is referring to a Resource class Android creates for you. It’s basically a file where Android keeps track of all the shit you're using, like our button and its ID. But look at this red squiggly line! That's Android Studio telling us we have an error, but it also helps us fix it! If we put our cursor on this it shows us the error of incompatible types which means whatever findViewById is returning doesn’t match the Button type of the variable. If we click the red light bulb to the left it will gives a suggestion to fix this error, we want the first one, cast to Android widget button, click it and it adds Button in parenthesis before findViewbyId, this is called type casting.
FindViewbyId is returning a view, but Android doesn’t automatically know what kind of view it is, is it textView, button, imageView? But WE know it’s grabbing a button view so we typecast it to Button and now it’s the data type it was meant to be!
Before we move on let’s add a comment above this explaining what we did. When learning programming writing a lot of comments explaining what is happening in plain english is a great exercise. To write a comment put two forward slashes, then the comment. Try to explain what we did.
Last let’s make a String variable equal to a pickup line, how about, “You had me at hello world”, then in the next lesson we’ll change the button text to that line when it gets clicked, and we’ll see if that line got me to take off another piece of clothing.