0

New to kotlin/android so excuse my beginner skills.

However, why is View being passed in onClick(v: View) where the type should usually be? (e.g Int, String) etc.

I didn't think this was possible/allowed.

My code:

class MainActivity : AppCompatActivity(), View.OnClickListener  {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        choose_1_player.setOnClickListener(this)
    }

    override fun onClick(v: View) {

        when (v.id){
            R.id.choose_1_player -> println("Hello")
            R.id.choose_2_player -> println("Hello")
        }

    }


}

Is it because the function is written in Java and not Kotlin?

Sergio
  • 27,326
  • 8
  • 128
  • 149
Zorgan
  • 8,227
  • 23
  • 106
  • 207

3 Answers3

2

Because

1.We click on a View it may be a Button , TextView ,EditText or any layout so on the basis of Integer ID difficult to match with actual views.

2.View has many information including its Id ,that can be used like (Tag , Text etc).

3.Also view has its parent Layout Information can be used .

Please find more details at below link:

Why do we have to add 'View' as parameter in onClick method and what it does?

View class has interface with method onClick(View view).

/**
     * Interface definition for a callback to be invoked when a view is clicked.
     */
    public interface OnClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v);
    }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • I'm mainly asking about the syntax of the parameter: `(v: View)` (because I'm writing in Kotlin) - `View` isn't a data type and that position usually holds a data type (parameterName: dataType) – Zorgan Jan 28 '19 at 20:57
1

MainActivity class is implementing View.OnClickListener interface, which has the following signature:

public interface OnClickListener {
    void onClick(View v);
}

So onClick method is implemeted and it receives View as the parameter.

v - is a view which was clicked.


Addition: The syntax of the overridden onClick method in Kotlin is the following:

override fun onClick(v: View) { ... }

where v - is a view which was clicked; View - is the data type of object v from Android SDK. It is a parent class for many children, such as Button, TextView etc. That means onClick method can receive a Button, TextView, ImageView and other views that inherit from View. Here works one of the principles of OOP - Polymorphism.

Sergio
  • 27,326
  • 8
  • 128
  • 149
  • I'm mainly asking about the syntax of the parameter: `(v: View)` (because I'm writing in Kotlin) - `View` isn't a data type and that position usually holds a data type `(parameterName: dataType)` – Zorgan Jan 28 '19 at 20:54
  • `View` is a data Type from Android SDK. I edited my answer. – Sergio Jan 29 '19 at 06:37
0

The View attribute inform which View you clicked (every visual component of Android is a View) so you can use this information to identify "who" is calling the onClick.

If in my layout I have 3 buttons (button1, button2, button3) I can do something like this (in Java):

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.id) {
            case R.id.button1:
                button1Action();
            break;

            case R.id.button2:
                button2Action();
            break;

            case R.id.button3:
                button3Action();
            break;
        }
    }

Or you can do something like this and ignore the View attribute

public class MainActivity extends AppCompatActivity {
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button1Action();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button2Action();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button3Action();
            }
        });
    }

PS.: I didn't run this code. Probably it has some errors.

Carlos Aires
  • 151
  • 2
  • 4