If I want something to happen when a button is clicked what is the difference between the two methods? The first seems much simpler.
In layout
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
In activity
public void sendMessage(View v) {
// do whatever
}
OR
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
// do whatever
}
};
protected void onCreate(Bundle savedValues) {
// Capture our button from layout
Button button = (Button)findViewById(R.id.mybutton);
// Register the onClick listener with the implementation above
button.setOnClickListener(listener);
}