0

I'm working through the android developer tutorial and we're now creating a method to match to the method name we gave to android:onClick="sendMessage".

Here's the method:

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
}

The text says this about the method:

In order for the system to match this method to the method name given to android:onClick, the signature must be exactly as shown. Specifically, the method must:

  • Be public
  • Have a void return value
  • Have a View as the only parameter (this will be the View that was clicked)

I understand why it must be public and why the return value is void, but why does the method take (View view) instead of just (view)? I'm coming from a Ruby background so the syntax is confusing me. Why do we pass parameters like this?

6
  • if you put only view, you would not know the Object type, you always need the Object type of the parameters in a method. Commented Jun 17, 2014 at 6:08
  • In Ruby everything is an object and the type of object, aka Class, is implicit. In Java the Object type is explicit so the developer needs to make it clear to the compiler. Commented Jun 17, 2014 at 6:11
  • view parameter associated with Object you clicked. If its ImageView, TextView..etc . Type cast them and get the value or set the value . Ex:- ImageView im = (ImageView) view. Commented Jun 17, 2014 at 6:11
  • Why you used View parameter in method,use as a string param because you sending the message only Commented Jun 17, 2014 at 6:12
  • Thank you all. @Boopathi, I'm following the Android Dev guide, so can't answer that questions given my limited knowledge of Java/Android. Commented Jun 17, 2014 at 6:13

2 Answers 2

1
why does the method take (View view) instead of just (view)?

View means it is a class and view is just a variable adding those 2 is making the view variable an object of View class that can call all of its method.

Sign up to request clarification or add additional context in comments.

2 Comments

I see, that makes sense. Lowercase view is like an instance of View? I'll mark as accepted in ten mins.
@settheline yes it is an instance of View class. +1 this as well thank you.
0

This is due to the fact that onClick() method in the OnClickListener Interface which requires a parameter of type View. When you remove the parameter, Android will still attempt to call method sendMessage(View view) but that method does not exist any more, therefore you get error and app will force close.

Parameter view is the actual View (Button in your case) that was clicked. With this, you can assign multiple buttons to invoke the same method and inside the method check which button was clicked.

For more information, see this LINK

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.