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

Text Fields

There are a lot of Text Fields provided in the Toolbox but they all work in the same way differing only in the type of input they expect the user to type.

If you have used another UI Framework then you will have encountered some form of the Text Field before but the Android control is slightly different in that it generally offers the user a customized virtual keyboard - an IME Input Method Editor. You can build your own IMEs but for the moment lets just use the ones provided by the system. 

If you want the Android simulator to make use of the IME that a real Android device would use then you need to deselect the Hardware Keyboard Present option when you are creating the AVD (Android Virtual device). If the simulator makes use of the host machine's keyboard you don't see the IME at all.

The first thing to clear up is that even though there appear to be a lot of different types of Text Field controls in the Toolbox they are all examples of the EditText control with its inputType property set to a particular value. When you place one on the design surface you will see that it is of type EditText and if you scroll down to its inputType property you can change the type of input the Text Field will handle. 

When you use a particular inputType the user is presented with a virtual keyboard that is suitable for typing a value - e.g. if you specify a numeric value then the keyboard only displays numbers. You also get an action key at the bottom right of the keyboard that the user can press to complete the action e.g.Send for an SMS message say. 

To select the action button for a particular keyboard, use a Send button for a general text input then you will need to use the Expert Properties view and select a setting for the imeOptions property. For example setting it to ActionSend forces a Send button to be displayed:

 

send

 

There are many other properties that you can use to customise a Text Field but there is one big problem that we need to solve  - writing a handler for the onEditorAction event.

The reason that this is a problem is that so far we only know how to create an event handler for the onClick event using XML via the Properties Window. The onClick event is the only one event that can be handled in this way. Now we cannot avoid learning how to do the complete job and set an event handler using code.

The General Event Handler

If you have used languages other than Java you might expect event handlers to simply be functions that are passed to controls to register them as event listeners - i.e. functions that are called when an event occurs. Unfortunately events in Java are slightly more complicated because you cannot pass a function as an argument - you can in the latest version of Java but it will be sometime before UI frameworks catchup. 

As a result what has to be passed to register an event listener is more complicated in Java than in many other languages. The way that it all works was invented in the early Java UI frameworks such as GWT and later refined in Swing. Android uses the same approach. As you can't pass a function you have to pass an object which contains a function that can handle the event. Clearly the event handling function has to be capable of handling the event and hence it is defined exactly depending on the type of the event. For example an onClick event needs a handler that returns void and accepts a View object, which is the object the event occurred on. 

To make sure that an event listener has the right sort of event handling function events are defined using an Interface. 

If you recall an interface is like a class but it simply defines the functions that a class has to support. If a class inherits or implements an interface then you have to write code for each function defined in the interface. 

For example the onClickListener is an interface that defines a single function 

void onClick(View v)

any class that implements onClickListener has to have an onClick function of this type. 

To create an event handler for the onClick event you have to do the following things:

  1. Create a new class that implements onClickListener
  2. Define your custom onClick event handler within the class
  3. create an instance of the new class within the Activity
  4. use the button's setOnClickListener method to add the listener object created in step 3 to the event. 

Sounds complicated - well it is and there are ways of simplifying it but first let's see how it is done exactly as described. This is as much an exercise for Java beginners as it is an explanation of event handling. If your Java is good feel free to skip ahead. 

To implement an event handler for the onClick event first we need to add a new class to the project. Right click on the folder under the Java folder and select New, Java Class and call it MyOnClickListener. 

Note: For users of other languages it is worth pointing out that Java has an almost unbreakable convention that each class is stored in its own file of the the same name. 

Next edit MyOnClickListener to read:

 

import android.view.View;
public class MyOnClickListener
            implements View.OnClickListener {

 public void onClick(View v){
   //process event
 }
}

Notice we have now created a class with a single method which is our event handler. In a real case you would write lots of code in place of the single remark that did something when the event occurred.

That is step one completed. Next we mover to the Activity and first retrieve the Button object:

Button button=(Button)findViewById(R.id.button);

Next we create an instance of our event listener:

MyOnClickListener Listener=
                        new MyOnClickListener();

and finally we use the button's setOnClickEventListener method to connect its onClick event to our new listener object:

button.setOnClickListener(Listener);

That is putting it all together:

@Override
protected void onCreate(
                  Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Button  button=(Button)findViewById(R.id.button);
 MyOnClickListener Listener=new
                        MyOnClickListener();
 button.setOnClickListener(Listener);
}

Now if you run the program you will find that the onClick function in the MyOnClickListener instance is called when the button is clicked. 

The same overall method works for any event. You simply have to discover the name of the event listener interface, create a class that implements it, create an instance of the class and register the listener with the component that generates the event.

This isn't very practical because you are going to need to create a new class for every event you want to handle and in addition these classes are going to contain one method and be instantiated just once. 



Last Updated ( Friday, 17 October 2014 )
 
 

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