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?
Use OnTouchListener when you want to receive events from someone's finger on the screen. Use OnClickListener when you want to detect clicks.
onClick is prompted deprecated.
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.
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.
Both are the same in performance. Xml is pre-parsed into binary code while compiling. so there is no over-head in Xml.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With