So in the last tutorial we learnt a bit about logic and math. I think we want to build on that a bit more before moving on.
Today we are going to pass stuff around the activity.
The objective of this application is to show you how you can split things up into smaller methods.
First we want to add some things together, I know that it would be easier to just write in straight in but this is just showing you how it is done.
You know how in the last tutorial we had the two strings and we "translated" it to make a number, well the type of number that it translated to was called an int (or integer).
A int represents a number, with this number you can do things like math, that is why when we added two strings together it just joined them but when we added the two ints together it returned the sum of the two values.
So now you know what a int is we can do something.
Did you notice that for a method, like onCreate it is called a void? The definition is a void is "A completely empty space", in terms of java I believe that they use the term void because it does not return anything to the thing that called it, it just does what it needs to do without telling the caller that it completed its task or did anything at all.
Instead of using void for a method you can instead call it a int and instead when you call it it will return an int instead of nothing.
So to make a int like a method instead of using this:
int randomNum = 1;
You can call this
private int randomNum(){
return 1;
}
I know that looks almost pointless but it does have more purpose then that.
So what you can do to make it more useful you can do something like this
private int multiNum(){
int firstNum = 2;
int secondNum = 3;
int num = firstNum * secondNum;
return num;
}
So if you used this when you called multiNum it will return firstNum multiplied by secondNum.
So how do you use this?
You can define an int like you would and then call this after it like this.
int numTotal = multiNum();
So it still doesn't make much sense to use it or not, so lets do something more.
You know how when we called Integer.parse(String string) in the last tutorial, well what it does is sends a the string to the "method" which changes it to a int. So we want to do the same.
In the normal brackets next to the name of the method we say that we want something when the method is called, we want two different ints to be sent when the method is called so we can multiply them so we define them both in the brackets like this
private int multiNum(int firstNum, int secondNum){
int num = 0;
return num;
}
Notice how there is a comma between them? this is to define more then one variable in it. So now we have them defined we can use them.
When calling it since they are numbers all we need to do is write the number straight in to the brackets, if you were using a string you would write in "hello" instead of just 1.
So to call it you use.
int numTotal = multiNum(2, 4);
and in the method you use.
private int multiNum(int firstNum, int secondNum){
int num = firstNum * secondNum;
return num;
}
So when we call multiNum it will return the two numbers we sent to it back but multiplied together.
So this still isnt very useful as we can just write in
int numTotal = 2*4;
But this is showing that we dont need to do everything in one go, we can split it up into methods and then send stuff between the methods and have the methods return something, this will become really useful in future tutorials.
So how are we going to use this in our application?
well we are going to continue the one that we were doing in the last tutorial.
This is what you should have to start with:
Java (src > package > HelloMathActivity.java)
package <country code>.<your developers name>.hellomath;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloMathActivity extends Activity {
private EditText edit1;
private EditText edit2;
private TextView textView;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit1 = (EditText) findViewById(R.id.edit1);
edit2 = (EditText) findViewById(R.id.edit2);
textView = (TextView) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(Listener);
}
private OnClickListener Listener = new OnClickListener() {
@Override
public void onClick(View v) {
String text1 = edit1.getText().toString();
String text2 = edit2.getText().toString();
textView.setText(String.valueOf(Integer.valueOf(text1) + Integer.valueOf(text2)));
}
};
}
XML layout (res > layouts > main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/edit2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonText" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
So we need to open up the HelloMathActivity.java file and edit it.
When onClick right now we add the two numbers from the EditText views and display it.
We are going to replace
String text1 = edit1.getText().toString(); String text2 = edit2.getText().toString(); textView.setText(String.valueOf(Integer.valueOf(text1) + Integer.valueOf(text2)));
With a method call that takes in two strings, first we need to create a method that takes these two strings.
Out side of the listener and onCreate methods we want to make another method. This will be a void as we are not going to return any value.
private void display(String stringOne, String stringTwo){
int numOne = Integer.valueOf(stringOne);
int numTwo = Integer.valueOf(stringTwo);
int numTotal = numOne + numTwo;
textView.setText(String.valueOf(numTotal));
}
What this does is translate the two strings to ints and then add them together, see how it becomes useful? It makes the logic more controlable and easier to read instead of all in one big heap.
We want to call this in the onClick method by replacing the code in it to:
display(edit1.getText().toString(), edit2.getText().toString());
When we run it it comes out like this.

But this doesn't just need to be called in the onClick method, you can call it in any other methods, we want to call it in onCreate so that the number is set to 0 at the start.
in onCreate put this:
display("0", "0");
This will display 0 + 0 which comes out to be 0 :D
Your java file should look like this at the end.
package <country code>.<developers name>.hellomath;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloMathActivity extends Activity {
private EditText edit1;
private EditText edit2;
private TextView textView;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit1 = (EditText) findViewById(R.id.edit1);
edit2 = (EditText) findViewById(R.id.edit2);
textView = (TextView) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(Listener);
display("0", "0");
}
private OnClickListener Listener = new OnClickListener() {
@Override
public void onClick(View v) {
display(edit1.getText().toString(), edit2.getText().toString());
}
};
private void display(String stringOne, String stringTwo){
int numOne = Integer.valueOf(stringOne); //translate the strings to ints
int numTwo = Integer.valueOf(stringTwo);
int numTotal = numOne + numTwo; //add the strings together
textView.setText(String.valueOf(numTotal)); //translate the int to a string and set the text in textView to that string
}
}
When we run it, it comes out like this.

Thanks for reading, I hope you enjoyed this tutorial, there will be many more where these came from, including some things on Android 4.0 and how to get it next week, in the coming weeks I will also be covering ROM's and rooting your Android devices.
Comments, questions and feedback below :D












