user3662833
user3662833

Reputation: 11

View concept in onClick event

@Override public void onClick(View arg0)

In this ....what is the View that we are referencing. Does it refers to all the buttons in the corresponding layout? Please explain in depth.Thanks in advance.

Upvotes: 1

Views: 4439

Answers (6)

zgc7009
zgc7009

Reputation: 3389

That is a public method for the View class. If your class implements OnClickListener, you can use something like this (for example in your Activity's onCreate method):

Button yourButton = (Button) findViewById(R.id.your_button_id);
yourButton.setOnClickListener(this);

To set a click listener to yourButton. In that case when you click yourButton, the view arg0 in onClick would be yourButton. In onClick you can do:

switch(arg0.getId()){
    case R.id.your_button_id:
        // do stuff
       break;
    //repeat for other buttons
}

This allows you to set click listeners within your class for multiple buttons. All you have to do is set the classes on click listener to the button the same way you did for yourButton.

Upvotes: 0

Chris Feist
Chris Feist

Reputation: 1676

As others have said, the view passed into the callback is a reference to the view that was clicked. You can have multiple buttons using the same onClick method. So one useful thing to do is this:

public void onClick(View view)
{
    switch(view.getId())
    {
        case R.id.my_cool_button:
            //do some cool stuff
            break;
        default:
            //who knows what button was pressed
     }
}

Upvotes: 0

sai Gorantla
sai Gorantla

Reputation: 179

you are partially correct this view means the layout in context but all the purpose of this onClick being so generic is that you don't have to write onClick for each button in your view where you can set buttons on click listener like this and

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);

call it like this

@Override
 public void onClick(View v) 
{
   // TODO Auto-generated method stub
   if(v == btn1)
   {
    //Things to do  
   }
   if(v == btn2)
   {
    //Things to do      
   }
   if(v == btn3)
   {
    //Things to do  
   } 
}

instead of doing something like this

private OnClickListener button1ClickListener = new View.OnClickListener()
{
 @Override
 public void onClick(View arg0) {
    **Button button = (Button)findViewById(R.id.button1);**
    handleButtonClick(button);
}
};

private OnClickListener button2ClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
    **Button button = (Button)findViewById(R.id.button2);**
    handleButtonClick(button);

}
};

private OnClickListener button3ClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
    **Button button = (Button)findViewById(R.id.button3);**
    handleButtonClick(button);

}
};

private OnClickListener button4ClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
    **Button button = (Button)findViewById(R.id.button4);**
    handleButtonClick(button);

}
};

Upvotes: 0

Blo
Blo

Reputation: 11978

The parameter for onClick method refers to the view pressed clicked/pressed. This can be a Button, a TextView, an ImageView, whatever. According to the Documentation and specially this topic: Responding to Click Events, this method must:

  • Be public
  • Return void
  • Define a View as its only parameter (this will be the View that was clicked)

Since you call a listener to the view as follows:

textview.setOnClickListener(this);

You'll use this method:

public void onClick(View view) { ... }  

As I said, the View view parameter refer to the element previously clicked. Each views which are attached to this listener are contain in this parameter.

However, to know which view has been clicked and perform some actions regarding them, you have the ability to use getId() method to retrieve the exact view:

@Override
public void onClick(View view) {
    if(view.getId() == R.id.text1) {
        // do something when textview is clicked
    } else {
        // do something else for all the views attached to this listener...
    }
}

Upvotes: 2

Alejandro Alcalde
Alejandro Alcalde

Reputation: 6220

The View argument will be the view in which the user is touching. So you can access it from within that method. You have its reference here http://developer.android.com/reference/android/view/View.OnClickListener.html#onClick(android.view.View)

Upvotes: 0

Chefes
Chefes

Reputation: 1942

public void onClick (View arg0)

Called when a view has been clicked.

Parameters arg0 The view that was clicked.

Upvotes: 0

Related Questions