Monday, December 10, 2012

Android beginner tutorial: Part 16 - More click listeners

Today we will learn of more ways to add event listeners in Android applications.

Last time we created 2 buttons and a listener for each of them. The handlers were functions that were written anonymously right in the setOnClickListener() function's parameter.

Another way to add listeners is to only pass a reference to an existing function as the parameter of the setOnClickListener() method .

Let's do that now.

Go to MainActivity.java file of your project and clean up the onCreate() function - remove everything from the parameters of setOnClickListener() functions and pass "listener1" and "listener2" instead.

final Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(listener1);

final Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(listener2);

The handlers are then declared like this:

private OnClickListener listener1 = new View.OnClickListener() {
@Override
public void onClick(View v) {
myField.setText("The first button was clicked!");
}
};

private OnClickListener listener2 = new View.OnClickListener() {
@Override
public void onClick(View v) {
myField.setText("The second button was clicked!");
}
};

Full class code:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView myField;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myField = (TextView)findViewById(R.id.myText);

final Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(listener1);

final Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(listener2);

}

private OnClickListener listener1 = new View.OnClickListener() {
@Override
public void onClick(View v) {
myField.setText("The first button was clicked!");
}
};

private OnClickListener listener2 = new View.OnClickListener() {
@Override
public void onClick(View v) {
myField.setText("The second button was clicked!");
}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

There's a third way to create listeners, which is even more effective.

The condition for listening to events using this third method I'm about to show is that all the events that we listen to have to be of the same type. In our case, it's the click events.

Basically, we realize the View.OnClickListener interface in our MainActivity class, which will enable us to use the methods of the interface. There is only one method though - onClick(), which is simply the handler of the event. The Activity class will itself handle the events. That's why we can set the handler of the listeners to "this".

Implement the interface:

public class MainActivity extends Activity implements OnClickListener{

Set the listeners in onCreate():

final Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);

final Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(this);

Remove the existing handler functions "listener1" and "listener2".

Now create a new function called onClick(). Receive a View as the parameter. Inside of the function, we can use a switch case statement to check the id of the View that has been clicked on. We can check if the id is button1 or button2 and set the text approporiately if so.

@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button1:
myField.setText("The first button was clicked!");
break;
case R.id.button2:
myField.setText("The second button was clicked!");
break;
}
}

Full class:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

private TextView myField;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myField = (TextView)findViewById(R.id.myText);

final Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);

final Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(this);
}

@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button1:
myField.setText("The first button was clicked!");
break;
case R.id.button2:
myField.setText("The second button was clicked!");
break;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

You can run the application on your device or emulator - it all works just the same!

Now we know 3 ways of adding listeners in Android SDK.

Thanks for reading!

No comments:

Post a Comment