This method is called when the user touches the item or focuses upon the item with the navigation-keys or trackball and presses the suitable “enter” key or presses down on the trackball.
onLongClick()
View.OnLongClickListener
This is called when the user either touches and holds the item, or focuses upon the item with the navigation-keys or trackball and presses and holds the suitable “enter” key or presses and holds down on the trackball (for one second).
onFocusChange()
View.OnFocusChangeListener
This is called when the user navigates onto or away from the item, using the navigation-keys or trackball.
onKey()
View.OnKeyListener
This is called when the user is focused on the item and presses or releases a hardware key on the device.
onTouch()
View.OnTouchListener
This is called when the user acts qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).
onCreateContextMenu()
View.OnCreateContextMenuListener
This is called when a Context Menu is being built (as the result of a sustained “long click”).
Note:
onClick() callback does not return any value. But onLongClick(), onKey(),onTouch() callback returns a boolean value that indicates that you simply have consumed the event and it should not be carried further; That is, return true to indicate that you have handled the event and it should stop here; return false if you’ve not handled it and/or the event should continue to any other on-click listeners.
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Register OnClickListener for button
// call Click Handler method
findViewById(R.id.btnClick).setOnClickListener {
displayTextOnButtonClick(it)
}
// Register OnLongClickListener for button
// call Click Handler method
// Return true to indicate that the event is consumed
findViewById(R.id.btnClick).setOnLongClickListener {
displayTextOnLongButtonClick(it)
true
}
}
// Click handler for the Click me button.
// Display the text below Click me Button
// Display text after Button click
// It has No Return Value
private fun displayTextOnButtonClick(view: View) {
val text = findViewById(R.id.textResult)
text.text = "Button Clicked !"
}
// Click handler for the Click me button.
// Display the text below Click me Button
// Display text after Button click for a bit long time (1-2sec)
private fun displayTextOnLongButtonClick(view: View) {
val text = findViewById(R.id.textResult)
text.text = "Long Button Clicked !"
}
}