Android Adventures - Basic Controls And Events
Android Adventures - Basic Controls And Events
Written by Mike James   
Tuesday, 03 March 2015
Article Index
Android Adventures - Basic Controls And Events
Basic Input Controls
Three Buttons
Checkboxes

 

Two Ways To Handle An Event

To avoid the overheads you can use one of two possible methods:

  • use an anonymous class
  • implement the interface in the activity. 

Anonymous class

Java has anonymous classes for just the situation we are trying to solve. It lets you effectively create an object, i.e. an instance of a class, without having to explicitly create a class. The only down side is that you can't create a second instance of the class - this is a short cut way of creating one instance of an object.

In the case of an event handler you can take an interface and implement it as a class and create an instance in one go. This is just shorthand, syntactic sugar if you like for what we did in the previous section.  

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 and in fact we don't even need 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 anonymous 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 really good news is that Android Studio will help you create the anonymous class. If you start to type in OnClickListener it will provide you with options to complete the name and you can select OnClickListener - it will then generate dummy functions for all of the methods defined in the interface. All you have to do is provide the code in the body of each of the functions that does what you want. 

Implement the interface in the activity

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. That is you will write a single onClick function and this will be used for all of the controls that you use setOnClickEvent(this) on.

If you want to use multiple event handlers - one for each control then you need to use the anonymous class approach.

Which Approach To Event Handlers Should You Use?

In practice the anonymous class approach seems to be the best because, while it is a little more complicated it is completely general. Using the Activity to implement the interface fails when you need different event handlers linked to each control. 

In this sense it is worth the extra effort and  is the one used in the rest of this book. If you want to use the XML method introduced earlier for the onClick event or implement the interface in the activity approach then this should cause no problems. 

The Basic Input Controls

Now that we have discovered how events work it is time to take a look at the most commonly used input controls. 

The basic input controls are:

  • Buttons
  • Text Fields
  • Checkboxes
  • Radio Buttons
  • Toggle Buttons

If you have programmed using other UI frameworks many of these will be known to you and you can probably skip to just the ones that interest you.

Starting right at the beginning however let's revisit the Button but in a little more detail

Buttons

There are two basic types of button - Button and ImageButton - but the Android Studio Toolbox has three Button, Small Button and ImageButton.

Small Button is just a standard Button control but with a style applied to make the font smaller and hence more suitable for a small button. The ImageButton is much the same as the Button but it has a src property which can be set to an image which will be displayed as the button icon and it doesn't display any text. 

The main properties that you work with in the case of a button is are things like background - which can be set to a color or a graphic. You can spend a long time changing the way buttons look but in the main the only important property of a button is its onclick handler.

A button is in your UI to be clicked. 

If you place one example of a Button, Small Button and ImageButton on the design surface you can use the properties window to customize them.

Set the Background of the Button to dark grey by clicking on the ellipsis at the right of the property and then selecting the System tab in the Resource browser that appears. Scroll down and you will see a set of predefined colors that you can use. Find darker_grey and select it.

 

resources1

 

For the small button set the Background property to a drawable in the same way - i.e. scroll down in the Resource browser until you see "drawables" and then select ic_menu_add.

We will discuss the use of resources and drawables in a later chapter. For now just assume that they are styles and graphics elements that you can select.

 

resources

 

Repeat the task for the ImageButton but set the src property to ic_menu_add. 

 

The result is the three buttons shown in the image below:

buttons2

 

You can spend a great deal of time exploring ways to make use of a button's properties to style it just as you want it to look. 



Last Updated ( Tuesday, 03 March 2015 )
 
 

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