android:onClick
attribute that streamlines the coding of an app's actions. Here's what you do:
- Launch Android Studio. Make sure you have already created the “look” for your app.
- 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. - 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
, andtextView
.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 secondTextView
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
You can change a component's id, if you want. (For example, you can change the nameEditText
isn't the same as the wordeditText
. In this example, the wordEditText
stands for a kind of component (a kind of text field), andeditText
stands for a particular component (the text field in your app — the text field that you dropped onto the preview screen).editText
tothatTextThingie
.) 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.)
- 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.
- In the Properties pane, type onButtonClick in the
onClick
field.Actually, the word you type in the
onClick
field doesn't have to be onButtonClick. But in these instructions, the word onButtonClick is used. - Inside the
app/java branch
of the Project tool window, double-clickMainActivity
.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, theMainActivity
branch is located in a branch that's labeled with your app's package name. (The package name iscom.example.myapplication
orcom.allyourcode.a03_01
or something like that.) That package name branch is directly in thejava
branch, which is, in turn, in theapp
branch. When you're finished with double-clicking, the activity's code appears in Android Studio's editor. - 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
, andtextView
. If your app's components have different ids, change the code accordingly. For example, if your firstEditText
component has the ideditText2
, change your firstfindViewById
call tofindViewById(R.id.editText2)
. - Run the app.
- 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 yourEditText
component to yourTextView
component.
package com.allyourcode.a03_01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
<strong>import android.view.View;</strong>
<strong>import android.widget.EditText;</strong>
<strong>import android.widget.TextView;</strong>
public class MainActivity extends AppCompatActivity {
<strong> EditText editText;</strong>
<strong> TextView textView;</strong>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<strong> editText = (EditText) findViewById(R.id.editText);</strong>
<strong> textView = (TextView) findViewById(R.id.textView);</strong>
}
<strong> public void onButtonClick(View view) {</strong>
<strong> textView.setText(editText.getText());</strong>
<strong> }</strong>
}
If your app doesn't run, you can ask for help via email. The address is [email protected].