Android Adventures - Basic Controls And Events
Written by Mike James   
Wednesday, 17 July 2013
Article Index
Android Adventures - Basic Controls And Events
General Event Handler
Checkboxes
Switches and Toggle Buttons

 

Two Ways To Handle An Event

To avoid the overheads you can use one of two possible methods - use an anonymous class or implement the interface in the activity. 

Java has anonymous classes for just the situation we are trying to solve. You can take an interface and implement it as a class and create an instance in one go. 

For example:

MyInterface instance=new MyInterface{
  functions which are needed to
  implement MyInterface
} 

This creates an instance of the implementation of MyInterface that is contained within the braces. No need to define a class to get an instance - but notice you can only get one instance.

So for our onClick event handler using an anonymous class makes things much easier because we don't need a separate file to hold the new class just:

 

View.OnClickListener Listener=
                      new View.OnClickListener(){
 @Override
 public void onClick(View view) {
  //Implement event handling
 }
}

and the instance of our custom listener complete with our onClick event handler is ready to be used as before:

button.setOnClickListener(Listener);

Of course you can save a variable and simply use the annoymous class in the setOnClickListener as in:

button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {

  //Implement event handling
 }
});

We no long have to use the Listener variable but the cost is that we need to be careful to remember to close the function call with a right parenthesis. 

The alternative way of doing things is to simply use the MainActivity class to implement the event listener interface. This seems a little strange when you first see it but it is the closest approach to the way other languages do the job.

For example, to handle the onClick event we need to add the "implements" for the interface to the Activity: 

public class MainActivity extends Activity
  implements View.OnClickListener {

Now all that has to happen is that the Activity class has to implement the onClick event handler:

@Override
public void onClick(View view) {
//Handle Event
}

All that remains is to add the event listener using setOnClickEventListener but what is the instance?

If you recall what happens is that the system creates an instance of the MainActivity class to get your app running and the current instance of a class is always "this". So we need to add to the onCreate method:

button.setOnClickListener(this);

In many ways this last way of doing things is the simplest. It has the disadvantage that you have to add an "implements" for each event you are going to handle but this could be an advantage in that it enables you to see what events are being used. Also notice that using this method you set all of the event handlers using

somecontrol.setOnSomeEvent(this);

This also implies that you will be using the same event handler for an event irrespective of which control generated the event. If you want to use multiple event handlers - one for each control then you need to use the anonymous class approach.

Finally lets see how this works for something other than the onClick event. 

The EditorAction Event

Returning to the EditText control let's add a handler for the Send button. First place an EditText for an email on the design surface and use the Expert properties to add a Send button. 

Before you can handle any new event you have to discover the name of the event listener interface and the setOn method for that event. For the EditorAction event the documentation reveals that the listener interface is called OnEditorActionListener and the setOn method is setOnEditorActionListerner. So using this information we can proceed as before and add an implements to the MainActivity class:

public class MainActivity extends Activity
implements TextView.OnEditorActionListener {

and in the onCreate we can add:

 

EditText email = (EditText) findViewById(R.id.editText);
email.setOnEditorActionListener(this);

Now all that remains is to write the event handler. You can lookup the details of the event handler in the documentation, but it is easier to use the help provided by Android Studio, which will offer to implement the Interface for you - accept and you get a stub for the handler function:

 

@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
 return false;
}
 

Notice that the function is generated complete with return type and parameters. This makes life easy but you still, in general, have to go and look up what the parameters are. In this case textView is the control that the event is associated with, i is the action id and keyEvent is null unless the enter key was used. If the action has been consumed then the routine should return true when no other handlers will get a chance to process it. 

Checkboxes

A Checkbox is a fairly obvious UI element. It displays a small label, controlled by the text property and a tick or no tick. The user can select or deselect as many checkboxes as desired. 

In most cases you don't bother to deal with the state of a Checkbox until the user presses some other control - usually a big button marked Done or similar. Then you can discover the Checkbox's state by simply using the isChecked method which returns true or false. 

 

check

 

For example, if you have a Checkbox with id checkBox then you can discover its state when a button somewhere on the view is clicked using:

public void onClick(View v){
 CheckBox box=
   (CheckBox) findViewById(R.id.checkBox);
 boolean checked = box.isChecked();
}

The Checkbox also supports the onClick event and this can be used to process changes to its state and you can setup the onClick event handler using the Property window as in the case of a Button. 

So to handle the checkBox change of state all you have to do is set its onClick event handler to:

public void onClick(View v){
 CheckBox box=(CheckBox) v;
 boolean checked = box.isChecked();
}

If you need to modify a CheckBox value then use the setChecked or the toggle methods.

 



Last Updated ( Wednesday, 09 April 2014 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.