25 May 2013

fabian's picture
Posted by fabian on July 19, 2012

So now we want to put something more useful in there, I think we want it to do some math.

What we want it to do is get the text from the first EditText and the second EditText and then add them together and then display them in a TextView.

So first of we need to add another TextView to the layout, open up res > layout > main.xml, for people who are using the graphical layout open up forms widgets and find the TextView and then drag that into the layout, for people who are using the text editor add this under the button add this

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

Once you have the text view delete android:text="TextView" and then set the ID to text. It should look like this after

<TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

You should now save this file and press CTRL-B to build the work-space.

Head back to src > package > HelloMathActivity.java and then make another variable underneath the EditText variables, this one should be TextView textView; and then in the onCreate method under the EditText views set textView by using findViewById. If you don't know how to do this look back in your code, its always good to look back at what you have done, no harm in reusing your old code and editing it a little to suit the context either.

So now back in the onClick method we want to get a String from both of the EditText views, store them in separate Strings one called text1 and the other called text2.

Now instead of setting the EditText text to the String text you need to set the textView text, just replace edit2 with textView and it should set the text.

When setting the String though we want to add the strings together, because we want to do "math" with them. so in the setText brackets write text1 + text2 and see what happens, it should look like this

package nz.thesmartlemon.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(text1 + text2);
  }
 
 };

}

Now save it and then press CTRL+B then you should be able to press the green run button and run it, if you don't know how to run it read through this here. (just scroll down a bit till you see the image below).

This is what should happen when you type something in each EditText and then press the button.

Notice how it doesn't add the two numbers together like you would in math but it just joins them? That's because Strings aren't numbers, they are only a linear sequence of characters, to do math first you would need to convert them to strings.

Download Files 

Don't forget to go to the next chapter down below :D

Tags: