Handling user clicks in Android

User input handling is one of the most basic things that an Android developers needs to implement in every project. A click on a UI component is probably the most common interaction method and this tutorial shows how to handle it.

This tutorial uses Android Toast message. If you’re not familiar with them, see my tutorial on Toasts.


There are two ways to handle click events, you can add the listener name to the layout file or you can add the listener programmatically in the application code. Both of these methods have their places as well as pros and cons. We’ll go through both methods next.

Add listener in layout

Adding the listener through the xml is a simple thing to implement. The idea is to add the listener name to the layout xml. The listing below shows how its done.

    android:text="Listener in layout file" />

The onclick attribute adds a click listener to the button. In order to handle the click event, we need to add a method named onLayoutClick to the corresponding activity.

 * Handle button click with listener declared in layout xml
 * @param view
public void onLayoutClick(View view) {
    Toast.makeText(this, "Layout file click listener", Toast.LENGTH_SHORT).show();

The method signature needs to be public void onLayoutClick(View v) in order for Android to be able to call the method when the component is clicked. If the signature is wrong, the app will crash with an IllegalStateException since the corresponding method cannot be found.

…Or in code

The other way to add the click listener is to do it fully in the java code. Below is an example on how to add an OnClickListener to a View:

protected void onCreate(Bundle savedInstanceState) {

    View codeButton = findViewById(R.id.onclick_button_code);

    // Add click listener programmatically
    codeButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(OnclickActivity.this, "Code click listener", Toast.LENGTH_SHORT).show();

The addition is pretty simple, you just add an OnClickListener with setOnClickListener and then implement the onClick method.

Pros and cons

Both of the methods have their pros and cons. Setting the listener in the layout file makes your code a little bit cleaner. But since the method name is not checked by the compiler, there’s more room for error.

On the other hand, adding the click listener programmatically creates a little more code. But it makes sure that the validity of the listener method is checked at compile time and the app cannot crash because of a missing listener.

Also worth noting is that if you’re using fragments and want to handle clicks in the Fragment, then you need to add the listener in the java code. Listener added with the onclick attribute is always searched from the Activity, never from the Fragment.


Layout click Code click

Source code

The source code for this whole example project is available here

Further reading


comments powered by Disqus