Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android setOnClickListener method - How does it work?

I have trouble understanding this code. I get that findViewById will get the button widget and then it'll cast it. Then, it's going to use the button to call the setOnClickListener method. However, I don't know what is that argument being passed into the setOnClickListener and I have never seen code like that before. How is it that it creates a new object but is able to create a method of its own within another method's argument? Would be great if someone could explain that. Also, what type of object is the setOnClickListener method taking in?

btn = (Button)findViewById(R.id.firstButton); btn.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View v)     {         tv.setText(months[rand.nextInt(12)]);         tv.setTextColor(Color.rgb(rand.nextInt(255)+1, rand.nextInt(255)+1, rand.nextInt(255)+1));     } }); 
like image 804
rayleigh Avatar asked Sep 12 '14 08:09

rayleigh


People also ask

What does setOnClickListener do in Android?

OnClickListener and wires the listener to the button using setOnClickListener(View. OnClickListener) . As a result, the system executes the code you write in onClick(View) after the user presses the button. The system executes the code in onClick on the main thread.

What is the purpose of setOnClickListener?

One of the most usable methods in android is setOnClickListener method which helps us to link a listener with certain attributes. While invoking this method a callback function will run. One can also create a class for more than one listener, so this can lead you to code reusability.

How do you implement setOnClickListener?

To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

What is the difference between onClick and OnClickListener?

OnClickListener is an interface, which defines the onClick(View) method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.


2 Answers

It works like this. View.OnClickListenere is defined -

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

As far as we know you cannot instantiate an object OnClickListener, as it doesn't have a method implemented. So there are two ways you can go by - you can implement this interface which will override onClick method like this:

public class MyListener implements View.OnClickListener {     @Override     public void onClick (View v) {          // your code here;     } } 

But it's tedious to do it each time as you want to set a click listener. So in order to avoid this you can provide the implementation for the method on spot, just like in an example you gave.

setOnClickListener takes View.OnClickListener as its parameter.

like image 92
cliffroot Avatar answered Sep 17 '22 15:09

cliffroot


This is the best way to implement Onclicklistener for many buttons in a row implement View.onclicklistener.

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

This is a button in the MainActivity

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);      bt_submit = (Button) findViewById(R.id.submit);      bt_submit.setOnClickListener(this); } 

This is an override method

    @Override     public void onClick(View view) {         switch (view.getId()){             case R.id.submit:                 //action                  break;              case R.id.secondbutton:                 //action                 break;         }     } 
like image 38
nafees ahmed Avatar answered Sep 20 '22 15:09

nafees ahmed