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 theonClick()
method because it's the View that theOnClickListener
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 clickingFetching the
Context
throughview.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 theView
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 becauseOnClickListener
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.
As you can see at the documentation : Responding to Click Events.
The method you declare in the android : onClick attribute must have a signature exactly as shown above. Specifically, the method must:
Because the same method could be used for several Views, then you need to know which one is emitting the event.
View is a parameter I believe because it is your way of saying which view initiated the callback.
And you set it up like this:
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 fromView.setOnClickListener
). You are basically saying that the Button is the view that is talking right now and that theView.OnClickListener
should listen to what this view has to say.onClick(View v)
. The view that is speaking will be theView 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 didnew View.OnClickListener()
. But what if we didn't? What if multiple buttons referenced the same listener? This is whenView v
becomes important. It is so we can do something likev.getId()
so we know which view was speaking (initiated the callback). This function will return the id ofR.id
of whichever view called it. In this case we declared a button withR.id.button_id
so this is how we would know that the button called it.