I have a doubt when i am specifying a onClick
function on button why View is used as a parameter ?
and
What happens when we don't specify that view
Example:
public void ara(View button){
}
here onClick
function of a button : ara
Thanks
I have a doubt when i am specifying a onClick
function on button why View is used as a parameter ?
and
What happens when we don't specify that view
Example:
public void ara(View button){
}
here onClick
function of a button : ara
Thanks
View
is being used as a parameter in the onClick()
method because it's the View that the OnClickListener
is attached to.
This is very useful in multiple ways, such as if the View is a TextView
and you want to update it's text when the view is clicked.
Other common uses might be:
Setting View visibility
Passing the View to an ObjectAnimator
for animations when clicking
Fetching the Context
through view.getContext()
Overall, it's just a necessary parameter for you to either perform modifications on the View that the OnClickListener
belongs to, or for you to use the View
for other purposes or calculations.
Additionally, if you don't include the View in the onClick()
method, then your code will fail to compile. This is because OnClickListener
is an interface, and by default, methods inside an interface don't have a method body.
Instead, it's up to you to override the method and give it a proper method body. However, since you're overriding the method, this also means you must include the same method name, same method modifiers, and same method parameters to properly reflect the method you are overriding.
Failure to do so would mean that the required interface method isn't being overridden, so your code will fail to compile.
View is a parameter I believe because it is your way of saying which view initiated the callback.
Interface definition for a callback to be invoked when a view is clicked. ViewOnClickListener
And you set it up like this:
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// View v is the button in this case.
// Here you add what to do when this callback is initiated.
}
});
}
In this piece of code make note that:
final Button button = findViewById(R.id.button_id);
take note of the findViewById.button.setOnClickListener
(which is from View.setOnClickListener
). You are basically saying that the Button is the view that is talking right now and that the View.OnClickListener
should listen to what this view has to say. onClick(View v)
. The view that is speaking will be the View v
. Why is this useful? this is so you can reference the same listener (callback) multiple times. In this case we made a callback on the fly when we did new View.OnClickListener()
. But what if we didn't? What if multiple buttons referenced the same listener? This is when View v
becomes important. It is so we can do something like v.getId()
so we know which view was speaking (initiated the callback). This function will return the id of R.id
of whichever view called it. In this case we declared a button with R.id.button_id
so this is how we would know that the button called it.Because the same method could be used for several Views, then you need to know which one is emitting the event.
public void ara(View button) {
switch (button.getId() {
...
}
}
As you can see at the documentation : Responding to Click Events.
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
The method you declare in the android : onClick attribute must have a signature exactly as shown above. Specifically, the method must:
Be public
Return void
Define a View as its only parameter (this will be the View that was clicked).