Why do you need to pass an object of type View to this method?
android
Solution
You might have several buttons in your layout, and only one method in your activity's code. In such a situation, it becomes necessary to differentiate between different buttons.
That's where this can be used.
public void onClickButton(View view){
if(view.getId() == R.id.buttonSave){
// Do something
} else if(view.getId() == R.id.buttonCancel){
// Do something else
}
}
Although, you can bind different methods to different views, by having a method for each view type.
Yet another use case could be:
After clicking on the button, you want to modify the button itself, say, hide it, or change the label, then you obviously need a reference to the button.
Problem
``` <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_alignParentTop="true" android:text="@string/button1" android:onClick="onClickButton"/> public void onClickButton(View view){ TextView textview = (TextView) findViewById(R.id.textView1); textview.setVisibility(View.VISIBLE); } ``` That is the code that makes the text appear in the main activity interface when the button is pressed. What is the point of passing in an View object when you don't use it in the "onClickButton" method block? I am asking this because the app crashes if I leave out the parameter even when I am not using the view object in the code block.