philomath
philomath

Reputation: 2211

Android - the button view has never been used in the function

Please pay attention to onClickButton(Button button) function. Here the button view has never been used in the function so why it was placed there and what does this "phenomenon" called in Java world?

package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.test_layout);
    super.onCreate(savedInstanceState);
    setUpUI();
}

//BUTTON
private void setUpUI(){
    Button b=(Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onClickButton((Button) view);
        }
    });

}

public void onClickButton(Button button){
    Toast.makeText(this,"Button clicked",Toast.LENGTH_SHORT).show();
}
}

Upvotes: 2

Views: 1112

Answers (3)

Chintan Soni
Chintan Soni

Reputation: 25267

There's no need of this onClickButton:

private void setUpUI(){
    Button b=(Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onClickButton((Button) view);
        }
    });

}

public void onClickButton(Button button){
    Toast.makeText(this,"Button clicked",Toast.LENGTH_SHORT).show();
}

You are already defining the buttons onClick(), just do this way:

private void setUpUI(){
    Button b=(Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Toast.makeText(getBaseContext(),"Button clicked",Toast.LENGTH_SHORT).show();
        }
    });
}

Edit: Your question: the button view has never been used in the function

The code here you presented doesn't demonstrate the use of View Passed in the onClick(View view), but if you think, you'll see that a view is passed to function so as to make changes to that specific view, for example, change the backgroundColor of view, hide the view - and much more things can be done here related to view on click of the button.

I guess you are getting, what i am trying to explain.

Upvotes: 2

Ridcully
Ridcully

Reputation: 23655

The OnClickListener interface defines the onClick() method in a way that it has a View as an parameter. That the programmer in your case creates another method and even passes the parameter along although it's not used later on, lets me suppose that he has either no idea what he's doing or the "Button clicked" toast is just used as an example.

The idea behind passing the View that received the click is that you can create a single OnClickListener and assign it to more than one View (thus saving some memory). In the onClick() method then you will need to find out which of the Views has actually been clicked and that's why the View is passed to the method.

A standard implementation then is something like so:

public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btn_a : /* handle click on button a */ break;
        case R.id.btn_b : /* handle click on button b */ break;
        /* and so on */
    }
}

Upvotes: 1

Juan Sánchez
Juan Sánchez

Reputation: 990

I would call it bad programming, to be honest.

Upvotes: 1

Related Questions