Android Button setOnClickListener Method Guide | Generated by AI
Question: To set an onClick listener for a Button in code, which method is commonly used?
A) button.setOnClickListener()
B) button.addClickListener()
C) button.onClick()
D) button.setClickEvent()
Answer:
The correct answer is A) button.setOnClickListener().
In Android development (using Java or Kotlin), the standard and most commonly used method to programmatically set an onClick listener for a Button (or any View) is setOnClickListener(). This method takes an implementation of the View.OnClickListener interface, typically provided via an anonymous inner class, lambda (in Kotlin), or a separate listener object.
Example in Java:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle click event
}
});
Example in Kotlin:
button.setOnClickListener {
// Handle click event
}
Why not the others?
- B) addClickListener(): No such method exists in the Android SDK.
- C) onClick(): This is the method inside the listener that gets called when a click occurs, not the method to set the listener.
- D) setClickEvent(): No such method exists in the Android SDK.
Note: The user’s selection of “C” is incorrect. The correct choice is A.
References:
- https://developer.android.com/reference/android/view/View#setOnClickListener(android.view.View.OnClickListener)
- https://developer.android.com/develop/ui/views/components/button