Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need to pass an object of type View to this method?

Tags:

android

<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.

like image 336
Joe Dugan Avatar asked Feb 07 '14 02:02

Joe Dugan


People also ask

How do object objects get passed as arguments in a method?

Objects aren't passed at all. By default, the argument is evaluated and its value is passed, by value, as the initial value of the parameter of the method you're calling. Now the important point is that the value is a reference for reference types - a way of getting to an object (or null).

What happens when an object reference is passed to a method?

Note: When an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does.

What is the default method of passing objects in a function?

In the latest version of C#, which is C# 9 at this time of writing, objects are by default passed by ref. So any changes made to the object in the calling function will persist in the object in the called function. Show activity on this post.

Why do we pass object parameters to methods in Java?

Since values of ‘a’ and ‘b’ are not the same for both the references, so if (condition) is false, so else block will execute, and false will be returned. In Java we can pass objects to methods as one can perceive from the below program as follows: One of the most common uses of object parameters involves constructors.


2 Answers

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.

like image 173
Kumar Bibek Avatar answered Oct 18 '22 21:10

Kumar Bibek


what is the point of passing in an object view of type View when you don't use it in the "onClickButton" method block?

First of all, the Button Docs tell you to. Secondly, it can get used in the function. Since Buttons aren't the only Views which are clickable, having the View parameter allows you to check that View type to see what has been pressed and to perform other operations with it.

like image 26
codeMagic Avatar answered Oct 18 '22 19:10

codeMagic