Danel October Danel October - 7 months ago 26
Java Question

In Android programming: How can I call a method that accepts arguments of type View?

Ok, so I'm trying to follow this tutorial: http://developer.android.com/training/index.html

And I've gotten to the

training. At this point, I have an app with an action bar and a button. The button is beside an edit text field, and if you press it, it just takes you to another view with the text you typed in.

Now, I'm trying to call the same method that takes you to that 2nd view using one of the action bar menu options. The handler for that is in the main activity source code, a snippet of which i paste here:

public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.action_settings:
// openSettings();
return true;
return super.onOptionsItemSelected(item);

public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);

I want try and call the sendMessage method in one of the cases in the switch operation, but I don't know what arguments to put in. When the button is clicked, the handler is in the xml file:

android:onClick="sendMessage" />

I don't know how that works. Is what I'm trying to do impossible? Thank you for your help.


you should not call directly. Android calls it when you press the Button where do you declare android:onClick property. If you want to call it in any case just pass a null parameter and check against null values