How to Code the Behavior for Your Android App with Java

By Barry A. Burd

Coding the behavior of your Android app depends on how much work you want to do. Android 1.6 (also known as Donut) introduced an android:onClick attribute that streamlines the coding of an app’s actions. Here’s what you do:

  1. Launch Android Studio. Make sure you have already created the “look” for your app.
  2. If you don’t see the Designer tool with its preview screens, double-click the app/res/layout/activity_main.xml branch in the Project tool window. When the Designer tool appears, select the Design tab.
  3. Make note of the labels on the branches in the component tree.The component tree is on the left side of the Designer tool, immediately below the palette. Notice the labels on the branches of the tree. Each element on the screen has an id (a name to identify that element). Here, the ids of some of the screen’s elements are editText, button, and textView.
    java-programming-for-android-developers-2e-component-tree
    You may be wondering why, in place of the word “identification,” the strange lowercase abbreviation id is used instead of the more conventional English language abbreviation ID. To find out what’s going on, select the Text tab in Android Studio’s designer tool. In the XML code for the activity’s layout you’ll find lines such as android:id="@+id/textView". In Android’s XML files, id is a code word.
    When you drop a component onto the preview screen, Android Studio assigns that component an id. You can experiment with this by dropping a second TextView component onto the preview screen. If you do, the component tree has an additional branch, and the label on the branch (the id of the new component) is likely to be textView2.

    Java is case-sensitive, so you have to pay attention to the way words are capitalized. For example, the word EditText isn’t the same as the word editText. In this example, the word EditText stands for a kind of component (a kind of text field), and editText stands for a particular component (the text field in your app — the text field that you dropped onto the preview screen).

    You can change a component’s id, if you want. (For example, you can change the name editText to thatTextThingie.) In this example, you probably want to accept whatever you see in the component tree. But before proceeding to the next step, make note of the ids in your app’s component tree.

    To change a component’s id, select that component on the preview screen or in the component tree. Then, in the Properties pane on the right side of the Designer tool, look for an ID field. Change the text that you find in this ID field. (Yes. In the Properties pane, ID has capital letters.)

  4. On the preview screen or in the component tree, select the COPY button.As a result, the Properties pane displays information about your button component.
  5. In the Properties pane, type onButtonClick in the onClick field.
    java-programming-for-android-developers-2e-onclick-value
    Actually, the word you type in the onClick field doesn’t have to be onButtonClick. But in these instructions, the word onButtonClick is used.
  6. Inside the app/java branch of the Project tool window, double-click MainActivity.Of course, if you didn’t accept the default activity name (MainActivity) when you created the new project, double-click whatever activity name you used.
    In the Project tool window, the MainActivity branch is located in a branch that’s labeled with your app’s package name. (The package name is com.example.myapplication or com.allyourcode.a03_01 or something like that.) That package name branch is directly in the java branch, which is, in turn, in the app branch.
    When you’re finished with double-clicking, the activity’s code appears in Android Studio’s editor.
  7. Modify the activity’s code.

    In the code below, it’s assumed that the branches on your app’s component tree have the same labels as the tree above. In other words, it’s assumed that your app’s components have the ids editText, button, and textView. If your app’s components have different ids, change the code accordingly. For example, if your first EditText component has the id editText2, change your first findViewById call to findViewById(R.id.editText2).

  8. Run the app.
  9. When the app starts running, type something (anything) in your app’s EditText component. Then click the button.When you click the button, Android copies the text from your EditText component to your TextView component.

package com.allyourcode.a03_01;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText editText;

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editText = (EditText) findViewById(R.id.editText);

textView = (TextView) findViewById(R.id.textView);

}

public void onButtonClick(View view) {

textView.setText(editText.getText());

}

}

If your app doesn’t run, you can ask for help via email. The address is Java4Android@allmycode.com.