Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which One Is Better Performance Wise: setOnClickListener VS android:onclick="onClick"

In Android we have 2 ways to set an onClick event for a buttom (or any other view I think):

Scenario one (programmatically):

Button b = (Button) findViewById(R.id.mybutton);
b.setOnClickListener(this);

Scenario two (in the XML file):

<Button android:onClick="handler" />

Is there any performance penalty for doing this in XML or programmatically or is it the same?

like image 903
Dzhuneyt Avatar asked Sep 26 '12 11:09

Dzhuneyt


People also ask

What is the difference between Ontouch and onClick Android?

Use OnTouchListener when you want to receive events from someone's finger on the screen. Use OnClickListener when you want to detect clicks.

Is onClick deprecated Android?

onClick is prompted deprecated.

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 setOnTouchListener?

setOnTouchListener gives you a chance for something to respond before the view does, whereas. setOnClickListener is fired after the view responds. Follow this answer to receive notifications.


1 Answers

Both are the same in performance. Xml is pre-parsed into binary code while compiling. so there is no over-head in Xml.

like image 63
App Work Avatar answered Sep 30 '22 13:09

App Work