Crap Sums App
If you have followed my blog in the order it was created you will now know that you can make a crap app in Android easily. The only real learning curve was how to use the Eclipse environment and how some of the files link together.
This next app is again crap, but I remind you that from crap apps can come okay apps. Our app will allow the entry of a number into a nice text box. In order to make anything happen a button will be clicked and the value in the text box will be doubled and show as a message on the screen.
As you can guess this app will need more screen elements that used before, and will involve checking for the button being clicked, and from that using some java code to do maths and change the message on the screen. The learning curve has now just shot up from flatline to almost getting a pulse - enjoy.
Now that was a lot of setting of screen elements and strings. When making apps we need to spend some time doing this sort of thing so when we actually code the app, we will have places to refer to in the code. Let's do some programming now.
Locate the file called Sumz.java which is in the SRC folder. Open Sumz.java and have a look at the default code provided.
INSERT GUIDE
If you are familiar with Java (which I hope you are) you will recognise the definition of a class. You will see that the class is called Sumz and it is within the package matthew.harkin.uk.sums. You will see extends Activity which is an Android portion of code that means you are using the Activity class. Each screen or task you see an Android app produce/use is usually a new activity. So Sumz is an activity in Android.
Below the package name you will see which Android classes are being use by default. We will use these and add some of our own as well as we add in more Android concepts. Since we are using a TextView we see a reference to that here. We do not see one for the Button or the EditText. This means that we need to ensure that the program can use them by adding some lines here near the top, just under all of the other android references. See below.
INSERT GUIDE
Now lets look at the code inside the class Sumz. We see a method called onCreate. This method is Android saying when my app is created/started this is what will happen. Within this we see a line referring to R.layour.main - this is saying use main.xml as the default user interface screen when the app is started. This is good because we are using main. If you wanted to use another layout you had created called highscores.xml you would change this to R.layout.highscores - that way the default user interface screen would be different.
In order for our program to work we need to be able to enter something into the EditText area when we run our app, by default this will do so and also provide a nice little on screen keyboard so let's not worry about that part of the app yet. The part we need to worry about is detecting the button being clicked. There are a couple of ways we can do that but we will look at the simplest method right now.
We need to add a new method to the Sumz class called eventHandler. Type the following in at the appropriate location. If you don't know where, just scroll down this blog and copy the code. I do believe however that typing code is better because it learns you more by seeing the code as you type.
//This method will detect the button click
public void eventHandler(){
} //end eventHandler method
This is how you define a simple method that does not return a value (void means do not return anything) and the // mean comments by the programmer. I use // to comment quite a lot to remind myself what I did. The use of the word public means that this method is available for use by all. The insides of the method are enclosed by { and }, so when I add code between these 2 {} it means this is the code to execute when the button is clicked. I like this method eventHandler because if I have lots of buttons in an app I can use it to see which button was clicked and then execute code depending. In order to detect which button we can use some more code to read the state of the button. When I say state I mean the properties of the button just as it was clicked. Below is the code so far to make it more readable.
INSERT IMAGE
Now lets add the code that reads the button state. We will use a switch command to identify the correct button. If you are not familiar with switch then I suggest a quick read up on java and the switch command. It operates like a neater version of an if > if else > if else > else command.
If you have followed my blog in the order it was created you will now know that you can make a crap app in Android easily. The only real learning curve was how to use the Eclipse environment and how some of the files link together.
This next app is again crap, but I remind you that from crap apps can come okay apps. Our app will allow the entry of a number into a nice text box. In order to make anything happen a button will be clicked and the value in the text box will be doubled and show as a message on the screen.
As you can guess this app will need more screen elements that used before, and will involve checking for the button being clicked, and from that using some java code to do maths and change the message on the screen. The learning curve has now just shot up from flatline to almost getting a pulse - enjoy.
- Create a new project. My project is matthew.harkin.uk.sums with title Sums, package matthew.harkin.uk.sums, activity Sumz, version 2,3 mini SDK 10. Now do all of that for yourself using the names you find suitable.
- Open main.xml, delete the default TextView and drop in an EditText, a Button and a TextView in that order. Don't bother about the layout right now, we can address that later. Right now we are working on the mechanics of the app only.
- The screen elements have default names. We will change these default names for practise and because I want different ones. Using the properties of each of the screen elements, change the EditText name to editNumber, the Button name to buttonGo and the TextView name to textNumber. You could also easily do this by changing to code view and editing the XML.
- Open strings.xml and make a new string called myNumber with a message that is blank.
- Make another string called startNumber with a message that is 10.
- Go back to mani.xml and ensure that the string property of textNumber is set to myNumber. This means when the app is first launched the TextView will contain nothing, but we will of course change that when we need to while the app is running.
- Set the Button buttonGo property onClick to eventHandler. This will be a new method we will code into our app - yes that's right, we will write some actual code soon.
- Set the EditText editNumber property Text to startNumber. This means that we will see the number 10 in the edit text box when the app is run. This can be the default number.
Now that was a lot of setting of screen elements and strings. When making apps we need to spend some time doing this sort of thing so when we actually code the app, we will have places to refer to in the code. Let's do some programming now.
Locate the file called Sumz.java which is in the SRC folder. Open Sumz.java and have a look at the default code provided.
INSERT GUIDE
If you are familiar with Java (which I hope you are) you will recognise the definition of a class. You will see that the class is called Sumz and it is within the package matthew.harkin.uk.sums. You will see extends Activity which is an Android portion of code that means you are using the Activity class. Each screen or task you see an Android app produce/use is usually a new activity. So Sumz is an activity in Android.
Below the package name you will see which Android classes are being use by default. We will use these and add some of our own as well as we add in more Android concepts. Since we are using a TextView we see a reference to that here. We do not see one for the Button or the EditText. This means that we need to ensure that the program can use them by adding some lines here near the top, just under all of the other android references. See below.
INSERT GUIDE
Now lets look at the code inside the class Sumz. We see a method called onCreate. This method is Android saying when my app is created/started this is what will happen. Within this we see a line referring to R.layour.main - this is saying use main.xml as the default user interface screen when the app is started. This is good because we are using main. If you wanted to use another layout you had created called highscores.xml you would change this to R.layout.highscores - that way the default user interface screen would be different.
In order for our program to work we need to be able to enter something into the EditText area when we run our app, by default this will do so and also provide a nice little on screen keyboard so let's not worry about that part of the app yet. The part we need to worry about is detecting the button being clicked. There are a couple of ways we can do that but we will look at the simplest method right now.
We need to add a new method to the Sumz class called eventHandler. Type the following in at the appropriate location. If you don't know where, just scroll down this blog and copy the code. I do believe however that typing code is better because it learns you more by seeing the code as you type.
//This method will detect the button click
public void eventHandler(){
} //end eventHandler method
This is how you define a simple method that does not return a value (void means do not return anything) and the // mean comments by the programmer. I use // to comment quite a lot to remind myself what I did. The use of the word public means that this method is available for use by all. The insides of the method are enclosed by { and }, so when I add code between these 2 {} it means this is the code to execute when the button is clicked. I like this method eventHandler because if I have lots of buttons in an app I can use it to see which button was clicked and then execute code depending. In order to detect which button we can use some more code to read the state of the button. When I say state I mean the properties of the button just as it was clicked. Below is the code so far to make it more readable.
INSERT IMAGE
Now lets add the code that reads the button state. We will use a switch command to identify the correct button. If you are not familiar with switch then I suggest a quick read up on java and the switch command. It operates like a neater version of an if > if else > if else > else command.