From that I've read you can assign a onClick
handler to a button in two ways.
Using the android:onClick
XML attribute where you just use the name of a public method with the signaturevoid name(View v)
or by using the setOnClickListener
method where you pass an object that implement the OnClickListener
interface. The latter often requires an anonymous class which personally I don't like (personal taste) or defining an internal class that implements the OnClickListener
.
By using the XML attribute you just need to define a method instead of a class so I was wondering if the same can be done via code and not in the XML layout.
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.
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.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
TextView OnClickListener in Kotlin AndroidIn Android, TextView is a child class of View, and hence can use the method setOnClickListener() on the object of TextView. In this tutorial, we will learn how to set OnClickListener for TextView in Kotlin file, with the help of an example.
No, that is not possible via code. Android just implements the OnClickListener
for you when you define the android:onClick="someMethod"
attribute.
Those two code snippets are equal, just implemented in two different ways.
Code Implementation
Button btn = (Button) findViewById(R.id.mybutton); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myFancyMethod(v); } }); // some more code public void myFancyMethod(View v) { // does something very interesting }
Above is a code implementation of an OnClickListener
. And this is the XML implementation.
XML Implementation
<?xml version="1.0" encoding="utf-8"?> <!-- layout elements --> <Button android:id="@+id/mybutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me!" android:onClick="myFancyMethod" /> <!-- even more layout elements -->
In the background, Android does nothing else than the Java code, calling your method on a click event.
Note that with the XML above, Android will look for the onClick
method myFancyMethod()
only in the current Activity. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick
method in the .java
file of the fragment used to add the XML.
Another important thing I noticed. You mentioned you don't prefer anonymous methods. You meant to say you don't like anonymous classes.
When I saw the top answer, it made me realize that my problem was not putting the parameter (View v) on the fancy method:
public void myFancyMethod(View v) {}
When trying to access it from the xml, one should use
android:onClick="myFancyMethod"/>
Hope that helps someone.
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