[Android] Beginner Tutorials – Lesson 9. Event listeners with Button example

In this lesson we will:

– learn how to process button clicks and find out what a listener is.

Translated by Taras Leskiv (http://android-by-example.blogspot.com/)

Let’s create a project:

Project name: P0091_OnClickButtons
Build Target: Android 2.3.3
Application name: OnClickButtons
Package name: ru.startandroid.develop.onclickbuttons
Create Activity: MainActivity

In layout file main.xml write the following and save:

We have a TextView with text and two buttons: OK and Cancel. Now we will make the TextView content change when a button is clicked. When OK is clicked – we will display text “Button OK clicked”, when Cancel – “Cancel button clicked”.

Open MainActivity.java. We will declare instance variables for our objects outside onCreate method. It’s done like this to enable us referencing these variables from any method. We will initialize these object using findViewById method, which we are already familiar with. In the end the resulted code should be like this:

Update the import section (CTRL+SHIFT+O). Objects tvOut, btnOk and btnCancel correspond to View-elements on screen and we can work with them. Now we have to teach buttons to react when it is clicked. To do so setOnClickListener (View.OnClickListener l) method is available. The parameter is an object that implements View.OnClickListener interface. That’s why, the button will delegate click processing to this method. Let’s create such an object. Continue writing code inside onCreate method:

Eclipse underlines OnClickListener with a red line:

L0009_010

as it is not familiar with it yet. We must update the import section. Press CTRL+SHIFT+O. Eclipse shows us that it knows two interfaces with OnClickListener names and offers us to choose. We need View.OnClickListener, as button method setOnClickListener requires it as a parameter.

L0009_020

We have created oclBtnOk object which implements View.OnClickListener interface. This object has onClick method – this is exactly what we need. This is the method, that will be invoked when the button is clicked. We have decided that we will display “Button OK clicked” text in the TextView (tvOut) when the button is clicked. Let’s implement this.

In onClick method write:

The click listener is ready. The only thing left is to set it to the button using setOnClickListener method.

The resulting code:

Save and run the application. Click OK button and see that the text has changed.

L0009_030_en

Clicking Cancel button does nothing for now, because we haven’t created and assigned a click listener for it. Let’s do it similarly as with OK button. To begin with, create a listener:

Then set it to the button:

Save, run and check the result. Both buttons now can process clicks.

Let’s talk once again about event processing mechanism by button click example. A button cannot process clicks on its own, it needs a listener for this, which is assigned using setOnClickListener method. When a button is clicked, listener reacts and runs the code from onClick method. It can be displayed like this:

L0009_040_en

It means that to implement this you have to follow these steps:

– create a listener
– implement onClick method
– assign listener to a button

And now the event processing system is ready.

Comments

comments

Share this Post :

No comments yet.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *