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

Working with Android Studio makes building the UI easy with an interactive editor, but you still need to find out how to handle the things it isn't quite so good at. In this chapter of our ebook on Android the easy way we look at the basic UI components and general event handling - i.e beyond onClick. 

Updated for Android Studio 1

 

Android Adventures With Android Studio

Contents

  1. Getting Started With Android Studio
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Basic Controls And Events
  5. Layout Containers
  6. UI Graphics A Deep Dive
  7. Menus & The Action Bar
  8. Menus, Context & Popup
  9. Resources
  10. Graphics - Coming Soon
  11. Lifecycle and State
  12. Spinners
  13. Pickers
  14. ListView And Adapters

    Fragments
  15. Introducing Fragments
  16. Fragments and XML
  17. Fragment And Activity Working Together
  18. Managing Fragments
  19. Custom dialogs using DialogFragment
  20. Dialog Classes In DialogFragment
  21. A NumberPicker DialogFragment Project
  22. ViewPager

If you are interested in creating custom template also see:

Custom Projects In Android Studio

Androidgears

 


 

We have looked at the general principles of building a UI but now we have to spend some time looking at the most basic components that can be used to build an application.

We also need to find out how to work with any event and not just onClick. Even though most controls can be used very successfully using just the onClick event - there are other types of event that cannot be handled simply by setting an onEvent property in the Property Window. In this chapter we look in depth at Android event handling.

As before the emphasis will be on using Android Studio and the Layout editor to get as much of the work done as possible. 

Let's start with the problem of how to write an event handler. 

The General Android 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. You might even have used an IDE that adds event handlers automatically for you but Android Studio doesn't currently do this - you have to add code to handle and event. It does however help with the automatic generation of this code. 

Events in Java are slightly more complicated because you cannot pass a function as an argument like you can in JavaScript, C# and many other languages - you can in the latest version of Java but it will be sometime before UI frameworks and Android catchup if they ever do. In Java functions can only exist as methods of objects so if you want to pass a function you have to pass an object that has the function as a method.

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 already mentioned 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 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 the onClickListener interface
  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. 

This is the general pattern that you have to follow when implementing any event handler - only the name of the object you have to create, the method you have to define. You also have to discover what information is passed as standard to the event handler. 

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 not the best way to do the job but it is how it all works in principle and it is important to understand what is going on. 

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. 

There has to be a better way! 

Now that you have seen the fundamentals of Java event handling we can example the two ways that it tends to be done in practice.

 



Last Updated ( Tuesday, 03 March 2015 )
 
 
Banner

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