Q:

Is there a good code example for accessing the elements in a layout after the user has changed them?

I am currently learning how to program Java in android. I read various artices on how the layouts work with xml. There are some good examples that show this.. However, I have not seen any examples that show you how to access the elements from the layout once they are changed on the screen.. 
Does anybody know of a good code example to show the onClick Event for buttons.. and the syntax to enter the value that is typed in by the user in and EditText field..
any help is appreciated..
Regards,
Jeff

A:

Here's a simple example.
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
            android:id="@+id/edit" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />  
        <Button 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/button"
            android:onClick="getText" />
</LinearLayout>

The Button has an onClick method called getText, and the Edit text has an ID of edit. In the Activity that uses this Layout, you would write the following code:
public void getText(View view) {
    EditText editText = (EditText)findViewById(R.id.edit);
    String text = editText.getText().toString();
   // do something with the text
}

Alternatively, if your Button XML has an ID but no onClick, like this:
<Button 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/button"
    android:id="@+id/button" />

Then you would write the onClick handler in your Activity like this:
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new onClickListener() {
    EditText editText = (EditText)findViewById(R.id.edit);
    String text = editText.getText().toString();
   // do something with the text    
});

