Getting Started in Android Development
Part 5 -
After creating a layout file the next task will be to link that file to a java file so that the app can actually perform some useful tasks. In this tutorial, we shall work with the MainActivity.java file to do this for the first time.
If you look at the java file created as part of this project you will see two override methods associated with the action bar menu created by the project. These methods are the onCreateOptionsMenu and onOptionsItemSelected. Since we are not using the action bar in this tutorial, these can be deleted. If you wish to know more about the action bar menu, then the subject fully covered in my two part tutorial set.
This leaves the onCreate method. This was discussed as part of the activity lifecycle and it is the method called first when the activity is started for the first time. Tasks executed within the onCreate would include the creation or inflation of the layout.
It is worth noting the imports at this point. You can consider our MainActivity class as a standalone java class that knows nothing about Android. The imports means that our class is now aware Android classes and methods associated with the import and they can be used in the MainActivity class. By extending the MainActivity class with Activity then it inherits all of the methods of the Activity class. Initially our MainActivity class was extended by ActionBarActivity. This is fine as the ActionBarActivity itself extends Activity, but as we are not using the action bar menu, we can make MainActivity simply extend Activity. The concepts of importing classes and extending may be confusing so I have a more detailed overview here.
The first method under the onCreate method we need to pay attention to is in the line
setContentView ties the layout file activity_main.xml to the MainActivity java class.
We then need to set up variables for the views in the layout file. My example is shown below.
public static EditText firstNameEditText;
public static EditText lastNameEditText;
public static TextView resultTextField;
These are placed above the onCreate method immediately after the MainActivity class name declaration. This makes them accessible by all methods in the MainActivity class. Note the types associated with the variable. This is EditText or TextView. Both EditText and TextView are imported into the MainActivity class.
These variables then need to be associated with a specific view in the layout file. This is achieved with the findViewById method. The ID of the view set up in the layout file is passed as a parameter.
firstNameEditText = (EditText) findViewById(R.id.etFirstName);
lastNameEditText = (EditText) findViewById(R.id.etSurname);
resultTextField = (TextView) findViewById(R.id.tvResult);
In the case of the button, I declared a local variable. The button has to perform some tasks when clicked. To do this we set and onClick listener on the button with the following code. Any type of view, TextView etc., can be made clickable, not just buttons.
An error will be generated until you import onClickListener with the following code:
and then implement OnClickListener in the MainActivity class. The final task will be to add the unimplemented onClick method.
Under the onClick method you can place the code to combine the two names. The switch and case is useful as the same onClick method is called each time a view is clicked. Therefore, checking for the ID is essential. The line of code below shows how to get the text from an EditText as a string:
String firstName = firstNameEditText.getText().toString();
Setting the text in a TextView is straightforward with the example below:
At the end of this tutorial, the MainActivity java file should look similar to the one below. In the next tutorial we will look at refining the layout file.
Download Download tutorial set 1