There are 2 ways to handle the click event in button
- Onclick in xml layout
- Using an OnClickListener
Onclick in XML layout
When the user clicks a button, the Button object receives an on-click event.
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.
If you use this event handler in your code, make sure that you are having that button in your MainActivity. It won’t work if you use this event handler in fragment because onClick attribute only works in Activity or MainActivity.
In MainActivity class
Make sure that your sendMessage method should have the following :
Using an OnClickListener
You can also declare the click event handler programmatically rather than in an XML layout. This event handler code is mostly preferred because it can be used in both Activities and Fragments.
There are two ways to do this event handler programmatically :
- Implementing View.OnClickListener in your Activity or fragment.
- Creating new anonymous View.OnClickListener.
Implementing View.OnClickListener in your Activity or fragment
View.OnClickListener in your Activity or Fragment, you have to override
onClick method on your class.
Firstly, link the button in xml layout to java by calling
R.id.button_send refers the button in XML.
mButton.setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents
OnClickListener and for this reason your class have to implement that interface.
If you have more than one button click event, you can use switch case to identify which button is clicked.
Creating Anonymous View.OnClickListener
Link the button from the XML by calling
findViewById() method and set the onClick listener by using
setOnClickListener takes an
OnClickListener object as the parameter. Basically it’s creating an anonymous subclass OnClickListener in the parameter.
It’s like the same in java when you can create a new thread with an anonymous subclass.
- Difference Between Pay Per Click and Search Engine Optimization
- Handling Ajax request in Django
- Socket Programming in C/C++: Handling multiple clients on server without multi threading
- Youtube Data API for handling videos | Set-1
- Youtube Data API for handling videos | Set-2
- Youtube Data API for handling videos | Set-3
- iOS vs Android
- Resolving frequently occurring errors in Android Development
- Android Launcher Apps Users Must Watch
- Creating a Calculator for Android devices
- Kotlin | Language for Android, now Official by Google
- Introduction to Fragments | Android
- Introduction to Retofit 2 in android | Set 1
- Why Kotlin will replace Java for Android App Development
- Image loading and caching library for Android | Set 1
- Android System Architecture
- Components of an Android Application
- App Development for Android in 2017: Challenges and Solutions
- Layouts in Android UI Design
- Image loading and caching library for Android | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Vijayaraghavan