Instance variables and methods are vital. This video shows how to set up the instance variables and methods for the calculator app we're creating in this chapter.
- [Instructor] Now that our interface is complete we can start writing the code for our application. And real quick, on the topic of the interface being complete, you may have noticed all of these warning messages inside of the component tree. If you hover over them, it says hardcoded string "0", should use '@string' resource. You may remember that I've talked about a few times in the course that Android frowns upon using these quote unquote hard coded strings. A hard coded string is where you actually type in the value of something so when you click on a button and you change the text inside of the button, that's what it's talking about when it says a hard coded string.
The reason why Android doesn't like that is because it makes it difficult to translate your application into other languages. Because our application is made up of numbers and symbols all of this advice is irrelevant. However, if at some point you do have text in your application and you want to change it, or right now if you just want to get rid of these warnings, you can simply click on the warning, scroll down and find the button to fix it, click that button, and then hit OK.
And it takes away that issue and you could also do that for all of the other buttons if you wanted to but, again, since it doesn't apply to our application, I'm not going to do that now. And, as I mentioned earlier, the idea of string resources is beyond the scope of this course but I will show you where to go at the end of the course. So let's close that window and head over to MainActivity.kt. The first thing we're going to create in terms of our properties is our enum value.
So we'll need to create a calculator mode enum. And we do that above the class declaration. Type enum there and you should have enum class and then CalculatorMode and some curly braces. Inside of the curly braces we list the different calculator modes. So None, Add, and Subtract. You may be wondering where Multiply is. Multiply is something I'm going to leave out on purpose and give you the challenge to complete yourself at the end of this chapter.
So keep that in mind as we're creating all of our code how you might add a multiply functionality to the calculator. Now let's create our properties. We have four to create here. So use the var keyword for all of them. The first one is lastButtonWasMode. We'll initialize that to false. So remember this keeps track of whether the last button press on the calculator was either the plus or the minus button.
Create a new property called currentMode. This keeps track of the calculator mode. So I'll initialize it o CalculatorMode.None. Now you may be wondering why lastButtonWasMode is false and why currentMode is None. These are the initial values of the properties. So when the calculator first launches, the user hasn't pressed any buttons so the last button was not mode and the user hasn't changed a mode so the current mode is none.
So these are the initial values of the calculator. New property called labelString. We'll initialize that to an empty string. Remember labelString is going to contain the string that's been entered inside of the text view and just like our other properties when the calculator first loads up, the user will not have entered anything, so that's why we have an empty string. On the next line we'll create our final property savedNum which will initialize to zero.
Just like labelString, the user has not yet entered any numeric values into the calculator so we initialize it at zero. Below the onCreate method, we'll start creating our own methods. I'll start by creating one called setupCalculator. Remember, use the keyword fun, the name of the method, parenthesis, and curly braces to create a method. We'll then run that method at the bottom of onCreate. And, of course, setupCalculator is not responding to an event, it's just a way to organize our code.
We could have all of our calculator setup inside of the onCreate method but there's already code in there so it's easier to read if we group all of that setup code together. Below setupCalculator let's create the rest of our methods. So fun didPressEquals parenthesis and curly braces. We're not writing any code in there right now. And then the next one, fun didPressClear. You may be wondering why we're creating these empty methods.
As a developer, you might do this all the time. You create an application. You know what events you're going to have to handle. You know what methods you might use to organize your code, so you create the skeletons of those methods and fill them in as you go. The next method we'll create is the updateText method. Below that changeMode.
And this is going to receive a parameter. We'll name the parameter mode and the type of data will be a CalculatorMode. Finally, the last method we'll create is called didPressNumber and that's going to receive a parameter as well, but it's going to be an integer. We'll call this num, short for number, and the data type will be int. So colon Int with a capital I and some curly braces after the parenthesis.
So now we've created all of the properties and methods that we're going to be using in our Calculator app.
- Installing Android Studio, Android SDKs, and build tools
- Working with variables
- Using methods
- Connecting interface elements to methods
- Controlling flow with conditional statements, arrays, and loops
- Designing the interface
- Using themes
- Building an Android app from start to finish